-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
circumfix.rakutest
140 lines (129 loc) Β· 3.84 KB
/
circumfix.rakutest
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use v6.e.PREVIEW;
use Test;
plan 7;
my $ast;
my $deparsed;
my @type = <AST Str>;
sub ast(RakuAST::Node:D $node --> Nil) {
$ast := $node;
$deparsed := $ast.DEPARSE;
diag $deparsed.chomp;
}
subtest 'Parenthesized expressions compile correctly' => {
# 2 * (3 + 4)
ast RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(2),
infix => RakuAST::Infix.new('*'),
right => RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(3),
infix => RakuAST::Infix.new('+'),
right => RakuAST::IntLiteral.new(4)
)
)
)
)
);
is-deeply $deparsed, '2 * (3 + 4)', 'deparsed';
is-deeply $_, 14, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Multi-statement semilist compiles into a List' => {
# (3, 4)
ast RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(','),
operands => (
RakuAST::IntLiteral.new(3),
RakuAST::IntLiteral.new(4)
)
)
)
)
);
is-deeply $deparsed, '(3, 4)', 'deparsed';
is-deeply $_, (3,4), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Array composer produces an array' => {
# [9, 10, 11]
ast RakuAST::Circumfix::ArrayComposer.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(','),
operands => (
RakuAST::IntLiteral.new(9),
RakuAST::IntLiteral.new(10),
RakuAST::IntLiteral.new(11)
)
)
)
)
);
is-deeply $deparsed, '[9, 10, 11]', 'deparsed';
is-deeply $_, [9,10,11], @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Array composer works correctly with a single argument' => {
# [5 .. 9]
ast RakuAST::Circumfix::ArrayComposer.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(5),
infix => RakuAST::Infix.new('..'),
right => RakuAST::IntLiteral.new(9)
)
)
)
);
is-deeply $deparsed, '[5 .. 9]', 'deparsed';
is-deeply $_, [5,6,7,8,9], @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Empty hash composer works correctly' => {
# {}
ast RakuAST::Circumfix::HashComposer.new;
is-deeply $deparsed, '{}', 'deparsed';
is-deeply $_, hash(), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Hash composer with fatarrow works correctly' => {
# {a => 42}
ast RakuAST::Circumfix::HashComposer.new(
RakuAST::FatArrow.new(
key => 'a',
value => RakuAST::IntLiteral.new(42)
)
);
is-deeply $deparsed, '{a => 42}', 'deparsed';
is-deeply $_, {:42a}, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Hash composer with list of fat arrows works correctly' => {
# {x => 11, y => 22}
ast RakuAST::Circumfix::HashComposer.new(
RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(','),
operands => [
RakuAST::FatArrow.new(
key => 'x',
value => RakuAST::IntLiteral.new(11)
),
RakuAST::FatArrow.new(
key => 'y',
value => RakuAST::IntLiteral.new(22)
)
]
)
);
is-deeply $deparsed, '{x => 11, y => 22}', 'deparsed';
is-deeply $_, {:11x, :22y}, @type[$++]
for EVAL($ast), EVAL($deparsed);
}
# vim: expandtab shiftwidth=4