/
method_test.rb
106 lines (93 loc) · 2.42 KB
/
method_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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