Quellcode durchsuchen

Merge pull request #1227 from TimoleonLatinopoulos/branch5

Added test cases as per #1143
FridaTveit vor 8 Jahren
Ursprung
Commit
56b0df39c1
Es ist kein Account mit dieser Commiter-Email verbunden

+ 1
- 1
exercises/word-count/.meta/src/reference/java/WordCount.java Datei anzeigen

@@ -7,7 +7,7 @@ public class WordCount {
7 7
 
8 8
     public Map<String, Integer> phrase( String input ) {
9 9
         Map<String, Integer> countMap = new HashMap<>();
10
-        input = input.trim().toLowerCase().replaceAll("[\\W]", " ");
10
+        input = input.trim().toLowerCase().replaceAll("([^A-Za-z0-9']|\\B'|'\\B)", " ");
11 11
         final String[] tokenizedInput = input.split("\\s+");
12 12
         for( String aWord : tokenizedInput ) {
13 13
             Integer count = countMap.get(aWord);

+ 1
- 0
exercises/word-count/.meta/version Datei anzeigen

@@ -0,0 +1 @@
1
+1.1.0

+ 73
- 3
exercises/word-count/src/test/java/WordCountTest.java Datei anzeigen

@@ -34,7 +34,7 @@ public class WordCountTest {
34 34
 
35 35
     @Ignore("Remove to run test")
36 36
     @Test
37
-    public void countOneOfEach() {
37
+    public void countOneOfEachWord() {
38 38
         expectedWordCount.put("one", 1);
39 39
         expectedWordCount.put("of", 1);
40 40
         expectedWordCount.put("each", 1);
@@ -47,7 +47,7 @@ public class WordCountTest {
47 47
 
48 48
     @Ignore("Remove to run test")
49 49
     @Test
50
-    public void countMultipleOccurrences() {
50
+    public void multipleOccurrencesOfAWord() {
51 51
         expectedWordCount.put("one", 1);
52 52
         expectedWordCount.put("fish", 4);
53 53
         expectedWordCount.put("two", 1);
@@ -59,6 +59,32 @@ public class WordCountTest {
59 59
             expectedWordCount, actualWordCount
60 60
         );
61 61
     }
62
+    
63
+    @Ignore("Remove to run test")
64
+    @Test
65
+    public void handlesCrampedLists() {
66
+        expectedWordCount.put("one", 1);
67
+        expectedWordCount.put("two", 1);
68
+        expectedWordCount.put("three", 1);
69
+
70
+        actualWordCount = wordCount.phrase("one,two,three");
71
+        assertEquals(
72
+            expectedWordCount, actualWordCount
73
+        );
74
+    }
75
+    
76
+    @Ignore("Remove to run test")
77
+    @Test
78
+    public void handlesExpandedLists() {
79
+        expectedWordCount.put("one", 1);
80
+        expectedWordCount.put("two", 1);
81
+        expectedWordCount.put("three", 1);
82
+
83
+        actualWordCount = wordCount.phrase("one,\ntwo,\nthree");
84
+        assertEquals(
85
+            expectedWordCount, actualWordCount
86
+        );
87
+    }
62 88
 
63 89
     @Ignore("Remove to run test")
64 90
     @Test
@@ -93,8 +119,52 @@ public class WordCountTest {
93 119
     @Test
94 120
     public void normalizeCase() {
95 121
         expectedWordCount.put("go", 3);
122
+        expectedWordCount.put("stop", 2);
123
+
124
+        actualWordCount = wordCount.phrase("go Go GO Stop stop");
125
+        assertEquals(
126
+            expectedWordCount, actualWordCount
127
+        );
128
+    }
129
+    
130
+    @Ignore("Remove to run test")
131
+    @Test
132
+    public void withApostrophes() {
133
+        expectedWordCount.put("first", 1);
134
+        expectedWordCount.put("don't", 2);
135
+        expectedWordCount.put("laugh", 1);
136
+        expectedWordCount.put("then", 1);
137
+        expectedWordCount.put("cry", 1);
138
+
139
+        actualWordCount = wordCount.phrase("First: don't laugh. Then: don't cry.");
140
+        assertEquals(
141
+            expectedWordCount, actualWordCount
142
+        );
143
+    }
144
+    
145
+    @Ignore("Remove to run test")
146
+    @Test
147
+    public void withQuotations() {
148
+        expectedWordCount.put("joe", 1);
149
+        expectedWordCount.put("can't", 1);
150
+        expectedWordCount.put("tell", 1);
151
+        expectedWordCount.put("between", 1);
152
+        expectedWordCount.put("large", 2);
153
+        expectedWordCount.put("and", 1);
154
+
155
+        actualWordCount = wordCount.phrase("Joe can't tell between 'large' and large.");
156
+        assertEquals(
157
+            expectedWordCount, actualWordCount
158
+        );
159
+    }
160
+    
161
+    @Ignore("Remove to run test")
162
+    @Test
163
+    public void multipleSpacesNotDetectedAsAWord() {
164
+        expectedWordCount.put("multiple", 1);
165
+        expectedWordCount.put("whitespaces", 1);
96 166
 
97
-        actualWordCount = wordCount.phrase("go Go GO");
167
+        actualWordCount = wordCount.phrase(" multiple   whitespaces");
98 168
         assertEquals(
99 169
             expectedWordCount, actualWordCount
100 170
         );