-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonkeyPatch.t
executable file
·85 lines (50 loc) · 2.97 KB
/
MonkeyPatch.t
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
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use MonkeyPatch;
package main;
%VAR::HASH_VALUES = (KEY1 => 'V1');
sub getGlobal {
return %VAR::HASH_VALUES;
}
subtest 'monkey patch by name' => sub {
is ( $VAR::HASH_VALUES{KEY1}, 'V1', 'Initial setup' );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY1}, 'V1', 'getGlobal should be working');
is ( { MonkeyPatch::runOriginal( 'main::getGlobal', "HASH_VALUES" ) }->{KEY1}, 'V1',
'Run Original possible before patch');
MonkeyPatch::patch( 'main::getGlobal', sub {return (KEY1 => 'hi');} );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY1}, 'hi', 'Successful MonkeyPatch');
MonkeyPatch::patch( 'main::getGlobal', sub {return (KEY1 => 'hello');} );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY1}, 'hello', 'Successful Second MonkeyPatch');
is ( { MonkeyPatch::runOriginal( 'main::getGlobal', "HASH_VALUES" ) }->{KEY1}, 'V1', 'Run Original');
is ( { getGlobal( "HASH_VALUES" ) }->{KEY1}, 'hello', 'Run Original does not affect MonkeyPatch');
MonkeyPatch::unpatch( 'main::getGlobal' );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY1}, 'V1', 'Gets original result after unpatch');
MonkeyPatch::patch( 'main::getGlobal', sub {return (KEY1 => 'hi');} );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY1}, 'hi', 'Successful MonkeyPatch');
MonkeyPatch::unpatchAll();
is ( { getGlobal( "HASH_VALUES" ) }->{KEY1}, 'V1', 'Gets original result after unpatchAll');
done_testing();
};
subtest 'monkey patch by code reference' => sub {
%VAR::HASH_VALUES = (KEY2 => 'V2');
is ( $VAR::HASH_VALUES{KEY2}, 'V2', 'Initial setup' );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY2}, 'V2', 'getGlobal should be working');
is ( { MonkeyPatch::runOriginal( \&main::getGlobal, "HASH_VALUES" ) }->{KEY2}, 'V2',
'Run Original possible before patch');
MonkeyPatch::patch( \&main::getGlobal, sub {return (KEY2 => 'hi');} );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY2}, 'hi', 'Successful MonkeyPatch');
MonkeyPatch::patch( \&main::getGlobal, sub {return (KEY2 => 'hello');} );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY2}, 'hello', 'Successful Second MonkeyPatch');
is ( { MonkeyPatch::runOriginal( \&main::getGlobal, "HASH_VALUES" ) }->{KEY2}, 'V2', 'Run Original');
is ( { getGlobal( "HASH_VALUES" ) }->{KEY2}, 'hello', 'Run Original does not affect MonkeyPatch');
MonkeyPatch::unpatch( \&main::getGlobal );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY2}, 'V2', 'Gets original result after unpatch');
MonkeyPatch::patch( \&main::getGlobal, sub {return (KEY2 => 'hi');} );
is ( { getGlobal( "HASH_VALUES" ) }->{KEY2}, 'hi', 'Successful MonkeyPatch');
MonkeyPatch::unpatchAll();
is ( { getGlobal( "HASH_VALUES" ) }->{KEY2}, 'V2', 'Gets original result after unpatchAll');
done_testing();
};
done_testing();