lib/Lingy/Test.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« script/_lingy.pl ↑ Summary lib/Lingy/Lang/RT.pm »
1 33 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Lingy::Test;
3  
4 33 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 33 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 ▶use Lingy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Lingy::Lang::RT;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Lingy::Common;
12  
13 use Capture::Tiny qw'capture capture_merged';
14 use File::Temp 'tempfile';
15  
16 BEGIN {
17 if (defined $INC{'Carp/Always.pm'}) {
18 eval "no Carp::Always";
19 }
20 }
21  
22 use lib 'lib', './test/lib', './t/lib';
23  
24 symlink 't', 'test' if -d 't' and not -e 'test';
25  
26 my $ypp = YAML::PP->new;
27  
28 RT->init;
29 $Lingy::Lang::RT::ready = 0;
30  
31 $ENV{LINGY_TEST} = 1;
32  
33 our $lingy =
34 -f './blib/script/lingy' ? './blib/script/lingy' :
35 -f './bin/lingy' ? './bin/lingy' :
36 undef;
37  
38 our $eg =
39 -d 'eg' ? 'eg' :
40 -d 'example' ? 'example' :
41 die "Can't find eg/example directory";
42  
43 our @EXPORT = qw<
44 done_testing
45 is
46 like
47 note
48 pass
49 plan
50 subtest
51 use_ok
52  
53 capture
54 capture_merged
55 tempfile
56  
57 $lingy
58 $eg
59  
60 rep
61 run_is
62 test
63 test_out
64 tests
65  
66 PPP WWW XXX YYY ZZZ
67 >;
68  
69 sub collapse;
70 sub line;
71  
72 sub import {
73 strict->import;
74 warnings->import;
75 shift->export_to_level(1, @_);
76 }
77  
78 sub rep {
79 RT->rep(@_);
80 }
81  
82 sub tests {
83 my ($spec) = @_;
84 my $list = $ypp->load_string($spec);
85 for my $elem (@$list) {
86 if (ref($elem) eq 'HASH'){
87 my ($key, $val) = %$elem;
88 no strict 'refs';
89 $key->($val);
90 } else {
91 test(@$elem);
92 }
93 }
94 }
95  
96 # Test 'rep' for return value or error:
97 my $test_i = 0;
98 sub test {
99 RT->nextID(10);
100 $test_i++;
101 if ($ENV{ONLY} and $ENV{ONLY} != $test_i) {
102 return;
103 }
104 my ($input, $want, $label) = @_;
105 $label //= "'${\ collapse $input}' -> '${\line $want}'";
106  
107 $Lingy::Lang::RT::ready = 1;
108 my $got = eval { join("\n", RT->rep($input)) };
109 $got = $@ if $@;
110 chomp $got;
111  
112 $got =~ s/^Error: //;
113  
114 if (ref($want) eq 'Regexp') {
115 like $got, $want, $label;
116 } elsif ($want =~ s{^/(.*)/$}{$1}) {
117 like $got, qr/$want/, $label;
118 } else {
119 is $got, $want, $label;
120 }
121 }
122  
123 sub test_out {
124 my ($input, $want, $label) = @_;
125 $label //= "'${\ collapse $input}' -> '${\line $want}'";
126 my ($got) = Capture::Tiny::capture_merged {
127 RT->rep($input);
128 };
129 chomp $got;
130 chomp $want;
131  
132 $got =~ s/^Error: //;
133  
134 if (ref($want) eq 'Regexp') {
135 like $got, $want, $label;
136 } else {
137 is $got, $want, $label;
138 }
139 }
140  
141 sub run_is {
142 my ($cmd, $want, $label) = @_;
143 $label //= "'$cmd' -> '$want'";
144 $label =~ s/\$cmd/$cmd/g;
145 $label =~ s/\$want/$want/g;
146 $label =~ s/\n/\\n/g;
147 my $got = `( $cmd ) 2>&1`;
148 chomp $got;
149 if (ref($want) eq 'Regexp') {
150 like $got, $want, $label;
151 } else {
152 chomp $got;
153 is $got, $want, $label;
154 }
155 }
156  
157 sub collapse {
158 local $_ = shift;
159 s/\s\s+/ /g;
160 s/^ //;
161 chomp;
162 $_;
163 }
164  
165 sub line {
166 local $_ = shift;
167 s/\n/\\n/g;
168 $_;
169 }
170  
171 no warnings 'redefine';
172  
173 my $done_testing = 0;
174 sub done_testing {
175 return if $done_testing++;
176 goto &Test::More::done_testing;
177 }
178  
179 END {
180 package main;
181 done_testing();
182 }
183  
184 1;
« script/_lingy.pl ↑ Summary lib/Lingy/Lang/RT.pm »