Loading spec/lexer_spec.rb 0 → 100644 +119 −0 Original line number Diff line number Diff line require "jsduck/lexer" describe JsDuck::Lexer do def lex(source) lex = JsDuck::Lexer.new(source) tokens = [] while !lex.empty? t = lex.next(true) tokens << [t[:type], t[:value]] end tokens end it "tokenizes simple expression" do lex("var foo = 8;").should == [ [:keyword, "var"], [:ident, "foo"], [:operator, "="], [:number, 8], [:operator, ";"] ] end describe "differenciates regex from division" do it "when regex after operator" do lex("x = / /; y / 2").should == [ [:ident, "x"], [:operator, "="], [:regex, "/ /"], [:operator, ";"], [:ident, "y"], [:operator, "/"], [:number, 2] ] end it "when regex after return" do lex("return /foo/.test;").should == [ [:keyword, "return"], [:regex, "/foo/"], [:operator, "."], [:ident, "test"], [:operator, ";"] ] end it "when regex after typeof" do lex("typeof /foo/;").should == [ [:keyword, "typeof"], [:regex, "/foo/"], [:operator, ";"] ] end it "when division after this" do lex("this / 3").should == [ [:keyword, "this"], [:operator, "/"], [:number, 3] ] end end describe "identifies strings" do before do @d = '"' # double-quote @s = "'" # single-quote @b = "\\" # backslash end it "when single-quote inside double-quoted string" do lex(@d+@s+@d + ' "blah"').should == [[:string, @s], [:string, "blah"]] end it "when double-quote inside single-quoted string" do lex(@s+@d+@s + ' "blah"').should == [[:string, @d], [:string, "blah"]] end it "when escaped double-quote inside double-quoted string" do lex(@d+@b+@d+@d + ' "blah"').should == [[:string, @d], [:string, "blah"]] end it "when escaped single-quote inside single-quoted string" do lex(@s+@b+@s+@s + ' "blah"').should == [[:string, @s], [:string, "blah"]] end end it "ignores one-line comments" do lex("a // foo\n b").should == [[:ident, "a"], [:ident, "b"]] end it "ignores multi-line comments" do lex("a /* foo */ b").should == [[:ident, "a"], [:ident, "b"]] end it "identifies doc-comments" do lex("/** foo */").should == [[:doc_comment, "/** foo */"]] end describe "handles unfinished" do it "single-line comment" do lex("// ").should == [] end it "multi-line comment" do lex("/* ").should == [] end it "doc-comment" do lex("/** ").should == [[:doc_comment, "/** "]] end end end test/tc_lexer.rbdeleted 100644 → 0 +0 −92 Original line number Diff line number Diff line require "jsduck/lexer" require "test/unit" class TestLexer < Test::Unit::TestCase def assert_tokens(source, expected_tokens) lex = JsDuck::Lexer.new(source) expected_tokens.each do |t| assert_equal({:type => t[0], :value => t[1]}, lex.next(true)) end assert(lex.empty?) end def test_simple assert_tokens("var foo = 8;", [ [:keyword, "var"], [:ident, "foo"], [:operator, "="], [:number, 8], [:operator, ";"] ]) end def test_regex_vs_division assert_tokens("x = / /; y / 2", [ [:ident, "x"], [:operator, "="], [:regex, "/ /"], [:operator, ";"], [:ident, "y"], [:operator, "/"], [:number, 2] ]) end def test_regex_after_return assert_tokens("return /foo/.test;", [ [:keyword, "return"], [:regex, "/foo/"], [:operator, "."], [:ident, "test"], [:operator, ";"] ]) end def test_regex_after_typeof assert_tokens("typeof /foo/;", [ [:keyword, "typeof"], [:regex, "/foo/"], [:operator, ";"] ]) end def test_division_after_this assert_tokens("this / 3", [ [:keyword, "this"], [:operator, "/"], [:number, 3] ]) end def test_strings d = '"' # double-quote s = "'" # single-quote b = "\\" # backslash assert_tokens(d+s+d + ' "blah"', [[:string, s], [:string, "blah"]]) assert_tokens(s+d+s + ' "blah"', [[:string, d], [:string, "blah"]]) assert_tokens(d+b+d+d + ' "blah"', [[:string, d], [:string, "blah"]]) assert_tokens(s+b+s+s + ' "blah"', [[:string, s], [:string, "blah"]]) end def test_comments assert_tokens("a // foo\n b", [[:ident, "a"], [:ident, "b"]]) assert_tokens("a /* foo */ b", [[:ident, "a"], [:ident, "b"]]) end def test_tokens_until_file_end assert_tokens("// ", []) assert_tokens("/* ", []) assert_tokens("/** ", [[:doc_comment, "/** "]]) end def test_doc_comment assert_tokens("/** foo */", [[:doc_comment, "/** foo */"]]) end end Loading
spec/lexer_spec.rb 0 → 100644 +119 −0 Original line number Diff line number Diff line require "jsduck/lexer" describe JsDuck::Lexer do def lex(source) lex = JsDuck::Lexer.new(source) tokens = [] while !lex.empty? t = lex.next(true) tokens << [t[:type], t[:value]] end tokens end it "tokenizes simple expression" do lex("var foo = 8;").should == [ [:keyword, "var"], [:ident, "foo"], [:operator, "="], [:number, 8], [:operator, ";"] ] end describe "differenciates regex from division" do it "when regex after operator" do lex("x = / /; y / 2").should == [ [:ident, "x"], [:operator, "="], [:regex, "/ /"], [:operator, ";"], [:ident, "y"], [:operator, "/"], [:number, 2] ] end it "when regex after return" do lex("return /foo/.test;").should == [ [:keyword, "return"], [:regex, "/foo/"], [:operator, "."], [:ident, "test"], [:operator, ";"] ] end it "when regex after typeof" do lex("typeof /foo/;").should == [ [:keyword, "typeof"], [:regex, "/foo/"], [:operator, ";"] ] end it "when division after this" do lex("this / 3").should == [ [:keyword, "this"], [:operator, "/"], [:number, 3] ] end end describe "identifies strings" do before do @d = '"' # double-quote @s = "'" # single-quote @b = "\\" # backslash end it "when single-quote inside double-quoted string" do lex(@d+@s+@d + ' "blah"').should == [[:string, @s], [:string, "blah"]] end it "when double-quote inside single-quoted string" do lex(@s+@d+@s + ' "blah"').should == [[:string, @d], [:string, "blah"]] end it "when escaped double-quote inside double-quoted string" do lex(@d+@b+@d+@d + ' "blah"').should == [[:string, @d], [:string, "blah"]] end it "when escaped single-quote inside single-quoted string" do lex(@s+@b+@s+@s + ' "blah"').should == [[:string, @s], [:string, "blah"]] end end it "ignores one-line comments" do lex("a // foo\n b").should == [[:ident, "a"], [:ident, "b"]] end it "ignores multi-line comments" do lex("a /* foo */ b").should == [[:ident, "a"], [:ident, "b"]] end it "identifies doc-comments" do lex("/** foo */").should == [[:doc_comment, "/** foo */"]] end describe "handles unfinished" do it "single-line comment" do lex("// ").should == [] end it "multi-line comment" do lex("/* ").should == [] end it "doc-comment" do lex("/** ").should == [[:doc_comment, "/** "]] end end end
test/tc_lexer.rbdeleted 100644 → 0 +0 −92 Original line number Diff line number Diff line require "jsduck/lexer" require "test/unit" class TestLexer < Test::Unit::TestCase def assert_tokens(source, expected_tokens) lex = JsDuck::Lexer.new(source) expected_tokens.each do |t| assert_equal({:type => t[0], :value => t[1]}, lex.next(true)) end assert(lex.empty?) end def test_simple assert_tokens("var foo = 8;", [ [:keyword, "var"], [:ident, "foo"], [:operator, "="], [:number, 8], [:operator, ";"] ]) end def test_regex_vs_division assert_tokens("x = / /; y / 2", [ [:ident, "x"], [:operator, "="], [:regex, "/ /"], [:operator, ";"], [:ident, "y"], [:operator, "/"], [:number, 2] ]) end def test_regex_after_return assert_tokens("return /foo/.test;", [ [:keyword, "return"], [:regex, "/foo/"], [:operator, "."], [:ident, "test"], [:operator, ";"] ]) end def test_regex_after_typeof assert_tokens("typeof /foo/;", [ [:keyword, "typeof"], [:regex, "/foo/"], [:operator, ";"] ]) end def test_division_after_this assert_tokens("this / 3", [ [:keyword, "this"], [:operator, "/"], [:number, 3] ]) end def test_strings d = '"' # double-quote s = "'" # single-quote b = "\\" # backslash assert_tokens(d+s+d + ' "blah"', [[:string, s], [:string, "blah"]]) assert_tokens(s+d+s + ' "blah"', [[:string, d], [:string, "blah"]]) assert_tokens(d+b+d+d + ' "blah"', [[:string, d], [:string, "blah"]]) assert_tokens(s+b+s+s + ' "blah"', [[:string, s], [:string, "blah"]]) end def test_comments assert_tokens("a // foo\n b", [[:ident, "a"], [:ident, "b"]]) assert_tokens("a /* foo */ b", [[:ident, "a"], [:ident, "b"]]) end def test_tokens_until_file_end assert_tokens("// ", []) assert_tokens("/* ", []) assert_tokens("/** ", [[:doc_comment, "/** "]]) end def test_doc_comment assert_tokens("/** foo */", [[:doc_comment, "/** foo */"]]) end end