/plugins/spellchecker/testSrc/com/intellij/spellchecker/inspector/SplitterTest.java

https://bitbucket.org/nbargnesi/idea · Java · 407 lines · 289 code · 103 blank · 15 comment · 5 complexity · 9455012a15a10c7e4855e708df2a3a59 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. package com.intellij.spellchecker.inspector;
  17. import com.intellij.openapi.util.TextRange;
  18. import com.intellij.spellchecker.inspections.*;
  19. import com.intellij.util.Consumer;
  20. import junit.framework.Assert;
  21. import junit.framework.TestCase;
  22. import org.jetbrains.annotations.NotNull;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.util.ArrayList;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. public class SplitterTest extends TestCase {
  31. public void testSplitSimpleCamelCase() {
  32. String text = "simpleCamelCase";
  33. correctListToCheck(IdentifierSplitter.getInstance(), text, "simple", "Camel", "Case");
  34. }
  35. public void testSplitCamelCaseWithUpperCasedWord() {
  36. String text = "camelCaseJSP";
  37. correctListToCheck(IdentifierSplitter.getInstance(), text, "camel", "Case");
  38. }
  39. public void testArrays() {
  40. String text = "Token[]";
  41. correctListToCheck(IdentifierSplitter.getInstance(), text, "Token");
  42. }
  43. public void testIdentifierInSingleQuotes() {
  44. String text = "'fill'";
  45. correctListToCheck(IdentifierSplitter.getInstance(), text, "fill");
  46. }
  47. public void testWordsInSingleQuotesWithSep() {
  48. String text = "'test-something'";
  49. correctListToCheck(PlainTextSplitter.getInstance(), text, "test", "something");
  50. }
  51. public void testComplexWordsInQuotes() {
  52. String text = "\"test-customer's'\"";
  53. correctListToCheck(PlainTextSplitter.getInstance(), text, "test", "customer's");
  54. }
  55. public void testCapitalizedWithShortWords() {
  56. String text = "IntelliJ";
  57. correctListToCheck(IdentifierSplitter.getInstance(), text, "Intelli");
  58. }
  59. public void testWords() {
  60. String text = "first-last";
  61. correctListToCheck(IdentifierSplitter.getInstance(), text, "first", "last");
  62. }
  63. public void testCapitalizedWithShortAndLongWords() {
  64. String text = "IntelliJTestTest";
  65. correctListToCheck(IdentifierSplitter.getInstance(), text, "Intelli", "Test", "Test");
  66. }
  67. public void testWordWithApostrophe1() {
  68. String text = "don't check";
  69. correctListToCheck(PlainTextSplitter.getInstance(), text, "don't", "check");
  70. }
  71. public void testHexInPlainText() {
  72. String text = "some text 0xacvfgt";
  73. correctListToCheck(PlainTextSplitter.getInstance(), text, "some", "text");
  74. }
  75. public void testHexInStringLiteral() {
  76. String text = "qwerty 0x12acfgt test";
  77. correctListToCheck(PlainTextSplitter.getInstance(), text, "qwerty", "test");
  78. }
  79. public void testHex() {
  80. String text = "0xacvfgt";
  81. correctListToCheck(WordSplitter.getInstance(), text);
  82. }
  83. public void testCheckXmlIgnored() {
  84. String text = "abcdef" + new String(new char[]{0xDC00}) + "test";
  85. correctListToCheck(PlainTextSplitter.getInstance(), text);
  86. }
  87. public void testIdentifiersWithNumbers() {
  88. String text = "result1";
  89. correctListToCheck(IdentifierSplitter.getInstance(), text, "result");
  90. }
  91. public void testIdentifiersWithNumbersInside() {
  92. String text = "result1result";
  93. correctListToCheck(IdentifierSplitter.getInstance(), text, "result","result");
  94. }
  95. public void testWordWithApostrophe2() {
  96. String text = "customers'";
  97. correctListToCheck(PlainTextSplitter.getInstance(), text, "customers");
  98. }
  99. public void testWordWithApostrophe3() {
  100. String text = "customer's";
  101. correctListToCheck(PlainTextSplitter.getInstance(), text, "customer's");
  102. }
  103. public void testWordWithApostrophe4() {
  104. String text = "we'll";
  105. correctListToCheck(PlainTextSplitter.getInstance(), text, "we'll");
  106. }
  107. public void testWordWithApostrophe5() {
  108. String text = "I'm you're we'll";
  109. correctListToCheck(PlainTextSplitter.getInstance(), text, "you're", "we'll");
  110. }
  111. public void testConstantName() {
  112. String text = "TEST_CONSTANT";
  113. correctListToCheck(IdentifierSplitter.getInstance(), text, "TEST", "CONSTANT");
  114. }
  115. public void testLongConstantName() {
  116. String text = "TEST_VERY_VERY_LONG_AND_COMPLEX_CONSTANT";
  117. correctListToCheck(IdentifierSplitter.getInstance(), text, "TEST", "VERY", "VERY", "LONG", "COMPLEX", "CONSTANT");
  118. }
  119. public void testJavaComments() {
  120. String text = "/*special symbols*/";
  121. correctListToCheck(CommentSplitter.getInstance(), text, "special", "symbols");
  122. }
  123. public void testXmlComments() {
  124. String text = "<!--special symbols-->";
  125. correctListToCheck(CommentSplitter.getInstance(), text, "special", "symbols");
  126. }
  127. public void testCamelCaseInXmlComments() {
  128. String text = "<!--specialCase symbols-->";
  129. correctListToCheck(CommentSplitter.getInstance(), text, "special", "Case", "symbols");
  130. }
  131. public void testWordsWithNumbers() {
  132. String text = "testCamelCase123";
  133. correctListToCheck(IdentifierSplitter.getInstance(), text, "test", "Camel", "Case");
  134. }
  135. public void testCommentsWithWordsWithNumbers() {
  136. String text = "<!--specialCase456 symbols-->";
  137. correctListToCheck(CommentSplitter.getInstance(), text, "special", "Case", "symbols");
  138. }
  139. public void testCommentsWithAbr() {
  140. String text = "<!--JSPTestClass-->";
  141. correctListToCheck(CommentSplitter.getInstance(), text, "Test", "Class");
  142. }
  143. public void testStringLiterals() {
  144. String text = "test\ntest\n";
  145. correctListToCheck(PlainTextSplitter.getInstance(), text, "test", "test");
  146. }
  147. public void testCommentWithHtml() {
  148. String text = "<!--<li>something go here</li> <li>next content</li> foooo barrrr <p> text -->";
  149. correctListToCheck(CommentSplitter.getInstance(), text, "something", "here", "next", "content", "foooo", "barrrr",
  150. "text");
  151. }
  152. public void testCommentWithHtmlTagsAndAtr() {
  153. String text = "<!-- <li style='color:red;'>something go here</li> foooo <li style='color:red;'>barrrr</li> <p> text text -->";
  154. correctListToCheck(CommentSplitter.getInstance(), text, "something", "here", "foooo", "barrrr", "text", "text");
  155. }
  156. public void testSpecial() {
  157. String text = "test &nbsp; test";
  158. correctListToCheck(PlainTextSplitter.getInstance(), text, "test", "test");
  159. }
  160. public void testColorUC() {
  161. String text = "#AABBFF";
  162. correctListToCheck(WordSplitter.getInstance(), text);
  163. }
  164. public void testColorUCSC() {
  165. String text = "#AABBFF;";
  166. correctListToCheck(WordSplitter.getInstance(), text);
  167. }
  168. public void testColorUCSurrounded() {
  169. String text = "\"#AABBFF\"";
  170. correctListToCheck(WordSplitter.getInstance(), text);
  171. }
  172. public void testColorLC() {
  173. String text = "#fff";
  174. correctListToCheck(TextSplitter.getInstance(), text);
  175. }
  176. public void testTooShort() {
  177. String text = "bgColor carLight";
  178. correctListToCheck(PlainTextSplitter.getInstance(), text, "Color", "Light");
  179. }
  180. public void testPhpVariableCorrectSimple() {
  181. String text = "$this";
  182. correctListToCheck(IdentifierSplitter.getInstance(), text, "this");
  183. }
  184. public void testPhpVariableCorrect() {
  185. String text = "$this_this$this";
  186. correctListToCheck(IdentifierSplitter.getInstance(), text, "this", "this", "this");
  187. }
  188. public void testEmail() {
  189. String text = "some text with email (shkate.test@gmail.com) inside";
  190. correctListToCheck(PlainTextSplitter.getInstance(), text, "some", "text", "with", "email", "inside");
  191. }
  192. public void testEmailOnly() {
  193. String text = "shkate123-\u00DC.test@gmail.com";
  194. correctListToCheck(PlainTextSplitter.getInstance(), text);
  195. }
  196. public void testUrl() {
  197. String text = "http://www.jetbrains.com/idea";
  198. correctListToCheck(PlainTextSplitter.getInstance(), text);
  199. }
  200. public void testUrlThenSpaces() {
  201. String text = "http://www.jetbrains.com/idea asdasdasd sdfsdf";
  202. correctListToCheck(PlainTextSplitter.getInstance(), text, "asdasdasd", "sdfsdf");
  203. }
  204. public void testWordBeforeDelimiter() {
  205. String text = "badd,";
  206. correctListToCheck(PlainTextSplitter.getInstance(), text, "badd");
  207. }
  208. public void testWordAfterDelimiter() {
  209. String text = ",badd";
  210. correctListToCheck(PlainTextSplitter.getInstance(), text, "badd");
  211. }
  212. public void testWordInCapsBeforeDelimiter() {
  213. String text = "BADD,";
  214. correctListToCheck(PlainTextSplitter.getInstance(), text, "BADD");
  215. }
  216. public void testWordInCapsAfterDelimiter() {
  217. String text = ",BADD";
  218. correctListToCheck(PlainTextSplitter.getInstance(), text, "BADD");
  219. }
  220. public void testWordInCapsAfterDelimiter2() {
  221. String text = "BADD;";
  222. correctListToCheck(PlainTextSplitter.getInstance(), text, "BADD");
  223. }
  224. public void testWordInCapsAfterDelimiter3() {
  225. String text = ";BADD;";
  226. correctListToCheck(PlainTextSplitter.getInstance(), text, "BADD");
  227. }
  228. public void testWordWithUmlauts() {
  229. String text = "rechtsb\u00FCndig";
  230. correctListToCheck(PlainTextSplitter.getInstance(), text, text);
  231. }
  232. public void testWordUpperCasedWithUmlauts() {
  233. String text = "RECHTSB\u00DCNDIG";
  234. correctListToCheck(PlainTextSplitter.getInstance(), text, text);
  235. }
  236. public void testCommaSeparatedList() {
  237. String text = "properties,test,properties";
  238. correctListToCheck(PlainTextSplitter.getInstance(), text, "properties", "test", "properties");
  239. }
  240. public void testSemicolonSeparatedList() {
  241. String text = "properties;test;properties";
  242. correctListToCheck(PlainTextSplitter.getInstance(), text, "properties", "test", "properties");
  243. }
  244. public void testProperties1() {
  245. String text = "properties.test.properties";
  246. correctListToCheck(PropertiesSplitter.getInstance(), text, "properties", "test", "properties");
  247. }
  248. public void testPropertiesWithCamelCase() {
  249. String text = "upgrade.testCommit.propertiesSomeNews";
  250. correctListToCheck(PropertiesSplitter.getInstance(), text, "upgrade", "test", "Commit", "properties", "Some",
  251. "News");
  252. }
  253. public void testWordUpperCasedWithUmlautsInTheBeginning() {
  254. String text = "\u00DCNDIG";
  255. correctListToCheck(PlainTextSplitter.getInstance(), text, text);
  256. }
  257. public void testTCData() {
  258. final InputStream stream = SplitterTest.class.getResourceAsStream("contents.txt");
  259. String text = convertStreamToString(stream);
  260. List<String> words = wordsToCheck(PlainTextSplitter.getInstance(), text);
  261. assertEquals(0, words.size());
  262. }
  263. private static List<String> wordsToCheck(Splitter splitter, final String text) {
  264. final List<String> words = new ArrayList<String>();
  265. splitter.split(text, TextRange.allOf(text), new Consumer<TextRange>() {
  266. @Override
  267. public void consume(TextRange textRange) {
  268. words.add(textRange.substring(text));
  269. }
  270. });
  271. return words;
  272. }
  273. private static void correctListToCheck(Splitter splitter, String text, @NotNull String... expected) {
  274. List<String> words = wordsToCheck(splitter, text);
  275. List<String> expectedWords = Arrays.asList(expected);
  276. Assert.assertEquals("Splitting:'" + text + "'", expectedWords.toString(), words!=null ? words.toString() : "[]");
  277. }
  278. private String convertStreamToString(InputStream is) {
  279. if (is != null) {
  280. StringBuilder sb = new StringBuilder();
  281. String line;
  282. try {
  283. BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  284. while ((line = reader.readLine()) != null) {
  285. sb.append(line).append("\n");
  286. }
  287. }
  288. catch (Exception e) {
  289. throw new RuntimeException(e);
  290. }
  291. finally {
  292. try {
  293. is.close();
  294. }
  295. catch (IOException ignore) {
  296. }
  297. }
  298. return sb.toString();
  299. }
  300. else {
  301. return "";
  302. }
  303. }
  304. }