summaryrefslogtreecommitdiffstats
path: root/t/01-basic.t
blob: 2d75e591efac3652db71cc6b22f87c532dde9e21 (plain) (blame)
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
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;

use Scalar::Util qw(reftype);

{
    package Foo;
    use Moose;
    use MooseX::ArrayRef;

    has foo => (is => 'rw');
    has bar => (is => 'ro', lazy_build => 1);
    sub _build_bar { 'BAR' }
}

my $foo = Foo->new;
is(reftype($foo), 'ARRAY', "got an array instance");
isa_ok($foo, 'Foo');
ok(!$foo->has_bar, "bar not initialized yet");
is($foo->bar, 'BAR', "lazy-built properly");
ok($foo->has_bar, "bar initialized now");
is($foo->foo, undef, "foo not initialized yet");
$foo->foo('FOO');
is($foo->foo, 'FOO', "foo initialized now");
my @contents = @$foo;
is_deeply(\@$foo, ['FOO', 'BAR'], "got the right instance data");

{
    package Bar;
    use Moose;
    extends 'Foo';

    has baz => (is => 'rw');
}

my $bar = Bar->new;
is(reftype($bar), 'ARRAY', "got an array instance");
isa_ok($bar, 'Bar');
isa_ok($bar, 'Foo');
ok(!$bar->has_bar, "bar not initialized yet");
is($bar->bar, 'BAR', "lazy-built properly");
ok($bar->has_bar, "bar initialized now");
is($bar->foo, undef, "foo not initialized yet");
$bar->foo('FOO');
is($bar->foo, 'FOO', "foo initialized now");
is($bar->baz, undef, "baz not initialized yet");
$bar->baz('BAZ');
is($bar->baz, 'BAZ', "baz initialized now");
is_deeply(\@$bar, ['FOO', 'BAR', 'BAZ'], "got the right instance data");

done_testing;