~ubuntu-branches/ubuntu/wily/libtest-mockmodule-perl/wily

« back to all changes in this revision

Viewing changes to t/mockmodule.t

  • Committer: Package Import Robot
  • Author(s): gregor herrmann, Salvatore Bonaccorso, Ansgar Burchardt, Axel Beckert, gregor herrmann
  • Date: 2015-06-21 03:26:33 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20150621032633-4920jlh05etcl4k0
Tags: 0.10-1
* Team upload.

[ Salvatore Bonaccorso ]
* debian/control: Changed: Replace versioned (build-)dependency on
  perl (>= 5.6.0-{12,16}) with an unversioned dependency on perl (as
  permitted by Debian Policy 3.8.3).

[ Ansgar Burchardt ]
* Email change: Ansgar Burchardt -> ansgar@debian.org
* debian/control: Convert Vcs-* fields to Git.

[ Salvatore Bonaccorso ]
* Change Vcs-Git to canonical URI (git://anonscm.debian.org)
* Change search.cpan.org based URIs to metacpan.org based URIs

[ Axel Beckert ]
* debian/copyright: migrate pre-1.0 format to 1.0 using "cme fix dpkg-
  copyright"

[ gregor herrmann ]
* Strip trailing slash from metacpan URLs.

[ Salvatore Bonaccorso ]
* Update Vcs-Browser URL to cgit web frontend

[ gregor herrmann ]
* New upstream release 0.10.
  - Fixes "unmock() on an inherited method does not behave properly"
    (Closes: #674625)
  - Fixes "FTBFS with perl 5.22 (CGI build-dep)"
    (Closes: #789437)
* Add debian/upstream/metadata.
* debian/copyright: new upstream maintainer/copyright holder/license.
* Add build dependency on libmodule-build-perl.
* Add (build) dependency on libsuper-perl.
* Mark package as autopkgtest-able.
* Switch to source format "3.0 (quilt)".
* Bump debhelper compatibility level to 9.
* Declare compliance with Debian Policy 3.9.6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/perl -w
2
2
use strict;
3
 
use Test::More tests => 41;
4
 
 
5
 
require_ok('Test::MockModule');
 
3
use Test::More;
 
4
 
 
5
use lib "t/lib";
 
6
 
 
7
BEGIN {
 
8
        use_ok('Test::MockModule') or BAIL_OUT "Could not load Test::MockModule. Giving up";
 
9
}
6
10
 
7
11
package Test_Package;
8
12
our $VERSION=1;
9
13
sub listify {
10
 
    my ($lower, $upper) = @_;
11
 
    return ($lower .. $upper);
 
14
        my ($lower, $upper) = @_;
 
15
        return ($lower .. $upper);
12
16
}
13
17
package main;
14
18
 
22
26
like($@, qr/Invalid package name/, ' ... croaks if package is undefined');
23
27
 
24
28
{
25
 
    {
26
 
        Test::MockModule->new('CGI', no_auto => 1);
27
 
        ok(!$INC{'CGI.pm'}, '... no_auto prevents module being loaded');
28
 
    }
29
 
 
30
 
    my $mcgi = Test::MockModule->new('CGI');
31
 
    ok($INC{'CGI.pm'}, '... module loaded if !$VERSION');
32
 
    ok($mcgi->isa('Test::MockModule'), '... returns a Test::MockModule object');
33
 
    my $mcgi2 = Test::MockModule->new('CGI');
34
 
    is($mcgi, $mcgi2,
35
 
       "... returns existing object if there's already one for the package");
36
 
 
37
 
    # get_package()
38
 
    ok($mcgi->can('get_package'), 'get_package');
39
 
    is($mcgi->get_package, 'CGI', '... returns the package name');
40
 
 
41
 
    # mock()
42
 
    # prime CGI routines
43
 
    CGI->Vars; CGI->param;
44
 
 
45
 
    ok($mcgi->can('mock'), 'mock()');
46
 
    eval {$mcgi->mock(q[p-ram])};
47
 
 
48
 
    like($@, qr/Invalid subroutine name: /,
49
 
        '... dies if a subroutine name is invalid');
50
 
 
51
 
    my $orig_param = \&CGI::param;
52
 
    $mcgi->mock('param', sub {return qw(abc def)});
53
 
    my @params = CGI::param();
54
 
    is_deeply(\@params, ['abc', 'def'],
55
 
        '... replaces the subroutine with a mocked sub');
56
 
 
57
 
    $mcgi->mock('param' => undef);
58
 
    @params = CGI::param();
59
 
    is_deeply(\@params, [], '... which is an empty sub if !defined');
60
 
 
61
 
    $mcgi->mock(param => 'The quick brown fox jumped over the lazy dog');
62
 
    my $a2z = CGI::param();
63
 
    is($a2z, 'The quick brown fox jumped over the lazy dog',
64
 
       '... or a subroutine returning the supplied value');
65
 
 
66
 
    my $ref = [1,2,3];
67
 
    $mcgi->mock(param => $ref);
68
 
    @params = CGI::param();
69
 
    is($params[0], $ref,
70
 
       '... given a reference, install a sub that returns said reference');
71
 
 
72
 
    my $blessed_code = bless sub { return 'Hello World' }, 'FOO';
73
 
    $mcgi->mock(param => $blessed_code);
74
 
    @params = CGI::param();
75
 
    is($params[0], 'Hello World', '... a blessed coderef is properly detected');
76
 
 
77
 
    $mcgi->mock(Just => 'another', Perl => 'Hacker');
78
 
    @params = (CGI::Just(), CGI::Perl());
79
 
    is_deeply(\@params, ['another', 'Hacker'],
80
 
              '... can mock multiple subroutines at a time');
81
 
 
82
 
 
83
 
    # original()
84
 
    ok($mcgi->can('original'), 'original()');
85
 
    is($mcgi->original('param'), $orig_param,
86
 
       '... returns the original subroutine');
87
 
    my ($warn);
88
 
    local $SIG{__WARN__} = sub {$warn = shift};
89
 
    $mcgi->original('Vars');
90
 
    like($warn, qr/ is not mocked/, "... warns if a subroutine isn't mocked");
91
 
 
92
 
    # unmock()
93
 
    ok($mcgi->can('unmock'), 'unmock()');
94
 
    eval {$mcgi->unmock('V@rs')};
95
 
    like($@, qr/Invalid subroutine name/,
96
 
         '... dies if the subroutine is invalid');
97
 
 
98
 
    $warn = '';
99
 
    $mcgi->unmock('Vars');
100
 
    like($warn, qr/ was not mocked/, "... warns if a subroutine isn't mocked");
101
 
 
102
 
    $mcgi->unmock('param');
103
 
    is(\&{"CGI::param"}, $orig_param, '... restores the original subroutine');
104
 
 
105
 
    # unmock_all()
106
 
    ok($mcgi->can('unmock_all'), 'unmock_all');
107
 
    $mcgi->mock('Vars' => sub {1}, param => sub {2});
108
 
    ok(CGI::Vars() == 1 && CGI::param() == 2,
109
 
       'mock: can mock multiple subroutines');
110
 
    my @orig = ($mcgi->original('Vars'), $mcgi->original('param'));
111
 
    $mcgi->unmock_all();
112
 
    ok(\&CGI::Vars eq $orig[0] && \&CGI::param eq $orig[1],
113
 
       '... removes all mocked subroutines');
114
 
 
115
 
    # is_mocked()
116
 
    ok($mcgi->can('is_mocked'), 'is_mocked');
117
 
    ok(!$mcgi->is_mocked('param'), '... returns false for non-mocked sub');
118
 
    $mcgi->mock('param', sub { return 'This sub is mocked' });
119
 
    is(CGI::param(), 'This sub is mocked', '... mocked params');
120
 
    ok($mcgi->is_mocked('param'), '... returns true for non-mocked sub');
 
29
        {
 
30
                Test::MockModule->new('ExampleModule', no_auto => 1);
 
31
                ok(!$INC{'ExampleModule.pm'}, '... no_auto prevents module being loaded');
 
32
        }
 
33
 
 
34
        my $mcgi = Test::MockModule->new('ExampleModule');
 
35
        ok($INC{'ExampleModule.pm'}, '... module loaded if !$VERSION');
 
36
        ok($mcgi->isa('Test::MockModule'), '... returns a Test::MockModule object');
 
37
        my $mcgi2 = Test::MockModule->new('ExampleModule');
 
38
        is($mcgi, $mcgi2,
 
39
                "... returns existing object if there's already one for the package");
 
40
 
 
41
        # get_package()
 
42
        ok($mcgi->can('get_package'), 'get_package');
 
43
        is($mcgi->get_package, 'ExampleModule', '... returns the package name');
 
44
 
 
45
        # mock()
 
46
 
 
47
        ok($mcgi->can('mock'), 'mock()');
 
48
        eval {$mcgi->mock(q[p-ram])};
 
49
 
 
50
        like($@, qr/Invalid subroutine name: /,
 
51
                '... dies if a subroutine name is invalid');
 
52
 
 
53
        my $orig_param = \&ExampleModule::param;
 
54
        $mcgi->mock('param', sub {return qw(abc def)});
 
55
        my @params = ExampleModule::param();
 
56
        is_deeply(\@params, ['abc', 'def'],
 
57
                '... replaces the subroutine with a mocked sub');
 
58
 
 
59
        $mcgi->mock('param' => undef);
 
60
        @params = ExampleModule::param();
 
61
        is_deeply(\@params, [], '... which is an empty sub if !defined');
 
62
 
 
63
        $mcgi->mock(param => 'The quick brown fox jumped over the lazy dog');
 
64
        my $a2z = ExampleModule::param();
 
65
        is($a2z, 'The quick brown fox jumped over the lazy dog',
 
66
                '... or a subroutine returning the supplied value');
 
67
 
 
68
        my $ref = [1,2,3];
 
69
        $mcgi->mock(param => $ref);
 
70
        @params = ExampleModule::param();
 
71
        is($params[0], $ref,
 
72
                '... given a reference, install a sub that returns said reference');
 
73
 
 
74
        my $blessed_code = bless sub { return 'Hello World' }, 'FOO';
 
75
        $mcgi->mock(param => $blessed_code);
 
76
        @params = ExampleModule::param();
 
77
        is($params[0], 'Hello World', '... a blessed coderef is properly detected');
 
78
 
 
79
        $mcgi->mock(Just => 'another', Perl => 'Hacker');
 
80
        @params = (ExampleModule::Just(), ExampleModule::Perl());
 
81
        is_deeply(\@params, ['another', 'Hacker'],
 
82
                '... can mock multiple subroutines at a time');
 
83
 
 
84
 
 
85
        # original()
 
86
        ok($mcgi->can('original'), 'original()');
 
87
        is($mcgi->original('param'), $orig_param,
 
88
                '... returns the original subroutine');
 
89
        my ($warn);
 
90
        local $SIG{__WARN__} = sub {$warn = shift};
 
91
        $mcgi->original('Vars');
 
92
        like($warn, qr/ is not mocked/, "... warns if a subroutine isn't mocked");
 
93
 
 
94
        # unmock()
 
95
        ok($mcgi->can('unmock'), 'unmock()');
 
96
        eval {$mcgi->unmock('V@rs')};
 
97
        like($@, qr/Invalid subroutine name/,
 
98
                '... dies if the subroutine is invalid');
 
99
 
 
100
        $warn = '';
 
101
        $mcgi->unmock('Vars');
 
102
        like($warn, qr/ was not mocked/, "... warns if a subroutine isn't mocked");
 
103
 
 
104
        $mcgi->unmock('param');
 
105
        is(\&{"ExampleModule::param"}, $orig_param, '... restores the original subroutine');
 
106
 
 
107
        # unmock_all()
 
108
        ok($mcgi->can('unmock_all'), 'unmock_all');
 
109
        $mcgi->mock('Vars' => sub {1}, param => sub {2});
 
110
        ok(ExampleModule::Vars() == 1 && ExampleModule::param() == 2,
 
111
                'mock: can mock multiple subroutines');
 
112
        my @orig = ($mcgi->original('Vars'), $mcgi->original('param'));
 
113
        $mcgi->unmock_all();
 
114
        ok(\&ExampleModule::Vars eq $orig[0] && \&ExampleModule::param eq $orig[1],
 
115
                '... removes all mocked subroutines');
 
116
 
 
117
        # is_mocked()
 
118
        ok($mcgi->can('is_mocked'), 'is_mocked');
 
119
        ok(!$mcgi->is_mocked('param'), '... returns false for non-mocked sub');
 
120
        $mcgi->mock('param', sub { return 'This sub is mocked' });
 
121
        is(ExampleModule::param(), 'This sub is mocked', '... mocked params');
 
122
        ok($mcgi->is_mocked('param'), '... returns true for non-mocked sub');
121
123
}
122
124
 
123
 
isnt(CGI::param(), 'This sub is mocked',
124
 
     '... params is unmocked when object goes out of scope');
 
125
isnt(ExampleModule::param(), 'This sub is mocked',
 
126
        '... params is unmocked when object goes out of scope');
125
127
 
126
128
# test inherited methods
127
129
package Test_Parent;
142
144
$test_mock->mock(ISA => sub {'basic test'});
143
145
can_ok(Test_Child => 'ISA');
144
146
is(Test_Child::ISA(), 'basic test',
145
 
   "testing a mocked sub that didn't exist before");
 
147
        "testing a mocked sub that didn't exist before");
146
148
$test_mock->unmock('ISA');
147
149
ok(!Test_Child->can('ISA') && $Test_Child::ISA[0] eq 'Test_Parent',
148
 
   "restoring an undefined sub doesn't clear out the rest of the symbols");
 
150
        "restoring an undefined sub doesn't clear out the rest of the symbols");
149
151
 
 
152
done_testing;