forked from Raku/roast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
begin.t
65 lines (54 loc) · 1.52 KB
/
begin.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
use v6;
use Test;
plan 9;
# the boundary between run time and compile time is hard to implement right.
# Some of those tests might look trivial, but nearly all of them are based
# on things that at one point or another failed in even rather advanced
# compilers.
{
is (BEGIN { "foo" }), "foo", 'Can use BEGIN <block> as an expression';
is (BEGIN "foo" ), "foo", 'Can use BEGIN <expr> as an expression';
}
{
my $my;
BEGIN { $my = 'foo' }
is $my, 'foo', 'can set outer lexical from a BEGIN block';
}
#?rakudo skip 'cannot assing to non-container'
{
our $our;
BEGIN { $our = 'foo' }
is $our, 'foo', 'can set outer package var from a BEGIN block';
}
{
sub my-uc($x) { $x.uc };
my ($my-uc, $setting-uc);
BEGIN { $my-uc = my-uc 'Ab' }
BEGIN { $setting-uc = uc 'Cd' }
is $my-uc, 'AB', 'can call subs from an outer scope in BEGIN';
is $setting-uc, 'CD', 'can call subs from the setting in BEGIN';
}
{
class SomeClass { };
my $var;
BEGIN { $var = SomeClass };
isa_ok $var, SomeClass, 'use a class at BEGIN time';
}
{
my $code;
BEGIN { $code = sub { 'returnvalue' } }
is $code(), 'returnvalue', 'Can execute an anonymous sub return from BEGIN';
}
{
my $tracker = '';
try {
eval q[
BEGIN { $tracker = "begin" }
$tracker = "run";
# syntax error (two terms in a row):
1 1
];
}
is $tracker, 'begin',
'BEGIN block was executed before a parse error happened later in the file';
}