-
Notifications
You must be signed in to change notification settings - Fork 135
/
nil.t
95 lines (74 loc) · 2.37 KB
/
nil.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
use v6;
use Test;
# Nil may be a type now. Required?
plan 29;
sub empty_sub {}
sub empty_do { do {} }
sub empty_branch_true { if 1 {} else { 1; } }
sub empty_branch_false { if 0 { 1; } else {} }
sub bare_return { return; }
sub rt74448 { eval '' }
ok empty_sub() === Nil, 'empty sub returns Nil';
ok empty_do() === Nil, 'do {} is Nil';
ok empty_branch_true() === Nil, 'if 1 {} is Nil';
ok empty_branch_false() === Nil, 'else {} is Nil';
ok bare_return() === Nil, 'bare return returns Nil';
ok rt74448() === Nil, 'eval of empty string is Nil';
nok Nil.defined, 'Nil is not defined';
ok ().defined, '() is defined';
nok (my $x = Nil).defined, 'assigning Nil to scalar leaves it undefined'; #OK
ok (my $y = ()).defined, 'assigning () to scalar results in a defined parcel'; #OK
# RT #63894
{
my $calls;
sub return_nil { $calls++; return; }
$calls = 0;
ok return_nil() === Nil, 'return_nil() === Nil';
is return_nil().perl, 'Nil', 'return_nil().perl says Nil';
is $calls, 2, 'return_nil() called twice';
my $n = return_nil();
nok $n.defined, 'variable holding nil is not defined';
}
{
my $x = 0;
$x++ for Nil;
is $x, 0, '$Statement for Nil; does zero iterations';
}
# RT 93980
#?rakudo todo 'RT 93980'
ok (my $rt93980 = Nil) === Any, 'Nil assigned to scalar produces an Any'; #OK
#?rakudo skip 'RT 93980'
ok (my Str $str93980 = Nil) === Str; #OK
is Nil.gist, 'Nil', 'Nil.gist eq "Nil"';
ok !Nil.new.defined, 'Nil.new is not defined';
{
subset MyInt of Int where True;
my MyInt $x = 5;
lives_ok { $x = Nil }, 'can assign Nil to subsets';
#?rakudo skip 'triage'
ok $x === Int, 'assigns to base-type object';
}
{
my $z := Nil;
ok $z === Nil, 'can bind to Nil';
}
{
sub f1($x) { } #OK
#?rakudo todo 'triage'
dies_ok { f1(Nil) }, 'param: dies for mandatory';
sub f2(Int $x?) { $x }
my $z;
#?rakudo todo 'triage'
lives_ok { $z = f2(Nil) }, 'param: lives for optional';
#?rakudo todo 'triage'
ok $z === Int, '... set to type object';
sub f3($x = 123) { $x }
lives_ok { $z = f3(Nil) }, 'param: lives for with-default';
#?rakudo todo 'triage'
is $z, 123, '... set to default';
sub f4($x = Nil) { $x }
ok f4() === Nil, 'can use Nil as a default (natural)';
ok f4(Nil) === Nil, 'can use Nil as a default (nil-triggered)';
}
done;
# vim: ft=perl6