File: 02_basic-mock.t

package info (click to toggle)
liboverload-filecheck-perl 0.013-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,088 kB
  • sloc: perl: 2,030; ansic: 97; makefile: 8
file content (69 lines) | stat: -rw-r--r-- 1,476 bytes parent folder | download
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
#!/usr/bin/perl -w

# Copyright (c) 2018, cPanel, LLC.
# All rights reserved.
# http://cpanel.net
#
# This is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself. See L<perlartistic>.

use strict;
use warnings;

use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;

use Overload::FileCheck qw{:all};

my @calls;

{
    note "no mocks at this point";

    ok -e q[/tmp],           "/tmp/exits";
    ok !-e q[/do/not/exist], "/do/not/exist";

    is \@calls, [], 'no calls';
}

{
    note "we are mocking -e => CHECK_IS_TRUE";
    mock_file_check(
        '-e' => sub {
            my $f = shift;

            note "mocked -e called....";

            push @calls, $f;
            return CHECK_IS_TRUE;
        }
    );

    ok -e q[/tmp],          "/tmp exits";
    ok -e q[/do/not/exist], "/do/not/exist now exist thanks to mock=1";
    is \@calls, [qw{/tmp /do/not/exist}], 'got two calls calls';
}

{
    note "mocking a second time with CHECK_IS_FALSE";

    like(
        dies {
            mock_file_check( '-e' => sub { CHECK_IS_FALSE } )
        },
        qr/\Q-e is already mocked by Overload::FileCheck/,
        "die when mocking a second time"
    );

    unmock_file_check('-e');

    unmock_file_check(qw{-e -f});

    note "we are mocking -e => CHECK_IS_FALSE";
    mock_file_check( '-e' => sub { CHECK_IS_FALSE } );

    ok !-e q[/tmp], "/tmp does not exist now...";
}

done_testing;