Skip to content
Browse files

method tests. finishedddd

  • Loading branch information...
1 parent 2729564 commit fcdb446854337a4a7e18d7bb31bf9720a15df155 @txus committed Sep 12, 2011
Showing with 120 additions and 16 deletions.
  1. +2 −6 lib/noscript/ast.rb
  2. +12 −10 lib/noscript/method.rb
  3. +106 −0 test/method_test.rb
View
8 lib/noscript/ast.rb
@@ -17,7 +17,7 @@ def compile(context)
class Identifier < Struct.new(:name)
def compile(context)
- context.lookup_var(name) || raise("Undefined local variable: #{name}")
+ context.lookup_var(name)
end
def to_s
name
@@ -50,11 +50,7 @@ def compile(context)
class MethodCall < Struct.new(:name, :args)
def compile(context)
- if method = context.lookup_method(name)
- method.call(context, *args)
- else
- raise "Undefined procedure: #{name}"
- end
+ context.lookup_method(name).call(context, *args)
end
end
View
22 lib/noscript/method.rb
@@ -1,23 +1,25 @@
module Noscript
- class Method
- attr_reader :params, :body
- def initialize(params, body)
- @params = params
- @body = body
- end
-
+ class Method < Struct.new(:params, :body)
def call(context, *args)
+ raise_argument_error(args) if args.size > params.size
+
ctx = Context.new(context)
- @params.each_with_index do |param, idx|
+ params.each_with_index do |param, idx|
if passed_value = args[idx]
ctx.store_var(param.name, passed_value.compile(ctx))
elsif param.is_a?(AST::DefaultParameter)
ctx.store_var(param.name, param.value)
else
- raise "This method expected #{@params.size} arguments, not #{args.size}"
+ raise_argument_error(args)
end
end
- @body.compile(ctx)
+ body.compile(ctx)
+ end
+
+ private
+
+ def raise_argument_error(args)
+ raise "This method expected #{params.size} arguments, not #{args.size}"
end
end
end
View
106 test/method_test.rb
@@ -0,0 +1,106 @@
+require 'test_helper'
+
+class MethodTest < MiniTest::Unit::TestCase
+
+ def setup
+ @context = Noscript::Context.new
+
+ # Define this method:
+ #
+ # def foo(bar=23)
+ # a = 3
+ # 74
+ # end
+ @method = Noscript::Method.new(
+ # PARAMS
+ [
+ Noscript::AST::DefaultParameter.new(
+ Noscript::AST::Identifier.new('bar'),
+ Noscript::AST::Digit.new(23)
+ )
+ ],
+
+ # BODY
+ Noscript::AST::Nodes.new([
+ Noscript::AST::AssignNode.new(
+ Noscript::AST::Identifier.new('a'),
+ Noscript::AST::Digit.new(3)
+ ),
+ Noscript::AST::Digit.new(74)
+ ])
+ )
+ end
+
+ def test_method_returns_last_value
+ assert_equal Noscript::AST::Digit.new(74), @method.call(@context)
+ end
+
+ def test_method_local_scope
+ @method.call(@context)
+ assert_nil @context.lvars['a']
+ end
+
+ def test_method_using_default_param
+ # Add a last line to the body:
+ #
+ # bar + 2
+ #
+ @method.body.nodes.push(
+ Noscript::AST::AddNode.new(
+ Noscript::AST::Identifier.new('bar'),
+ Noscript::AST::Digit.new(2),
+ )
+ )
+ assert_equal Noscript::AST::Digit.new(25), @method.call(@context)
+ end
+
+ def test_method_overriding_default_param
+ # Add a last line to the body:
+ #
+ # bar + 2
+ #
+ @method.body.nodes.push(
+ Noscript::AST::AddNode.new(
+ Noscript::AST::Identifier.new('bar'),
+ Noscript::AST::Digit.new(2),
+ )
+ )
+ assert_equal Noscript::AST::Digit.new(100), @method.call(@context, Noscript::AST::Digit.new(98))
+ end
+
+ def test_method_using_local_var
+ # Add a last line to the body:
+ #
+ # a + 2
+ #
+ @method.body.nodes.push(
+ Noscript::AST::AddNode.new(
+ Noscript::AST::Identifier.new('a'),
+ Noscript::AST::Digit.new(2),
+ )
+ )
+ assert_equal Noscript::AST::Digit.new(5), @method.call(@context)
+ end
+
+ def test_method_with_too_few_arguments
+ # def bar()
+ # end
+ @method = Noscript::Method.new(
+ # PARAMS
+ [ Noscript::AST::Identifier.new('bar') ],
+
+ # BODY
+ Noscript::AST::Nodes.new([])
+ )
+ assert_raises RuntimeError, "This method expected 1 arguments, not 0" do
+ @method.call(@context)
+ end
+ end
+
+ def test_method_with_too_many_arguments
+ assert_raises RuntimeError, "This method expected 1 arguments, not 2" do
+ @method.call(@context, Noscript::AST::Digit.new(10), Noscript::AST::Digit.new(9))
+ end
+ end
+
+end

0 comments on commit fcdb446

Please sign in to comment.
Something went wrong with that request. Please try again.