changelog
[p5sagit/Eval-WithLexicals.git] / t / simple.t
1 use strictures 1;
2 use Test::More;
3 use Eval::WithLexicals;
4
5 my $eval = Eval::WithLexicals->new;
6
7 is_deeply(
8   [ $eval->eval('my $x; $x++; $x;') ],
9   [ 1 ],
10   'Basic eval ok'
11 );
12
13 is_deeply(
14   $eval->lexicals, { '$x' => \1 },
15   'Lexical stored ok'
16 );
17
18 is_deeply(
19   [ $eval->eval('$x+1') ],
20   [ 2 ],
21   'Use lexical ok'
22 );
23
24 is_deeply(
25   [ $eval->eval('{ my $x = 0 }; $x') ],
26   [ 1 ],
27   'Inner scope plus lexical ok'
28 );
29
30 is_deeply(
31   [ $eval->eval('{ my $y = 0 }; $x') ],
32   [ 1 ],
33   'Inner scope and other lexical ok'
34 );
35
36 is_deeply(
37   [ keys %{$eval->lexicals} ],
38   [ '$x' ],
39   'No capture of invisible $y'
40 );
41
42 $eval->eval('my $y = sub { $_[0]+1 }');
43
44 is_deeply(
45   [ $eval->eval('$y->(2)') ],
46   [ 3 ],
47   'Sub created ok'
48 );
49
50 done_testing;