Просмотр исходного кода

Merge remote-tracking branch 'upstream/master'

Souvatzis Vasilios 9 лет назад
Родитель
Сommit
b644b8e76a

+ 11
- 1
exercises/acronym/src/example/java/Acronym.java Просмотреть файл

2
 import java.util.regex.Pattern;
2
 import java.util.regex.Pattern;
3
 
3
 
4
 public class Acronym {
4
 public class Acronym {
5
-    public static String generate(String phrase){
5
+    private final String acronym;
6
+
7
+    public Acronym(String phrase) {
8
+        acronym = generateAcronym(phrase);
9
+    }
10
+
11
+    public String get() {
12
+        return acronym;
13
+    }
14
+
15
+    private String generateAcronym(String phrase){
6
         final Pattern BREAK_WORDS = Pattern.compile("[A-Z]+[a-z]*|[a-z]+");
16
         final Pattern BREAK_WORDS = Pattern.compile("[A-Z]+[a-z]*|[a-z]+");
7
         final Matcher matcher = BREAK_WORDS.matcher(phrase);
17
         final Matcher matcher = BREAK_WORDS.matcher(phrase);
8
         final StringBuilder b = new StringBuilder();
18
         final StringBuilder b = new StringBuilder();

+ 8
- 9
exercises/acronym/src/test/java/AcronymTest.java Просмотреть файл

5
 
5
 
6
 public class AcronymTest {
6
 public class AcronymTest {
7
 
7
 
8
-
9
     @Test
8
     @Test
10
     public void fromTitleCasedPhrases() {
9
     public void fromTitleCasedPhrases() {
11
         final String phrase = "Portable Network Graphics";
10
         final String phrase = "Portable Network Graphics";
12
         final String expected = "PNG";
11
         final String expected = "PNG";
13
-        assertEquals(expected, Acronym.generate(phrase));
12
+        assertEquals(expected, new Acronym(phrase).get());
14
     }
13
     }
15
 
14
 
16
     @Ignore
15
     @Ignore
18
     public void fromOtherTitleCasedPhrases() {
17
     public void fromOtherTitleCasedPhrases() {
19
         final String phrase = "Ruby on Rails";
18
         final String phrase = "Ruby on Rails";
20
         final String expected = "ROR";
19
         final String expected = "ROR";
21
-        assertEquals(expected, Acronym.generate(phrase));
20
+        assertEquals(expected, new Acronym(phrase).get());
22
     }
21
     }
23
 
22
 
24
     @Ignore
23
     @Ignore
26
     public void fromInconsistentlyCasedPhrases() {
25
     public void fromInconsistentlyCasedPhrases() {
27
         final String phrase = "HyperText Markup Language";
26
         final String phrase = "HyperText Markup Language";
28
         final String expected = "HTML";
27
         final String expected = "HTML";
29
-        assertEquals(expected, Acronym.generate(phrase));
28
+        assertEquals(expected, new Acronym(phrase).get());
30
     }
29
     }
31
 
30
 
32
     @Ignore
31
     @Ignore
34
     public void fromPhrasesWithPunctuation() {
33
     public void fromPhrasesWithPunctuation() {
35
         final String phrase = "First In, First Out";
34
         final String phrase = "First In, First Out";
36
         final String expected = "FIFO";
35
         final String expected = "FIFO";
37
-        assertEquals(expected, Acronym.generate(phrase));
36
+        assertEquals(expected, new Acronym(phrase).get());
38
     }
37
     }
39
 
38
 
40
     @Ignore
39
     @Ignore
42
     public void fromOtherPhrasesWithPunctuation() {
41
     public void fromOtherPhrasesWithPunctuation() {
43
         final String phrase = "PHP: Hypertext Preprocessor";
42
         final String phrase = "PHP: Hypertext Preprocessor";
44
         final String expected = "PHP";
43
         final String expected = "PHP";
45
-        assertEquals(expected, Acronym.generate(phrase));
44
+        assertEquals(expected, new Acronym(phrase).get());
46
     }
45
     }
47
 
46
 
48
     @Ignore
47
     @Ignore
50
     public void fromPhrasesWithNonAcronymAllCapsWord() {
49
     public void fromPhrasesWithNonAcronymAllCapsWord() {
51
         final String phrase = "GNU Image Manipulation Program";
50
         final String phrase = "GNU Image Manipulation Program";
52
         final String expected = "GIMP";
51
         final String expected = "GIMP";
53
-        assertEquals(expected, Acronym.generate(phrase));
52
+        assertEquals(expected, new Acronym(phrase).get());
54
     }
53
     }
55
 
54
 
56
     @Ignore
55
     @Ignore
58
     public void fromPhrasesWithPunctuationAndSentenceCasing() {
57
     public void fromPhrasesWithPunctuationAndSentenceCasing() {
59
         final String phrase = "Complementary metal-oxide semiconductor";
58
         final String phrase = "Complementary metal-oxide semiconductor";
60
         final String expected = "CMOS";
59
         final String expected = "CMOS";
61
-        assertEquals(expected, Acronym.generate(phrase));
60
+        assertEquals(expected, new Acronym(phrase).get());
62
     }
61
     }
63
 
62
 
64
     @Ignore
63
     @Ignore
66
     public void fromPhraseWithSingleLetterWord() {
65
     public void fromPhraseWithSingleLetterWord() {
67
         final String phrase = "Cat in a Hat";
66
         final String phrase = "Cat in a Hat";
68
         final String expected = "CIAH";
67
         final String expected = "CIAH";
69
-        assertEquals(expected, Acronym.generate(phrase));
68
+        assertEquals(expected, new Acronym(phrase).get());
70
     }
69
     }
71
 
70
 
72
 }
71
 }

+ 0
- 0
exercises/allergies/src/main/java/.keep Просмотреть файл


+ 0
- 0
exercises/binary-search-tree/src/test/java/.keep Просмотреть файл


+ 0
- 0
exercises/binary-search/src/main/java/.keep Просмотреть файл


+ 13
- 6
exercises/diamond/src/test/java/DiamondPrinterTest.java Просмотреть файл

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
+import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
4
 import java.util.List;
5
 import java.util.List;
9
 import static org.junit.Assert.assertThat;
10
 import static org.junit.Assert.assertThat;
10
 
11
 
11
 public final class DiamondPrinterTest {
12
 public final class DiamondPrinterTest {
12
-
13
+    private DiamondPrinter diamondPrinter;
14
+    
15
+    @Before
16
+    public void setUp() {
17
+        diamondPrinter = new DiamondPrinter();
18
+    }
19
+        
13
     @Test
20
     @Test
14
     public void testOneByOneDiamond() {
21
     public void testOneByOneDiamond() {
15
-        List<String> output = new DiamondPrinter().printToList('A');
22
+        List<String> output = diamondPrinter.printToList('A');
16
         assertThat(output, is(singletonList("A")));
23
         assertThat(output, is(singletonList("A")));
17
     }
24
     }
18
 
25
 
19
     @Ignore
26
     @Ignore
20
     @Test
27
     @Test
21
     public void testTwoByTwoDiamond() {
28
     public void testTwoByTwoDiamond() {
22
-        List<String> output = new DiamondPrinter().printToList('B');
29
+        List<String> output = diamondPrinter.printToList('B');
23
         assertThat(output, is(asList(" A ",
30
         assertThat(output, is(asList(" A ",
24
                                      "B B",
31
                                      "B B",
25
                                      " A ")));
32
                                      " A ")));
28
     @Ignore
35
     @Ignore
29
     @Test
36
     @Test
30
     public void testThreeByThreeDiamond() {
37
     public void testThreeByThreeDiamond() {
31
-        List<String> output = new DiamondPrinter().printToList('C');
38
+        List<String> output = diamondPrinter.printToList('C');
32
         assertThat(output, is(asList("  A  ",
39
         assertThat(output, is(asList("  A  ",
33
                                      " B B ",
40
                                      " B B ",
34
                                      "C   C",
41
                                      "C   C",
39
     @Ignore
46
     @Ignore
40
     @Test
47
     @Test
41
     public void testFiveByFiveDiamond() {
48
     public void testFiveByFiveDiamond() {
42
-        List<String> output = new DiamondPrinter().printToList('E');
49
+        List<String> output = diamondPrinter.printToList('E');
43
         assertThat(output, is(asList("    A    ",
50
         assertThat(output, is(asList("    A    ",
44
                                      "   B B   ",
51
                                      "   B B   ",
45
                                      "  C   C  ",
52
                                      "  C   C  ",
54
     @Ignore
61
     @Ignore
55
     @Test
62
     @Test
56
     public void testFullDiamond() {
63
     public void testFullDiamond() {
57
-        List<String> output = new DiamondPrinter().printToList('Z');
64
+        List<String> output = diamondPrinter.printToList('Z');
58
         assertThat(output, is(asList("                         A                         ",
65
         assertThat(output, is(asList("                         A                         ",
59
                                      "                        B B                        ",
66
                                      "                        B B                        ",
60
                                      "                       C   C                       ",
67
                                      "                       C   C                       ",

+ 8
- 2
exercises/grade-school/src/test/java/SchoolTest.java Просмотреть файл

1
+import org.junit.Before;
1
 import org.junit.Ignore;
2
 import org.junit.Ignore;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
15
 import static org.junit.Assert.assertEquals;
16
 import static org.junit.Assert.assertEquals;
16
 
17
 
17
 public class SchoolTest {
18
 public class SchoolTest {
18
-  private final School school = new School();
19
+  private School school;
20
+
21
+  @Before
22
+  public void setUp() {
23
+    school = new School();
24
+  }
19
 
25
 
20
   @Test
26
   @Test
21
   public void startsWithNoStudents() {
27
   public void startsWithNoStudents() {
120
       // Such as UnsupportedOperationException when an unmodifiableMap is used
126
       // Such as UnsupportedOperationException when an unmodifiableMap is used
121
     }
127
     }
122
 
128
 
123
-    assertThat(school.studentsByGradeAlphabetical().get(grade), 
129
+    assertThat(school.studentsByGradeAlphabetical().get(grade),
124
       not(hasItem(studentWhichShouldNotBeAdded)));
130
       not(hasItem(studentWhichShouldNotBeAdded)));
125
   }
131
   }
126
 }
132
 }

+ 20
- 13
exercises/luhn/src/test/java/LuhnValidatorTest.java Просмотреть файл

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
+import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
4
 import static org.junit.Assert.assertFalse;
5
 import static org.junit.Assert.assertFalse;
5
 import static org.junit.Assert.assertTrue;
6
 import static org.junit.Assert.assertTrue;
6
 
7
 
7
 public class LuhnValidatorTest {
8
 public class LuhnValidatorTest {
9
+    private LuhnValidator luhnValidator;
10
+
11
+    @Before
12
+    public void setUp() {
13
+      luhnValidator = new LuhnValidator();
14
+    }
8
 
15
 
9
     @Test
16
     @Test
10
     public void testThatSingleDigitStringIsNotValid() {
17
     public void testThatSingleDigitStringIsNotValid() {
11
-        assertFalse(new LuhnValidator().isValid("1"));
18
+        assertFalse(luhnValidator.isValid("1"));
12
     }
19
     }
13
 
20
 
14
     @Ignore
21
     @Ignore
15
     @Test
22
     @Test
16
     public void testThatTheStringConsistingOfASingleZeroIsInvalid() {
23
     public void testThatTheStringConsistingOfASingleZeroIsInvalid() {
17
-        assertFalse(new LuhnValidator().isValid("0"));
24
+        assertFalse(luhnValidator.isValid("0"));
18
     }
25
     }
19
 
26
 
20
     @Ignore
27
     @Ignore
21
     @Test
28
     @Test
22
     public void testThatASimpleValidNumberIsIdentifiedAsValid() {
29
     public void testThatASimpleValidNumberIsIdentifiedAsValid() {
23
-        assertTrue(new LuhnValidator().isValid(" 5 9 "));
30
+        assertTrue(luhnValidator.isValid(" 5 9 "));
24
     }
31
     }
25
 
32
 
26
     @Ignore
33
     @Ignore
27
     @Test
34
     @Test
28
     public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV1() {
35
     public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV1() {
29
-        assertTrue(new LuhnValidator().isValid("046 454 286"));
36
+        assertTrue(luhnValidator.isValid("046 454 286"));
30
     }
37
     }
31
 
38
 
32
     @Ignore
39
     @Ignore
33
     @Test
40
     @Test
34
     public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV2() {
41
     public void testThatAValidCanadianSocialInsuranceNumberIsIdentifiedAsValidV2() {
35
-        assertTrue(new LuhnValidator().isValid("055 444 285"));
42
+        assertTrue(luhnValidator.isValid("055 444 285"));
36
     }
43
     }
37
 
44
 
38
     @Ignore
45
     @Ignore
39
     @Test
46
     @Test
40
     public void testThatAnInvalidCanadianSocialInsuranceNumberIsIdentifiedAsInvalid() {
47
     public void testThatAnInvalidCanadianSocialInsuranceNumberIsIdentifiedAsInvalid() {
41
-        assertFalse(new LuhnValidator().isValid("046 454 287"));
48
+        assertFalse(luhnValidator.isValid("046 454 287"));
42
     }
49
     }
43
 
50
 
44
     @Ignore
51
     @Ignore
45
     @Test
52
     @Test
46
     public void testThatAnInvalidCreditCardIsIdentifiedAsInvalid() {
53
     public void testThatAnInvalidCreditCardIsIdentifiedAsInvalid() {
47
-        assertFalse(new LuhnValidator().isValid("8273 1232 7352 0569"));
54
+        assertFalse(luhnValidator.isValid("8273 1232 7352 0569"));
48
     }
55
     }
49
 
56
 
50
     @Ignore
57
     @Ignore
51
     @Test
58
     @Test
52
     public void testThatAddingANonDigitCharacterToAValidStringInvalidatesTheString() {
59
     public void testThatAddingANonDigitCharacterToAValidStringInvalidatesTheString() {
53
-        assertFalse(new LuhnValidator().isValid("046a 454 286"));
60
+        assertFalse(luhnValidator.isValid("046a 454 286"));
54
     }
61
     }
55
 
62
 
56
     @Ignore
63
     @Ignore
57
     @Test
64
     @Test
58
     public void testThatStringContainingPunctuationIsInvalid() {
65
     public void testThatStringContainingPunctuationIsInvalid() {
59
-        assertFalse(new LuhnValidator().isValid("055-444-285"));
66
+        assertFalse(luhnValidator.isValid("055-444-285"));
60
     }
67
     }
61
 
68
 
62
     @Ignore
69
     @Ignore
63
     @Test
70
     @Test
64
     public void testThatStringContainingSymbolsIsInvalid() {
71
     public void testThatStringContainingSymbolsIsInvalid() {
65
-        assertFalse(new LuhnValidator().isValid("055£ 444$ 285"));
72
+        assertFalse(luhnValidator.isValid("055£ 444$ 285"));
66
     }
73
     }
67
 
74
 
68
     @Ignore
75
     @Ignore
69
     @Test
76
     @Test
70
     public void testThatTheStringConsistingOfASpaceAndASingleZeroIsInvalid() {
77
     public void testThatTheStringConsistingOfASpaceAndASingleZeroIsInvalid() {
71
-        assertFalse(new LuhnValidator().isValid(" 0"));
78
+        assertFalse(luhnValidator.isValid(" 0"));
72
     }
79
     }
73
 
80
 
74
     @Ignore
81
     @Ignore
75
     @Test
82
     @Test
76
     public void testThatStringContainingMultipleZerosIsValid() {
83
     public void testThatStringContainingMultipleZerosIsValid() {
77
-        assertTrue(new LuhnValidator().isValid(" 00000"));
84
+        assertTrue(luhnValidator.isValid(" 00000"));
78
     }
85
     }
79
 
86
 
80
     @Ignore
87
     @Ignore
81
     @Test
88
     @Test
82
     public void testThatDoublingNineIsHandledCorrectly() {
89
     public void testThatDoublingNineIsHandledCorrectly() {
83
-        assertTrue(new LuhnValidator().isValid("091"));
90
+        assertTrue(luhnValidator.isValid("091"));
84
     }
91
     }
85
 
92
 
86
 }
93
 }

exercises/raindrops/src/example/java/Raindrops.java → exercises/raindrops/src/example/java/RaindropConverter.java Просмотреть файл

1
-public class Raindrops {
1
+public class RaindropConverter {
2
 
2
 
3
-    public static String convert(int number) {
3
+    public String convert(int number) {
4
         String result = "";
4
         String result = "";
5
 
5
 
6
         if (number % 3 == 0) {
6
         if (number % 3 == 0) {

exercises/raindrops/src/test/java/RaindropsTest.java → exercises/raindrops/src/test/java/RaindropConverterTest.java Просмотреть файл

10
 import static org.junit.Assert.assertEquals;
10
 import static org.junit.Assert.assertEquals;
11
 
11
 
12
 @RunWith(Parameterized.class)
12
 @RunWith(Parameterized.class)
13
-public class RaindropsTest {
13
+public class RaindropConverterTest {
14
 
14
 
15
     private int inputNumber;
15
     private int inputNumber;
16
     private String outputFromRaindropConversion;
16
     private String outputFromRaindropConversion;
46
         });
46
         });
47
     }
47
     }
48
 
48
 
49
-    public RaindropsTest(int inputNumber, String outputFromRaindropConversion) {
49
+    public RaindropConverterTest(int inputNumber, String outputFromRaindropConversion) {
50
         this.inputNumber = inputNumber;
50
         this.inputNumber = inputNumber;
51
         this.outputFromRaindropConversion = outputFromRaindropConversion;
51
         this.outputFromRaindropConversion = outputFromRaindropConversion;
52
     }
52
     }
54
 
54
 
55
     @Test
55
     @Test
56
     public void test() {
56
     public void test() {
57
-        assertEquals(outputFromRaindropConversion, Raindrops.convert(inputNumber));
57
+        assertEquals(outputFromRaindropConversion, new RaindropConverter().convert(inputNumber));
58
     }
58
     }
59
 }
59
 }

+ 6
- 1
exercises/robot-name/src/test/java/RobotTest.java Просмотреть файл

1
 import org.junit.Test;
1
 import org.junit.Test;
2
 import org.junit.Ignore;
2
 import org.junit.Ignore;
3
+import org.junit.Before;
3
 
4
 
4
 import static org.hamcrest.CoreMatchers.equalTo;
5
 import static org.hamcrest.CoreMatchers.equalTo;
5
 import static org.hamcrest.core.Is.is;
6
 import static org.hamcrest.core.Is.is;
9
 public class RobotTest {
10
 public class RobotTest {
10
 
11
 
11
     private static final String EXPECTED_ROBOT_NAME_PATTERN = "[A-Z]{2}\\d{3}";
12
     private static final String EXPECTED_ROBOT_NAME_PATTERN = "[A-Z]{2}\\d{3}";
12
-    private final Robot robot = new Robot();
13
+    private Robot robot;
13
 
14
 
15
+    @Before
16
+    public void setUp() {
17
+      robot = new Robot();
18
+    }
14
 
19
 
15
     @Test
20
     @Test
16
     public void hasName() {
21
     public void hasName() {

+ 21
- 13
exercises/secret-handshake/src/test/java/HandshakeCalculatorTest.java Просмотреть файл

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
+import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
4
 import static java.util.Arrays.asList;
5
 import static java.util.Arrays.asList;
7
 import static org.junit.Assert.assertEquals;
8
 import static org.junit.Assert.assertEquals;
8
 
9
 
9
 public final class HandshakeCalculatorTest {
10
 public final class HandshakeCalculatorTest {
10
-
11
+    private HandshakeCalculator handshakeCalculator;
12
+    
13
+    
14
+    @Before
15
+    public void setUp() {
16
+        handshakeCalculator = new HandshakeCalculator();
17
+    }
18
+    
11
     @Test
19
     @Test
12
     public void testThatInput1YieldsAWink() {
20
     public void testThatInput1YieldsAWink() {
13
         assertEquals(
21
         assertEquals(
14
                 singletonList(Signal.WINK),
22
                 singletonList(Signal.WINK),
15
-                new HandshakeCalculator().calculateHandshake(1));
23
+                handshakeCalculator.calculateHandshake(1));
16
     }
24
     }
17
 
25
 
18
     @Ignore
26
     @Ignore
20
     public void testThatInput2YieldsADoubleBlink() {
28
     public void testThatInput2YieldsADoubleBlink() {
21
         assertEquals(
29
         assertEquals(
22
                 singletonList(Signal.DOUBLE_BLINK),
30
                 singletonList(Signal.DOUBLE_BLINK),
23
-                new HandshakeCalculator().calculateHandshake(2));
31
+                handshakeCalculator.calculateHandshake(2));
24
     }
32
     }
25
 
33
 
26
     @Ignore
34
     @Ignore
28
     public void testThatInput4YieldsACloseYourEyes() {
36
     public void testThatInput4YieldsACloseYourEyes() {
29
         assertEquals(
37
         assertEquals(
30
                 singletonList(Signal.CLOSE_YOUR_EYES),
38
                 singletonList(Signal.CLOSE_YOUR_EYES),
31
-                new HandshakeCalculator().calculateHandshake(4));
39
+                handshakeCalculator.calculateHandshake(4));
32
     }
40
     }
33
 
41
 
34
     @Ignore
42
     @Ignore
36
     public void testThatInput8YieldsAJump() {
44
     public void testThatInput8YieldsAJump() {
37
         assertEquals(
45
         assertEquals(
38
                 singletonList(Signal.JUMP),
46
                 singletonList(Signal.JUMP),
39
-                new HandshakeCalculator().calculateHandshake(8));
47
+               handshakeCalculator.calculateHandshake(8));
40
     }
48
     }
41
 
49
 
42
     @Ignore
50
     @Ignore
44
     public void testAnInputThatYieldsTwoActions() {
52
     public void testAnInputThatYieldsTwoActions() {
45
         assertEquals(
53
         assertEquals(
46
                 asList(Signal.WINK, Signal.DOUBLE_BLINK),
54
                 asList(Signal.WINK, Signal.DOUBLE_BLINK),
47
-                new HandshakeCalculator().calculateHandshake(3));
55
+                handshakeCalculator.calculateHandshake(3));
48
     }
56
     }
49
 
57
 
50
     @Ignore
58
     @Ignore
52
     public void testAnInputThatYieldsTwoReversedActions() {
60
     public void testAnInputThatYieldsTwoReversedActions() {
53
         assertEquals(
61
         assertEquals(
54
                 asList(Signal.DOUBLE_BLINK, Signal.WINK),
62
                 asList(Signal.DOUBLE_BLINK, Signal.WINK),
55
-                new HandshakeCalculator().calculateHandshake(19));
63
+               handshakeCalculator.calculateHandshake(19));
56
     }
64
     }
57
 
65
 
58
     @Ignore
66
     @Ignore
60
     public void testReversingASingleActionYieldsTheSameAction() {
68
     public void testReversingASingleActionYieldsTheSameAction() {
61
         assertEquals(
69
         assertEquals(
62
                 singletonList(Signal.JUMP),
70
                 singletonList(Signal.JUMP),
63
-                new HandshakeCalculator().calculateHandshake(24));
71
+                handshakeCalculator.calculateHandshake(24));
64
     }
72
     }
65
 
73
 
66
     @Ignore
74
     @Ignore
68
     public void testReversingNoActionsYieldsNoActions() {
76
     public void testReversingNoActionsYieldsNoActions() {
69
         assertEquals(
77
         assertEquals(
70
                 emptyList(),
78
                 emptyList(),
71
-                new HandshakeCalculator().calculateHandshake(16));
79
+                handshakeCalculator.calculateHandshake(16));
72
     }
80
     }
73
 
81
 
74
     @Ignore
82
     @Ignore
76
     public void testInputThatYieldsAllActions() {
84
     public void testInputThatYieldsAllActions() {
77
         assertEquals(
85
         assertEquals(
78
                 asList(Signal.WINK, Signal.DOUBLE_BLINK, Signal.CLOSE_YOUR_EYES, Signal.JUMP),
86
                 asList(Signal.WINK, Signal.DOUBLE_BLINK, Signal.CLOSE_YOUR_EYES, Signal.JUMP),
79
-                new HandshakeCalculator().calculateHandshake(15));
87
+                handshakeCalculator.calculateHandshake(15));
80
     }
88
     }
81
 
89
 
82
     @Ignore
90
     @Ignore
84
     public void testInputThatYieldsAllActionsReversed() {
92
     public void testInputThatYieldsAllActionsReversed() {
85
         assertEquals(
93
         assertEquals(
86
                 asList(Signal.JUMP, Signal.CLOSE_YOUR_EYES, Signal.DOUBLE_BLINK, Signal.WINK),
94
                 asList(Signal.JUMP, Signal.CLOSE_YOUR_EYES, Signal.DOUBLE_BLINK, Signal.WINK),
87
-                new HandshakeCalculator().calculateHandshake(31));
95
+                handshakeCalculator.calculateHandshake(31));
88
     }
96
     }
89
 
97
 
90
     @Ignore
98
     @Ignore
92
     public void testThatInput0YieldsNoActions() {
100
     public void testThatInput0YieldsNoActions() {
93
         assertEquals(
101
         assertEquals(
94
                 emptyList(),
102
                 emptyList(),
95
-                new HandshakeCalculator().calculateHandshake(0));
103
+                handshakeCalculator.calculateHandshake(0));
96
     }
104
     }
97
 
105
 
98
     @Ignore
106
     @Ignore
100
     public void testThatInputWithLower5BitsNotSetYieldsNoActions() {
108
     public void testThatInputWithLower5BitsNotSetYieldsNoActions() {
101
         assertEquals(
109
         assertEquals(
102
                 emptyList(),
110
                 emptyList(),
103
-                new HandshakeCalculator().calculateHandshake(32));
111
+                handshakeCalculator.calculateHandshake(32));
104
     }
112
     }
105
 
113
 
106
 }
114
 }

+ 24
- 16
exercises/wordy/src/test/java/WordProblemSolverTest.java Просмотреть файл

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
 import org.junit.Rule;
2
 import org.junit.Rule;
3
 import org.junit.Test;
3
 import org.junit.Test;
4
+import org.junit.Before;
4
 import org.junit.rules.ExpectedException;
5
 import org.junit.rules.ExpectedException;
5
 
6
 
6
 import static org.junit.Assert.assertEquals;
7
 import static org.junit.Assert.assertEquals;
14
     @Rule
15
     @Rule
15
     public ExpectedException expectedException = ExpectedException.none();
16
     public ExpectedException expectedException = ExpectedException.none();
16
 
17
 
18
+    WordProblemSolver solver;
19
+
20
+    @Before
21
+    public void setup() {
22
+        solver = new WordProblemSolver();
23
+    }
24
+
17
     @Test
25
     @Test
18
     public void testSingleAddition1() {
26
     public void testSingleAddition1() {
19
-        assertEquals(2, new WordProblemSolver().solve("What is 1 plus 1?"));
27
+        assertEquals(2, solver.solve("What is 1 plus 1?"));
20
     }
28
     }
21
 
29
 
22
     @Ignore
30
     @Ignore
23
     @Test
31
     @Test
24
     public void testSingleAddition2() {
32
     public void testSingleAddition2() {
25
-        assertEquals(55, new WordProblemSolver().solve("What is 53 plus 2?"));
33
+        assertEquals(55, solver.solve("What is 53 plus 2?"));
26
     }
34
     }
27
 
35
 
28
     @Ignore
36
     @Ignore
29
     @Test
37
     @Test
30
     public void testSingleAdditionWithNegativeNumbers() {
38
     public void testSingleAdditionWithNegativeNumbers() {
31
-        assertEquals(-11, new WordProblemSolver().solve("What is -1 plus -10?"));
39
+        assertEquals(-11, solver.solve("What is -1 plus -10?"));
32
     }
40
     }
33
 
41
 
34
     @Ignore
42
     @Ignore
35
     @Test
43
     @Test
36
     public void testSingleAdditionOfLargeNumbers() {
44
     public void testSingleAdditionOfLargeNumbers() {
37
-        assertEquals(45801, new WordProblemSolver().solve("What is 123 plus 45678?"));
45
+        assertEquals(45801, solver.solve("What is 123 plus 45678?"));
38
     }
46
     }
39
 
47
 
40
     @Ignore
48
     @Ignore
41
     @Test
49
     @Test
42
     public void testSingleSubtraction() {
50
     public void testSingleSubtraction() {
43
-        assertEquals(16, new WordProblemSolver().solve("What is 4 minus -12?"));
51
+        assertEquals(16, solver.solve("What is 4 minus -12?"));
44
     }
52
     }
45
 
53
 
46
     @Ignore
54
     @Ignore
47
     @Test
55
     @Test
48
     public void testSingleMultiplication() {
56
     public void testSingleMultiplication() {
49
-        assertEquals(-75, new WordProblemSolver().solve("What is -3 multiplied by 25?"));
57
+        assertEquals(-75, solver.solve("What is -3 multiplied by 25?"));
50
     }
58
     }
51
 
59
 
52
     @Ignore
60
     @Ignore
53
     @Test
61
     @Test
54
     public void testSingleDivision() {
62
     public void testSingleDivision() {
55
-        assertEquals(-11, new WordProblemSolver().solve("What is 33 divided by -3?"));
63
+        assertEquals(-11, solver.solve("What is 33 divided by -3?"));
56
     }
64
     }
57
 
65
 
58
     @Ignore
66
     @Ignore
59
     @Test
67
     @Test
60
     public void testMultipleAdditions() {
68
     public void testMultipleAdditions() {
61
-        assertEquals(3, new WordProblemSolver().solve("What is 1 plus 1 plus 1?"));
69
+        assertEquals(3, solver.solve("What is 1 plus 1 plus 1?"));
62
     }
70
     }
63
 
71
 
64
     @Ignore
72
     @Ignore
65
     @Test
73
     @Test
66
     public void testAdditionThenSubtraction() {
74
     public void testAdditionThenSubtraction() {
67
-        assertEquals(8, new WordProblemSolver().solve("What is 1 plus 5 minus -2?"));
75
+        assertEquals(8, solver.solve("What is 1 plus 5 minus -2?"));
68
     }
76
     }
69
 
77
 
70
     @Ignore
78
     @Ignore
71
     @Test
79
     @Test
72
     public void testMultipleSubtractions() {
80
     public void testMultipleSubtractions() {
73
-        assertEquals(3, new WordProblemSolver().solve("What is 20 minus 4 minus 13?"));
81
+        assertEquals(3, solver.solve("What is 20 minus 4 minus 13?"));
74
     }
82
     }
75
 
83
 
76
     @Ignore
84
     @Ignore
77
     @Test
85
     @Test
78
     public void testSubtractionThenAddition() {
86
     public void testSubtractionThenAddition() {
79
-        assertEquals(14, new WordProblemSolver().solve("What is 17 minus 6 plus 3?"));
87
+        assertEquals(14, solver.solve("What is 17 minus 6 plus 3?"));
80
     }
88
     }
81
 
89
 
82
     @Ignore
90
     @Ignore
83
     @Test
91
     @Test
84
     public void testMultipleMultiplications() {
92
     public void testMultipleMultiplications() {
85
-        assertEquals(-12, new WordProblemSolver().solve("What is 2 multiplied by -2 multiplied by 3?"));
93
+        assertEquals(-12, solver.solve("What is 2 multiplied by -2 multiplied by 3?"));
86
     }
94
     }
87
 
95
 
88
     @Ignore
96
     @Ignore
89
     @Test
97
     @Test
90
     public void testAdditionThenMultiplication() {
98
     public void testAdditionThenMultiplication() {
91
-        assertEquals(-8, new WordProblemSolver().solve("What is -3 plus 7 multiplied by -2?"));
99
+        assertEquals(-8, solver.solve("What is -3 plus 7 multiplied by -2?"));
92
     }
100
     }
93
 
101
 
94
     @Ignore
102
     @Ignore
95
     @Test
103
     @Test
96
     public void testMultipleDivisions() {
104
     public void testMultipleDivisions() {
97
-        assertEquals(2, new WordProblemSolver().solve("What is -12 divided by 2 divided by -3?"));
105
+        assertEquals(2, solver.solve("What is -12 divided by 2 divided by -3?"));
98
     }
106
     }
99
 
107
 
100
     @Ignore
108
     @Ignore
103
         expectedException.expect(IllegalArgumentException.class);
111
         expectedException.expect(IllegalArgumentException.class);
104
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
112
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
105
 
113
 
106
-        new WordProblemSolver().solve("What is 52 cubed?");
114
+        solver.solve("What is 52 cubed?");
107
     }
115
     }
108
 
116
 
109
     @Ignore
117
     @Ignore
113
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
121
         expectedException.expectMessage("I'm sorry, I don't understand the question!");
114
 
122
 
115
         // See https://en.wikipedia.org/wiki/President_of_the_United_States if you really need to know!
123
         // See https://en.wikipedia.org/wiki/President_of_the_United_States if you really need to know!
116
-        new WordProblemSolver().solve("Who is the President of the United States?");
124
+        solver.solve("Who is the President of the United States?");
117
     }
125
     }
118
 
126
 
119
 }
127
 }