File: accessor-reader-writer.t

package info (click to toggle)
libmoo-perl 1.006001-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 772 kB
  • ctags: 202
  • sloc: perl: 2,348; sh: 18; makefile: 6
file content (80 lines) | stat: -rw-r--r-- 1,466 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
74
75
76
77
78
79
80
use strictures 1;
use Test::More;
use Test::Fatal;

my @result;

{
  package Foo;

  use Moo;

  has one => (
    is     => 'rw',
    reader => 'get_one',
    writer => 'set_one',
  );

  sub one {'sub'}

  has two => (
    is     => 'lazy',
    default => sub { 2 },
    reader => 'get_two',
  );

  has three => (
    is     => 'rwp',
    reader => 'get_three',
    writer => 'set_three',
  );
}

{
  package Bar;

  use Moo;

  has two => (
    is     => 'rw',
    accessor => 'TWO',
  );
}

my $foo = Foo->new(one => 'lol');
my $bar = Bar->new(two => '...');

is( $foo->get_one, 'lol', 'reader works' );
$foo->set_one('rofl');
is( $foo->get_one, 'rofl', 'writer works' );
is( $foo->one, 'sub', 'reader+writer = no accessor' );

is( $foo->get_two, 2, 'lazy doesn\'t override reader' );

is( $foo->can('two'), undef, 'reader+ro = no accessor' );

ok( $foo->can('get_three'), 'rwp doesn\'t override reader');
ok( $foo->can('set_three'), 'rwp doesn\'t override writer');

ok( exception { $foo->get_one('blah') }, 'reader dies on write' );

is( $bar->TWO, '...', 'accessor works for reading' );
$bar->TWO('!!!');
is( $bar->TWO, '!!!', 'accessor works for writing' );

{
  package Baz;
  use Moo;

  ::is(::exception {
    has '@three' => (
      is     => 'lazy',
      default => sub { 3 },
      reader => 'three',
    );
  }, undef, 'declaring non-identifier attribute with proper reader works');
}

is( Baz->new->three, 3, '... and reader works');

done_testing;