KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > RubyLexerTest


1 /*
2  * RubyLexerTest.java
3  *
4  * Created on November 28, 2006, 8:46 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9 package org.netbeans.modules.ruby;
10
11 import junit.framework.TestCase;
12
13 import org.netbeans.api.lexer.TokenHierarchy;
14 import org.netbeans.api.lexer.TokenSequence;
15 import org.netbeans.lib.lexer.test.LexerTestUtilities;
16 import org.netbeans.modules.ruby.lexer.RubyTokenId;
17 import org.netbeans.modules.ruby.lexer.RubyTokenId;
18
19
20 /**
21  *
22  * @author Tor Norbye
23  */

24 public class RubyLexerTest extends TestCase {
25     public RubyLexerTest(String JavaDoc testName) {
26         super(testName);
27     }
28
29     protected void setUp() throws java.lang.Exception JavaDoc {
30         // Set-up testing environment
31
LexerTestUtilities.setTesting(true);
32     }
33
34     protected void tearDown() throws java.lang.Exception JavaDoc {
35     }
36
37     public void testComments() {
38         String JavaDoc text = "# This is my comment";
39         TokenHierarchy hi = TokenHierarchy.create(text, RubyTokenId.language());
40         TokenSequence ts = hi.tokenSequence();
41         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.LINE_COMMENT, text);
42     }
43
44     public void testRubyEmbedding() {
45         String JavaDoc text = "%r{foo#{code}bar}";
46         TokenHierarchy hi = TokenHierarchy.create(text, RubyTokenId.language());
47         TokenSequence ts = hi.tokenSequence();
48         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.REGEXP_BEGIN, "%r{");
49         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.REGEXP_LITERAL, "foo");
50         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.REGEXP_LITERAL, "#{");
51         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.EMBEDDED_RUBY, "code");
52         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.REGEXP_LITERAL, "}bar");
53     }
54
55     public void testStatementModifiers() {
56         String JavaDoc text = "foo if false}";
57         TokenHierarchy hi = TokenHierarchy.create(text, RubyTokenId.language());
58         TokenSequence ts = hi.tokenSequence();
59         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.IDENTIFIER, "foo");
60         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.WHITESPACE, " ");
61         // Not RubyTokenId.IF - test that this if is just a statement modifier!
62
LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.ANY_KEYWORD, "if");
63         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.WHITESPACE, " ");
64         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.ANY_KEYWORD, "false");
65         
66         // Make sure if when used not as a statement modifier is recognized
67
text = "if false foo}";
68         hi = TokenHierarchy.create(text, RubyTokenId.language());
69         ts = hi.tokenSequence();
70         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.IF, "if");
71         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.WHITESPACE, " ");
72         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.ANY_KEYWORD, "false");
73         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.WHITESPACE, " ");
74         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.IDENTIFIER, "foo");
75     }
76     
77     public void testStrings() {
78         String JavaDoc[] strings =
79             new String JavaDoc[] {
80             "\"Hello\"",
81             "'Hello'",
82             "%(Hello)",
83             "%q(Hello)",
84             "% Hello "};
85         for (int i = 0; i < strings.length; i++) {
86             TokenHierarchy hi = TokenHierarchy.create(strings[i], RubyTokenId.language());
87             TokenSequence ts = hi.tokenSequence();
88             assertTrue(ts.moveNext());
89             assertTrue(ts.token().id() == RubyTokenId.STRING_BEGIN || ts.token().id() == RubyTokenId.QUOTED_STRING_BEGIN);
90             assertTrue(ts.moveNext());
91             assertTrue(ts.token().id() == RubyTokenId.STRING_LITERAL || ts.token().id() == RubyTokenId.QUOTED_STRING_LITERAL);
92             assertTrue(ts.moveNext());
93             assertTrue(ts.token().id() == RubyTokenId.STRING_END || ts.token().id() == RubyTokenId.QUOTED_STRING_END);
94         }
95     }
96
97     public void test96485() {
98         String JavaDoc text = "\"foo#{\"";
99         TokenHierarchy hi = TokenHierarchy.create(text, RubyTokenId.language());
100         TokenSequence ts = hi.tokenSequence();
101         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.QUOTED_STRING_BEGIN, "\"");
102         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.QUOTED_STRING_LITERAL, "foo");
103         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.STRING_LITERAL, "#{");
104         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.QUOTED_STRING_END, "\"");
105
106         // Try related scenario for fields
107
text = "\"foo#@\"";
108         hi = TokenHierarchy.create(text, RubyTokenId.language());
109         ts = hi.tokenSequence();
110         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.QUOTED_STRING_BEGIN, "\"");
111         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.QUOTED_STRING_LITERAL, "foo");
112         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.STRING_LITERAL, "#");
113         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.EMBEDDED_RUBY, "@");
114         LexerTestUtilities.assertNextTokenEquals(ts, RubyTokenId.QUOTED_STRING_END, "\"");
115     }
116 }
117
Popular Tags