use our own pragma for hints hash test
[p5sagit/Eval-WithLexicals.git] / t / hints.t
1 use strictures ();
2 my $strictures_hints;
3 BEGIN {
4   local $ENV{PERL_STRICTURES_EXTRA} = 0;
5   strictures->VERSION(1); strictures->import();
6   # Find the hint value that 'use strictures 1' sets on this perl.
7   $strictures_hints = $^H;
8 }
9 use strictures 1;
10
11 use Test::More;
12 use Eval::WithLexicals;
13 use lib 't/lib';
14
15 my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new(prelude => '');
16
17 is_deeply(
18   [ $eval->eval('$x = 1') ],
19   [ 1 ],
20   'Basic non-strict eval ok'
21 );
22
23 is_deeply(
24   $eval->lexicals, { },
25   'Lexical not stored'
26 );
27
28 $eval->eval('use strictures 1');
29
30 {
31   local $SIG{__WARN__} = sub { };
32
33   ok !eval { $eval->eval('$x') }, 'Unable to use undeclared variable';
34   like $@, qr/requires explicit package/, 'Correct message in $@';
35 }
36
37 is_deeply(
38   $eval->hints->{q{$^H}}, \$strictures_hints,
39  'Hints are set per strictures'
40 );
41
42 is_deeply(
43   $eval->lexicals, { },
44   'Lexical not stored'
45 );
46
47 # Assumption about perl internals: sort pragma will set a key in %^H.
48 $eval->eval(q{ { use hint_hash_pragma 'param' } }),
49 ok !exists $eval->hints->{q{%^H}}->{hint_hash_pragma},
50   "Lexical pragma used below main scope not captured";
51
52 $eval->eval(q{ use hint_hash_pragma 'param' }),
53 is $eval->hints->{q{%^H}}->{hint_hash_pragma}, 'param',
54   "Lexical pragma captured";
55
56 done_testing;