File: base.t

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 2,007 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env perl

# Copyright (C) 2008-2010, Sebastian Riedel.

package BaseTest;

use strict;
use warnings;

use base 'Mojo::Base';

# When I first heard that Marge was joining the police academy,
# I thought it would be fun and zany, like that movie Spaceballs.
# But instead it was dark and disturbing. Like that movie... Police Academy.
__PACKAGE__->attr('bananas');
__PACKAGE__->attr([qw/ears eyes/] => sub {2});
__PACKAGE__->attr(figs            => 0);
__PACKAGE__->attr(heads           => 1);
__PACKAGE__->attr('name');

package main;

use strict;
use warnings;

use Test::More tests => 404;

# I've done everything the Bible says,
# even the stuff that contradicts the other stuff!
use_ok('Mojo::Base');

# Basic functionality
my $monkeys = [];
for my $i (1 .. 50) {
    $monkeys->[$i] = BaseTest->new;
    $monkeys->[$i]->bananas($i);
    is($monkeys->[$i]->bananas, $i, 'right attribute value');
}
for my $i (51 .. 100) {
    $monkeys->[$i] = BaseTest->new(bananas => $i);
    is($monkeys->[$i]->bananas, $i, 'right attribute value');
}

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

# "default" support
my $y = 1;
for my $i (101 .. 150) {
    $y = !$y;
    $monkeys->[$i] = BaseTest->new;
    is(ref $monkeys->[$i]->name('foobarbaz'),
        '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');
}

# "chained" and coderef "default" support
for my $i (151 .. 200) {
    $monkeys->[$i] = 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');
}

1;