File: Magic.t

package info (click to toggle)
libexporter-declare-perl 0.114-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 228 kB
  • sloc: perl: 1,607; makefile: 4
file content (213 lines) | stat: -r--r--r-- 5,226 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl
use strict;
use warnings;

use Fennec::Lite;

BEGIN {
    my $skip = qq{Exporter::Declare::Magic 0.107 and Devel::Declare::Parser are required for -magic};
    Test::More->import( skip_all => $skip )
        unless eval <<"        EOT";
            require Devel\::Declare\::Parser;
            require Exporter\::Declare\::Magic;
            \$Exporter::Declare::Magic::VERSION >= 0.107;
        EOT
}

our $CLASS;

BEGIN {
    $CLASS = 'Exporter::Declare::Magic';
    require_ok $CLASS;
}

{

    package Export::Stuff;
    use Exporter::Declare qw/-magic/;
    use Fennec::Lite;

    BEGIN {
        can_ok( __PACKAGE__, qw/
                exports
                default_exports
                import_options
                import_arguments
                export_tag
                export
                gen_export
                default_export
                gen_default_export
                parsed_exports
                parsed_default_exports
                parser
                /
        );
    }

    sub a    { 'a' }
    sub b    { 'b' }
    sub c    { 'c' }
    sub meth { return @_ }

    our $X = 'x';
    our $Y = 'y';
    our $Z = 'z';

    exports qw/ $Y b /;
    default_exports qw/ $X a /;
    import_options qw/xxx yyy/;
    import_arguments qw/ foo bar /;
    export_tag vars => qw/ $X $Y /;
    export_tag subs => qw/ a b /;

    export $Z;
    export c;
    export baz { 'baz' } export eexport export { return @_ }

    my $gen = 0;
    gen_export gexp {
        my $out = $gen++;
        sub { $out }
    }
    gen_default_export defgen {
        my $out = $gen++;
        sub { $out }
    }
}

tests magic_tag => sub {
    # This tests that the magic tag brings in the magic methods as well as the
    # default which is a nested tag.
    can_ok(
        'Export::Stuff', qw/
            export gen_export default_export gen_default_export parser
            parsed_exports parsed_default_exports import exports default_exports
            import_options import_arguments export_tag
            /
    );
};

tests generator => sub {
    Export::Stuff->import(qw/gexp/);
    is( gexp(), 0, "Generated first" );
    Export::Stuff->import(qw/defgen/);
    is( defgen(), 1, "Generated second" );
    Export::Stuff->import( defgen => {-as => 'blah'} );
    is( blah(), 2, "Generated again" );
};

tests tags_options_and_exports => sub {
    is_deeply(
        [sort keys %{Export::Stuff->export_meta->exports}],
        [sort qw/ $Y &b $X &a &Stuff $Z &c &baz &gexp &defgen &eexport /],
        "All exports accounted for"
    );
    is_deeply(
        [sort @{Export::Stuff->export_meta->export_tags->{default}}],
        [sort qw/ $X a defgen /],
        "Default Exports"
    );
    is_deeply(
        [sort @{Export::Stuff->export_meta->export_tags->{all}}],
        [sort qw/ $Y &b $X &a &Stuff $Z &c &baz &gexp &defgen &eexport /],
        "All Exports"
    );
    is_deeply(
        Export::Stuff->export_meta->options,
        {
            xxx => 1,
            yyy => 1,
        },
        "Options"
    );
    is_deeply(
        Export::Stuff->export_meta->arguments,
        {
            foo    => 1,
            bar    => 1,
            prefix => 1,
            suffix => 1,
        },
        "Arguments"
    );

    is_deeply(
        Export::Stuff->export_meta->export_tags,
        {
            alias => ['Stuff'],
            vars  => [qw/ $X $Y /],
            subs  => [qw/ a b /],
            # These are checked elsware
            default => Export::Stuff->export_meta->export_tags->{'default'},
            all     => Export::Stuff->export_meta->export_tags->{all}
        },
        "Extra tags"
    );
};

tests magic_import => sub {

    package MyMagic;
    use strict;
    use warnings;
    use Exporter::Declare qw/-magic -all/;
    use Fennec::Lite;

    sub xxx { 'xxx' }

    can_ok( __PACKAGE__, qw/
            exports
            default_exports
            import_options
            import_arguments
            export_tag
            export
            gen_export
            default_export
            gen_default_export
            parsed_exports
            parsed_default_exports
            parser
            /
    );

    lives_ok { export a b {} } "export magic";
    lives_ok {
        export b => sub { };
        export c => \&xxx;
        export 'xxx';
    }
    "export magic non-interfering";

    is( __PACKAGE__->export_meta->exports_get('xxx'), \&xxx, "export added" );
};

tests magic_import_args => sub {

    package MyMagic2;
    use strict;
    use warnings;
    use Exporter::Declare -magic => ['-default', -prefix => "magic_", '!export'];
    use Fennec::Lite;

    can_ok( __PACKAGE__, qw/
            exports
            default_exports
            import_options
            import_arguments
            export_tag
            magic_gen_export
            magic_default_export
            magic_gen_default_export
            magic_parsed_exports
            magic_parsed_default_exports
            magic_parser
            /
    );
    ok( !__PACKAGE__->can('export'),       "export() was excluded" );
    ok( !__PACKAGE__->can('magic_export'), "magic_export() was excluded" );
};

run_tests;
done_testing;