fix incompatibilities with TB2
Karen Etheridge [Sat, 13 Apr 2013 21:36:22 +0000 (14:36 -0700)]
I tested this with Test-Simple-1.005000_006.
The return value of with_immutable still indicates whether all tests contained
in its block had passed, even though this is undocumented.

Changes
lib/Test/Moose.pm
t/test_moose/with_immutable.t
t/test_moose/with_immutable_tb2.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index ea0c078..9799f8c 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@ Also see Moose::Manual::Delta for more details of, and workarounds
 for, noteworthy changes.
 
 {{$NEXT}}
+  * fix incompatibilities with Test::Builder 1.005+ (Karen Etheridge)
 
 2.0801 Thu, Mar 28, 2013
 
index 38f5fe6..f2f0d9e 100644 (file)
@@ -71,11 +71,17 @@ sub has_attribute_ok ($$;$) {
 sub with_immutable (&@) {
     my $block = shift;
     my $before = $Test->current_test;
+    my $passing_before = (Test::Builder->VERSION < 1.005 ? 0 : $Test->history->pass_count) || 0;
+
     $block->();
     Class::MOP::class_of($_)->make_immutable for @_;
     $block->();
+
     my $num_tests = $Test->current_test - $before;
-    return all { $_ } ($Test->summary)[-$num_tests..-1];
+    my $all_passed = Test::Builder->VERSION < 1.005
+        ? all { $_ } ($Test->summary)[-$num_tests..-1]
+        : $num_tests == $Test->history->pass_count - $passing_before;
+    return $all_passed;
 }
 
 1;
index 7690ce2..26f992e 100644 (file)
@@ -6,6 +6,9 @@ use warnings;
 use Test::Builder::Tester;
 use Test::More;
 
+plan skip_all => 'These tests are only for Test::Builder 0.9x'
+    if Test::Builder->VERSION >= 1.005;
+
 use Test::Moose;
 
 {
diff --git a/t/test_moose/with_immutable_tb2.t b/t/test_moose/with_immutable_tb2.t
new file mode 100644 (file)
index 0000000..634a0d4
--- /dev/null
@@ -0,0 +1,70 @@
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+    use Test::More;
+    plan skip_all => 'These tests are only for Test::Builder 1.005+'
+        if Test::Builder->VERSION < 1.005;
+}
+
+{
+    package Foo;
+    use Moose;
+}
+
+{
+    package Bar;
+    use Moose;
+}
+
+package main;
+
+use Test::Moose;
+use TB2::Tester;
+use TB2::History;   # FIXME - this should not need to be loaded here explicitly
+
+my ($ret1, $ret2);
+my $capture = capture {
+    $ret1 = with_immutable {
+        ok(Foo->meta->is_mutable, 'is mutable');
+    } qw(Foo);
+
+    $ret2 = with_immutable {
+        ok(Bar->meta->find_method_by_name('new'), 'can find "new" method');
+    } qw(Bar);
+};
+
+my $results = $capture->results;
+
+my @tests = (
+    [
+        'first test runs while Foo is mutable' => { name => 'is mutable',
+                                                    is_pass => 1,
+                                                   },
+    ],
+    [
+        'first test runs while Foo is immutable' => { name => 'is mutable',
+                                                      is_pass => 0,
+                                                    },
+    ],
+    [
+        'can find "new" while Bar is mutable'   => { name => 'can find "new" method',
+                                                     is_pass => 1,
+                                                   },
+    ],
+    [
+        'can find "new" while Bar is immutable' => { name => 'can find "new" method',
+                                                     is_pass => 1,
+                                                   },
+    ],
+);
+
+result_like(shift(@$results), $_->[1], $_->[0]) foreach @tests;
+
+ok(!$ret1, 'one of the is_immutable tests failed');
+ok($ret2, 'the find_method_by_name tests passed');
+
+done_testing;
+