PageRenderTime 40ms CodeModel.GetById 22ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 93 lines | 56 code | 15 blank | 22 comment | 0 complexity | 327514c5a6de4f0e9cfe0e1938fed04f MD5 | raw file
 1/*
 2 * TestRubyCache.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 org.jedit.ruby.cache.RubyCache;
23import org.jedit.ruby.ast.Member;
24import org.jedit.ruby.ast.Method;
25
26import java.util.List;
27
28import junit.framework.TestCase;
29
30/**
31 * @author robmckinnon at users.sourceforge.net
32 */
33public final class TestRubyCache extends TestCase {
34
35    private static void addClassesToCache() {
36        RubyCache.instance().addMembers(TestRubyParser.CLASS, "CLASS");
37        RubyCache.instance().addMembers(TestRubyParser.ARR_CLASS, "ARR_CLASS");
38    }
39
40    private static void addModuleToCache() {
41        RubyCache.instance().addMembers(TestRubyParser.DEF_IN_MODULE, "DEF_IN_MODULE");
42    }
43
44    public final void testGetMethods() {
45        addClassesToCache();
46        List<Method> methods = RubyCache.instance().getMethods("red");
47
48        assertEquals("Assert file name correct", "CLASS", methods.get(0).getFileName());
49        assertEquals("Assert file name correct", "red", methods.get(0).getName());
50    }
51
52    public final void testGetClassByMethod() {
53        addClassesToCache();
54        assertFindByMethodCorrect("red", 0, "Green", 1);
55        assertFindByMethodCorrect("[]", 0, "Green", 1);
56    }
57
58    public final void testGetMethodByClass() {
59        addClassesToCache();
60        assertFindByClassCorrect("Green", 0, "[]", 2, "ARR_CLASS");
61        assertFindByClassCorrect("Green", 1, "red", 2, "CLASS");
62    }
63
64    public final void testGetClassByCombo() {
65        addClassesToCache();
66        addModuleToCache();
67        assertFindByMethodCorrect("red", 0, "Blue", 2);
68        assertFindByMethodCorrect("red", 1, "Green", 2);
69        assertFindByMethodCorrect("[]", 0, "Green", 1);
70    }
71
72    public final void testGetMethodByCombo() {
73        addClassesToCache();
74        addModuleToCache();
75        assertFindByClassCorrect("Green", 0, "[]", 2, "ARR_CLASS");
76        assertFindByClassCorrect("Green", 1, "red", 2, "CLASS");
77        assertFindByClassCorrect("Blue", 0, "red", 1, "DEF_IN_MODULE");
78    }
79
80    private static void assertFindByMethodCorrect(String method, int index, String parentName, int parentCount) {
81        List<Member> members = RubyCache.instance().getMembersWithMethodAsList(method);
82        assertEquals("Assert parent match correct for: " + method, parentCount, members.size());
83        assertEquals("Assert name correct", parentName, members.get(index).getName());
84    }
85
86    private static void assertFindByClassCorrect(String parentName, int index, String methodName, int methodCount, String filePath) {
87        List<Method> members = RubyCache.instance().getMethodsOfMemberAsList(parentName);
88        assertEquals("Assert child match correct", methodCount, members.size());
89        assertEquals("Assert name correct", methodName, members.get(index).getName());
90        assertEquals("Assert path correct", filePath, members.get(index).getFilePath());
91    }
92
93}