summaryrefslogtreecommitdiffstats
path: root/t/error.t
blob: 56820523d32eae8bfad7e957a79754f7590bca3a (plain) (blame)
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
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use lib 't/lib';

use My::Parser;

{
    my $ret = eval 'foo';
    # not testing the value of $@ because it's just "whatever the parser
    # happens to do after getting into a confused state"
    ok($@);
    ok(!$ret);
    ok(!$My::Parser::got_code);
}
{
    my $ret = eval 'foo { }';
    ok(!$@);
    ok($ret);
    ok($My::Parser::got_code);
}
{
    my $ret = eval 'foo { $baz }';
    like($@, qr/^Global symbol "\$baz" requires explicit package name/);
    ok(!$ret);
    ok(!$My::Parser::got_code);
}

# wrapping a parsing function in an eval doesn't actually help, because parsing
# doesn't throw errors in the same way. errors are all saved up until parsing
# finishes, and then they are all reported at once if there were any.
{
    my $ret = eval 'bar';
    # not testing the value of $@ because it's just "whatever the parser
    # happens to do after getting into a confused state"
    ok($@);
    ok(!$ret);
    ok(!$My::Parser::got_code);
}
{
    my $ret = eval 'bar { }';
    ok(!$@);
    ok($ret);
    ok($My::Parser::got_code);
}
{
    my $ret = eval 'bar { $baz }';
    # the eval does, however, prevent perl from seeing what the message was
    like($@, qr/^Compilation error/);
    ok(!$ret);
    ok(!$My::Parser::got_code);
}

SKIP: {
    skip "Capture::Tiny is required here", 1
        unless eval { require Capture::Tiny };
    my ($out, $err, $exit) = Capture::Tiny::capture(sub {
        system($^X, (map { qq[-I$_] } @INC), 't/error.pl')
    });
    is($out, '');
    is(
        $err,
        <<'ERR'
Global symbol "$baz" requires explicit package name at t/error.pl line 9.
Execution of t/error.pl aborted due to compilation errors.
ERR
    );
    isnt($exit, 0);
}

done_testing;