-
Notifications
You must be signed in to change notification settings - Fork 135
/
nil.t
132 lines (104 loc) · 3.99 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
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
use v6;
use Test;
# Nil may be a type now. Required?
plan 51;
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
nok Nil.so, 'Nil.so is False';
ok Nil.ACCEPTS(Any) == Nil, 'Nil.ACCEPTS always returns Nil';
ok Nil.JustAnyMethod == Nil, 'Any method on Nil should return Nil';
# 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
ok (my $rt93980 = Nil) === Any, 'Nil assigned to scalar produces an Any'; #OK
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';
ok $x === MyInt, 'assigns to subset type object';
}
{
my $z := Nil;
ok $z === Nil, 'can bind to Nil';
}
{
sub f1($x) { } #OK
#?rakudo todo 'triage'
throws_like { f1(Nil) },
Exception, # XXX fix when this starts to fail
'param: dies for mandatory';
sub f2(Int $x?) { $x }
my $z;
#?rakudo skip 'triage'
lives_ok { $z = f2(Nil) }, 'param: lives for optional';
#?rakudo todo 'triage'
ok $z === Int, '... set to type object';
my $z2 is default(Nil);
#?rakudo todo 'triage'
lives_ok { $z = f2($z2) }, 'param: lives for optional from var';
#?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)';
}
#?niecza todo '$/!_ does not default to Nil'
{
ok $/ === Nil, '$/ is by default Nil';
ok $! === Nil, '$! is by default Nil';
ok $_ === Nil, '$_ is by default Nil';
ok $/.VAR.default === Nil, '$/ has Nil as default';
ok $!.VAR.default === Nil, '$! has Nil as default';
ok $_.VAR.default === Nil, '$_ has Nil as default';
}
# calling methods and similar things on Nil should return Nil again
{
sub niltest { return Nil };
ok niltest() === Nil, "sanity";
ok niltest.foo === Nil, "calling methods on Nil gives Nil again I";
ok niltest.foo.bar === Nil, "calling methods on Nil gives Nil again II";
ok niltest.foo.bar.baz === Nil, "calling methods on Nil gives Nil again III";
ok niltest[0] === Nil, "array access on Nil gives Nil again I";
ok niltest[0][2] === Nil, "array access on Nil gives Nil again II";
ok niltest[0][2][4] === Nil, "array access on Nil gives Nil again III";
ok niltest<foo> === Nil, "hash access on Nil gives Nil again I";
ok niltest<foo><bar> === Nil, "hash access on Nil gives Nil again II";
ok niltest<foo><bar><A> === Nil, "hash access on Nil gives Nil again II";
ok niltest.foo.bar.<bar>.[12].[99].<foo> === Nil, ".<> and .[] works properly, too";
}
# vim: ft=perl6