|
|
|
@ -57,7 +57,7 @@ class TestScanner < Minitest::Test
|
|
|
|
|
>= GREATER_EQUAL
|
|
|
|
|
> GREATER
|
|
|
|
|
/ SLASH].each_slice(2).to_h.transform_values(&:to_sym).each do |str, token_type|
|
|
|
|
|
assert_equal [token_type.to_sym], @scanner.scan(str).map(&:type)
|
|
|
|
|
assert_equal token_type.to_sym, @scanner.scan(str)[0].type
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -67,7 +67,7 @@ class TestScanner < Minitest::Test
|
|
|
|
|
.
|
|
|
|
|
SRC
|
|
|
|
|
|
|
|
|
|
assert_equal %i[LEFT_PAREN RIGHT_PAREN DOT], tokens.map(&:type)
|
|
|
|
|
assert_equal %i[LEFT_PAREN RIGHT_PAREN DOT EOF], tokens.map(&:type)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_line_numbers
|
|
|
|
@ -76,20 +76,33 @@ class TestScanner < Minitest::Test
|
|
|
|
|
)
|
|
|
|
|
SRC
|
|
|
|
|
|
|
|
|
|
assert_equal [1, 2], tokens.map(&:line)
|
|
|
|
|
assert_equal [1, 2, 3], tokens.map(&:line)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_strings
|
|
|
|
|
assert_equal [Lox::Token.new(:STRING, '""', "", 1)], @scanner.scan('""')
|
|
|
|
|
assert_equal [], @scanner.scan('"') # TODO test the error once it's exposed
|
|
|
|
|
assert_equal [Lox::Token.new(:STRING, '"foo"', "foo", 1)], @scanner.scan('"foo"')
|
|
|
|
|
assert_equal [Lox::Token.new(:STRING, "\"foo\nbar\"", "foo\nbar", 2)], @scanner.scan("\"foo\nbar\"")
|
|
|
|
|
assert_equal [
|
|
|
|
|
Lox::Token.new(:STRING, '""', "", 1),
|
|
|
|
|
Lox::Token.new(:EOF, "", nil, 1),
|
|
|
|
|
], @scanner.scan('""')
|
|
|
|
|
|
|
|
|
|
assert_equal [Lox::Token.new(:EOF, "", nil, 1)], @scanner.scan('"') # TODO test the error once it's exposed
|
|
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
|
Lox::Token.new(:STRING, '"foo"', "foo", 1),
|
|
|
|
|
Lox::Token.new(:EOF, "", nil, 1),
|
|
|
|
|
], @scanner.scan('"foo"')
|
|
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
|
Lox::Token.new(:STRING, "\"foo\nbar\"", "foo\nbar", 2),
|
|
|
|
|
Lox::Token.new(:EOF, "", nil, 2),
|
|
|
|
|
], @scanner.scan("\"foo\nbar\"")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_numbers
|
|
|
|
|
assert_equal [
|
|
|
|
|
Lox::Token.new(:NUMBER, "123", 123.0, 1),
|
|
|
|
|
Lox::Token.new(:NUMBER, "123.4", 123.4, 1),
|
|
|
|
|
Lox::Token.new(:EOF, "", nil, 1),
|
|
|
|
|
], @scanner.scan("123 123.4")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -97,6 +110,7 @@ class TestScanner < Minitest::Test
|
|
|
|
|
assert_equal [
|
|
|
|
|
Lox::Token.new(:OR, "or", nil, 1),
|
|
|
|
|
Lox::Token.new(:IDENTIFIER, "orchid", nil, 1),
|
|
|
|
|
Lox::Token.new(:EOF, "", nil, 1),
|
|
|
|
|
], @scanner.scan("or orchid")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -111,8 +125,9 @@ class TestScanner < Minitest::Test
|
|
|
|
|
assert_equal [
|
|
|
|
|
Lox::Token.new(:IDENTIFIER, "foo", nil, 1),
|
|
|
|
|
Lox::Token.new(:IDENTIFIER, "bar", nil, 4),
|
|
|
|
|
Lox::Token.new(:EOF, "", nil, 5),
|
|
|
|
|
], tokens
|
|
|
|
|
|
|
|
|
|
assert_equal [], @scanner.scan("/*")
|
|
|
|
|
assert_equal [Lox::Token.new(:EOF, "", nil, 1)], @scanner.scan("/*")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|