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   88126 use strict;
  1         12  
4 1     1   4 use warnings;
  1         2  
  1         17  
5 1     1   3  
  1         2  
  1         23  
6             use Test::Data::Sah qw(run_spectest all_match);
7 1     1   394 use Test::More 0.98;
  1         2  
  1         56  
8 1     1   7  
  1         19  
  1         6  
9             use Data::Sah qw(gen_validator);
10 1     1   229  
  1         2  
  1         41  
11             use Exporter qw(import);
12 1     1   5  
  1         2  
  1         698  
13             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14             our $DATE = '2022-08-20'; # DATE
15             our $DIST = 'Data-Sah'; # DIST
16             our $VERSION = '0.912'; # 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   3253 my $data = $test->{input};
26             my $ho = exists($test->{output}); # has output
27 1503         3434 my $vbool;
28 1503         3319 eval { $vbool = gen_validator(
29 1503         2357 $test->{schema}, {accept_ref=>$ho}) };
30 1503         2248 my $eval_err = $@;
31 1503         7149 if ($test->{dies}) {
32 1503         4694 ok($eval_err, "compile error");
33 1503 100       5627 return;
34 33         167 } else {
35 33         15649 ok(!$eval_err, "compile success") or do {
36             diag $eval_err;
37 1470 50       6277 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       712868 for my $i (0..@{ $test->{valid_inputs} }-1) {
    50          
44             my $data = $test->{valid_inputs}[$i];
45 25         52 ok($vbool->($ho ? \$data : $data), "valid input [$i]");
  25         82  
46 51         9821 }
47 51 50       1173 for my $i (0..@{ $test->{invalid_inputs} }-1) {
48             my $data = $test->{invalid_inputs}[$i];
49 25         9326 ok(!$vbool->($ho ? \$data : $data), "invalid input [$i]");
  25         93  
50 60         12938 }
51 60 50       1342 } 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       4589 if ($ho) {
56 833 100       20524 is_deeply($data, $test->{output}, "output");
57 833 100       321123 }
58 6         40 } else {
59             ok(!$vbool->($ho ? \$data : $data), "invalid (rt=bool_valid)");
60             }
61 612 50       14440 }
62              
63             my $vstr = gen_validator($test->{schema},
64             {return_type=>'str_errmsg'});
65             if (exists $test->{valid}) {
66 1470         253633 if ($test->{valid}) {
67 1470 100       7063 is($vstr->($test->{input}), "", "valid (rt=str_errmsg)");
68 1445 100       4227 } else {
69 833         19198 like($vstr->($test->{input}), qr/\S/, "invalid (rt=str_errmsg)");
70             }
71 612         13924 }
72              
73             my $vfull = gen_validator($test->{schema},
74             {return_type=>'hash_details'});
75             my $res = $vfull->($test->{input});
76 1470         659482 is(ref($res), 'HASH', "validator (rt=hash_details) returns hash");
77 1470         36782 if (exists($test->{errors}) || exists($test->{warnings}) ||
78 1470         8320 exists($test->{valid})) {
79 1470 100 100     687320 my $errors = $test->{errors} // ($test->{valid} ? 0 : 1);
      100        
80             is(scalar(keys %{ $res->{errors} // {} }), $errors, "errors (rt=hash_details)")
81 1445 100 100     7721 or diag explain $res;
82 1445 50 100     2379 my $warnings = $test->{warnings} // 0;
  1445         8256  
83             is(scalar(keys %{ $res->{warnings} // {} }), $warnings,
84 1445   100     581825 "warnings (rt=hash_details)")
85 1445 50 100     2882 or diag explain $res;
  1445         9728  
86             }
87             }, # test_func
88              
89             skip_if => sub {
90             my $t = shift;
91             return 0 unless $t->{tags};
92 1584     1584   2964  
93 1584 50       6690 # 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       7652 for (qw/
100              
101 1556         4776 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       38035  
118             return "properties are not yet implemented"
119             if grep {/^prop:/} @{ $t->{tags} };
120              
121 1524 100       2313 0;
  6162         11505  
  1524         3386  
122             }, # skip_if
123 1503         3286  
124             );
125             }
126 1     1 1 118  
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.912 of Test::Data::Sah::Perl (from Perl distribution Data-Sah), released on 2022-08-20.
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