-
Notifications
You must be signed in to change notification settings - Fork 135
/
defined.t
140 lines (106 loc) · 4.1 KB
/
defined.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
use v6;
use Test;
plan 56;
# L<S32::Basics/Mu/=item defined>
=begin pod
Tests for the defined() builtin
=end pod
ok(!defined(Mu), 'Mu is not defined');
ok(!defined(Int), 'Int is not defined');
ok(!defined(Num), 'Num is not defined');
ok(!defined(Str), 'Str is not defined');
ok(defined(1), 'numeric literal 1 is defined');
ok(defined(""), 'empty string is defined');
ok(defined("a"), '"a" is defined');
ok(defined(0), 'numeric literal 0 is defined');
ok(defined(()), 'empty List is defined');
ok(defined([]), 'empty Array is defined');
ok(defined({}), 'empty Hash is defined');
my $foo;
ok(!defined($foo), 'unassigned variable $foo is undefined');
$foo = 1;
ok(defined($foo), 'variable $foo is now defined (as numeric literal 1)');
$foo = "";
ok(defined($foo), 'variable $foo is now defined (as a empty string)');
$foo = Nil;
ok(!defined($foo), 'variable $foo is now undefined again');
$foo = "a";
ok(defined($foo), 'variable $foo is now defined (as string "a")');
$foo = Mu;
ok(!defined($foo), 'variable $foo is now undefined again');
$foo = "b";
ok(defined($foo), 'variable $foo is now defined (as string "b")');
$foo = 0;
ok(defined($foo), 'variable $foo is now defined (as numeric literal 0)');
{
undefine($foo);
ok(!defined($foo), 'undefine $foo works');
}
# containers
my @bax;
ok(defined(@bax), 'unassigned variable @bax is defined');
@bax = 3, 4, 5;
ok(defined(@bax), 'unassigned variable @bax is defined');
@bax = Nil;
ok(defined(@bax), 'variable @bax is defined after assigning Nil');
# try the invocant syntax
{
my Mu $foo;
ok(!$foo.defined, 'unassigned variable $foo is undefined');
$foo = 1;
ok($foo.defined, 'variable $foo is now defined (as numeric literal 1)');
$foo = "";
ok($foo.defined, 'variable $foo is now defined (as a empty string)');
$foo = Nil;
ok(!$foo.defined, 'variable $foo is now undefined again');
$foo = "a";
ok($foo.defined, 'variable $foo is now defined (as string "a")');
$foo = Mu;
ok(!$foo.defined, 'variable $foo is now undefined again');
$foo = "b";
ok($foo.defined, 'variable $foo is now defined (as string "b")');
$foo = 0;
ok($foo.defined, 'variable $foo is now defined (as numeric literal 0)');
{
undefine($foo);
ok(!$foo.defined, 'undefine $foo works');
}
}
# RT #81352
# Ensure that we always get Bools
{
isa-ok defined(Mu), Bool, 'defined(Mu) returns a Bool';
isa-ok Mu.defined, Bool, 'Mu.defined returns a Bool';
isa-ok defined(Int), Bool, 'defined(Int) returns a Bool';
isa-ok Int.defined, Bool, 'Int.defined returns a Bool';
isa-ok defined(1), Bool, 'defined(1) returns a Bool';
isa-ok 1.defined, Bool, '1.defined returns a Bool';
isa-ok defined("a"), Bool, 'defined("a") returns a Bool';
isa-ok "a".defined, Bool, '"a".defined returns a Bool';
isa-ok defined(()), Bool, 'defined(()) returns a Bool';
isa-ok ().defined, Bool, '().defined returns a Bool';
isa-ok defined({}), Bool, 'defined({}) returns a Bool';
isa-ok {}.defined, Bool, '{}.defined returns a Bool';
my $bar;
isa-ok defined($bar), Bool, 'defined($bar) with $bar unset returns a Bool';
isa-ok $bar.defined, Bool, '$bar.defined with $bar unset returns a Bool';
$bar = "";
isa-ok defined($bar), Bool, 'defined($bar) with $bar eq "" returns a Bool';
isa-ok $bar.defined, Bool, '$bar.defined with $bar eq "" returns a Bool';
$bar = 7;
isa-ok defined($bar), Bool, 'defined($bar) with $bar == 7 returns a Bool';
isa-ok $bar.defined, Bool, '$bar.defined with $bar == 7 returns a Bool';
$bar = Mu;
isa-ok defined($bar), Bool, 'defined($bar) with $bar set to Mu returns a Bool';
isa-ok $bar.defined, Bool, '$bar.defined with $bar set to Mu returns a Bool';
}
# While porting a Perl 5 solution to QoTW regular #24, I noticed the following bug:
# my %a = (a => 1);
# defined %a{"b"}; # true!
my %a = (a => 1);
ok defined(%a{"a"}), "defined on a hash with parens (1)";
ok !defined(%a{"b"}), "defined on a hash with parens (2)";
# RT #76448
ok defined('a' => 5) ~~ Bool, 'defined is a listop, not a prefix op';
ok &defined, '&defined is available';
# vim: ft=perl6