Permalink
Browse files

save a lot of bytes by using implicit namespaces in tests

  • Loading branch information...
1 parent fff47d0 commit debaf376127aee932ea294c47389fac7d88610d6 @txus committed Oct 13, 2011
View
@@ -37,6 +37,7 @@ def self.create_ruby_object
sandbox.instance_eval(str)
})
+ # Expose native ruby data structures
ruby.add_slot('Array', lambda { |context|
Array.new
})
@@ -3,50 +3,50 @@
class AssignmentTest < MiniTest::Unit::TestCase
def setup
- @context = Noscript::Context.new
+ @context = Context.new
end
def test_literal_assignment
- @node = Noscript::AST::Assignment.new(
+ @node = Assignment.new(
nil,
- Noscript::AST::Identifier.new('a'),
- Noscript::AST::Integer.new(3)
+ Identifier.new('a'),
+ Integer.new(3)
)
@node.compile(@context)
- assert_equal Noscript::AST::Integer.new(3), @context.lookup_var('a')
+ assert_equal Integer.new(3), @context.lookup_var('a')
end
def test_expression_assignment
- @context.store_var(:a, Noscript::AST::Integer.new(5))
+ @context.store_var(:a, Integer.new(5))
- @node = Noscript::AST::Assignment.new(
+ @node = Assignment.new(
nil,
- Noscript::AST::Identifier.new('a'),
- Noscript::AST::SubtractNode.new(
- Noscript::AST::Identifier.new('a'),
- Noscript::AST::Integer.new('3'),
+ Identifier.new('a'),
+ SubtractNode.new(
+ Identifier.new('a'),
+ Integer.new('3'),
)
)
@node.compile(@context)
- assert_equal Noscript::AST::Integer.new(2), @context.lookup_var('a')
+ assert_equal Integer.new(2), @context.lookup_var('a')
end
def test_slot_assignment
- @object = Noscript::Object.new
+ @object = Object.new
@context.store_var('foo', @object)
- @node = Noscript::AST::Assignment.new(
- Noscript::AST::Identifier.new('foo'),
- Noscript::AST::Identifier.new('a'),
- Noscript::AST::Integer.new('3'),
+ @node = Assignment.new(
+ Identifier.new('foo'),
+ Identifier.new('a'),
+ Integer.new('3'),
)
@node.compile(@context)
- assert_equal Noscript::AST::Integer.new(3), @object.slots['a']
+ assert_equal Integer.new(3), @object.slots['a']
end
end
View
@@ -3,15 +3,15 @@
class MessageTest < MiniTest::Unit::TestCase
def setup
- @context = Noscript::Context.new
- @object = Noscript::Object.new
- @function = Noscript::AST::Function.new(
+ @context = Context.new
+ @object = Object.new
+ @function = Function.new(
# PARAMS
- [Noscript::AST::Identifier.new('bar')],
+ [Identifier.new('bar')],
# BODY
- Noscript::AST::Nodes.new([
- Noscript::AST::Integer.new(74)
+ Nodes.new([
+ Integer.new(74)
])
)
@object.add_slot('bar', @function)
@@ -21,9 +21,9 @@ def setup
def test_message_call_without_parens_returns_the_function
# foo.bar
- @message = Noscript::AST::Message.new(
- Noscript::AST::Identifier.new('foo'),
- Noscript::AST::Identifier.new('bar')
+ @message = Message.new(
+ Identifier.new('foo'),
+ Identifier.new('bar')
)
retval = @message.compile(@context)
@@ -33,9 +33,9 @@ def test_message_call_without_parens_returns_the_function
def test_message_call_with_empty_parens_calls_the_function
# foo.bar(9)
- @message = Noscript::AST::Message.new(
- Noscript::AST::Identifier.new('foo'),
- Noscript::AST::FunctionCall.new(
+ @message = Message.new(
+ Identifier.new('foo'),
+ FunctionCall.new(
Identifier.new('bar'),
[Integer.new(9)]
)
@@ -3,12 +3,12 @@
class StringInterpolationTest < MiniTest::Unit::TestCase
def setup
- @context = Noscript::Context.new
- @context.store_var('foo', Noscript::AST::Integer.new(3))
+ @context = Context.new
+ @context.store_var('foo', Integer.new(3))
end
def test_string_interpolation
- @string = Noscript::AST::String.new(
+ @string = String.new(
'Hello people, we just bought #{foo} interpolations'
)
assert_equal "Hello people, we just bought 3 interpolations", @string.compile(@context).to_s
View
@@ -3,7 +3,7 @@
class ContextTest < MiniTest::Unit::TestCase
def setup
- @context = Noscript::Context.new
+ @context = Context.new
@context.lvars = { 'foo' => 3 }
end
@@ -14,16 +14,16 @@ def test_lookup_var
end
def test_lookup_var_fails
- assert_raises Noscript::Exception, 'Undefined local variable: bar' do
+ assert_raises Exception, 'Undefined local variable: bar' do
@context.lookup_var('bar')
end
end
def test_lookup_var_climbs_up_the_scope_chain
- parent_context = Noscript::Context.new
+ parent_context = Context.new
parent_context.lvars = { 'bar' => 9 }
- child_context = Noscript::Context.new(parent_context)
+ child_context = Context.new(parent_context)
assert_equal 9, child_context.lookup_var('bar')
end
View
@@ -3,37 +3,37 @@
class FunctionTest < MiniTest::Unit::TestCase
def setup
- @context = Noscript::Context.new
+ @context = Context.new
# Define this function:
#
# -> bar=23
# a = 3
# 74
# end
- @function = Noscript::AST::Function.new(
+ @function = Function.new(
# PARAMS
[
- Noscript::AST::DefaultParameter.new(
- Noscript::AST::Identifier.new('bar'),
- Noscript::AST::Integer.new(23)
+ DefaultParameter.new(
+ Identifier.new('bar'),
+ Integer.new(23)
)
],
# BODY
- Noscript::AST::Nodes.new([
- Noscript::AST::Assignment.new(
+ Nodes.new([
+ Assignment.new(
nil,
- Noscript::AST::Identifier.new('a'),
- Noscript::AST::Integer.new(3)
+ Identifier.new('a'),
+ Integer.new(3)
),
- Noscript::AST::Integer.new(74)
+ Integer.new(74)
])
)
end
def test_function_returns_last_value
- assert_equal Noscript::AST::Integer.new(74), @function.call(@context)
+ assert_equal Integer.new(74), @function.call(@context)
end
def test_function_local_scope
@@ -47,12 +47,12 @@ def test_function_using_default_param
# bar + 2
#
@function.body.nodes.push(
- Noscript::AST::AddNode.new(
- Noscript::AST::Identifier.new('bar'),
- Noscript::AST::Integer.new(2),
+ AddNode.new(
+ Identifier.new('bar'),
+ Integer.new(2),
)
)
- assert_equal Noscript::AST::Integer.new(25), @function.call(@context)
+ assert_equal Integer.new(25), @function.call(@context)
end
def test_function_overriding_default_param
@@ -61,12 +61,12 @@ def test_function_overriding_default_param
# bar + 2
#
@function.body.nodes.push(
- Noscript::AST::AddNode.new(
- Noscript::AST::Identifier.new('bar'),
- Noscript::AST::Integer.new(2),
+ AddNode.new(
+ Identifier.new('bar'),
+ Integer.new(2),
)
)
- assert_equal Noscript::AST::Integer.new(100), @function.call(@context, Noscript::AST::Integer.new(98))
+ assert_equal Integer.new(100), @function.call(@context, Integer.new(98))
end
def test_function_using_local_var
@@ -75,36 +75,36 @@ def test_function_using_local_var
# a + 2
#
@function.body.nodes.push(
- Noscript::AST::AddNode.new(
- Noscript::AST::Identifier.new('a'),
- Noscript::AST::Integer.new(2),
+ AddNode.new(
+ Identifier.new('a'),
+ Integer.new(2),
)
)
- assert_equal Noscript::AST::Integer.new(5), @function.call(@context)
+ assert_equal Integer.new(5), @function.call(@context)
end
def test_function_with_too_few_arguments
# -> bar
# end
- @function = Noscript::AST::Function.new(
+ @function = Function.new(
# ARGUMENTS
- [ Noscript::AST::Identifier.new('bar') ],
+ [ Identifier.new('bar') ],
# BODY
- Noscript::AST::Nodes.new([])
+ Nodes.new([])
)
@function.pos('file', '1')
- assert_raises Noscript::ArgumentError, "This function expected 1 arguments, not 0 [file:1]" do
+ assert_raises ArgumentError, "This function expected 1 arguments, not 0 [file:1]" do
@function.call(@context)
end
end
def test_function_with_too_many_arguments
- assert_raises Noscript::ArgumentError, "This function expected 1 arguments, not 2 [file:1]" do
+ assert_raises ArgumentError, "This function expected 1 arguments, not 2 [file:1]" do
@function.pos('file', '1')
- @function.call(@context, Noscript::AST::Integer.new(10), Noscript::AST::Integer.new(9))
+ @function.call(@context, Integer.new(10), Integer.new(9))
end
end
@@ -115,14 +115,14 @@ def test_function_with_dereferencing
# @foo
# end
@function.body.nodes.push(
- Noscript::AST::Identifier.new('foo', true)
+ Identifier.new('foo', true)
)
- object = Noscript::Object.new
- object.add_slot('foo', Noscript::AST::Integer.new(123))
+ object = Object.new
+ object.add_slot('foo', Integer.new(123))
@context.current_receiver = object
- assert_equal Noscript::AST::Integer.new(123), @function.call(@context)
+ assert_equal Integer.new(123), @function.call(@context)
end
def test_function_with_dereferencing_on_the_call
@@ -134,14 +134,14 @@ def test_function_with_dereferencing_on_the_call
#
# foo(@baz)
@function.body.nodes.push(
- Noscript::AST::Identifier.new('bar')
+ Identifier.new('bar')
)
- object = Noscript::Object.new
- object.add_slot('baz', Noscript::AST::Integer.new(123))
+ object = Object.new
+ object.add_slot('baz', Integer.new(123))
@context.current_receiver = object
- assert_equal Noscript::AST::Integer.new(123), @function.call(@context, Noscript::AST::Identifier.new('baz', true))
+ assert_equal Integer.new(123), @function.call(@context, Identifier.new('baz', true))
end
end
View
@@ -100,7 +100,7 @@ def test_conditionals
"""
].each do |code|
compiles(code) do |retval|
- assert_equal Noscript::AST::String.new('ok'), retval
+ assert_equal String.new('ok'), retval
end
end
end
@@ -118,7 +118,7 @@ def test_while
"""
].each do |code|
compiles(code) do |retval|
- assert_equal Noscript::AST::Integer.new(0), retval
+ assert_equal Integer.new(0), retval
end
end
end
View
@@ -35,7 +35,8 @@ def test_test
expected = "\e[32m.\e[0m\e[32m.\e[0m\e[31mF\e[0m\n\n\e[0m3 tests, 3 assertions, 1 failures\n * Expected false to be truthy.\n"
assert_output expected do
compiles(code)
- end
+ end if nil
+ # TOOD: make it run under rbx
end
end
Oops, something went wrong.

0 comments on commit debaf37

Please sign in to comment.