Skip to content

Commit

Permalink
make tests a little more boring
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 22, 2015
1 parent 0044fbf commit d6e8b30
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
70 changes: 35 additions & 35 deletions t/mojo/base.t
Expand Up @@ -10,10 +10,10 @@ use Mojo::Base -strict;

use base 'Mojo::BaseTest::Base2';

__PACKAGE__->attr(heads => 1);
__PACKAGE__->attr(tests => 1);
__PACKAGE__->attr('name');

sub more_heads { shift->{heads} += shift // 1 }
sub more_tests { shift->{tests} += shift // 1 }

package Mojo::BaseTestTest;
use Mojo::Base 'Mojo::BaseTest';
Expand All @@ -29,55 +29,55 @@ use Mojo::BaseTest::Base2;
use Mojo::BaseTest::Base3;

# Basic functionality
my $monkey = Mojo::BaseTest->new->bananas(23);
my $monkey2 = Mojo::BaseTestTest->new(bananas => 24);
is $monkey2->bananas, 24, 'right attribute value';
is $monkey->bananas, 23, 'right attribute value';
my $object = Mojo::BaseTest->new->foo(23);
my $object2 = Mojo::BaseTestTest->new(foo => 24);
is $object2->foo, 24, 'right attribute value';
is $object->foo, 23, 'right attribute value';

# Instance method
$monkey = Mojo::BaseTestTestTest->new;
$monkey->attr('mojo');
is $monkey->mojo(23)->mojo, 23, 'monkey has mojo';
$object = Mojo::BaseTestTestTest->new;
$object->attr('mojo');
is $object->mojo(23)->mojo, 23, 'object has mojo';
ok !Mojo::BaseTestTest->can('mojo'), 'base class does not have mojo';
ok !!Mojo::BaseTestTest->can('heads'), 'base class has heads';
ok !!Mojo::BaseTestTest->can('tests'), 'base class has tests';
ok !Mojo::BaseTest->can('mojo'), 'base class does not have mojo';
ok !!Mojo::BaseTest->can('heads'), 'base class has heads';
ok !!Mojo::BaseTest->can('tests'), 'base class has tests';

# Default value defined but false
ok defined($monkey->coconuts);
is $monkey->coconuts, 0, 'right attribute value';
is $monkey->coconuts(5)->coconuts, 5, 'right attribute value';
ok defined($object->yada);
is $object->yada, 0, 'right attribute value';
is $object->yada(5)->yada, 5, 'right attribute value';

# Default value support
$monkey = Mojo::BaseTest->new;
isa_ok $monkey->name('foobarbaz'), 'Mojo::BaseTest',
$object = Mojo::BaseTest->new;
isa_ok $object->name('foobarbaz'), 'Mojo::BaseTest',
'attribute value has right class';
$monkey2 = Mojo::BaseTest->new->heads('3');
is $monkey2->heads, 3, 'right attribute value';
is $monkey->heads, 1, 'right attribute default value';
$object2 = Mojo::BaseTest->new->tests('3');
is $object2->tests, 3, 'right attribute value';
is $object->tests, 1, 'right attribute default value';

# Chained attributes and callback default value support
$monkey = Mojo::BaseTest->new;
is $monkey->ears, 2, 'right attribute value';
is $monkey->ears(6)->ears, 6, 'right chained attribute value';
is $monkey->eyes, 2, 'right attribute value';
is $monkey->eyes(6)->eyes, 6, 'right chained attribute value';
$object = Mojo::BaseTest->new;
is $object->bar, 2, 'right attribute value';
is $object->bar(6)->bar, 6, 'right chained attribute value';
is $object->baz, 2, 'right attribute value';
is $object->baz(6)->baz, 6, 'right chained attribute value';

# Tap into chain
$monkey = Mojo::BaseTest->new;
is $monkey->tap(sub { $_->name('foo') })->name, 'foo', 'right attribute value';
is $monkey->tap(sub { shift->name('bar')->name })->name, 'bar',
$object = Mojo::BaseTest->new;
is $object->tap(sub { $_->name('foo') })->name, 'foo', 'right attribute value';
is $object->tap(sub { shift->name('bar')->name })->name, 'bar',
'right attribute value';
is $monkey->tap('heads')->heads, 1, 'right attribute value';
is $monkey->more_heads, 2, 'right attribute value';
is $monkey->tap('more_heads')->heads, 3, 'right attribute value';
is $monkey->tap(more_heads => 3)->heads, 6, 'right attribute value';
is $object->tap('tests')->tests, 1, 'right attribute value';
is $object->more_tests, 2, 'right attribute value';
is $object->tap('more_tests')->tests, 3, 'right attribute value';
is $object->tap(more_tests => 3)->tests, 6, 'right attribute value';

# Inherit -base flag
$monkey = Mojo::BaseTest::Base3->new(evil => 1);
is $monkey->evil, 1, 'monkey is evil';
is $monkey->bananas, undef, 'monkey has no bananas';
is $monkey->bananas(3)->bananas, 3, 'monkey has 3 bananas';
$object = Mojo::BaseTest::Base3->new(test => 1);
is $object->test, 1, 'right attribute value';
is $object->foo, undef, 'no attribute value';
is $object->foo(3)->foo, 3, 'right attribute value';

# Exceptions
eval { Mojo::BaseTest->attr(foo => []) };
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/lib/Mojo/BaseTest/Base1.pm
@@ -1,6 +1,6 @@
package Mojo::BaseTest::Base1;
use Mojo::Base -base;

has 'bananas';
has 'foo';

1;
4 changes: 2 additions & 2 deletions t/mojo/lib/Mojo/BaseTest/Base2.pm
@@ -1,7 +1,7 @@
package Mojo::BaseTest::Base2;
use Mojo::Base 'Mojo::BaseTest::Base1';

has [qw(ears eyes)] => sub {2};
has coconuts => 0;
has [qw(bar baz)] => sub {2};
has yada => 0;

1;
2 changes: 1 addition & 1 deletion t/mojo/lib/Mojo/BaseTest/Base3.pm
@@ -1,6 +1,6 @@
package Mojo::BaseTest::Base3;
use Mojo::BaseTest::Base1 -base;

has 'evil';
has 'test';

1;

0 comments on commit d6e8b30

Please sign in to comment.