6 class TestRubyLexer < Test::Unit::TestCase
7 def deny cond, msg = nil
13 @lex.src = StringIO.new("blah blah")
14 @lex.lex_state = :expr_beg # HACK ? I have no idea actually
18 assert @lex.advance # blah
19 assert @lex.advance # blah
20 deny @lex.advance # nada
23 def test_is_next_identchar
24 assert @lex.is_next_identchar
25 @lex.src = StringIO.new(" ")
26 deny @lex.is_next_identchar
27 @lex.src = StringIO.new("-")
28 deny @lex.is_next_identchar
31 def test_is_next_no_case # TODO: worst name evah
32 @lex.src = StringIO.new("123 456")
33 assert @lex.is_next_no_case("123")
35 deny @lex.is_next_no_case("begin")
36 assert_equal " 456", @lex.src.read_all, "must put back contents"
40 node = @lex.number_token("42", false, "\0")
41 assert_equal :tINTEGER, node
42 assert_equal 42, @lex.yacc_value
46 @lex.src = StringIO.new '42'
47 node = @lex.parse_number('1')
48 assert_equal :tINTEGER, node
49 assert_equal 142, @lex.yacc_value
53 @lex.src = StringIO.new 'blah)'
54 node = @lex.parse_quote('(')
55 assert_equal :tSTRING_BEG, node
56 assert_equal s(:strterm, RubyLexer::STR_DQUOTE, ")", "("), @lex.lex_strterm
57 assert_equal ["%)"], @lex.yacc_value.args # FIX double check this
60 def test_yylex_integer
61 util_lex_token "42", :tINTEGER, 42
64 def test_yylex_integer_eh_a
69 def test_yylex_integer_eh_escape_M_escape_C
70 util_lex_token('?\M-\C-a',
75 util_lex_token "1.0", :tFLOAT, 1.0
78 def test_yylex_constant
79 util_lex_token("ArgumentError",
80 :tCONSTANT, t("ArgumentError"))
83 def test_yylex_constant_semi
84 util_lex_token("ArgumentError;",
85 :tCONSTANT, t("ArgumentError"),
89 def test_yylex_identifier
90 util_lex_token("identifier",
91 :tIDENTIFIER, t("identifier"))
95 util_lex_token("/regexp/",
97 :tSTRING_CONTENT, s(:str, "regexp"),
101 def test_yylex_regexp_nm
102 util_lex_token("/.*/nm",
103 :tREGEXP_BEG, t("/"),
104 :tSTRING_CONTENT, s(:str, ".*"),
108 def test_yylex_regexp_escapes
109 util_lex_token('/re\tge\nxp/',
110 :tREGEXP_BEG, t("/"),
111 :tSTRING_CONTENT, s(:str, "re\\tge\\nxp"),
115 def test_yylex_regexp_escape_oct
116 util_lex_token('/re\tge\101xp/',
117 :tREGEXP_BEG, t("/"),
118 :tSTRING_CONTENT, s(:str, "re\\tge\\101xp"),
122 def test_yylex_regexp_escape_hex
123 util_lex_token('/re\tge\x61xp/',
124 :tREGEXP_BEG, t("/"),
125 :tSTRING_CONTENT, s(:str, "re\\tge\\x61xp"),
129 def test_yylex_string_double
130 util_lex_token('"string"',
131 :tSTRING_BEG, t('"'),
132 :tSTRING_CONTENT, s(:str, "string"),
133 :tSTRING_END, t('"'))
136 def test_yylex_string_double_escapes
137 util_lex_token('"s\tri\ng"',
138 :tSTRING_BEG, t('"'),
139 :tSTRING_CONTENT, s(:str, "s\tri\ng"),
140 :tSTRING_END, t('"'))
143 def test_yylex_string_double_escape_M
144 util_lex_token('"\M-g"',
145 :tSTRING_BEG, t('"'),
146 :tSTRING_CONTENT, s(:str, "\347"),
147 :tSTRING_END, t('"'))
150 def test_yylex_string_double_escape_octal
151 util_lex_token('"n = \101\102\103"',
152 :tSTRING_BEG, t('"'),
153 :tSTRING_CONTENT, s(:str, "n = ABC"),
154 :tSTRING_END, t('"'))
157 def test_yylex_string_double_escape_hex
158 util_lex_token('"n = \x61\x62\x63"',
159 :tSTRING_BEG, t('"'),
160 :tSTRING_CONTENT, s(:str, "n = abc"),
161 :tSTRING_END, t('"'))
164 def test_yylex_string_single
165 util_lex_token("'string'",
166 :tSTRING_BEG, t("'"),
167 :tSTRING_CONTENT, s(:str, "string"),
168 :tSTRING_END, t("'"))
171 def test_yylex_string_pct_Q
172 util_lex_token("%Q[string]",
173 :tSTRING_BEG, t("%Q["),
174 :tSTRING_CONTENT, s(:str, "string"),
175 :tSTRING_END, t("]"))
178 def test_yylex_string_single_escapes
179 util_lex_token("'s\\tri\\ng'",
180 :tSTRING_BEG, t("'"),
181 :tSTRING_CONTENT, s(:str, "s\\tri\\ng"),
182 :tSTRING_END, t("'"))
185 def test_yylex_global
186 util_lex_token("$blah",
190 def test_yylex_global_wierd
191 util_lex_token("$__blah",
192 :tGVAR, t("$__blah"))
195 def test_yylex_global_dollar_underscore
200 def test_yylex_symbol
201 util_lex_token(":symbol",
203 :tIDENTIFIER, t("symbol"))
206 def test_yylex_comment_begin
207 util_lex_token("=begin\nblah\nblah\n=end\n42",
211 def util_lex_token input, *args
212 @lex.src = StringIO.new input
217 assert @lex.advance, "no more tokens"
218 assert_equal [token, value], [@lex.token, @lex.yacc_value]
221 deny @lex.advance, "must be empty, but had #{[@lex.token, @lex.yacc_value].inspect}"
225 class TestStackState < Test::Unit::TestCase
227 s = StackState.new :test
231 assert_equal [false, true], s.stack
235 s = StackState.new :test
236 assert_equal false, s.is_in_state
238 assert_equal false, s.is_in_state
240 assert_equal true, s.is_in_state
242 assert_equal false, s.is_in_state
246 s = StackState.new :test
247 assert_equal [false], s.stack
250 assert_equal [false, true, false], s.stack
252 assert_equal [false, true], s.stack
256 s = StackState.new :test
257 assert_equal [false], s.stack
259 assert_equal [false, true], s.stack
260 assert_equal true, s.pop
261 assert_equal [false], s.stack
265 s = StackState.new :test
266 assert_equal [false], s.stack
269 assert_equal [false, true, false], s.stack
273 class TestEnvironment < Test::Unit::TestCase
279 @env = Environment.new
281 assert_equal 42, @env[:blah]
286 expected = [{ :blah => true }]
287 assert_equal expected, @env.instance_variable_get(:"@use")
293 expected = [{}, { :blah => true }]
294 assert_equal expected, @env.instance_variable_get(:"@use")
301 assert_equal true, @env.used?(:x)
304 def test_used_eh_none
305 assert_equal nil, @env.used?(:x)
308 def test_used_eh_scoped
311 assert_equal true, @env.used?(:x)
314 def test_var_scope_dynamic
316 assert_equal 42, @env[:blah]
318 assert_equal 42, @env[:blah]
321 def test_var_scope_static
323 assert_equal nil, @env[:blah]
325 assert_equal 42, @env[:blah]
330 expected2 = { :x => 42 }
332 assert_equal expected1, @env.dynamic
335 assert_equal expected1, @env.dynamic
338 assert_equal expected2, @env.dynamic
342 assert_equal expected2, @env.dynamic
346 assert_equal expected2, @env.dynamic
349 assert_equal expected1, @env.dynamic
353 expected = { :blah => 42 }
356 assert_equal expected, @env.all
358 assert_equal expected, @env.all
364 assert_equal expected, @env.all
367 expected = { :blah => 42 }
368 assert_equal expected, @env.all
372 assert_equal false, @env.dynamic?
374 assert_equal true, @env.dynamic?
376 assert_equal false, @env.dynamic?
379 def test_all_static_deeper
380 expected0 = { :blah => 42 }
381 expected1 = { :blah => 42, :blah2 => 24 }
382 expected2 = { :blah => 27 }
386 assert_equal expected1, @env.all
390 assert_equal expected2, @env.all
393 assert_equal expected1, @env.all
396 assert_equal expected0, @env.all