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
|
#!/usr/bin/perl -w
use strict;
use warnings;
use FindBin;
use lib map { "$FindBin::Bin/$_" } qw{ ./lib ../lib };
use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use File::Temp ();
use File::Path ();
use File::Slurper qw{ write_text };
use Test::TMF qw ( tmf_test_code );
my $test_code;
$test_code = <<'EOS';
use Test::MockFile ( plugin => q[Unknown] );
EOS
tmf_test_code(
name => q[Cannot find a Test::MockFile plugin for Unknown],
exit => 512,
test => sub {
my ($out) = @_;
#note explain $out;
like $out->{output}, qr{Cannot find a Test::MockFile plugin for Unknown}, 'Cannot find a Test::MockFile plugin for Unknown';
return;
},
test_code => $test_code,
debug => 0,
);
# ------------------------------------------------------------------------------------
my $tmp = File::Temp->newdir();
my $base_dir = "$tmp/Test/MockFile/Plugin";
ok File::Path::make_path($base_dir), "create Test/MockFile/Plugin dir for testing";
my $MyPlugin_filename = "$base_dir/MyPlugin.pm";
File::Slurper::write_text( $MyPlugin_filename, <<"EOS" );
package Test::MockFile::Plugin::MyPlugin;
use base 'Test::MockFile::Plugin';
sub register {
print qq[MyPlugin is now registered!\n];
}
1
EOS
$test_code = <<'EOS';
use Test::MockFile ( plugin => q[MyPlugin] );
ok 1;
EOS
tmf_test_code(
name => q[Loading a plugin from default namespace],
perl_args => ["-I$tmp"],
exit => 0,
test => sub {
my ($out) = @_;
like $out->{output}, qr{MyPlugin is now registered}, 'load and register plugin';
return;
},
test_code => $test_code,
debug => 0,
);
$test_code = <<'EOS';
use Test::MockFile ( plugin => [ 'MyPlugin' ] );
ok 1;
EOS
tmf_test_code(
name => q[use Test::MockFile ( plugin => [ 'MyPlugin' ] )],
perl_args => ["-I$tmp"],
exit => 0,
test => sub {
my ($out) = @_;
like $out->{output}, qr{MyPlugin is now registered}, 'load and register plugin';
return;
},
test_code => $test_code,
debug => 0,
);
# ------------------------------------------------------------------------------------
note "Testing a custom namespace";
$base_dir = "$tmp/CustomPluginNamespace";
ok File::Path::make_path($base_dir), "create Test/MockFile/Plugin dir for testing";
my $AnotherPlugin_filename = "$base_dir/Another.pm";
File::Slurper::write_text( $AnotherPlugin_filename, <<"EOS" );
package CustomPluginNamespace::Another;
use base 'Test::MockFile::Plugin';
sub register {
print qq[AnotherPlugin from a Custom namespace is now registered!\n];
}
1
EOS
$test_code = <<'EOS';
BEGIN {
require Test::MockFile::Plugins;
push @Test::MockFile::Plugins::NAMESPACES, 'CustomPluginNamespace';
}
use Test::MockFile ( plugin => q[Another] );
ok 1;
EOS
tmf_test_code(
name => q[Loading a plugin from default namespace],
perl_args => ["-I$tmp"],
exit => 0,
test => sub {
my ($out) = @_;
#note explain $out;
like $out->{output}, qr{AnotherPlugin from a Custom namespace is now registered!}, 'load and register plugin from a custom namespace';
return;
},
test_code => $test_code,
debug => 0,
);
done_testing;
|