Skip to content

Commit

Permalink
Light refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobie committed May 26, 2010
1 parent e79e6ff commit bafbc61
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions test/test_parser.rb
Expand Up @@ -3,46 +3,51 @@
require "../lib/modulr/parser"

class TestLibModulrParser < Test::Unit::TestCase
def get_module_identifier(src)
def assert_found_module(name, src, message="")
@parser ||= Modulr::Parser.new
@parser.get_require_expressions(src).first[:identifier]
template = "Could not find module ? in JavaScript source code ?."
message = build_message(message, template, name, src)
assert_block(message) do
requires = @parser.get_require_expressions(src)
requires.first && requires.first[:identifier] == name
end
end

def test_simple_require_function
assert_equal 'foo', get_module_identifier('require("foo")')
assert_found_module('foo', 'require("foo")')
end

def test_setting_variable
assert_equal 'foo', get_module_identifier("var bar = require('foo');")
assert_found_module('foo', "var bar = require('foo');")
end

def test_accessing_property
assert_equal 'foo', get_module_identifier("require('foo').bar;")
assert_found_module('foo', "require('foo').bar;")
end

def test_accessing_nested_property
assert_equal 'foo', get_module_identifier("require('foo').baz.bar;")
assert_found_module('foo', "require('foo').baz.bar;")
end

def test_calling_method
assert_equal 'foo', get_module_identifier("require('foo').bar();")
assert_found_module('foo', "require('foo').bar();")
end

def test_calling_nested_method
assert_equal 'foo', get_module_identifier("require('foo').bar.baz();")
assert_found_module('foo', "require('foo').bar.baz();")
end

def test_instanciating_constructor
assert_equal 'foo', get_module_identifier("new require('foo').Foo();")
assert_equal 'foo', get_module_identifier("new require('foo').Foo;")
assert_equal 'foo', get_module_identifier("new require('foo').Foo(1, 2, 3);")
assert_equal 'foo', get_module_identifier("new (require('foo')).Foo();")
assert_found_module('foo', "new require('foo').Foo();")
assert_found_module('foo', "new require('foo').Foo;")
assert_found_module('foo', "new require('foo').Foo(1, 2, 3);")
assert_found_module('foo', "new (require('foo')).Foo();")
end

def test_instanciating_nested_constructor
assert_equal 'foo', get_module_identifier("new require('foo').bar.Foo();")
assert_equal 'foo', get_module_identifier("new require('foo').bar.Foo;")
assert_equal 'foo', get_module_identifier("new require('foo').bar.Foo(1, 2, 3);")
assert_equal 'foo', get_module_identifier("new (require('foo').bar).Foo();")
assert_found_module('foo', "new require('foo').bar.Foo();")
assert_found_module('foo', "new require('foo').bar.Foo;")
assert_found_module('foo', "new require('foo').bar.Foo(1, 2, 3);")
assert_found_module('foo', "new (require('foo').bar).Foo();")
end
end

0 comments on commit bafbc61

Please sign in to comment.