Skip to content

Commit a3b1265

Browse files
committed
Added tests for property pattern
1 parent dca6851 commit a3b1265

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.iluwatar.property;
2+
3+
import org.junit.Test;
4+
5+
import static com.iluwatar.property.Character.Type;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertNull;
9+
import static org.junit.Assert.assertTrue;
10+
11+
/**
12+
* Date: 12/28/15 - 7:46 PM
13+
*
14+
* @author Jeroen Meulemeester
15+
*/
16+
public class CharacterTest {
17+
18+
@Test
19+
public void testPrototypeStats() throws Exception {
20+
final Character prototype = new Character();
21+
22+
for (final Stats stat : Stats.values()) {
23+
assertFalse(prototype.has(stat));
24+
assertNull(prototype.get(stat));
25+
26+
final Integer expectedValue = stat.ordinal();
27+
prototype.set(stat, expectedValue);
28+
assertTrue(prototype.has(stat));
29+
assertEquals(expectedValue, prototype.get(stat));
30+
31+
prototype.remove(stat);
32+
assertFalse(prototype.has(stat));
33+
assertNull(prototype.get(stat));
34+
}
35+
36+
}
37+
38+
@Test
39+
public void testCharacterStats() throws Exception {
40+
final Character prototype = new Character();
41+
for (final Stats stat : Stats.values()) {
42+
prototype.set(stat, stat.ordinal());
43+
}
44+
45+
final Character mage = new Character(Type.MAGE, prototype);
46+
for (final Stats stat : Stats.values()) {
47+
final Integer expectedValue = stat.ordinal();
48+
assertTrue(mage.has(stat));
49+
assertEquals(expectedValue, mage.get(stat));
50+
}
51+
}
52+
53+
@Test
54+
public void testToString() throws Exception {
55+
final Character prototype = new Character();
56+
prototype.set(Stats.ARMOR, 1);
57+
prototype.set(Stats.AGILITY, 2);
58+
prototype.set(Stats.INTELLECT, 3);
59+
assertEquals("Stats:\n - AGILITY:2\n - ARMOR:1\n - INTELLECT:3\n", prototype.toString());
60+
61+
final Character stupid = new Character(Type.ROGUE, prototype);
62+
stupid.remove(Stats.INTELLECT);
63+
assertEquals("Character type: ROGUE\nStats:\n - AGILITY:2\n - ARMOR:1\n", stupid.toString());
64+
65+
final Character weak = new Character("weak", prototype);
66+
weak.remove(Stats.ARMOR);
67+
assertEquals("Player: weak\nStats:\n - AGILITY:2\n - INTELLECT:3\n", weak.toString());
68+
69+
}
70+
71+
@Test
72+
public void testName() throws Exception {
73+
final Character prototype = new Character();
74+
prototype.set(Stats.ARMOR, 1);
75+
prototype.set(Stats.INTELLECT, 2);
76+
assertNull(prototype.name());
77+
78+
final Character stupid = new Character(Type.ROGUE, prototype);
79+
stupid.remove(Stats.INTELLECT);
80+
assertNull(stupid.name());
81+
82+
final Character weak = new Character("weak", prototype);
83+
weak.remove(Stats.ARMOR);
84+
assertEquals("weak", weak.name());
85+
}
86+
87+
@Test
88+
public void testType() throws Exception {
89+
final Character prototype = new Character();
90+
prototype.set(Stats.ARMOR, 1);
91+
prototype.set(Stats.INTELLECT, 2);
92+
assertNull(prototype.type());
93+
94+
final Character stupid = new Character(Type.ROGUE, prototype);
95+
stupid.remove(Stats.INTELLECT);
96+
assertEquals(Type.ROGUE, stupid.type());
97+
98+
final Character weak = new Character("weak", prototype);
99+
weak.remove(Stats.ARMOR);
100+
assertNull(weak.type());
101+
}
102+
103+
}

0 commit comments

Comments
 (0)