ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / plugins / spellchecker / testSrc / com / intellij / spellchecker / inspector / SplitterTest.java
blobb76cb2fcedbcb4c07168c5fe2875a933afa01f36
1 /*
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.spellchecker.inspections.CheckArea;
19 import com.intellij.spellchecker.inspections.TextSplitter;
20 import junit.framework.Assert;
21 import junit.framework.TestCase;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
30 public class SplitterTest extends TestCase {
33 public void testSplitSimpleCamelCase() {
34 String text = "simpleCamelCase";
35 List<CheckArea> checkAreas = TextSplitter.splitText(text);
36 correctListToCheck(checkAreas, text, new String[]{"simple", "Camel", "Case"});
37 correctIgnored(checkAreas, text, new String[]{});
40 public void testSplitCamelCaseWithUpperCasedWord() {
41 String text = "camelCaseJSP";
42 List<CheckArea> checkAreas = TextSplitter.splitText(text);
43 correctListToCheck(checkAreas, text, new String[]{"camel", "Case"});
44 correctIgnored(checkAreas, text, new String[]{"JSP"});
47 public void testCapitalizedWithShortWords() {
48 String text = "IntelliJ";
49 List<CheckArea> checkAreas = TextSplitter.splitText(text);
50 correctListToCheck(checkAreas, text, new String[]{});
51 correctIgnored(checkAreas, text, new String[]{"IntelliJ"});
54 public void testCapitalizedWithShortAndLongWords() {
55 String text = "IntelliJTestTest";
56 List<CheckArea> checkAreas = TextSplitter.splitText(text);
57 correctListToCheck(checkAreas, text, new String[]{});
58 correctIgnored(checkAreas, text, new String[]{"IntelliJTestTest"});
61 public void testWordWithApostrophe1() {
62 String text = "don't check";
63 List<CheckArea> checkAreas = TextSplitter.splitText(text);
64 correctListToCheck(checkAreas, text, new String[]{"don't", "check"});
65 correctIgnored(checkAreas, text, new String[]{});
69 public void testWordWithApostrophe2() {
70 String text = "customers'";
71 List<CheckArea> checkAreas = TextSplitter.splitText(text);
72 correctListToCheck(checkAreas, text, new String[]{"customers"});
73 correctIgnored(checkAreas, text, new String[]{});
76 public void testWordWithApostrophe3() {
77 String text = "customer's";
78 List<CheckArea> checkAreas = TextSplitter.splitText(text);
79 correctListToCheck(checkAreas, text, new String[]{"customer's"});
80 correctIgnored(checkAreas, text, new String[]{});
84 public void testWordWithApostrophe4() {
85 String text = "we'll";
86 List<CheckArea> checkAreas = TextSplitter.splitText(text);
87 correctListToCheck(checkAreas, text, new String[]{"we'll"});
88 correctIgnored(checkAreas, text, new String[]{});
91 public void testWordWithApostrophe5() {
92 String text = "I'm you're we'll";
93 List<CheckArea> checkAreas = TextSplitter.splitText(text);
94 correctListToCheck(checkAreas, text, new String[]{"you're","we'll"});
95 correctIgnored(checkAreas, text, new String[]{"I'm"});
98 public void testConstantName() {
99 String text = "TEST_CONSTANT";
100 List<CheckArea> checkAreas = TextSplitter.splitText(text);
101 correctListToCheck(checkAreas, text, new String[]{"TEST","CONSTANT"});
102 correctIgnored(checkAreas, text, new String[]{});
105 public void testLongConstantName() {
106 String text = "TEST_VERY_VERY_LONG_AND_COMPLEX_CONSTANT";
107 List<CheckArea> checkAreas = TextSplitter.splitText(text);
108 correctListToCheck(checkAreas, text, new String[]{"TEST","VERY","VERY","LONG","COMPLEX","CONSTANT"});
109 correctIgnored(checkAreas, text, new String[]{"AND"});
112 public void testJavaComments() {
113 String text = "/*special symbols*/";
114 List<CheckArea> checkAreas = TextSplitter.splitText(text);
115 correctListToCheck(checkAreas, text, new String[]{"special","symbols"});
116 correctIgnored(checkAreas, text, new String[]{});
120 public void testXmlComments() {
121 String text = "<!--special symbols-->";
122 List<CheckArea> checkAreas = TextSplitter.splitText(text);
123 correctListToCheck(checkAreas, text, new String[]{"special","symbols"});
124 correctIgnored(checkAreas, text, new String[]{});
127 public void testCamelCaseInXmlComments() {
128 String text = "<!--specialCase symbols-->";
129 List<CheckArea> checkAreas = TextSplitter.splitText(text);
130 correctListToCheck(checkAreas, text, new String[]{"special","Case","symbols"});
131 correctIgnored(checkAreas, text, new String[]{});
134 public void testWordsWithNumbers() {
135 String text = "testCamelCase123";
136 List<CheckArea> checkAreas = TextSplitter.splitText(text);
137 correctListToCheck(checkAreas, text, new String[]{"test","Camel","Case"});
138 correctIgnored(checkAreas, text, new String[]{});
141 public void testCommentsWithWordsWithNumbers() {
142 String text = "<!--specialCase456 symbols-->";
143 List<CheckArea> checkAreas = TextSplitter.splitText(text);
144 correctListToCheck(checkAreas, text, new String[]{"special","Case","symbols"});
145 correctIgnored(checkAreas, text, new String[]{});
148 public void testCommentsWithAbr() {
149 String text = "<!--JSPTestClass-->";
150 List<CheckArea> checkAreas = TextSplitter.splitText(text);
151 correctListToCheck(checkAreas, text, new String[]{"Test","Class"});
152 correctIgnored(checkAreas, text, new String[]{"JSP"});
155 public void testStringLiterals() {
156 String text = "test\ntest\n";
157 List<CheckArea> checkAreas = TextSplitter.splitText(text);
158 correctListToCheck(checkAreas, text, new String[]{"test", "test"});
159 correctIgnored(checkAreas, text, new String[]{});
162 public void testCommentWithHtml() {
163 String text = "<!--<li style='color:red;'>something go here</li> foooo barrrr <p> text text -->";
164 List<CheckArea> checkAreas = TextSplitter.splitText(text);
165 correctListToCheck(checkAreas, text, new String[]{"something","here","foooo","barrrr","text", "text"});
166 correctIgnored(checkAreas, text, new String[]{"go"});
169 public void testSpecial() {
170 String text = "test &nbsp; test";
171 List<CheckArea> checkAreas = TextSplitter.splitText(text);
172 correctListToCheck(checkAreas, text, new String[]{"test", "test"});
173 correctIgnored(checkAreas, text, new String[]{"&nbsp;"});
176 public void testTooShort() {
177 String text = "bgColor carLight";
178 List<CheckArea> checkAreas = TextSplitter.splitText(text);
179 correctListToCheck(checkAreas, text, new String[]{"Color", "Light"});
180 correctIgnored(checkAreas, text, new String[]{"bg","car"});
183 public void testComplex() {
184 String text = "shkate@gmail.com";
185 List<CheckArea> checkAreas = TextSplitter.splitText(text);
186 correctListToCheck(checkAreas, text, new String[]{});
187 correctIgnored(checkAreas, text, new String[]{});
190 public void testWordBeforeDelimeter() {
191 String text = "badd,";
192 List<CheckArea> checkAreas = TextSplitter.splitText(text);
193 correctListToCheck(checkAreas, text, new String[]{"badd"});
194 correctIgnored(checkAreas, text, new String[]{});
196 public void testWordAfterDelimeter() {
197 String text = ",badd";
198 List<CheckArea> checkAreas = TextSplitter.splitText(text);
199 correctListToCheck(checkAreas, text, new String[]{"badd"});
200 correctIgnored(checkAreas, text, new String[]{});
203 public void testWordInCapsBeforeDelimeter() {
204 String text = "BADD,";
205 List<CheckArea> checkAreas = TextSplitter.splitText(text);
206 correctListToCheck(checkAreas, text, new String[]{"BADD"});
207 correctIgnored(checkAreas, text, new String[]{});
209 public void testWordInCapsAfterDelimeter() {
210 String text = ",BADD";
211 List<CheckArea> checkAreas = TextSplitter.splitText(text);
212 correctListToCheck(checkAreas, text, new String[]{"BADD"});
213 correctIgnored(checkAreas, text, new String[]{});
215 public void testWordInCapsAfterDelimeter2() {
216 String text = "BADD;";
217 List<CheckArea> checkAreas = TextSplitter.splitText(text);
218 correctListToCheck(checkAreas, text, new String[]{"BADD"});
219 correctIgnored(checkAreas, text, new String[]{});
221 public void testWordInCapsAfterDelimeter3() {
222 String text = ";BADD;";
223 List<CheckArea> checkAreas = TextSplitter.splitText(text);
224 correctListToCheck(checkAreas, text, new String[]{"BADD"});
225 correctIgnored(checkAreas, text, new String[]{});
228 public void testWordWithUmlauts() {
229 String text = "rechtsb\u00FCndig";
230 List<CheckArea> checkAreas = TextSplitter.splitText(text);
231 correctListToCheck(checkAreas, text, new String[]{text});
232 correctIgnored(checkAreas, text, new String[]{});
236 public void testWordUpperCasedWithUmlauts() {
237 String text = "RECHTSB\u00DCNDIG";
238 List<CheckArea> checkAreas = TextSplitter.splitText(text);
239 correctListToCheck(checkAreas, text, new String[]{text});
240 correctIgnored(checkAreas, text, new String[]{});
244 @Nullable
245 private static List<String> wordsToCheck(List<CheckArea> toCheck, String text) {
246 if (text == null || toCheck == null) return null;
247 List<String> words = new ArrayList<String>();
248 for (CheckArea area : toCheck) {
249 if (!area.isIgnored()) {
250 words.add(area.getWord());
253 return (words.size() != 0) ? words : null;
257 @Nullable
258 private static List<String> wordsToIgnore(List<CheckArea> toCheck, String text) {
259 if (text == null || toCheck == null) return null;
260 List<String> words = new ArrayList<String>();
261 for (CheckArea area : toCheck) {
262 if (area.isIgnored()) {
263 words.add(area.getWord());
266 return (words.size() != 0) ? words : null;
269 private static void correctListToCheck(List<CheckArea> toCheck, String text, @NotNull String[] expected) {
270 List<String> words = wordsToCheck(toCheck, text);
271 if (expected.length == 0) {
272 Assert.assertNull(words);
274 else {
275 Assert.assertNotNull(words);
276 Assert.assertEquals(expected.length, words.size());
277 List<String> expectedWords = Arrays.asList(expected);
278 Assert.assertEquals( expectedWords,words);
282 private static void correctIgnored(List<CheckArea> toCheck, String text, @NotNull String[] expected) {
283 List<String> words = wordsToIgnore(toCheck, text);
284 if (expected.length == 0) {
285 Assert.assertNull(words);
287 else {
288 Assert.assertNotNull(words);
289 Assert.assertEquals(expected.length, words.size());
290 List<String> expectedWords = Arrays.asList(expected);
291 Assert.assertEquals( expectedWords,words);