Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Josep M. Bach
committed
Sep 11, 2011
1 parent
add31a8
commit f4f7a3c
Showing
6 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,59 @@ | |||
require 'test_helper' | |||
|
|||
class LexerIntegrationTest < MiniTest::Unit::TestCase | |||
|
|||
def test_assignment | |||
tokenizes "foo = 'bar'", [ | |||
[:IDENTIFIER, 'foo'], | |||
[:ASSIGN, '='], | |||
[:STRING, 'bar'], | |||
] | |||
end | |||
|
|||
def test_arithmetic_assignment | |||
tokenizes "foo = (3 + 4) * 2", [ | |||
[:IDENTIFIER, 'foo'], | |||
[:ASSIGN, '='], | |||
[:LPAREN, '('], | |||
[:DIGIT, 3], | |||
['+', '+'], | |||
[:DIGIT, 4], | |||
[:RPAREN, ')'], | |||
['*', '*'], | |||
[:DIGIT, 2], | |||
] | |||
end | |||
|
|||
def test_def_single_line | |||
tokenizes "def foo(bar, baz); 'lorem'; end", [ | |||
[:DEF, 'def'], | |||
[:IDENTIFIER, 'foo'], | |||
[:LPAREN, '('], | |||
[:IDENTIFIER, 'bar'], | |||
[:COMMA, ','], | |||
[:IDENTIFIER, 'baz'], | |||
[:RPAREN, ')'], | |||
[:SEMICOLON, ';'], | |||
[:STRING, "lorem"], | |||
[:SEMICOLON, ';'], | |||
[:END, "end"], | |||
] | |||
end | |||
|
|||
def test_def_multiline | |||
tokenizes "def foo(bar, baz)\n 'lorem'\n end", [ | |||
[:DEF, 'def'], | |||
[:IDENTIFIER, 'foo'], | |||
[:LPAREN, '('], | |||
[:IDENTIFIER, 'bar'], | |||
[:COMMA, ','], | |||
[:IDENTIFIER, 'baz'], | |||
[:RPAREN, ')'], | |||
[:NEWLINE, "\n "], | |||
[:STRING, "lorem"], | |||
[:NEWLINE, "\n "], | |||
[:END, "end"], | |||
] | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,66 @@ | |||
require 'test_helper' | |||
|
|||
class LexerTest < MiniTest::Unit::TestCase | |||
|
|||
def test_newline | |||
tokenizes "\n", [[:NEWLINE, "\n"]] | |||
tokenizes "\n ", [[:NEWLINE, "\n "]] | |||
tokenizes "\n\n ", [[:NEWLINE, "\n\n "]] | |||
tokenizes "\n\n ", [[:NEWLINE, "\n\n "]] | |||
end | |||
|
|||
def test_ignores_whitespace | |||
tokenizes " ", [] | |||
tokenizes ", ", [[:COMMA, ',']] | |||
end | |||
|
|||
def test_digit | |||
tokenizes "3", [[:DIGIT, 3]] | |||
tokenizes "3234", [[:DIGIT, 3234]] | |||
end | |||
|
|||
def test_assign | |||
tokenizes "=", [[:ASSIGN, '=']] | |||
end | |||
|
|||
def test_comma | |||
tokenizes ",", [[:COMMA, ',']] | |||
end | |||
|
|||
def test_semicolon | |||
tokenizes ";", [[:SEMICOLON, ';']] | |||
end | |||
|
|||
def test_string | |||
tokenizes "'foo'", [[:STRING, 'foo']] | |||
end | |||
|
|||
def test_def | |||
tokenizes "def", [[:DEF, 'def']] | |||
end | |||
|
|||
def test_lparen | |||
tokenizes "(", [[:LPAREN, '(']] | |||
end | |||
|
|||
def test_rparen | |||
tokenizes ")", [[:RPAREN, ')']] | |||
end | |||
|
|||
def test_end | |||
tokenizes "end", [[:END, 'end']] | |||
end | |||
|
|||
def test_identifier | |||
tokenizes "hello", [[:IDENTIFIER, 'hello']] | |||
tokenizes "hello_world", [[:IDENTIFIER, 'hello_world']] | |||
tokenizes "_hey", [[:IDENTIFIER, '_hey']] | |||
end | |||
|
|||
def test_everything_else | |||
%w(+ - * / ^ &).each do |symbol| | |||
tokenizes symbol, [[symbol, symbol]] | |||
end | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,19 @@ | |||
gem 'minitest' | |||
require 'minitest/unit' | |||
require 'minitest/autorun' | |||
require 'purdytest' | |||
|
|||
require 'noscript' | |||
|
|||
class MiniTest::Unit::TestCase | |||
def tokenizes(input, expected) | |||
lexer = Noscript::Parser.new | |||
lexer.scan_setup(input) | |||
tokens = [] | |||
while token = lexer.next_token | |||
tokens << token | |||
end | |||
|
|||
assert_equal expected, tokens | |||
end | |||
end |