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
|
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Exception;
{
package Foo;
use Moose;
use MooseX::Attribute::Shorthand string => {
is => 'ro',
isa => 'Str',
default => sub { $_[1] },
-meta_attr_options => { isa => 'Str' },
};
has foo => (string => 'FOO');
}
my $foo = Foo->new;
is($foo->foo, 'FOO', "expanded properly");
dies_ok { $foo->foo('sldkfj') } "expanded properly";
{
package Bar;
use Moose;
use MooseX::Attribute::Shorthand my_lazy_build => {
lazy => 1,
builder => sub { "_build_$_[0]" },
predicate => sub {
my $name = shift;
my $private = $name =~ s/^_//;
$private ? "_has_$name" : "has_$name";
},
clearer => sub {
my $name = shift;
my $private = $name =~ s/^_//;
$private ? "_clear_$name" : "clear_$name";
},
};
has public => (
is => 'ro',
isa => 'Str',
my_lazy_build => 1,
);
sub _build_public { 'PUBLIC' }
has _private => (
is => 'ro',
isa => 'Str',
my_lazy_build => 1,
);
sub _build__private { 'PRIVATE' }
}
my $bar = Bar->new;
can_ok($bar, $_) for qw(has_public clear_public _has_private _clear_private);
ok(!$bar->can($_), "Bar can't $_") for qw(has__private clear__private);
ok(!$bar->has_public, "doesn't have a value yet");
is($bar->public, 'PUBLIC', "gets a lazy value");
ok($bar->has_public, "has a value now");
$bar->clear_public;
ok(!$bar->has_public, "doesn't have a value again");
dies_ok { $bar->public('sldkfj') } "other options aren't overwritten";
ok(!$bar->_has_private, "doesn't have a value yet");
is($bar->_private, 'PRIVATE', "gets a lazy value");
ok($bar->_has_private, "has a value now");
$bar->_clear_private;
ok(!$bar->_has_private, "doesn't have a value again");
dies_ok { $bar->_private('sldkfj') } "other options aren't overwritten";
done_testing;
|