905d6c847a24c1a2be34ab4ca7baef819435407a
[gitmo/Eval-Closure.git] / t / errors.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5 use Test::Fatal;
6
7 use Eval::Closure;
8
9 like(
10     exception { eval_closure() },
11     qr/'source'.*required/,
12     "error when source isn't declared"
13 );
14
15 like(
16     exception { eval_closure(source => {}) },
17     qr/'source'.*string or array/,
18     "error when source isn't string or array"
19 );
20
21 like(
22     exception { eval_closure(source => 1) },
23     qr/'source'.*return.*sub/,
24     "error when source doesn't return a sub"
25 );
26
27 like(
28     exception {
29         eval_closure(
30             source      => 'sub { }',
31             environment => { 'foo' => \1 },
32         )
33     },
34     qr/should start with \@, \%, or \$/,
35     "error from malformed env"
36 );
37
38 like(
39     exception {
40         eval_closure(
41             source      => 'sub { }',
42             environment => { '$foo' => 1 },
43         )
44     },
45     qr/must be.*reference/,
46     "error from non-ref value"
47 );
48
49 like(
50     exception { eval_closure(source => '$1++') },
51     qr/Modification of a read-only value/,
52     "gives us compile errors properly"
53 );
54
55 like(
56     exception { eval_closure(source => 'sub { $x }') },
57     qr/sub \s* { \s* \$x \s* }/x,
58     "without terse_error, includes the source code"
59 );
60
61 unlike(
62     exception { eval_closure(source => 'sub { $x }', terse_error => 1) },
63     qr/sub \s* { \s* \$x \s* }/x,
64     "with terse_error, does not include the source code"
65 );
66
67 done_testing;