File: 04_option_parsing.t

package info (click to toggle)
libcpan-reporter-perl 1.2020-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,752 kB
  • sloc: perl: 5,440; makefile: 2
file content (145 lines) | stat: -rw-r--r-- 3,465 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
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
#!perl
use strict;
BEGIN{ if (not $] < 5.006) { require warnings; warnings->import } }

select(STDERR); $|=1;
select(STDOUT); $|=1;

use Test::More;
use lib 't/lib';
use Frontend;
use Helper;
use Capture::Tiny qw/capture/;

my @good_cases = (
    {
        label   => "empty input",
        option  => "edit_report",
        input   => "",
        output  => {
            default => "no",
        }
    },
    {
        label   => "action (by itself)",
        option  => "edit_report",
        input   => "yes",
        output  => {
            default => "yes",
        },
    },
    {
        label   => "grade (by itself)",
        option  => "edit_report",
        input   => "fail",
        output  => {
            "fail"  => "yes",
        },
    },
    {
        label   => "default:action",
        option  => "edit_report",
        input   => "default:no",
        output  => {
            default => "no",
        },
    },
    {
        label   => "grade:action",
        option  => "edit_report",
        input   => "fail:yes",
        output  => {
            "fail"  => "yes",
        },
    },
    {
        label   => "grade:action action",
        option  => "edit_report",
        input   => "fail:yes no",
        output  => {
            "fail"  => "yes",
            default => "no",
        },
    },
    {
        label   => "grade:action action grade:action",
        option  => "edit_report",
        input   => "fail:yes no fail:no",
        output  => {
            "fail"  => "no",
            default => "no",
        },
    },
    {
        label   => "grade:action action grade2:action",
        option  => "edit_report",
        input   => "fail:yes no na:no",
        output  => {
            "fail"  => "yes",
            "na"    => "no",
            default => "no",
        },
    },
    {
        label   => "grade/grade2:action",
        option  => "edit_report",
        input   => "fail/na:ask/yes",
        output  => {
            "fail"  => "ask/yes",
            "na"    => "ask/yes",
        },
    },
    {
        label   => "grade/grade2",
        option  => "edit_report",
        input   => "fail/na",
        output  => {
            "fail"  => "yes",
            "na"    => "yes",
        },
    },
);
        
my @bad_cases = (
    {
        label   => "bad grade",
        option  => "edit_report",
        input   => "failed",
        output  => undef,
        msg     => 
            "/ignoring invalid grade:action 'failed' for 'edit_report'/",
    },
    {
        label   => "bad action",
        option  => "edit_report",
        input   => "fail:run-away",
        output  => undef,
        msg     => 
            "/ignoring invalid action 'run-away' in 'fail:run-away' for 'edit_report'/",
    },
);

plan tests => 1 + 2 * ( @good_cases + @bad_cases ); 

#--------------------------------------------------------------------------#
# Begin tests
#--------------------------------------------------------------------------#

require_ok( "CPAN::Reporter::Config" );

for my $case ( @good_cases, @bad_cases ) {
    my $got;
    my ($stdout, $stderr) = capture {
        $got = CPAN::Reporter::Config::_validate_grade_action_pair(
            $case->{option}, $case->{input}
        );
    };
    is_deeply( $got, $case->{output}, $case->{label} );
    if ( $case->{msg} ) {
        like( $stdout, $case->{msg}, $case->{label} );
    }
    else {
        is( $stdout, '', "No warnings seen" );
    }
}