-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
repl.t
315 lines (266 loc) · 10.1 KB
/
repl.t
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use v6;
use lib <t/packages>;
use Test;
use Test::Helpers;
plan 45;
my $*REPL-SCRUBBER = -> $_ is copy {
s/^^ "You may want to `zef install Readline` or `zef install Linenoise`"
" or use rlwrap for a line editor\n\n"//;
s/^^ "To exit type 'exit' or '^D'\n"//;
s:g/ ^^ "> " //; # Strip out the prompts
s:g/ ">" $ //; # Strip out the final prompt
s:g/ ^^ "* "+ //; # Strip out the continuation-prompts
$_
}
{
(temp %*ENV)<RAKUDO_ERROR_COLOR RAKUDO_LINE_EDITOR>:delete;
subtest 'sanity check; load without tweaking line editor' => {
plan 3;
my $p := run $*EXECUTABLE, '--repl-mode=interactive', :in, :out, :err;
$p.in.say: '133742.flip.say';
$p.in.close;
like $p.out.slurp(:close), /247331/, 'result of code is on STDOUT';
is $p.err.slurp(:close).chars, 0, 'no STDERR output';
is $p.exitcode, 0, 'successful exit code';
}
}
# RT #123187
is-run-repl «'my int $t=4; $t.say;' '$t.say'», <4 4>,
'can use native typed variable on subsequent lines (1)';
subtest 'indented code parses correctly' => {
plan 4;
todo "indent styles don't parse right", 3;
is-run-repl q:to/END/,
if False {
say ":(";
}
else {
say ":)";
}
END
":)\n", 'uncuddled else is parsed correctly';
is-run-repl q:to/END/,
if False
{
say ":(";
}
else
{
say ":)";
}
END
":)\n", 'open brace on next line is parsed correctly';
is-run-repl q:to/END/,
if False { say ":("; }
else { say ":)"; }
END
":)\n", 'partially-cuddled else is parsed correctly';
is-run-repl q:to/END/,
if False {
say ":(";
} else {
say ":)";
}
END
":)\n", 'cuddled else';
}
is-run-repl «'sub f {' 'say "works"' '}' 'f()'», {
.lines == 2 and .lines.tail eq 'works'
}, 'multi-line sub decl';
is-run-repl «'sub f { say "works" }' 'f()'», {
.lines == 2 and .lines.tail eq 'works'
}, 'single-line sub declaration works';
# RT #122914
subtest 'assignment maintains values on subsequent lines' => {
plan 4;
is-run-repl «'my $a = 42; say 1' '$a.say'», "1\n42\n",
'Scalar';
is-run-repl «'my @a = 1, 2, 3; say 1' '@a.elems.say'», "1\n3\n",
'Array';
is-run-repl «'my %h = 1..4; say 1' 'say +%h.keys'», "1\n2\n",
'Hash';
is-run-repl «'my \a = 100; say 1' 'a.say'», "1\n100\n",
'sigilless value';
}
skip 'Different result on OSX';
#is-run-repl "\n", '> ', 'entering a blank line gives back the prompt';
is-run-repl "''\n", "\n", 'an empty string gives back one blank line';
is-run-repl "}\n", /'===SORRY!===' .* 'Unexpected closing bracket'/,
'syntax error gives a compile-time error';
is-run-repl "}\nsay 42", {
.match: /'===SORRY!===' .* 'Unexpected closing bracket' .* '42'/
and 1 == .comb: 'Error while compiling'
}, 'syntax error clears on further input';
is-run-repl ['meow-meow()'], /'===SORRY!===' .* 'Undeclared routine'/,
'undeclared routines give compile time errors';
is-run-repl ['sub f { meow-meow() }; f()'],
/'===SORRY!===' .* 'Undeclared routine'/,
'undeclared routines inside another routine give compile time errors';
is-run-repl ['[1].map:{[].grep:Str}'], /'Cannot resolve caller'/,
'run-time error error gives the expected error';
for <return redo next last proceed succeed> -> $cmd {
is-run-repl [$cmd], /'Control flow commands not allowed in toplevel'/,
"raises error when you run control flow command '$cmd' in top level";
}
is-run-repl ['emit 42' ], /'emit without'/, '`emit` errors usefully';
is-run-repl ['take 42' ], /'take without'/, '`take` errors usefully';
is-run-repl ['warn "foo"'], /'foo' /, 'warn() shows warnings';
# RT#130876
{
is-run-repl ['say "hi"; die "meows";'], :out(/meows/),
'previous output does not silence exceptions';
is-run-repl ['say "hi"; my $f = Failure.new: "meows"; $f.Bool; $f'],
*.contains('meows').not,
'previous output prevents output of handled failures';
is-run-repl ['say "hi"; X::AdHoc.new(:payload<meows>)'],
*.contains('meows').not,
'previous output prevents output of unthrown exceptions';
is-run-repl ['say "hi"; try +"a"; $!'],
*.contains('meows').not,
'previous output does not prevent output of unthrown exceptions';
is-run-repl [
「say "hi"; use nqp; my $x = REPL.new(nqp::getcomp("perl6"), %)」
~ 「.repl-eval(q|die "meows"|, $);」
], *.contains('meows').not,
「can't trick REPL into thinking an exception was thrown (RT#130876)」;
}
# RT#130874
is-run-repl ['Nil'], /Nil/, 'REPL outputs Nil as a Nil';
# Since there might be some differences in REPL sessions in whitespace
# or what not, strip all \W and then check what we have left over is what
# a normal session should have. This lets us catch any unexpected error
# messages and stuff.
{
my $*REPL-SCRUBBER := Nil;
skip 'Result differs on OSX';
# is-run-repl ['say "hi"'], {
# .subst(:g, /\W+/, '') eq
# 'YoumaywanttozefinstallReadlineorzefinstallLinenoise'
# ~ 'oruserlwrapforalineeditor' ~ 'ToexittypeexitorD' ~ 'hi'
# }, 'REPL session does not have unexpected stuff';
## XXX TODO: need to write tests that exercise the REPL with Linenoise
# and Readline installed. Particular things to check:
# 1. History file can be made on all OSes:
# https://github.com/rakudo/rakudo/commit/b4fa6d6792dd02424d2182b73c31a071cddc0b8e
# 2. Test REPL does not show errors when $*HOME is not set:
# https://rt.perl.org/Ticket/Display.html?id=130456
}
# RT #119339
{
is-run-repl ['say 069'], :out("69\n"), :err(/'Potential difficulties:'
.* 'Leading 0' .+ "use '0o' prefix,"
.* '69 is not a valid octal number'
/), 'prefix 0 on invalid octal warns in REPL';
is-run-repl ['say 067'], :out("67\n"), :err(/'Potential difficulties:'
.* 'Leading 0' .+ "use '0o' prefix" .* "like, '0o67'"
/), 'prefix 0 on valid octal warns in REPL';
}
# RT #70297
{
my $proc = run $*EXECUTABLE, :in, :out, :err;
$proc.in.close;
skip 'Result differs on OSX';
# subtest {
# plan 2;
# is $proc.err.slurp, '', 'stderr is correct';
# like $proc.out.slurp, /"To exit type 'exit' or '^D'\n> "/,
# 'stdout is correct';
# }, 'Pressing CTRL+D in REPL produces correct output on exit';
}
# RT #128470
{
my $code-to-run = q/[1..99].map:{[$_%%5&&'fizz', $_%%3&&'buzz'].grep:Str}/
~ "\nsay 'We are still alive';\n";
is-run-repl $code-to-run,
out => /'Cannot resolve caller grep' .* 'We are still alive'/,
err => '',
'exceptions from lazy-evaluated things do not crash REPL';
}
# RT #127933
{
my $code = [~] 'my ( int8 $a, int16 $b, int32 $c, int64 $d,',
'uint8 $e, uint16 $f, uint32 $g, uint64 $h,',
'num32 $i, num64 $j,',
') = 1, 2, 3, 4, 5, 6, 7, 8, 9e0, 10e0;';
todo 'RT#127933';
is-run-repl "$code\nsay 'test is good';\n",
:err(''),
:out(/'(1 2 3 4 5 6 7 8 9 10)' .* 'test is good'/),
'Using native numeric types does not break REPL';
}
# RT #128595
{
# REPL must not start, but if it does start and wait for input, it'll
# "hang", from our point of view, which the test function will detect
doesn't-hang \(:w, $*EXECUTABLE,
'--repl-mode=interactive', '-M', 'NonExistentModuleRT128595'
), :out(/^$/), :err(/'Could not find NonExistentModuleRT128595'/),
'REPL with -M with non-existent module does not start';
}
# RT #128973
{
is-run-repl "my \$x = 42;\nsay qq/The value is \$x/;\n",
:err(''),
:out(/'The value is 42'/),
'variables persist across multiple lines of input';
}
{
# If the REPL evaluates all of the previously-entered code on each
# entered line of code, then we'll have more than just two 'say' print
# outs. So we check the output each output happens just once
my $code = <one two>.map({ "say 'testing-repl-$_';"}).join("\n");
is-run-repl "$code\n",
:err(''),
:out({
$^o.comb('testing-repl-one') == 1
and $^o.comb('testing-repl-two') == 1
}),
'previously-entered code must not be re-run on every line of input';
}
# RT #125412
{
my $code = 'sub x() returns Array of Int { return my @x of Int = 1,2,3 };'
~ "x().WHAT.say\n";
is-run-repl $code x 10,
:err(''),
:out({ not $^o.contains: '[Int][Int]' }),
'no bizzare types returned from redeclared "returns an `of` Array" sub';
}
# RT #127631
{
is-run-repl join("\n", <last next redo>, 'say "rt127631-pass"', ''),
:err(''),
:out(/'rt127631-pass'/),
'loop controls do not exit the REPL';
}
# RT #130719
{
is-run-repl join("\n", 'Mu', ''),
:err(''),
:out{.contains('failed').not},
「REPL can handle `Mu` as line's return value」;
}
# RT #132780
is-run-repl "say 42; none True\n", :err(''), :out{
.contains('42') and not .contains: 'No such method';
}, 'REPL does not explode with none Junction return values';
# RT #112986
is-run-repl '$_**2',
:out{!.contains('message') and !.contains('not found')
and !.contains('No such method')},
:err(''),
'no complaints about .message';
# RT #123380
is-run-repl 'say "b".subst(/(.)/,{$0~$0}); say "%20" ~~ /:i \%(<[0..9A..F]>**2)/;'
~ "\n" ~ 'say "a".subst(/(.)/,{$0~$0});',
:out{.contains('bb') and .contains('aa') and not .contains('2020')},
:err(''),
「no sticky $0 values across lines」;
# https://github.com/rakudo/rakudo/issues/1925
is-run-repl '&say.package', :out{.contains: 'GLOBAL'}, :err(''),
「REPL can auto-print non-Mu things that lack .WHERE and .gist」;
#https://github.com/rakudo/rakudo/issues/2184
is-run-repl 'my $fh = $*EXECUTABLE.open(:r)',
:out{.contains: 'IO::Handle' and not .contains('Failed to write')},
:err(''),
「no complaints about failed writing to filehandle when opening a file」;