summaryrefslogtreecommitdiff
path: root/t/op/whereis.t
blob: 522e7d866187f04337db9ef61634d5544cbffb7e (plain)
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
#!./perl

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    set_up_inc('../lib');
}
use strict;
use warnings;
no warnings qw(uninitialized experimental::smartmatch);

plan tests => 19;

foreach(3) {
    CORE::whereis(qr/\A3\z/) {
	pass "CORE::whereis without feature flag";
    }
}

use feature 'switch';

foreach(3) {
    CORE::whereis(qr/\A3\z/) {
	pass "CORE::whereis with feature flag";
    }
}

foreach(3) {
    whereis(qr/\A3\z/) {
	pass "whereis with feature flag";
    }
}

package MatchAbc { use overload "~~" => sub { $_[1] eq "abc" }, fallback => 1; }
my $matchabc = bless({}, "MatchAbc");
my $regexpabc = qr/\Aabc\z/;

foreach("abc") {
    my $x = "foo";
    is($x, "foo", "whereis lexical scope not started yet");
    whereis(my $x = $matchabc) {
	is($x, $matchabc, "whereis lexical scope starts");
    }
    is($x, "foo", "whereis lexical scope ends");
}

foreach my $matcher (undef, 0, 1, [], {}, sub { 1 }) {
    my $res = eval {
	my $r;
	foreach("abc") {
	    whereis($matcher) {
		$r = 1;
	    }
	    $r = 0;
	}
	$r;
    };
    like $@, qr/\ACannot smart match without a matcher object /;
}

foreach my $matcher ($matchabc, $regexpabc) {
    foreach my $matchee (qw(abc xyz)) {
	my $res = eval {
	    my $r;
	    foreach($matchee) {
		whereis($matcher) {
		    $r = 1;
		}
		$r = 0;
	    }
	    $r;
	};
	is $@, "";
	is !!$res, $matchee eq "abc";
    }
}

1;