This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #90160] U* gives ‘U0 mode on an empty string’
[perl5.git] / t / comp / uproto.t
CommitLineData
b13fd70a
RGS
1#!perl
2
cf1e28d2
NC
3print "1..39\n";
4my $test = 0;
5
6sub failed {
9924e635 7 my ($got, $expected, $name) = @_;
cf1e28d2 8
9924e635 9 print "not ok $test - $name\n";
cf1e28d2
NC
10 my @caller = caller(1);
11 print "# Failed test at $caller[1] line $caller[2]\n";
12 if (defined $got) {
13 print "# Got '$got'\n";
14 } else {
15 print "# Got undef\n";
16 }
17 print "# Expected $expected\n";
18 return;
b13fd70a
RGS
19}
20
cf1e28d2
NC
21sub like {
22 my ($got, $pattern) = @_;
23 $test = $test + 1;
24 if (defined $got && $got =~ $pattern) {
25 print "ok $test\n";
26 # Principle of least surprise - maintain the expected interface, even
27 # though we aren't using it here (yet).
28 return 1;
29 }
9924e635 30 failed($got, $pattern, $name);
cf1e28d2
NC
31}
32
33sub is {
34 my ($got, $expect) = @_;
35 $test = $test + 1;
36 if (defined $expect) {
37 if (defined $got && $got eq $expect) {
38 print "ok $test\n";
39 return 1;
40 }
9924e635 41 failed($got, "'$expect'", $name);
cf1e28d2
NC
42 } else {
43 if (!defined $got) {
44 print "ok $test\n";
45 return 1;
46 }
9924e635 47 failed($got, 'undef', $name);
cf1e28d2
NC
48 }
49}
b13fd70a
RGS
50
51sub f($$_) { my $x = shift; is("@_", $x) }
52
53$foo = "FOO";
54my $bar = "BAR";
55$_ = 42;
56
57f("FOO xy", $foo, "xy");
58f("BAR zt", $bar, "zt");
59f("FOO 42", $foo);
60f("BAR 42", $bar);
61f("y 42", substr("xy",1,1));
62f("1 42", ("abcdef" =~ /abc/));
63f("not undef 42", $undef || "not undef");
64f(" 42", -f "no_such_file");
65f("FOOBAR 42", ($foo . $bar));
66f("FOOBAR 42", ($foo .= $bar));
67f("FOOBAR 42", $foo);
68
69eval q{ f("foo") };
70like( $@, qr/Not enough arguments for main::f at/ );
71eval q{ f(1,2,3,4) };
72like( $@, qr/Too many arguments for main::f at/ );
73
236b555a
RGS
74{
75 my $_ = "quarante-deux";
76 $foo = "FOO";
77 $bar = "BAR";
78 f("FOO quarante-deux", $foo);
79 f("BAR quarante-deux", $bar);
80 f("y quarante-deux", substr("xy",1,1));
81 f("1 quarante-deux", ("abcdef" =~ /abc/));
82 f("not undef quarante-deux", $undef || "not undef");
83 f(" quarante-deux", -f "no_such_file");
84 f("FOOBAR quarante-deux", ($foo . $bar));
85 f("FOOBAR quarante-deux", ($foo .= $bar));
86 f("FOOBAR quarante-deux", $foo);
87}
88
b13fd70a
RGS
89&f(""); # no error
90
236b555a
RGS
91sub g(_) { is(shift, $expected) }
92
93$expected = "foo";
94g("foo");
95g($expected);
96$_ = $expected;
97g();
3cd0a11a 98g;
236b555a
RGS
99undef $expected; &g; # $_ not passed
100{ $expected = my $_ = "bar"; g() }
f00d1d61
RGS
101
102eval q{ sub wrong1 (_$); wrong1(1,2) };
103like( $@, qr/Malformed prototype for main::wrong1/, 'wrong1' );
104
105eval q{ sub wrong2 ($__); wrong2(1,2) };
106like( $@, qr/Malformed prototype for main::wrong2/, 'wrong2' );
5df4b323 107
3d899d64
NC
108sub opt ($;_) {
109 is($_[0], "seen");
110 is($_[1], undef, "; has precedence over _");
111}
112
5df4b323 113opt("seen");
bfd79223 114
cb40c25d 115sub unop (_) { is($_[0], 11, "unary op") }
bfd79223 116unop 11, 22; # takes only the first parameter into account
cb40c25d
RGS
117
118sub mymkdir (_;$) { is("@_", $expected, "mymkdir") }
119$expected = $_ = "mydir"; mymkdir();
120mymkdir($expected = "foo");
121$expected = "foo 493"; mymkdir foo => 0755;
6a8363ef
RGS
122
123# $_ says modifiable, it's not passed by copy
124
125sub double(_) { $_[0] *= 2 }
126$_ = 21;
127double();
128is( $_, 42, '$_ is modifiable' );
129{
130 my $_ = 22;
131 double();
132 is( $_, 44, 'my $_ is modifiable' );
133}