Fix weird indentation
[gitmo/Eval-Closure.git] / t / 05-memoize.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5 use Test::Fatal;
6 use Test::Requires 'Test::Output';
7
8 use Eval::Closure;
9
10 {
11     my $source = 'BEGIN { warn "foo\n" } sub { $foo * 2 }';
12
13     my $code;
14     my $bar = 15;
15     stderr_is {
16         $code = eval_closure(
17             source      => $source,
18             environment => {
19                 '$foo' => \$bar,
20             },
21         );
22     } "foo\n", "BEGIN was run";
23
24     is($code->(), 30, "got the right sub");
25
26     my $code2;
27     my $baz = 8;
28     stderr_is {
29         $code2 = eval_closure(
30             source      => $source,
31             environment => {
32                 '$foo' => \$baz,
33             },
34         );
35     } '', "BEGIN was not run twice";
36
37     is($code2->(), 16, "got the right sub");
38 }
39
40 {
41     my $source = 'BEGIN { warn "bar\n" } sub { $bar * 2 }';
42
43     my $code;
44     my $foo = 60;
45     stderr_is {
46         $code = eval_closure(
47             source      => $source,
48             environment => {
49                 '$bar' => \$foo,
50             },
51             description => 'foo',
52         );
53     } "bar\n", "BEGIN was run";
54
55     is($code->(), 120, "got the right sub");
56
57     my $code2;
58     my $baz = 23;
59     { local $TODO = "description breaks memoization";
60     stderr_is {
61         $code2 = eval_closure(
62             source      => $source,
63             environment => {
64                 '$bar' => \$baz,
65             },
66             description => 'baz',
67         );
68     } '', "BEGIN was not run twice";
69     }
70
71     is($code2->(), 46, "got the right sub");
72 }
73
74 {
75     my $source = 'BEGIN { warn "baz\n" } sub { Carp::confess "baz" }';
76
77     my $code;
78     stderr_is {
79         $code = eval_closure(
80             source      => $source,
81             description => 'first',
82         );
83     } "baz\n", "BEGIN was run";
84
85     like(exception { $code->() }, qr/baz at first line 1/,
86          "got the right description");
87
88     my $code2;
89     { local $TODO = "description breaks memoization";
90     stderr_is {
91         $code2 = eval_closure(
92             source      => $source,
93             description => 'second',
94         );
95     } '', "BEGIN was not run twice";
96     }
97
98     like(exception { $code2->() }, qr/baz at second line 1/,
99          "got the right description");
100 }
101
102 done_testing;