Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert t/mojo/dynamic_methods.t to use subtests #1968

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions t/mojo/dynamic_methods.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ sub BUILD_DYNAMIC {

package main;

# Basics
my ($t1, $t2) = (Mojo::TestDynamic->new, Mojo::TestDynamic->new);
Mojo::DynamicMethods::register 'Mojo::TestDynamic', $t1->hashref, 'foo', sub { };
my $foo = \&Mojo::TestDynamic::_Dynamic::foo;
my ($called_foo, $dyn_methods);
Mojo::DynamicMethods::register 'Mojo::TestDynamic', $t1->hashref, 'foo', sub { $called_foo++; $dyn_methods = $_[1] };
is $foo, \&Mojo::TestDynamic::_Dynamic::foo, 'foo not reinstalled';
ok !Mojo::TestDynamic->can('foo'), 'dynamic method is hidden';
ok eval { $t1->foo; 1 }, 'foo called ok';
cmp_ok $called_foo, '==', 1, 'called dynamic method';
ok !eval { $t2->foo; 1 }, 'error calling foo on wrong object';

# Garbage collection
undef($t1);
undef($t2);
ok(!keys(%$dyn_methods), 'dynamic methods expired');
my ($dyn_methods);

subtest 'Basics' => sub {
Mojo::DynamicMethods::register 'Mojo::TestDynamic', $t1->hashref, 'foo', sub { };
my $foo = \&Mojo::TestDynamic::_Dynamic::foo;
my ($called_foo, $dyn_methods);
Mojo::DynamicMethods::register 'Mojo::TestDynamic', $t1->hashref, 'foo', sub { $called_foo++; $dyn_methods = $_[1] };
is $foo, \&Mojo::TestDynamic::_Dynamic::foo, 'foo not reinstalled';
ok !Mojo::TestDynamic->can('foo'), 'dynamic method is hidden';
ok eval { $t1->foo; 1 }, 'foo called ok';
cmp_ok $called_foo, '==', 1, 'called dynamic method';
ok !eval { $t2->foo; 1 }, 'error calling foo on wrong object';
};

subtest 'Garbage collection' => sub {
undef($t1);
undef($t2);
ok(!keys(%$dyn_methods), 'dynamic methods expired');
};

done_testing;