more tests
Jesse Luehrs [Wed, 20 Oct 2010 21:17:19 +0000 (16:17 -0500)]
dist.ini
t/01-basic.t
t/10-errors.t [new file with mode: 0644]

index 435dae4..f844b71 100644 (file)
--- a/dist.ini
+++ b/dist.ini
@@ -12,6 +12,7 @@ Sub::Exporter = 0
 Try::Tiny = 0
 
 [Prereq / TestRequires]
+Test::Exception = 0
 Test::More = 0.88
 Test::Requires = 0
 
index 82224d9..7856f6e 100644 (file)
@@ -2,22 +2,48 @@
 use strict;
 use warnings;
 use Test::More;
+use Test::Exception;
 
 use Eval::Closure;
 
-my $foo = [];
+{
+    my $code = eval_closure(
+        source => 'sub { die "called\n" }',
+    );
+    ok($code, "got something");
 
-my $code = eval_closure(
-    source      => 'sub { push @$bar, @_ }',
-    environment => {
-        '$bar' => \$foo,
-    },
-    name        => 'test',
-);
-ok($code, "got something");
+    throws_ok { $code->() } qr/^called$/, "got the right thing";
+}
 
-$code->(1);
+{
+    my $foo = [];
 
-is_deeply($foo, [1], "got the right thing");
+    my $code = eval_closure(
+        source      => 'sub { push @$bar, @_ }',
+        environment => {
+            '$bar' => \$foo,
+        },
+        name        => 'test',
+    );
+    ok($code, "got something");
+
+    $code->(1);
+
+    is_deeply($foo, [1], "got the right thing");
+}
+
+{
+    my $foo = [1, 2, 3];
+
+    my $code = eval_closure(
+        # not sure if strict leaking into evals is intended, i think i remember
+        # it being changed in newer perls
+        source => 'do { no strict; sub { $foo } }',
+    );
+
+    ok($code, "got something");
+
+    ok(!$code->(), "environment is clean");
+}
 
 done_testing;
diff --git a/t/10-errors.t b/t/10-errors.t
new file mode 100644 (file)
index 0000000..d8925ee
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+use Eval::Closure;
+
+throws_ok {
+    eval_closure()
+} qr/'source'.*required/, "error when source isn't declared";
+
+throws_ok {
+    eval_closure(
+        source => {},
+    )
+} qr/'source'.*string or array/, "error when source isn't string or array";
+
+throws_ok {
+    eval_closure(
+        source => '1',
+    )
+} qr/'source'.*return.*sub/, "error when source doesn't return a sub";
+
+throws_ok {
+    eval_closure(
+        source      => 'sub { }',
+        environment => { 'foo' => \1 },
+    )
+} qr/should start with \@, \%, or \$/, "error from malformed env";
+
+throws_ok {
+    eval_closure(
+        source      => 'sub { }',
+        environment => { '$foo' => 1 },
+    )
+} qr/must be.*reference/, "error from non-ref value";
+
+throws_ok {
+    eval_closure(
+        source => '$1++',
+    )
+} qr/Modification of a read-only value/, "gives us compile errors properly";
+
+done_testing;