add travis config
[p5sagit/Eval-WithLexicals.git] / t / hints.t
CommitLineData
2b376bd0 1use strictures ();
8d732f30 2use Test::More;
3use Eval::WithLexicals;
9661a07c 4use lib 't/lib';
8d732f30 5
14786ff8 6use strictures 1;
6c8bf56c 7use get_strictures_hints;
14786ff8 8
8d732f30 9my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new(prelude => '');
10
11is_deeply(
12 [ $eval->eval('$x = 1') ],
13 [ 1 ],
14 'Basic non-strict eval ok'
15);
16
17is_deeply(
18 $eval->lexicals, { },
19 'Lexical not stored'
20);
21
6c8bf56c 22my ($strictures_hints, $strictures_warn) = get_strictures_hints::hints();
8d732f30 23$eval->eval('use strictures 1');
24
25{
26 local $SIG{__WARN__} = sub { };
27
1f3ae1a2 28 ok !eval { $eval->eval('${"x"}') }, 'Unable to use undeclared variable';
29 like $@, qr/Can't use string .* as a SCALAR ref/,
30 'Correct message in $@';
8d732f30 31}
32
14786ff8 33is(
34 ${$eval->hints->{q{$^H}}}, $strictures_hints,
8d732f30 35 'Hints are set per strictures'
36);
37
14786ff8 38is(
52ec4728 39 (unpack "H*", ${ $eval->hints->{'${^WARNING_BITS}'} }),
14786ff8 40 (unpack "H*", $strictures_warn),
41 'Warning bits are set per strictures'
52ec4728 42) or do {
43 my @cats =
44 map {
45 [ $_ => $warnings::Bits{$_} ],
46 [ "fatal $_" => $warnings::DeadBits{$_} ],
47 }
48 grep $_ ne 'all',
49 keys %warnings::Bits;
50
51 my %info;
52 for my $check (
53 [ missing => $strictures_warn ],
54 [ extra => ${ $eval->hints->{'${^WARNING_BITS}'} } ],
55 ) {
56 my $bits = $check->[1];
57 $info{$check->[0]} = {
58 map { ($bits & $_->[1]) =~ /[^\0]/ ? ( $_->[0] => 1 ) : () }
59 @cats
60 };
61 }
62
63 {
64 my @extra = keys %{$info{extra}};
65 my @missing = keys %{$info{missing}};
66 delete @{$info{missing}}{ @extra };
67 delete @{$info{extra}}{ @missing };
68 }
69
70 for my $type (qw(missing extra)) {
71 my @found = grep $info{$type}{$_}, map $_->[0], @cats;
72 diag "$type:"
73 if @found;
74 diag " $_"
75 for @found;
76 }
77};
14786ff8 78
8d732f30 79is_deeply(
80 $eval->lexicals, { },
81 'Lexical not stored'
82);
83
84# Assumption about perl internals: sort pragma will set a key in %^H.
9661a07c 85$eval->eval(q{ { use hint_hash_pragma 'param' } }),
86ok !exists $eval->hints->{q{%^H}}->{hint_hash_pragma},
8d732f30 87 "Lexical pragma used below main scope not captured";
88
9661a07c 89$eval->eval(q{ use hint_hash_pragma 'param' }),
90is $eval->hints->{q{%^H}}->{hint_hash_pragma}, 'param',
8d732f30 91 "Lexical pragma captured";
92
a214c345 93$eval->eval('my $x = 1');
94is_deeply(
95 $eval->lexicals->{'$x'}, \1,
96 'Lexical captured when preserving hints',
97);
98
8d732f30 99done_testing;