1850e2753Smillert#!perl 2850e2753Smillert 3*b8851fccSafresh1print "1..32\n"; 4b39c5158Smillertmy $test = 0; 5b39c5158Smillert 6b39c5158Smillertsub failed { 7b39c5158Smillert my ($got, $expected, $name) = @_; 8b39c5158Smillert 9b39c5158Smillert print "not ok $test - $name\n"; 10b39c5158Smillert my @caller = caller(1); 11b39c5158Smillert print "# Failed test at $caller[1] line $caller[2]\n"; 12b39c5158Smillert if (defined $got) { 13b39c5158Smillert print "# Got '$got'\n"; 14b39c5158Smillert } else { 15b39c5158Smillert print "# Got undef\n"; 16b39c5158Smillert } 17b39c5158Smillert print "# Expected $expected\n"; 18b39c5158Smillert return; 19850e2753Smillert} 20850e2753Smillert 21b39c5158Smillertsub like { 22898184e3Ssthen my ($got, $pattern, $name) = @_; 23b39c5158Smillert $test = $test + 1; 24b39c5158Smillert if (defined $got && $got =~ $pattern) { 25898184e3Ssthen print "ok $test - $name\n"; 26b39c5158Smillert # Principle of least surprise - maintain the expected interface, even 27b39c5158Smillert # though we aren't using it here (yet). 28b39c5158Smillert return 1; 29b39c5158Smillert } 30b39c5158Smillert failed($got, $pattern, $name); 31b39c5158Smillert} 32b39c5158Smillert 33b39c5158Smillertsub is { 34898184e3Ssthen my ($got, $expect, $name) = @_; 35b39c5158Smillert $test = $test + 1; 36b39c5158Smillert if (defined $expect) { 37b39c5158Smillert if (defined $got && $got eq $expect) { 38898184e3Ssthen print "ok $test - $name\n"; 39b39c5158Smillert return 1; 40b39c5158Smillert } 41b39c5158Smillert failed($got, "'$expect'", $name); 42b39c5158Smillert } else { 43b39c5158Smillert if (!defined $got) { 44898184e3Ssthen print "ok $test - $name\n"; 45b39c5158Smillert return 1; 46b39c5158Smillert } 47b39c5158Smillert failed($got, 'undef', $name); 48b39c5158Smillert } 49b39c5158Smillert} 50850e2753Smillert 51850e2753Smillertsub f($$_) { my $x = shift; is("@_", $x) } 52850e2753Smillert 53850e2753Smillert$foo = "FOO"; 54850e2753Smillertmy $bar = "BAR"; 55850e2753Smillert$_ = 42; 56850e2753Smillert 57850e2753Smillertf("FOO xy", $foo, "xy"); 58850e2753Smillertf("BAR zt", $bar, "zt"); 59850e2753Smillertf("FOO 42", $foo); 60850e2753Smillertf("BAR 42", $bar); 61850e2753Smillertf("y 42", substr("xy",1,1)); 62850e2753Smillertf("1 42", ("abcdef" =~ /abc/)); 63850e2753Smillertf("not undef 42", $undef || "not undef"); 64850e2753Smillertf(" 42", -f "no_such_file"); 65850e2753Smillertf("FOOBAR 42", ($foo . $bar)); 66850e2753Smillertf("FOOBAR 42", ($foo .= $bar)); 67850e2753Smillertf("FOOBAR 42", $foo); 68850e2753Smillert 69850e2753Smillerteval q{ f("foo") }; 70850e2753Smillertlike( $@, qr/Not enough arguments for main::f at/ ); 71850e2753Smillerteval q{ f(1,2,3,4) }; 72850e2753Smillertlike( $@, qr/Too many arguments for main::f at/ ); 73850e2753Smillert 74850e2753Smillert&f(""); # no error 75850e2753Smillert 76850e2753Smillertsub g(_) { is(shift, $expected) } 77850e2753Smillert 78850e2753Smillert$expected = "foo"; 79850e2753Smillertg("foo"); 80850e2753Smillertg($expected); 81850e2753Smillert$_ = $expected; 82850e2753Smillertg(); 83850e2753Smillertg; 84850e2753Smillertundef $expected; &g; # $_ not passed 85850e2753Smillert 86850e2753Smillerteval q{ sub wrong1 (_$); wrong1(1,2) }; 87850e2753Smillertlike( $@, qr/Malformed prototype for main::wrong1/, 'wrong1' ); 88850e2753Smillert 89850e2753Smillerteval q{ sub wrong2 ($__); wrong2(1,2) }; 90850e2753Smillertlike( $@, qr/Malformed prototype for main::wrong2/, 'wrong2' ); 91850e2753Smillert 92b39c5158Smillertsub opt ($;_) { 93b39c5158Smillert is($_[0], "seen"); 94b39c5158Smillert is($_[1], undef, "; has precedence over _"); 95b39c5158Smillert} 96b39c5158Smillert 97850e2753Smillertopt("seen"); 98850e2753Smillert 99850e2753Smillertsub unop (_) { is($_[0], 11, "unary op") } 100850e2753Smillertunop 11, 22; # takes only the first parameter into account 101850e2753Smillert 102850e2753Smillertsub mymkdir (_;$) { is("@_", $expected, "mymkdir") } 103850e2753Smillert$expected = $_ = "mydir"; mymkdir(); 104850e2753Smillertmymkdir($expected = "foo"); 105850e2753Smillert$expected = "foo 493"; mymkdir foo => 0755; 106850e2753Smillert 107898184e3Ssthensub mylist (_@) { is("@_", $expected, "mylist") } 108898184e3Ssthen$expected = "foo"; 109898184e3Ssthen$_ = "foo"; 110898184e3Ssthenmylist(); 111898184e3Ssthen$expected = "10 11 12 13"; 112898184e3Ssthenmylist(10, 11 .. 13); 113898184e3Ssthen 114898184e3Ssthensub mylist2 (_%) { is("@_", $expected, "mylist2") } 115898184e3Ssthen$expected = "foo"; 116898184e3Ssthen$_ = "foo"; 117898184e3Ssthenmylist2(); 118898184e3Ssthen$expected = "10 a 1"; 119898184e3Ssthenmy %hash = (a => 1); 120898184e3Ssthenmylist2(10, %hash); 121898184e3Ssthen 122850e2753Smillert# $_ says modifiable, it's not passed by copy 123850e2753Smillert 124850e2753Smillertsub double(_) { $_[0] *= 2 } 125850e2753Smillert$_ = 21; 126850e2753Smillertdouble(); 127850e2753Smillertis( $_, 42, '$_ is modifiable' ); 128