-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
heredocs.rakutest
259 lines (212 loc) Β· 6.54 KB
/
heredocs.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# NOTE: if you're adding / adapting tests here, you probably want to do
# the same in t/12-rakuast/strings.t
use v6.e.PREVIEW;
use Test;
plan 12;
my $ast;
my $deparsed;
my @type = <AST Str>;
sub ast(RakuAST::Node:D $node --> Nil) {
$ast := $node;
$deparsed := $ast.DEPARSE;
diag $deparsed.chomp;
}
subtest 'One-part heredoc with literal piece' => {
# "hello\n\ngoodbye\n"
ast RakuAST::Heredoc.new(
# :segments[RakuAST::StrLiteral.new(" hello\n\n goodbye\n")], # XX enable after trimfix
:segments[RakuAST::StrLiteral.new("hello\n\ngoodbye\n")],
:stop(" CODE\n")
);
is-deeply $deparsed,
"qq:to/CODE/\n hello\n\n goodbye\n CODE\n",
'deparse';
is-deeply $_, "hello\n\ngoodbye\n", @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Heredoc with interpolated string variable works' => {
my $str = 'hello,';
# "$str world\n"
ast RakuAST::Heredoc.new(
:segments[
RakuAST::Var::Lexical.new('$str'),
RakuAST::StrLiteral.new(" world\n")
],
:stop("MESSAGE\n")
);
is-deeply $deparsed,
qq:!s|qq:to/MESSAGE/\n$str world\nMESSAGE\n|,
'deparse';
is-deeply $_, "hello, world\n", @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Heredoc with interpolated integer variable' => {
my $int = 42;
# "$int is the answer\n"
ast RakuAST::Heredoc.new(
:segments[
# RakuAST::StrLiteral.new(" "), # XXX enable after trim fix
RakuAST::Var::Lexical.new('$int'),
RakuAST::StrLiteral.new(" is the answer\n")
],
:stop(" INTY\n")
);
is-deeply $deparsed,
qq:!s|qq:to/INTY/\n $int is the answer\n INTY\n|,
'deparse';
is-deeply $_, "42 is the answer\n", @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Heredoc with 3 parts works' => {
my $int = 42;
# "The answer is $int of course"
ast RakuAST::Heredoc.new(
:segments[
# RakuAST::StrLiteral.new(' The answer is '), # XXX enable after trim fix
RakuAST::StrLiteral.new('The answer is '),
RakuAST::Var::Lexical.new('$int'),
RakuAST::StrLiteral.new(" of course!\n")
],
:stop(" MORE\n")
);
is-deeply $deparsed,
qq:!s|qq:to/MORE/\n The answer is $int of course!\n MORE\n|,
'deparse';
is-deeply $_, "The answer is 42 of course!\n", @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Heredoc involving an interpolated block' => {
my $bv = 'interpolated';
# "An { $bv } block\n"
ast RakuAST::Heredoc.new(
:segments[
RakuAST::StrLiteral.new('An '),
RakuAST::Block.new(
body => RakuAST::Blockoid.new(
RakuAST::StatementList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::Var::Lexical.new('$bv')
)
)
)
),
RakuAST::StrLiteral.new(" block\n")
],
:stop("\n")
);
is-deeply $deparsed,
qq:!s:!c|qq:to//\nAn { $bv } block\n\n|,
'deparse';
is-deeply $_, "An interpolated block\n", @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'words processor splits a heredoc into words' => {
# qqw/ foo bar 42 /
ast RakuAST::Heredoc.new(
:segments[RakuAST::StrLiteral.new("foo bar 42\n")],
:processors['words'],
:stop("WORDS\n")
);
is-deeply $deparsed,
qq|qq:w:to/WORDS/\nfoo bar 42\nWORDS\n|,
'deparse';
is-deeply $_, ("foo", "bar", "42"), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Words processor applied to a heredoc with interpolation' => {
my $stuff = 'r baz';
# qqw/ ba$stuff 66 /
ast RakuAST::Heredoc.new(
:segments[
RakuAST::StrLiteral.new('ba'),
RakuAST::Var::Lexical.new('$stuff'),
RakuAST::StrLiteral.new(" 66\n")
],
:processors['words'],
:stop("WORDS\n")
);
is-deeply $deparsed,
qq:!s|qq:w:to/WORDS/\nba$stuff 66\nWORDS\n|,
'deparse';
is-deeply $_, ("bar", "baz", "66"), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Using the val processor alone on a heredoc' => {
# qq:v/42/
ast RakuAST::Heredoc.new(
:segments[RakuAST::StrLiteral.new("42\n")],
:processors['val'],
:stop("VAL\n")
);
is-deeply $deparsed,
qq|qq:v:to/VAL/\n42\nVAL\n|,
'deparse';
# is-deeply $_, IntStr.new(42,"42\n"), @type[$++]
# for EVAL($ast), EVAL($ast.DEPARSE);
is-deeply EVAL($ast), IntStr.new(42,"42\n"), @type[$++];
todo("problem in parsing static heredocs and :v");
is-deeply EVAL($deparsed), IntStr.new(42,"42\n"), @type[$++];
}
subtest 'Using the val processor in heredoc with interpolation' => {
my $end = '6';
# qq:v/4$end/
ast RakuAST::Heredoc.new(
:segments[
RakuAST::StrLiteral.new('4'),
RakuAST::Var::Lexical.new('$end'),
RakuAST::StrLiteral.new("\n")
],
:processors['val'],
:stop("VAL\n")
);
is-deeply $deparsed,
qq:!s|qq:v:to/VAL/\n4$end\nVAL\n|,
'deparse';
is-deeply $_, IntStr.new(46,"46\n"), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Using the words and val processor in a heredoc' => {
# <foo bar 42>
ast RakuAST::Heredoc.new(
:segments[RakuAST::StrLiteral.new("foo bar 42\n")],
:processors['words', 'val'],
:stop("VALS\n")
);
is-deeply $deparsed,
qq|qq:w:v:to/VALS/\nfoo bar 42\nVALS\n|,
'deparse';
is-deeply $_, ("foo", "bar", IntStr.new(42,"42")), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Words processor applied to a heredoc with interpolation' => {
my $stuff = 'r baz';
# qq:w:v/ba$stuff 66/
ast RakuAST::Heredoc.new(
:segments[
RakuAST::StrLiteral.new('ba'),
RakuAST::Var::Lexical.new('$stuff'),
RakuAST::StrLiteral.new(" 66\n")
],
:processors['words', 'val'],
:stop("HUH\n")
);
is-deeply $deparsed,
qq:!s|qq:w:v:to/HUH/\nba$stuff 66\nHUH\n|,
'deparse';
is-deeply $_, ('bar', 'baz', IntStr.new(66,"66")), @type[$++]
for EVAL($ast), EVAL($deparsed);
}
subtest 'Using the exec processor alone gives expected result' => {
# qx/ echo 123 /
ast RakuAST::Heredoc.new(
:segments[RakuAST::StrLiteral.new('echo 123')],
:processors['exec'],
:stop("GO\n")
);
is-deeply $deparsed,
qq|qq:x:to/GO/\necho 123\nGO\n|,
'deparse';
is-deeply $_, "123\n", @type[$++]
for EVAL($ast), EVAL($deparsed);
}
# vim: expandtab shiftwidth=4