File Coverage

blib/lib/Test/Data/Sah/Perl.pm
Criterion Covered Total %
statement 70 72 97.2
branch 32 40 80.0
condition 15 15 100.0
subroutine 10 10 100.0
pod 1 1 100.0
total 128 138 92.7


line stmt bran cond sub pod time code
1              
2             use 5.010001;
3 1     1   95748 use strict;
  1         13  
4 1     1   4 use warnings;
  1         1  
  1         16  
5 1     1   4  
  1         1  
  1         23  
6             use Test::Data::Sah qw(run_spectest all_match);
7 1     1   387 use Test::More 0.98;
  1         3  
  1         62  
8 1     1   5  
  1         20  
  1         7  
9             use Data::Sah qw(gen_validator);
10 1     1   283  
  1         2  
  1         42  
11             use Exporter qw(import);
12 1     1   5  
  1         1  
  1         707  
13             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14             our $DATE = '2022-09-30'; # DATE
15             our $DIST = 'Data-Sah'; # DIST
16             our $VERSION = '0.913'; # VERSION
17              
18             our @EXPORT_OK = qw(run_spectest_for_perl);
19              
20             run_spectest(
21             test_merge_clause_sets => 1,
22             test_func => sub {
23             my $test = shift;
24              
25 1503     1503   3246 my $data = $test->{input};
26             my $ho = exists($test->{output}); # has output
27 1503         3442 my $vbool;
28 1503         3849 eval { $vbool = gen_validator(
29 1503         2426 $test->{schema}, {accept_ref=>$ho}) };
30 1503         2178 my $eval_err = $@;
31 1503         6495 if ($test->{dies}) {
32 1503         4213 ok($eval_err, "compile error");
33 1503 100       5195 return;
34 33         171 } else {
35 33         14501 ok(!$eval_err, "compile success") or do {
36             diag $eval_err;
37 1470 50       6237 return;
38 0         0 };
39 0         0 }
40              
41             if ($test->{valid_inputs}) {
42             # test multiple inputs, currently only done for rt=bool_valid
43 1470 100       665233 for my $i (0..@{ $test->{valid_inputs} }-1) {
    50          
44             my $data = $test->{valid_inputs}[$i];
45 25         57 ok($vbool->($ho ? \$data : $data), "valid input [$i]");
  25         91  
46 51         10087 }
47 51 50       1212 for my $i (0..@{ $test->{invalid_inputs} }-1) {
48             my $data = $test->{invalid_inputs}[$i];
49 25         9472 ok(!$vbool->($ho ? \$data : $data), "invalid input [$i]");
  25         115  
50 60         13276 }
51 60 50       1368 } elsif (exists $test->{valid}) {
52             # test a single input
53             if ($test->{valid}) {
54             ok($vbool->($ho ? \$data : $data), "valid (rt=bool_valid)");
55 1445 100       4203 if ($ho) {
56 833 100       21072 is_deeply($data, $test->{output}, "output");
57 833 100       324456 }
58 6         34 } else {
59             ok(!$vbool->($ho ? \$data : $data), "invalid (rt=bool_valid)");
60             }
61 612 50       14818 }
62              
63             my $vstr = gen_validator($test->{schema},
64             {return_type=>'str_errmsg'});
65             if (exists $test->{valid}) {
66 1470         257739 if ($test->{valid}) {
67 1470 100       6595 is($vstr->($test->{input}), "", "valid (rt=str_errmsg)");
68 1445 100       3964 } else {
69 833         19016 like($vstr->($test->{input}), qr/\S/, "invalid (rt=str_errmsg)");
70             }
71 612         14210 }
72              
73             my $vfull = gen_validator($test->{schema},
74             {return_type=>'hash_details'});
75             my $res = $vfull->($test->{input});
76 1470         626463 is(ref($res), 'HASH', "validator (rt=hash_details) returns hash");
77 1470         38005 if (exists($test->{errors}) || exists($test->{warnings}) ||
78 1470         8303 exists($test->{valid})) {
79 1470 100 100     654517 my $errors = $test->{errors} // ($test->{valid} ? 0 : 1);
      100        
80             is(scalar(keys %{ $res->{errors} // {} }), $errors, "errors (rt=hash_details)")
81 1445 100 100     7679 or diag explain $res;
82 1445 50 100     2224 my $warnings = $test->{warnings} // 0;
  1445         8428  
83             is(scalar(keys %{ $res->{warnings} // {} }), $warnings,
84 1445   100     589486 "warnings (rt=hash_details)")
85 1445 50 100     2623 or diag explain $res;
  1445         9469  
86             }
87             }, # test_func
88              
89             skip_if => sub {
90             my $t = shift;
91             return 0 unless $t->{tags};
92 1584     1584   3044  
93 1584 50       6255 # disabled temporarily because failing for bool, even though i've
94             # adjust stuffs. but 'between' clause should be very seldomly used
95             # on bool, moreover with op, so i haven't looked into it.
96             return "currently failing"
97             if all_match([qw/type:bool clause:between op/], $t->{tags});
98              
99 1584 100       6510 for (qw/
100              
101 1556         4252 check
102             check_each_elem
103             check_each_index
104             check_each_key
105             check_each_value
106             check_prop
107             exists
108             if
109             postfilters
110             prop
111             uniq
112              
113             /) {
114             return "clause $_ not yet implemented"
115             if all_match(["clause:$_"], $t->{tags});
116             }
117 16998 100       37290  
118             return "properties are not yet implemented"
119             if grep {/^prop:/} @{ $t->{tags} };
120              
121 1524 100       2231 0;
  6162         13303  
  1524         2746  
122             }, # skip_if
123 1503         3168  
124             );
125             }
126 1     1 1 112  
127             1;
128             # ABSTRACT: Routines for testing Data::Sah (perl compiler)
129              
130              
131             =pod
132              
133             =encoding UTF-8
134              
135             =head1 NAME
136              
137             Test::Data::Sah::Perl - Routines for testing Data::Sah (perl compiler)
138              
139             =head1 VERSION
140              
141             This document describes version 0.913 of Test::Data::Sah::Perl (from Perl distribution Data-Sah), released on 2022-09-30.
142              
143             =head1 FUNCTIONS
144              
145             =head2 run_spectest_for_perl()
146              
147             =head1 HOMEPAGE
148              
149             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah>.
150              
151             =head1 SOURCE
152              
153             Source repository is at L<https://github.com/perlancar/perl-Data-Sah>.
154              
155             =head1 AUTHOR
156              
157             perlancar <perlancar@cpan.org>
158              
159             =head1 CONTRIBUTING
160              
161              
162             To contribute, you can send patches by email/via RT, or send pull requests on
163             GitHub.
164              
165             Most of the time, you don't need to build the distribution yourself. You can
166             simply modify the code, then test via:
167              
168             % prove -l
169              
170             If you want to build the distribution (e.g. to try to install it locally on your
171             system), you can install L<Dist::Zilla>,
172             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
173             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
174             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
175             that are considered a bug and can be reported to me.
176              
177             =head1 COPYRIGHT AND LICENSE
178              
179             This software is copyright (c) 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012 by perlancar <perlancar@cpan.org>.
180              
181             This is free software; you can redistribute it and/or modify it under
182             the same terms as the Perl 5 programming language system itself.
183              
184             =head1 BUGS
185              
186             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah>
187              
188             When submitting a bug or request, please include a test-file or a
189             patch to an existing test-file that illustrates the bug or desired
190             feature.
191              
192             =cut