Przeglądaj źródła

Added test cases

TimoleonLatinopoulos 8 lat temu
rodzic
commit
25627166d1

+ 1
- 1
exercises/word-count/.meta/src/reference/java/WordCount.java Wyświetl plik

7
 
7
 
8
     public Map<String, Integer> phrase( String input ) {
8
     public Map<String, Integer> phrase( String input ) {
9
         Map<String, Integer> countMap = new HashMap<>();
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
         final String[] tokenizedInput = input.split("\\s+");
11
         final String[] tokenizedInput = input.split("\\s+");
12
         for( String aWord : tokenizedInput ) {
12
         for( String aWord : tokenizedInput ) {
13
             Integer count = countMap.get(aWord);
13
             Integer count = countMap.get(aWord);

+ 1
- 0
exercises/word-count/.meta/version Wyświetl plik

1
+1.1.0

+ 73
- 3
exercises/word-count/src/test/java/WordCountTest.java Wyświetl plik

34
 
34
 
35
     @Ignore("Remove to run test")
35
     @Ignore("Remove to run test")
36
     @Test
36
     @Test
37
-    public void countOneOfEach() {
37
+    public void countOneOfEachWord() {
38
         expectedWordCount.put("one", 1);
38
         expectedWordCount.put("one", 1);
39
         expectedWordCount.put("of", 1);
39
         expectedWordCount.put("of", 1);
40
         expectedWordCount.put("each", 1);
40
         expectedWordCount.put("each", 1);
47
 
47
 
48
     @Ignore("Remove to run test")
48
     @Ignore("Remove to run test")
49
     @Test
49
     @Test
50
-    public void countMultipleOccurrences() {
50
+    public void multipleOccurrencesOfAWord() {
51
         expectedWordCount.put("one", 1);
51
         expectedWordCount.put("one", 1);
52
         expectedWordCount.put("fish", 4);
52
         expectedWordCount.put("fish", 4);
53
         expectedWordCount.put("two", 1);
53
         expectedWordCount.put("two", 1);
59
             expectedWordCount, actualWordCount
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
     @Ignore("Remove to run test")
89
     @Ignore("Remove to run test")
64
     @Test
90
     @Test
93
     @Test
119
     @Test
94
     public void normalizeCase() {
120
     public void normalizeCase() {
95
         expectedWordCount.put("go", 3);
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
         assertEquals(
168
         assertEquals(
99
             expectedWordCount, actualWordCount
169
             expectedWordCount, actualWordCount
100
         );
170
         );