summaryrefslogtreecommitdiff
path: root/t/attributes/numeric_defaults.t
blob: 0691cde280bc2b1809b676bb02f6e95e52c267be (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use strict;
use warnings;
use Test::More;
use Test::Moose;
use B;

{
    package Foo;
    use Moose;

    has foo => (is => 'ro', default => 100);

    sub bar { 100 }
}

with_immutable {
    my $foo = Foo->new;
    for my $meth (qw(foo bar)) {
        my $val = $foo->$meth;
        my $b = B::svref_2object(\$val);
        my $flags = $b->FLAGS;
        ok($flags & B::SVf_IOK || $flags & B::SVp_IOK, "it's an int");
        ok(!($flags & B::SVf_POK), "not a string");
    }
} 'Foo';

{
    package Bar;
    use Moose;

    has foo => (is => 'ro', lazy => 1, default => 100);

    sub bar { 100 }
}

with_immutable {
    my $bar = Bar->new;
    for my $meth (qw(foo bar)) {
        my $val = $bar->$meth;
        my $b = B::svref_2object(\$val);
        my $flags = $b->FLAGS;
        ok($flags & B::SVf_IOK || $flags & B::SVp_IOK, "it's an int");
        ok(!($flags & B::SVf_POK), "not a string");
    }
} 'Bar';

{
    package Baz;
    use Moose;

    has foo => (is => 'ro', isa => 'Int', lazy => 1, default => 100);

    sub bar { 100 }
}

with_immutable {
    my $baz = Baz->new;
    for my $meth (qw(foo bar)) {
        my $val = $baz->$meth;
        my $b = B::svref_2object(\$val);
        my $flags = $b->FLAGS;
        ok($flags & B::SVf_IOK || $flags & B::SVp_IOK, "it's an int");
        ok(!($flags & B::SVf_POK), "not a string");
    }
} 'Baz';

{
    package Foo2;
    use Moose;

    has foo => (is => 'ro', default => 10.5);

    sub bar { 10.5 }
}

with_immutable {
    my $foo2 = Foo2->new;
    for my $meth (qw(foo bar)) {
        my $val = $foo2->$meth;
        my $b = B::svref_2object(\$val);
        my $flags = $b->FLAGS;
        ok($flags & B::SVf_NOK || $flags & B::SVp_NOK, "it's a num");
        ok(!($flags & B::SVf_POK), "not a string");
    }
} 'Foo2';

{
    package Bar2;
    use Moose;

    has foo => (is => 'ro', lazy => 1, default => 10.5);

    sub bar { 10.5 }
}

with_immutable {
    my $bar2 = Bar2->new;
    for my $meth (qw(foo bar)) {
        my $val = $bar2->$meth;
        my $b = B::svref_2object(\$val);
        my $flags = $b->FLAGS;
        ok($flags & B::SVf_NOK || $flags & B::SVp_NOK, "it's a num");
        ok(!($flags & B::SVf_POK), "not a string");
    }
} 'Bar2';

{
    package Baz2;
    use Moose;

    has foo => (is => 'ro', isa => 'Num', lazy => 1, default => 10.5);

    sub bar { 10.5 }
}

with_immutable {
    my $baz2 = Baz2->new;
    for my $meth (qw(foo bar)) {
        my $val = $baz2->$meth;
        my $b = B::svref_2object(\$val);
        my $flags = $b->FLAGS;
        ok($flags & B::SVf_NOK || $flags & B::SVp_NOK, "it's a num");
        # it's making sure that the Num value doesn't get converted to a string for regex matching
        # this is the reason for using a temporary variable, $val for regex matching,
        # instead of $_[1] in Num implementation in lib/Moose/Util/TypeConstraints/Builtins.pm
        ok(!($flags & B::SVf_POK), "not a string");
    }
} 'Baz2';

done_testing;