|
@@ -0,0 +1,92 @@
|
|
1
|
+RSpec.describe AST::Conditional do
|
|
2
|
+ it 'evaluates a true if statement' do
|
|
3
|
+ expect(
|
|
4
|
+ AST::Conditional.new(
|
|
5
|
+ [
|
|
6
|
+ AST::Branch.new(
|
|
7
|
+ AST::Binary.new(
|
|
8
|
+ AST::Operators::GREATER_THAN,
|
|
9
|
+ AST::Number.new(5.0),
|
|
10
|
+ AST::Number.new(4.0),
|
|
11
|
+ ),
|
|
12
|
+ AST::Block.new(
|
|
13
|
+ [
|
|
14
|
+ AST::String.new("5 is greater than 4")
|
|
15
|
+ ]
|
|
16
|
+ )
|
|
17
|
+ )
|
|
18
|
+ ]
|
|
19
|
+ ).execute(Environment.new)
|
|
20
|
+ ).to eq("5 is greater than 4")
|
|
21
|
+ end
|
|
22
|
+
|
|
23
|
+ it 'evaluates an else statement' do
|
|
24
|
+ expect(
|
|
25
|
+ AST::Conditional.new(
|
|
26
|
+ [
|
|
27
|
+ AST::Branch.new(
|
|
28
|
+ AST::Binary.new(
|
|
29
|
+ AST::Operators::GREATER_THAN,
|
|
30
|
+ AST::Number.new(3.0),
|
|
31
|
+ AST::Number.new(4.0),
|
|
32
|
+ ),
|
|
33
|
+ AST::Block.new(
|
|
34
|
+ [
|
|
35
|
+ AST::String.new("3 is greater than 4")
|
|
36
|
+ ]
|
|
37
|
+ )
|
|
38
|
+ ),
|
|
39
|
+ AST::Branch.new(
|
|
40
|
+ AST::Boolean.new(true),
|
|
41
|
+ AST::Block.new(
|
|
42
|
+ [
|
|
43
|
+ AST::String.new("3 is _not_ greater than 4")
|
|
44
|
+ ]
|
|
45
|
+ )
|
|
46
|
+ )
|
|
47
|
+ ]
|
|
48
|
+ ).execute(Environment.new)
|
|
49
|
+ ).to eq("3 is _not_ greater than 4")
|
|
50
|
+ end
|
|
51
|
+
|
|
52
|
+ it 'evaluates an else if statement' do
|
|
53
|
+ expect(
|
|
54
|
+ AST::Conditional.new(
|
|
55
|
+ [
|
|
56
|
+ AST::Branch.new(
|
|
57
|
+ AST::Binary.new(
|
|
58
|
+ AST::Operators::GREATER_THAN,
|
|
59
|
+ AST::Number.new(3.0),
|
|
60
|
+ AST::Number.new(4.0),
|
|
61
|
+ ),
|
|
62
|
+ AST::Block.new(
|
|
63
|
+ [
|
|
64
|
+ AST::String.new("3 is greater than 4")
|
|
65
|
+ ]
|
|
66
|
+ )
|
|
67
|
+ ),
|
|
68
|
+ AST::Branch.new(
|
|
69
|
+ AST::Binary.new(
|
|
70
|
+ AST::Operators::GREATER_THAN,
|
|
71
|
+ AST::Number.new(3.0),
|
|
72
|
+ AST::Number.new(2.0),
|
|
73
|
+ ),
|
|
74
|
+ AST::Block.new(
|
|
75
|
+ [
|
|
76
|
+ AST::String.new("3 is greater than 2")
|
|
77
|
+ ]
|
|
78
|
+ )
|
|
79
|
+ ),
|
|
80
|
+ AST::Branch.new(
|
|
81
|
+ AST::Boolean.new(true),
|
|
82
|
+ AST::Block.new(
|
|
83
|
+ [
|
|
84
|
+ AST::String.new("3 is _not_ greater than 4")
|
|
85
|
+ ]
|
|
86
|
+ )
|
|
87
|
+ )
|
|
88
|
+ ]
|
|
89
|
+ ).execute(Environment.new)
|
|
90
|
+ ).to eq("3 is greater than 2")
|
|
91
|
+ end
|
|
92
|
+end
|