Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
simplify Mojo::Base tests
  • Loading branch information
kraih committed Feb 26, 2014
1 parent c0b0587 commit ff1cb9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/Mojo/Base.pm
Expand Up @@ -41,8 +41,8 @@ sub import {
}

sub attr {
my ($class, $attrs, $default) = @_;
return unless ($class = ref $class || $class) && $attrs;
my ($self, $attrs, $default) = @_;
return unless (my $class = ref $self || $self) && $attrs;

Carp::croak 'Default has to be a code reference or constant value'
if ref $default && ref $default ne 'CODE';
Expand Down
59 changes: 21 additions & 38 deletions t/mojo/base.t
Expand Up @@ -27,55 +27,39 @@ use Mojo::BaseTest::Base2;
use Mojo::BaseTest::Base3;

# Basic functionality
my @monkeys;
for my $i (1 .. 50) {
$monkeys[$i] = Mojo::BaseTest->new;
$monkeys[$i]->bananas($i);
is $monkeys[$i]->bananas, $i, 'right attribute value';
}
for my $i (51 .. 100) {
$monkeys[$i] = Mojo::BaseTestTest->new(bananas => $i);
is $monkeys[$i]->bananas, $i, 'right attribute value';
}
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';

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

# Default value defined but false
my $m = $monkeys[1];
ok defined($m->coconuts);
is $m->coconuts, 0, 'right attribute value';
$m->coconuts(5);
is $m->coconuts, 5, 'right attribute value';
ok defined($monkey->coconuts);
is $monkey->coconuts, 0, 'right attribute value';
is $monkey->coconuts(5)->coconuts, 5, 'right attribute value';

# Default value support
my $y = 1;
for my $i (101 .. 150) {
$y = !$y;
$monkeys[$i] = Mojo::BaseTest->new;
isa_ok $monkeys[$i]->name('foobarbaz'), 'Mojo::BaseTest',
'attribute value has right class';
$monkeys[$i]->heads('3') if $y;
$y
? is($monkeys[$i]->heads, 3, 'right attribute value')
: is($monkeys[$i]->heads, 1, 'right attribute default value');
}
$monkey = Mojo::BaseTest->new;
isa_ok $monkey->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';

# Chained attributes and callback default value support
for my $i (151 .. 200) {
$monkeys[$i] = Mojo::BaseTest->new;
is $monkeys[$i]->ears, 2, 'right attribute value';
is $monkeys[$i]->ears(6)->ears, 6, 'right chained attribute value';
is $monkeys[$i]->eyes, 2, 'right attribute value';
is $monkeys[$i]->eyes(6)->eyes, 6, 'right chained attribute value';
}
$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';

# Tap into chain
$monkey = Mojo::BaseTest->new;
Expand All @@ -87,8 +71,7 @@ is $monkey->tap(sub { shift->name('bar')->name })->name, 'bar',
$monkey = Mojo::BaseTest::Base3->new(evil => 1);
is $monkey->evil, 1, 'monkey is evil';
is $monkey->bananas, undef, 'monkey has no bananas';
$monkey->bananas(3);
is $monkey->bananas, 3, 'monkey has 3 bananas';
is $monkey->bananas(3)->bananas, 3, 'monkey has 3 bananas';

# Exceptions
eval { Mojo::BaseTest->attr(foo => []) };
Expand Down

0 comments on commit ff1cb9f

Please sign in to comment.