PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 0ms 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 */
 20package org.jedit.ruby.test;
 21
 22import junit.framework.TestCase;
 23import org.jedit.ruby.completion.CodeAnalyzer;
 24import org.jedit.ruby.utils.EditorView;
 25
 26import java.util.List;
 27import java.util.regex.Pattern;
 28import java.util.regex.Matcher;
 29import java.util.regex.MatchResult;
 30
 31/**
 32 * @author robmckinnon at users.sourceforge.net
 33 */
 34public final class TestCodeAnalyzer extends TestCase {
 35
 36    private static final String TEXT = "a.respond_to?() \n" +
 37            "a#respond_to?() \n" +
 38            "a#respond_to? tree\n" +
 39            "a#respond_to(tree)\n" +
 40                    "a.[] \n" +
 41                    "3 \n";
 42
 43    public final void testMethodCalledOnThis() {
 44        String line = "        a.g";
 45        MatchResult match = CodeAnalyzer.getMatch(line);
 46        String methodCalledOnThis = CodeAnalyzer.getMethodCalledOnThis(match);
 47        assertEquals("Assert method called on this correct.", "a", methodCalledOnThis);
 48    }
 49
 50    public final void testOutsideMember() {
 51        CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView("", 0));
 52        assertCorrect(analyzer, null, null, false);
 53    }
 54
 55    public final void testPartialClass() {
 56        String clas = "Arr";
 57        CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(clas, clas.length()));
 58        assertCorrect(analyzer, null, clas, false);
 59    }
 60
 61    public final void testDotCompletePartialClass() {
 62        String clas = "Red::arr";
 63        CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(clas, clas.length()));
 64        assertCorrect(analyzer, "arr", null, true);
 65    }
 66
 67    public final void testPartialNamespaceClass() {
 68        String clas = "Red::Arr";
 69        CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(clas, clas.length()));
 70        assertCorrect(analyzer, null, clas, false);
 71    }
 72
 73    public final void testPartialMethod() {
 74        String method = "to_";
 75        CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(method, 3));
 76        assertCorrect(analyzer, method, null, false);
 77    }
 78
 79    public final void testDotCompleteMatchFalse() {
 80        boolean match = CodeAnalyzer.DotCompleteRegExp.instance.isMatch("ActiveRecord:");
 81        assertEquals("Assert dot completion match correct.", false, match);
 82    }
 83
 84    public final void testDotCompleteMatch() {
 85        boolean match = CodeAnalyzer.DotCompleteRegExp.instance.isMatch("ActiveRecord::");
 86        assertEquals("Assert dot completion match correct.", true, match);
 87    }
 88
 89    public final void testPartialClassPlusColonDotCompleteMatchFalse() {
 90        String text = "ActiveRecord:";
 91        CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(text, text.length()));
 92        assertCorrect(analyzer, null, text, false);
 93    }
 94
 95    public final void testPartialClassPlusColon() {
 96        String text = "ActiveRecord::";
 97        CodeAnalyzer analyzer = new CodeAnalyzer(new MockEditorView(text, text.length()));
 98        assertCorrect(analyzer, null, text, true);
 99    }
100
101    public final void testClassMatch() {
102        Pattern pattern = Pattern.compile("((@@|@|$)?\\S+(::\\w+)?)(\\.|::|#)((?:[^A-Z]\\S*)?)$");
103        Matcher matcher = pattern.matcher("      a.");
104        boolean match = matcher.find();
105
106        assertEquals("Assert class match correct.", true, match);
107    }
108
109    public final void testClassMatchOld() {
110        boolean match = CodeAnalyzer.ClassNameRegExp.instance.isMatch("ActiveRecord:");
111        assertEquals("Assert class match correct.", true, match);
112        match = CodeAnalyzer.ClassNameRegExp.instance.isMatch("ActiveRecord::");
113        assertEquals("Assert class match correct.", true, match);
114    }
115
116    private static void assertCorrect(CodeAnalyzer analyzer, String partialMethod, String partialClass, boolean isDotCompletion) {
117        assertEquals("Assert dot completion point correct", isDotCompletion, analyzer.isDotInsertionPoint());
118        assertEquals("Assert partial method correct", partialMethod, analyzer.getPartialMethod());
119        assertEquals("Assert partial class correct", partialClass, analyzer.getPartialClass());
120    }
121
122    public final void testFindMethod1() {
123        List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
124        assertEquals("assert found method", "respond_to?", methods.get(0));
125    }
126
127    public final void testFindMethod2() {
128        List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
129        assertEquals("assert found method", "respond_to?", methods.get(1));
130    }
131
132    public final void testFindMethod3() {
133        List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
134        assertEquals("assert found method", "respond_to?", methods.get(2));
135    }
136
137    public final void testFindMethod4() {
138        List<String> methods = CodeAnalyzer.getMethodsCalledOnVariable(TEXT, "a");
139        assertEquals("assert found method", "respond_to", methods.get(3));
140    }
141
142    public final void testClassName() {
143        boolean isClass = CodeAnalyzer.isClass("REXML::Element");
144        assertEquals("assert class name recognized", true, isClass);
145    }
146
147    public final void testClassName2() {
148        boolean isClass = CodeAnalyzer.isClass("IO::puts");
149        assertEquals("assert class method recognized", false, isClass);
150    }
151
152    public final void testIsDotInsertionPointWithIfStatement() {
153        assertTrue(CodeAnalyzer.isDotInsertionPoint("if a."));
154    }
155
156    public final void testIsDotInsertionPointWithOpenParenthesis() {
157        assertTrue(CodeAnalyzer.isDotInsertionPoint("(a."));
158    }
159
160    public final void testIsDotInsertionPointWithOpenBrace() {
161        assertTrue(CodeAnalyzer.isDotInsertionPoint("{a."));
162    }
163
164    public final void testIsDotInsertionPointWithOpenBracket() {
165        assertTrue(CodeAnalyzer.isDotInsertionPoint("[a."));
166    }
167
168    public final void testIsDotInsertionPointWithKeyAssignmentOperator() {
169        assertTrue(CodeAnalyzer.isDotInsertionPoint("b=>a."));
170    }
171
172    public final void testIsDotInsertionPointWithArrayComma() {
173        assertTrue(CodeAnalyzer.isDotInsertionPoint("c,a."));
174    }
175
176    public final void testFindMethod() {
177        MatchResult match = CodeAnalyzer.DotCompleteRegExp.instance.lastMatch("        [a.adapter_name,b.", 5);
178        assertEquals("b", match.group(1));
179    }
180
181    private static final class MockEditorView extends EditorView.NullEditorView {
182        private final String text;
183        private final int caret;
184
185        public MockEditorView(String text, int caret) {
186            this.text = text;
187            this.caret = caret;
188        }
189
190        public int getCaretPosition() {
191            return caret;
192        }
193
194        public String getLineUpToCaret() {
195            return text.substring(0, caret);
196        }
197
198        public String getLineAfterCaret() {
199            int start = caret + 1;
200            int end = text.length() - 1;
201            if (end > start) {
202                return text.substring(start, end);
203            } else {
204                return "";
205            }
206        }
207
208        public String getText(int start, int length) {
209            return text.substring(start, start+length);
210        }
211
212        public int getLength() {
213            return text.length();
214        }
215    }
216}