InventoryListTest.pm
上传用户:market2
上传日期:2018-11-18
资源大小:18786k
文件大小:4k
源码类别:

外挂编程

开发平台:

Windows_Unix

  1. package InventoryListTest;
  2. # TODO: write test for deepCopy()
  3. use strict;
  4. use Test::More;
  5. use InventoryList;
  6. use Actor::Item;
  7. use ObjectListTest;
  8. use base qw(ObjectListTest);
  9. sub start {
  10. print "### Starting InventoryListTestn";
  11. InventoryListTest->new()->run();
  12. }
  13. #########################
  14. my $count = 0;
  15. sub run {
  16. my ($self) = @_;
  17. $self->SUPER::run();
  18. $self->testGetAndRemoveByName();
  19. $self->testNameChange();
  20. $self->testNonstackableItems();
  21. }
  22. # overloaded
  23. sub init {
  24. my ($self) = @_;
  25. $count = 0;
  26. $self->{list} = new InventoryList();
  27. for (my $i = 1; $i <= 6; $i++) {
  28. $self->{"item$i"} = $self->createTestObject();
  29. }
  30. is($self->{list}->size(), 0);
  31. $self->{list}->checkValidity();
  32. }
  33. # overloaded
  34. sub createTestObject {
  35. my ($self, $name) = @_;
  36. my $item = new Actor::Item();
  37. $count++;
  38. if (defined $name) {
  39. $item->{name} = $name;
  40. } else {
  41. $item->{name} = "Item $count";
  42. }
  43. return $item;
  44. }
  45. # overloaded
  46. sub testDuplicate {
  47. # Do nothing; InventoryList doesn't allow duplicates.
  48. }
  49. sub testGetAndRemoveByName {
  50. my ($self) = @_;
  51. $self->init();
  52. my $list = $self->{list};
  53. $list->add($self->{item1});
  54. $list->add($self->{item2});
  55. $list->add($self->{item3});
  56. is($list->size(), 3);
  57. ok($list->getByName($self->{item1}{name}) == $self->{item1});
  58. ok($list->getByName($self->{item2}{name}) == $self->{item2});
  59. ok($list->getByName($self->{item3}{name}) == $self->{item3});
  60. ok(!defined $list->getByName($self->{item4}{name}));
  61. ok(!defined $list->getByName($self->{item5}{name}));
  62. ok(!defined $list->getByName($self->{item6}{name}));
  63. $list->checkValidity();
  64. my $result = $list->removeByName($self->{item2}{name});
  65. ok($result);
  66. is($list->size(), 2);
  67. ok($list->getByName($self->{item1}{name}) == $self->{item1});
  68. ok(!defined $list->getByName($self->{item2}{name}));
  69. ok($list->getByName($self->{item3}{name}) == $self->{item3});
  70. ok(!defined $list->getByName($self->{item4}{name}));
  71. ok(!defined $list->getByName($self->{item5}{name}));
  72. ok(!defined $list->getByName($self->{item6}{name}));
  73. $list->checkValidity();
  74. my $result = $list->removeByName($self->{item2}{name});
  75. ok(!$result);
  76. is($list->size(), 2);
  77. ok($list->getByName($self->{item1}{name}) == $self->{item1});
  78. ok(!defined $list->getByName($self->{item2}{name}));
  79. ok($list->getByName($self->{item3}{name}) == $self->{item3});
  80. ok(!defined $list->getByName($self->{item4}{name}));
  81. ok(!defined $list->getByName($self->{item5}{name}));
  82. ok(!defined $list->getByName($self->{item6}{name}));
  83. $list->checkValidity();
  84. my $result = $list->removeByName($self->{item4}{name});
  85. ok(!$result);
  86. is($list->size(), 2);
  87. ok($list->getByName($self->{item1}{name}) == $self->{item1});
  88. ok(!defined $list->getByName($self->{item2}{name}));
  89. ok($list->getByName($self->{item3}{name}) == $self->{item3});
  90. ok(!defined $list->getByName($self->{item4}{name}));
  91. ok(!defined $list->getByName($self->{item5}{name}));
  92. ok(!defined $list->getByName($self->{item6}{name}));
  93. $list->checkValidity();
  94. }
  95. sub testNameChange {
  96. my ($self) = @_;
  97. $self->init();
  98. my $list = $self->{list};
  99. my $item1 = $self->{item1};
  100. my $item2 = $self->{item2};
  101. my $item3 = $self->{item3};
  102. $list->add($item1);
  103. $item1->setName("Jellopy");
  104. ok($list->getByName("Jellopy") == $item1);
  105. ok(!$list->getByName("Item 1"));
  106. $list->checkValidity();
  107. $list->add($item2);
  108. $list->add($item3);
  109. ok($list->getByName("Jellopy") == $item1);
  110. ok($list->getByName("Item 2") == $item2);
  111. ok($list->getByName("Item 3") == $item3);
  112. ok(!$list->getByName("Item 1"));
  113. $list->checkValidity();
  114. $item2->setName("Gold");
  115. ok($list->getByName("Jellopy") == $item1);
  116. ok($list->getByName("Gold") == $item2);
  117. ok($list->getByName("Item 3") == $item3);
  118. ok(!$list->getByName("Item 1"));
  119. ok(!$list->getByName("Item 2"));
  120. $list->checkValidity();
  121. ok($list->getByName("jellopy") == $item1);
  122. ok($list->getByName("gold") == $item2);
  123. ok($list->getByName("item 3") == $item3);
  124. }
  125. sub testNonstackableItems {
  126. my ($self) = @_;
  127. $self->init();
  128. my $list = $self->{list};
  129. my $item1 = $self->createTestObject("Sword");
  130. my $item2 = $self->createTestObject("Jellopy");
  131. my $item3 = $self->createTestObject("Sword");
  132. my $item4 = $self->createTestObject("Katana");
  133. my $tmp;
  134. $list->add($item1);
  135. $list->add($item2);
  136. $list->add($item3);
  137. $list->add($item4);
  138. is($list->size(), 4);
  139. $tmp = $list->getByName('Sword');
  140. ok($tmp == $item1 || $tmp == $item3);
  141. $list->checkValidity();
  142. }
  143. 1;