PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/test/TestCodeAnalyzer.java

#
Java | 216 lines | 156 code | 38 blank | 22 comment | 2 complexity | 4e6a8b834d02eff6ccfad98cca625ae9 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * TestCodeAnalyzer.java -
  3. *
  4. * Copyright 2005 Robert McKinnon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.jedit.ruby.test;
  21. import junit.framework.TestCase;
  22. import org.jedit.ruby.completion.CodeAnalyzer;
  23. import org.jedit.ruby.utils.EditorView;
  24. import java.util.List;
  25. import java.util.regex.Pattern;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.MatchResult;
  28. /**
  29. * @author robmckinnon at users.sourceforge.net
  30. */
  31. public final class TestCodeAnalyzer extends TestCase {
  32. private static final String TEXT = "a.respond_to?() \n" +
  33. "a#respond_to?() \n" +
  34. "a#respond_to? tree\n" +
  35. "a#respond_to(tree)\n" +
  36. "a.[] \n" +
  37. "3 \n";
  38. public final void testMethodCalledOnThis() {
  39. String line = " a.g";
  40. MatchResult match = CodeAnalyzer.getMatch(line);
  41. String methodCalledOnThis = CodeAnalyzer.getMethodCalledOnThis(match);
  42. assertEquals("Assert method called on this correct.", "a", methodCalledOnThis);
  43. }
  44. public final void testOutsideMember() {
  45. CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView("", 0));
  46. assertCorrect(analyzer, null, null, false);
  47. }
  48. public final void testPartialClass() {
  49. String clas = "Arr";
  50. CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(clas, clas.length()));
  51. assertCorrect(analyzer, null, clas, false);
  52. }
  53. public final void testDotCompletePartialClass() {
  54. String clas = "Red::arr";
  55. CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(clas, clas.length()));
  56. assertCorrect(analyzer, "arr", null, true);
  57. }
  58. public final void testPartialNamespaceClass() {
  59. String clas = "Red::Arr";
  60. CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(clas, clas.length()));
  61. assertCorrect(analyzer, null, clas, false);
  62. }
  63. public final void testPartialMethod() {
  64. String method = "to_";
  65. CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(method, 3));
  66. assertCorrect(analyzer, method, null, false);
  67. }
  68. public final void testDotCompleteMatchFalse() {
  69. boolean match = CodeAnalyzer.DotCompleteRegExp.instance.isMatch("ActiveRecord:");
  70. assertEquals("Assert dot completion match correct.", false, match);
  71. }
  72. public final void testDotCompleteMatch() {
  73. boolean match = CodeAnalyzer.DotCompleteRegExp.instance.isMatch("ActiveRecord::");
  74. assertEquals("Assert dot completion match correct.", true, match);
  75. }
  76. public final void testPartialClassPlusColonDotCompleteMatchFalse() {
  77. String text = "ActiveRecord:";
  78. CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(text, text.length()));
  79. assertCorrect(analyzer, null, text, false);
  80. }
  81. public final void testPartialClassPlusColon() {
  82. String text = "ActiveRecord::";
  83. CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(text, text.length()));
  84. assertCorrect(analyzer, null, text, true);
  85. }
  86. public final void testClassMatch() {
  87. Pattern pattern = Pattern.compile("((@@|@|$)?\\S+(::\\w+)?)(\\.|::|#)((?:[^A-Z]\\S*)?)$");
  88. Matcher matcher = pattern.matcher(" a.");
  89. boolean match = matcher.find();
  90. assertEquals("Assert class match correct.", true, match);
  91. }
  92. public final void testClassMatchOld() {
  93. boolean match = CodeAnalyzer.ClassNameRegExp.instance.isMatch("ActiveRecord:");
  94. assertEquals("Assert class match correct.", true, match);
  95. match = CodeAnalyzer.ClassNameRegExp.instance.isMatch("ActiveRecord::");
  96. assertEquals("Assert class match correct.", true, match);
  97. }
  98. private static void assertCorrect(CodeAnalyzer analyzer, String partialMethod, String partialClass, boolean isDotCompletion) {
  99. assertEquals("Assert dot completion point correct", isDotCompletion, analyzer.isDotInsertionPoint());
  100. assertEquals("Assert partial method correct", partialMethod, analyzer.getPartialMethod());
  101. assertEquals("Assert partial class correct", partialClass, analyzer.getPartialClass());
  102. }
  103. public final void testFindMethod1() {
  104. List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
  105. assertEquals("assert found method", "respond_to?", methods.get(0));
  106. }
  107. public final void testFindMethod2() {
  108. List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
  109. assertEquals("assert found method", "respond_to?", methods.get(1));
  110. }
  111. public final void testFindMethod3() {
  112. List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
  113. assertEquals("assert found method", "respond_to?", methods.get(2));
  114. }
  115. public final void testFindMethod4() {
  116. List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
  117. assertEquals("assert found method", "respond_to", methods.get(3));
  118. }
  119. public final void testClassName() {
  120. boolean isClass = CodeAnalyzer.isClass("REXML::Element");
  121. assertEquals("assert class name recognized", true, isClass);
  122. }
  123. public final void testClassName2() {
  124. boolean isClass = CodeAnalyzer.isClass("IO::puts");
  125. assertEquals("assert class method recognized", false, isClass);
  126. }
  127. public final void testIsDotInsertionPointWithIfStatement() {
  128. assertTrue(CodeAnalyzer.isDotInsertionPoint("if a."));
  129. }
  130. public final void testIsDotInsertionPointWithOpenParenthesis() {
  131. assertTrue(CodeAnalyzer.isDotInsertionPoint("(a."));
  132. }
  133. public final void testIsDotInsertionPointWithOpenBrace() {
  134. assertTrue(CodeAnalyzer.isDotInsertionPoint("{a."));
  135. }
  136. public final void testIsDotInsertionPointWithOpenBracket() {
  137. assertTrue(CodeAnalyzer.isDotInsertionPoint("[a."));
  138. }
  139. public final void testIsDotInsertionPointWithKeyAssignmentOperator() {
  140. assertTrue(CodeAnalyzer.isDotInsertionPoint("b=>a."));
  141. }
  142. public final void testIsDotInsertionPointWithArrayComma() {
  143. assertTrue(CodeAnalyzer.isDotInsertionPoint("c,a."));
  144. }
  145. public final void testFindMethod() {
  146. MatchResult match = CodeAnalyzer.DotCompleteRegExp.instance.lastMatch(" [a.adapter_name,b.", 5);
  147. assertEquals("b", match.group(1));
  148. }
  149. private static final class MockEditorView extends EditorView.NullEditorView {
  150. private final String text;
  151. private final int caret;
  152. public MockEditorView(String text, int caret) {
  153. this.text = text;
  154. this.caret = caret;
  155. }
  156. public int getCaretPosition() {
  157. return caret;
  158. }
  159. public String getLineUpToCaret() {
  160. return text.substring(0, caret);
  161. }
  162. public String getLineAfterCaret() {
  163. int start = caret + 1;
  164. int end = text.length() - 1;
  165. if (end > start) {
  166. return text.substring(start, end);
  167. } else {
  168. return "";
  169. }
  170. }
  171. public String getText(int start, int length) {
  172. return text.substring(start, start+length);
  173. }
  174. public int getLength() {
  175. return text.length();
  176. }
  177. }
  178. }