2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.spellchecker
.inspector
;
18 import com
.intellij
.idea
.Bombed
;
19 import com
.intellij
.spellchecker
.inspections
.CheckArea
;
20 import com
.intellij
.spellchecker
.inspections
.TextSplitter
;
21 import junit
.framework
.Assert
;
22 import junit
.framework
.TestCase
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
26 import java
.util
.ArrayList
;
27 import java
.util
.Arrays
;
28 import java
.util
.Calendar
;
29 import java
.util
.List
;
32 public class SplitterTest
extends TestCase
{
35 public void testSplitSimpleCamelCase() {
36 String text
= "simpleCamelCase";
37 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
38 correctListToCheck(checkAreas
, text
, new String
[]{"simple", "Camel", "Case"});
39 correctIgnored(checkAreas
, text
, new String
[]{});
42 public void testSplitCamelCaseWithUpperCasedWord() {
43 String text
= "camelCaseJSP";
44 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
45 correctListToCheck(checkAreas
, text
, new String
[]{"camel", "Case"});
46 correctIgnored(checkAreas
, text
, new String
[]{"JSP"});
49 public void testCapitalizedWithShortWords() {
50 String text
= "IntelliJ";
51 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
52 correctListToCheck(checkAreas
, text
, new String
[]{});
53 correctIgnored(checkAreas
, text
, new String
[]{"IntelliJ"});
56 public void testCapitalizedWithShortAndLongWords() {
57 String text
= "IntelliJTestTest";
58 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
59 correctListToCheck(checkAreas
, text
, new String
[]{});
60 correctIgnored(checkAreas
, text
, new String
[]{"IntelliJTestTest"});
63 public void testWordWithApostrophe1() {
64 String text
= "don't check";
65 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
66 correctListToCheck(checkAreas
, text
, new String
[]{"don't", "check"});
67 correctIgnored(checkAreas
, text
, new String
[]{});
71 public void testWordWithApostrophe2() {
72 String text
= "customers'";
73 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
74 correctListToCheck(checkAreas
, text
, new String
[]{"customers"});
75 correctIgnored(checkAreas
, text
, new String
[]{});
78 public void testWordWithApostrophe3() {
79 String text
= "customer's";
80 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
81 correctListToCheck(checkAreas
, text
, new String
[]{"customer's"});
82 correctIgnored(checkAreas
, text
, new String
[]{});
86 public void testWordWithApostrophe4() {
87 String text
= "we'll";
88 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
89 correctListToCheck(checkAreas
, text
, new String
[]{"we'll"});
90 correctIgnored(checkAreas
, text
, new String
[]{});
93 public void testWordWithApostrophe5() {
94 String text
= "I'm you're we'll";
95 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
96 correctListToCheck(checkAreas
, text
, new String
[]{"you're","we'll"});
97 correctIgnored(checkAreas
, text
, new String
[]{"I'm"});
100 public void testConstantName() {
101 String text
= "TEST_CONSTANT";
102 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
103 correctListToCheck(checkAreas
, text
, new String
[]{"TEST","CONSTANT"});
104 correctIgnored(checkAreas
, text
, new String
[]{});
107 public void testLongConstantName() {
108 String text
= "TEST_VERY_VERY_LONG_AND_COMPLEX_CONSTANT";
109 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
110 correctListToCheck(checkAreas
, text
, new String
[]{"TEST","VERY","VERY","LONG","COMPLEX","CONSTANT"});
111 correctIgnored(checkAreas
, text
, new String
[]{"AND"});
114 public void testJavaComments() {
115 String text
= "/*special symbols*/";
116 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
117 correctListToCheck(checkAreas
, text
, new String
[]{"special","symbols"});
118 correctIgnored(checkAreas
, text
, new String
[]{});
122 public void testXmlComments() {
123 String text
= "<!--special symbols-->";
124 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
125 correctListToCheck(checkAreas
, text
, new String
[]{"special","symbols"});
126 correctIgnored(checkAreas
, text
, new String
[]{});
129 public void testCamelCaseInXmlComments() {
130 String text
= "<!--specialCase symbols-->";
131 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
132 correctListToCheck(checkAreas
, text
, new String
[]{"special","Case","symbols"});
133 correctIgnored(checkAreas
, text
, new String
[]{});
136 public void testWordsWithNumbers() {
137 String text
= "testCamelCase123";
138 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
139 correctListToCheck(checkAreas
, text
, new String
[]{"test","Camel","Case"});
140 correctIgnored(checkAreas
, text
, new String
[]{});
143 public void testCommentsWithWordsWithNumbers() {
144 String text
= "<!--specialCase456 symbols-->";
145 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
146 correctListToCheck(checkAreas
, text
, new String
[]{"special","Case","symbols"});
147 correctIgnored(checkAreas
, text
, new String
[]{});
150 public void testCommentsWithAbr() {
151 String text
= "<!--JSPTestClass-->";
152 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
153 correctListToCheck(checkAreas
, text
, new String
[]{"Test","Class"});
154 correctIgnored(checkAreas
, text
, new String
[]{"JSP"});
157 public void testStringLiterals() {
158 String text
= "test\ntest\n";
159 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
160 correctListToCheck(checkAreas
, text
, new String
[]{"test", "test"});
161 correctIgnored(checkAreas
, text
, new String
[]{});
164 public void testCommentWithHtml() {
165 String text
= "<!--<li style='color:red;'>something go here</li> foooo barrrr <p> text text -->";
166 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
167 correctListToCheck(checkAreas
, text
, new String
[]{"something","here","foooo","barrrr","text", "text"});
168 correctIgnored(checkAreas
, text
, new String
[]{"go"});
171 public void testSpecial() {
172 String text
= "test test";
173 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
174 correctListToCheck(checkAreas
, text
, new String
[]{"test", "test"});
175 correctIgnored(checkAreas
, text
, new String
[]{" "});
178 public void testTooShort() {
179 String text
= "bgColor carLight";
180 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
181 correctListToCheck(checkAreas
, text
, new String
[]{"Color", "Light"});
182 correctIgnored(checkAreas
, text
, new String
[]{"bg","car"});
185 public void testComplex() {
186 String text
= "shkate@gmail.com";
187 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
188 correctListToCheck(checkAreas
, text
, new String
[]{});
189 correctIgnored(checkAreas
, text
, new String
[]{});
192 public void testWordBeforeDelimeter() {
193 String text
= "badd,";
194 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
195 correctListToCheck(checkAreas
, text
, new String
[]{"badd"});
196 correctIgnored(checkAreas
, text
, new String
[]{});
198 public void testWordAfterDelimeter() {
199 String text
= ",badd";
200 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
201 correctListToCheck(checkAreas
, text
, new String
[]{"badd"});
202 correctIgnored(checkAreas
, text
, new String
[]{});
205 public void testWordInCapsBeforeDelimeter() {
206 String text
= "BADD,";
207 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
208 correctListToCheck(checkAreas
, text
, new String
[]{"BADD"});
209 correctIgnored(checkAreas
, text
, new String
[]{});
211 public void testWordInCapsAfterDelimeter() {
212 String text
= ",BADD";
213 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
214 correctListToCheck(checkAreas
, text
, new String
[]{"BADD"});
215 correctIgnored(checkAreas
, text
, new String
[]{});
217 public void testWordInCapsAfterDelimeter2() {
218 String text
= "BADD;";
219 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
220 correctListToCheck(checkAreas
, text
, new String
[]{"BADD"});
221 correctIgnored(checkAreas
, text
, new String
[]{});
223 public void testWordInCapsAfterDelimeter3() {
224 String text
= ";BADD;";
225 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
226 correctListToCheck(checkAreas
, text
, new String
[]{"BADD"});
227 correctIgnored(checkAreas
, text
, new String
[]{});
229 @Bombed(user
= "shkate", month
= Calendar
.NOVEMBER
, day
= 20, year
= 2009,
230 description
= "solve problems with encoding when run on server")
231 public void testWordWithUmlauts() {
232 String text
= "rechtsbündig";
233 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
234 correctListToCheck(checkAreas
, text
, new String
[]{"rechtsbündig"});
235 correctIgnored(checkAreas
, text
, new String
[]{});
238 @Bombed(user
= "shkate", month
= Calendar
.NOVEMBER
, day
= 20, year
= 2009,
239 description
= "solve problems with encoding when run on server")
240 public void testWordUpperCasedWithUmlauts() {
241 String text
= "rechtsbündig".toUpperCase();
242 List
<CheckArea
> checkAreas
= TextSplitter
.splitText(text
);
243 correctListToCheck(checkAreas
, text
, new String
[]{"rechtsbündig".toUpperCase()});
244 correctIgnored(checkAreas
, text
, new String
[]{});
249 private static List
<String
> wordsToCheck(List
<CheckArea
> toCheck
, String text
) {
250 if (text
== null || toCheck
== null) return null;
251 List
<String
> words
= new ArrayList
<String
>();
252 for (CheckArea area
: toCheck
) {
253 if (!area
.isIgnored()) {
254 words
.add(area
.getWord());
257 return (words
.size() != 0) ? words
: null;
262 private static List
<String
> wordsToIgnore(List
<CheckArea
> toCheck
, String text
) {
263 if (text
== null || toCheck
== null) return null;
264 List
<String
> words
= new ArrayList
<String
>();
265 for (CheckArea area
: toCheck
) {
266 if (area
.isIgnored()) {
267 words
.add(area
.getWord());
270 return (words
.size() != 0) ? words
: null;
273 private static void correctListToCheck(List
<CheckArea
> toCheck
, String text
, @NotNull String
[] expected
) {
274 List
<String
> words
= wordsToCheck(toCheck
, text
);
275 if (expected
.length
== 0) {
276 Assert
.assertNull(words
);
279 Assert
.assertNotNull(words
);
280 Assert
.assertEquals(expected
.length
, words
.size());
281 List
<String
> expectedWords
= Arrays
.asList(expected
);
282 Assert
.assertEquals( expectedWords
,words
);
286 private static void correctIgnored(List
<CheckArea
> toCheck
, String text
, @NotNull String
[] expected
) {
287 List
<String
> words
= wordsToIgnore(toCheck
, text
);
288 if (expected
.length
== 0) {
289 Assert
.assertNull(words
);
292 Assert
.assertNotNull(words
);
293 Assert
.assertEquals(expected
.length
, words
.size());
294 List
<String
> expectedWords
= Arrays
.asList(expected
);
295 Assert
.assertEquals( expectedWords
,words
);