sub _generate_trigger {
my ($self, $name, $obj, $value, $trigger) = @_;
+ if (my $quoted = quoted_from_sub($trigger)) {
+ die "Captures? ARGH!" if $quoted->[2];
+ my $code = $quoted->[1];
+ return 'do { local @_ = ('.join(', ', $obj, $value).'); '.$code.' }';
+ }
my $cap_name = qq{\$trigger_for_${name}};
$self->{captures}->{$cap_name} = \$trigger;
- "${cap_name}->(${obj}, ${value})";
+ return "${cap_name}->(${obj}, ${value})";
}
sub _generate_simple_set {
use strictures 1;
use Test::More;
-my @one_tr;
+our @tr;
-{
- package Foo;
+sub run_for {
+ my $class = shift;
- use Class::Tiny;
+ @tr = ();
- has one => (is => 'rw', trigger => sub { push @one_tr, $_[1] });
-}
+ my $obj = $class->new;
+
+ ok(!@tr, "${class}: trigger not fired with no value");
-my $foo = Foo->new;
+ $obj = $class->new(one => 1);
-ok(!@one_tr, "trigger not fired with no value");
+ is_deeply(\@tr, [ 1 ], "${class}: trigger fired on new");
-$foo = Foo->new(one => 1);
+ my $res = $obj->one(2);
-is_deeply(\@one_tr, [ 1 ], "trigger fired on new");
+ is_deeply(\@tr, [ 1, 2 ], "${class}: trigger fired on set");
-my $res = $foo->one(2);
+ is($res, 2, "${class}: return from set ok");
-is_deeply(\@one_tr, [ 1, 2 ], "trigger fired on set");
+ is($obj->one, 2, "${class}: return from accessor ok");
-is($res, 2, "return from set ok");
+ is_deeply(\@tr, [ 1, 2 ], "${class}: trigger not fired for accessor as get");
+}
-is($foo->one, 2, "return from accessor ok");
+{
+ package Foo;
+
+ use Class::Tiny;
+
+ has one => (is => 'rw', trigger => sub { push @::tr, $_[1] });
+}
+
+run_for 'Foo';
+
+{
+ package Bar;
+
+ use Sub::Quote;
+ use Class::Tiny;
+
+ has one => (is => 'rw', trigger => quote_sub q{ push @::tr, $_[1] });
+}
-is_deeply(\@one_tr, [ 1, 2 ], "trigger not fired for accessor as get");
+run_for 'Bar';
done_testing;