File: Plugins.pm

package info (click to toggle)
libmason-perl 2.24-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 748 kB
  • sloc: perl: 4,882; makefile: 7
file content (204 lines) | stat: -rw-r--r-- 6,039 bytes parent folder | download | duplicates (3)
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
package Mason::t::Plugins;    ## no critic (Moose::RequireMakeImmutable)
$Mason::t::Plugins::VERSION = '2.24';
use Test::Class::Most parent => 'Mason::Test::Class';
use Capture::Tiny qw(capture_merged);
use Mason::Util qw(dump_one_line);

sub test_notify_plugin : Tests {
    my $self = shift;

    $self->setup_interp(
        plugins                => ['+Mason::Test::Plugins::Notify'],
        no_source_line_numbers => 1,
    );
    $self->add_comp( path => '/test_plugin_support.mi', src => 'hi' );
    my $output = capture_merged {
        $self->test_comp(
            path   => '/test_plugin.mc',
            src    => '<& test_plugin_support.mi &>',
            expect => 'hi'
        );
    };

    my $like = sub { my $regex = shift; like( $output, $regex, $regex ) };
    $like->(qr/starting interp run/);
    $like->(qr/starting request run - \/test_plugin/);
    $like->(qr/starting request comp - test_plugin_support.mi/);
    $like->(qr/starting compilation parse - \/test_plugin.mc/);
}

# Call Mason::Test::RootClass->new, then make base classes like
# Mason::Test::RootClass::Interp are used automatically
#
sub test_notify_root_class : Tests {
    my $self = shift;
    my $mrc  = 'Mason::Test::RootClass';
    $self->setup_interp( mason_root_class => $mrc );
    is( $self->interp->mason_root_class,       $mrc,                  "mason_root_class" );
    is( $self->interp->base_compilation_class, "${mrc}::Compilation", "base_compilation_class" );
    is( $self->interp->base_component_class,   "${mrc}::Component",   "base_component_class" );
    is( $self->interp->base_request_class,     "${mrc}::Request",     "base_request_class" );
    is( $self->interp->base_result_class,      "Mason::Result",       "base_result_class" );
    isa_ok( $self->interp, "${mrc}::Interp", "base_interp_class" );

    $self->add_comp( path => '/test_plugin_support.mi', src => 'hi' );
    my $output = capture_merged {
        $self->test_comp(
            path   => '/test_plugin.mc',
            src    => '<& test_plugin_support.mi &>',
            expect => 'hi'
        );
    };

    my $like = sub { my $regex = shift; like( $output, $regex, $regex ) };
    $like->(qr/starting interp run/);
    $like->(qr/starting request run - \/test_plugin/);
    $like->(qr/starting request comp - test_plugin_support.mi/);
    $like->(qr/starting compilation parse - \/test_plugin.mc/);
}

sub test_strict_plugin : Tests {
    my $self = shift;

    $self->setup_interp(
        base_component_moose_class => 'Mason::Test::Overrides::Component::StrictMoose', );
    $self->add_comp( path => '/test_strict_plugin.mc', src => 'hi' );
    lives_ok { $self->interp->run('/test_strict_plugin') };
    throws_ok { $self->interp->run( '/test_strict_plugin', foo => 5 ) } qr/Found unknown attribute/;
}

{
    package Mason::Test::Plugins::A;
$Mason::Test::Plugins::A::VERSION = '2.24';
use Moose;
    with 'Mason::Plugin';
}
{
    package Mason::Plugin::B;
$Mason::Plugin::B::VERSION = '2.24';
use Moose;
    with 'Mason::Plugin';
}
{
    package Mason::Plugin::C;
$Mason::Plugin::C::VERSION = '2.24';

    use Moose;
    with 'Mason::Plugin';
}
{
    package Mason::Plugin::D;
$Mason::Plugin::D::VERSION = '2.24';

    use Moose;
    with 'Mason::Plugin';
}
{
    package Mason::Plugin::E;
$Mason::Plugin::E::VERSION = '2.24';

    use Moose;
    with 'Mason::Plugin';
}
{
    package Mason::PluginBundle::F;
$Mason::PluginBundle::F::VERSION = '2.24';

    use Moose;
    with 'Mason::PluginBundle';
    sub requires_plugins { return qw(C D) }
}
{
    package Mason::Test::PluginBundle::G;
$Mason::Test::PluginBundle::G::VERSION = '2.24';

    use Moose;
    with 'Mason::PluginBundle';
    sub requires_plugins { return qw(C E) }
}
{
    package Mason::Plugin::H;
$Mason::Plugin::H::VERSION = '2.24';

    use Moose;
    with 'Mason::Plugin';
    sub requires_plugins { return qw(@F) }
}
{
    package Mason::PluginBundle::I;
$Mason::PluginBundle::I::VERSION = '2.24';

    use Moose;
    with 'Mason::PluginBundle';

    sub requires_plugins {
        return ( '+Mason::Test::Plugins::A', 'B', '@F', '+Mason::Test::PluginBundle::G', );
    }
}

{
    package Mason::PluginBundle::J;
$Mason::PluginBundle::J::VERSION = '2.24';

    use Moose;
    with 'Mason::PluginBundle';

    sub requires_plugins {
        return ('@I');
    }
}

sub test_plugin_specs : Tests {
    my $self = shift;

    require Mason::PluginBundle::Default;
    my @default_plugins = Mason::PluginBundle::Default->requires_plugins
      or die "no default plugins";
    my $test = sub {
        my ( $plugin_list, $expected_plugins ) = @_;
        my $interp = Mason->new( comp_root => $self->comp_root, plugins => $plugin_list );
        my $got_plugins =
          [ map { /Mason::Plugin::/ ? substr( $_, 15 ) : $_ } @{ $interp->plugins } ];
        cmp_deeply(
            $got_plugins,
            [ @$expected_plugins, @default_plugins ],
            dump_one_line($plugin_list)
        );
    };
    $test->( [], [] );
    $test->( ['E'], ['E'] );
    $test->( ['H'], [ 'H', 'C', 'D' ] );
    $test->( ['@F'], [ 'C', 'D' ] );
    $test->( ['@I'], [ 'Mason::Test::Plugins::A', 'B', 'C', 'D', 'E' ] );
    $test->( [ '-C', '@I', '-+Mason::Test::Plugins::A' ], [ 'B', 'D', 'E' ] );
    $test->( [ '-@I', '@J' ], [] );
    throws_ok { $test->( ['@X'] ) } qr/could not load 'Mason::PluginBundle::X'/;
    throws_ok { $test->( ['Y'] ) } qr/could not load 'Mason::Plugin::Y'/;
}

{
    package Mason::Test::Plugins::Upper;
$Mason::Test::Plugins::Upper::VERSION = '2.24';

    use Moose;
    with 'Mason::Plugin'
}
{
    package Mason::Test::Plugins::Upper::Request;
$Mason::Test::Plugins::Upper::Request::VERSION = '2.24';

    use Mason::PluginRole;
    after 'process_output' => sub {
        my ( $self, $bufref ) = @_;
        $$bufref = uc($$bufref);
    };
}

sub test_process_output_plugin : Tests {
    my $self = shift;

    $self->setup_interp( plugins => ['+Mason::Test::Plugins::Upper'] );
    $self->test_comp( src => 'Hello', expect => 'HELLO' );
}

1;