File: intersection.t

package info (click to toggle)
libspecio-perl 0.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,132 kB
  • sloc: perl: 5,200; sh: 23; makefile: 2
file content (256 lines) | stat: -rw-r--r-- 6,749 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use strict;
use warnings;

use FindBin qw( $Bin );

my $lib_path;

BEGIN {
    if ( $Bin =~ /xt/ ) {
        $lib_path = "$Bin/../../t";
    }
    else {
        $lib_path = "$Bin";
    }
}

use lib "$lib_path/lib";

use Test::Fatal;
use Test::More 0.96;
use Test::Specio qw( test_constraint :vars );

use Specio::Constraint::Intersection;
use Specio::Declare;
use Specio::DeclaredAt;
use Specio::Library::Builtins;

# The test output looks something like this:
#
# "Attempt to free unreferenced scalar: SV 0xf64bf0 at /home/autarch/perl5/perlbrew/perls/perl-5.12.5/lib/site_perl/5.12.5/Test/Builder.pm line 302."
#
# But the problem isn't in Test::Builder. It's something to do with
# overloading, because it happens when we try to test the non-inlined types
# with a NumOverload object.
plan skip_all =>
    'This test triggers some odd overloading bug that causes a segfault on older perls'
    if $] < 5.014;

# The glob vars only work when they're use in the same package as where
# they're declared. Globs are weird.
my $GLOB = do {
    ## no critic (TestingAndDebugging::ProhibitNoWarnings)
    no warnings 'once';
    *SOME_GLOB;
};

## no critic (Variables::RequireInitializationForLocalVars)
local *FOO;
my $GLOB_OVERLOAD = _T::GlobOverload->new( \*FOO );

local *BAR;
{
    ## no critic (InputOutput::ProhibitBarewordFileHandles, InputOutput::RequireBriefOpen)
    open BAR, '<', $^X or die "Could not open $^X for the test";
}
my $GLOB_OVERLOAD_FH = _T::GlobOverload->new( \*BAR );

{
    package HashArray;

    use overload '@{}' => sub { return [ x => 42 ] };
    use overload '%{}' => sub { return { x => 42 } };
}

my $HASH_ARRAY_OBJECT = bless {}, 'HashArray';

my %tests = (
    accept => [$HASH_ARRAY_OBJECT],
    reject => [
        $ZERO,
        $ONE,
        $INT,
        $NEG_INT,
        $NUM_OVERLOAD_ZERO,
        $NUM_OVERLOAD_ONE,
        $NUM_OVERLOAD_NEG,
        qw(
            1e20
            1e100
            -1e10
            -1e+10
            1E20
        ),
        $ARRAY_REF,
        $ARRAY_OVERLOAD,
        $BOOL_OVERLOAD_TRUE,
        $BOOL_OVERLOAD_FALSE,
        $NUM,
        $NEG_NUM,
        $NUM_OVERLOAD_NEG_DECIMAL,
        $NUM_OVERLOAD_DECIMAL,
        $EMPTY_STRING,
        $STRING,
        $NUM_IN_STRING,
        $STR_OVERLOAD_EMPTY,
        $STR_OVERLOAD_FULL,
        $INT_WITH_NL1,
        $INT_WITH_NL2,
        $SCALAR_REF,
        $SCALAR_REF_REF,
        $SCALAR_OVERLOAD,
        $HASH_REF,
        $HASH_OVERLOAD,
        $CODE_REF,
        $CODE_OVERLOAD,
        $GLOB,
        $GLOB_REF,
        $GLOB_OVERLOAD,
        $GLOB_OVERLOAD_FH,
        $FH,
        $FH_OBJECT,
        $REGEX,
        $REGEX_OBJ,
        $REGEX_OVERLOAD,
        $FAKE_REGEX,
        $OBJECT,
        $UNDEF,
        qw(
            1e-10
            -1e-10
            1.23456e10
            1.23456e-10
            -1.23456e10
            -1.23456e-10
            -1.23456e+10
        ),
    ],
);

subtest(
    'unnamed intersection made of two builtins',
    sub {
        my $unnamed_intersection = Specio::Constraint::Intersection->new(
            of          => [ t('HashRef'), t('ArrayRef') ],
            declared_at => Specio::DeclaredAt->new_from_caller(0),
        );

        ok(
            $unnamed_intersection->_has_inline_generator,
            'intersection of two types with inline generator has a generator'
        );
        is(
            $unnamed_intersection->name,
            'HashRef & ArrayRef',
            'name is generated from constituent types'
        );
        ok(
            !$unnamed_intersection->is_anon,
            'unnamed intersection is not anonymous because name is generated'
        );
        is(
            $unnamed_intersection->parent,
            undef,
            'parent method returns undef'
        );
        ok(
            !$unnamed_intersection->_has_parent,
            'intersection has no parent'
        );

        test_constraint( $unnamed_intersection, \%tests );
    }
);

subtest(
    'explicitly named intersection made of two builtins',
    sub {
        my $named_intersection = intersection(
            'MyIntersection',
            of => [ t('HashRef'), t('ArrayRef') ],
        );
        is(
            $named_intersection->name,
            'MyIntersection',
            'name passed to intersection() is used'
        );

        test_constraint( $named_intersection, \%tests );
    }
);

subtest(
    'intersection made of two types without inline generators',
    sub {
        my $my_hashref = anon(
            parent     => t('Ref'),
            constraint => sub {
                return (
                    ref( $_[0] ) eq 'HASH' || ( Scalar::Util::blessed( $_[0] )
                        && overload::Overloaded( $_[0] )
                        && defined overload::Method( $_[0], '%{}' ) )
                );
            },
        );

        my $my_arrayref = anon(
            parent     => t('Ref'),
            constraint => sub {
                return (
                    ref( $_[0] ) eq 'ARRAY'
                        || ( Scalar::Util::blessed( $_[0] )
                        && overload::Overloaded( $_[0] )
                        && defined overload::Method( $_[0], '@{}' ) )
                );
            },
        );

        my $no_inline_intersection = intersection(
            of => [ $my_hashref, $my_arrayref ],
        );
        is(
            $no_inline_intersection->name,
            undef,
            'no name if intersection includes anonymous types',
        );
        ok(
            $no_inline_intersection->is_anon,
            'intersection is anonymous if any of its constituents are anonymous'
        );

        test_constraint( $no_inline_intersection, \%tests );
    }
);

subtest(
    'intersection made of builtin and type without inline generator',
    sub {
        my $my_hashref = anon(
            parent     => t('Ref'),
            constraint => sub {
                return (
                    ref( $_[0] ) eq 'HASH' || ( Scalar::Util::blessed( $_[0] )
                        && overload::Overloaded( $_[0] )
                        && defined overload::Method( $_[0], '%{}' ) )
                );
            },
        );

        my $mixed_inline_intersection = intersection(
            of => [ $my_hashref, t('ArrayRef') ],
        );
        is(
            $mixed_inline_intersection->name,
            undef,
            'no name if intersection includes anonymous types',
        );
        ok(
            $mixed_inline_intersection->is_anon,
            'intersection is anonymous if any of its constituents are anonymous'
        );

        test_constraint( $mixed_inline_intersection, \%tests );
    }
);

done_testing();