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   82193 use strict;
  1         13  
4 1     1   4 use warnings;
  1         2  
  1         31  
5 1     1   4  
  1         2  
  1         24  
6             use Test::Data::Sah qw(run_spectest all_match);
7 1     1   378 use Test::More 0.98;
  1         2  
  1         59  
8 1     1   6  
  1         21  
  1         7  
9             use Data::Sah qw(gen_validator);
10 1     1   261  
  1         2  
  1         43  
11             use Exporter qw(import);
12 1     1   4  
  1         2  
  1         665  
13             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14             our $DATE = '2022-10-19'; # DATE
15             our $DIST = 'Data-Sah'; # DIST
16             our $VERSION = '0.914'; # 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   3505 my $data = $test->{input};
26             my $ho = exists($test->{output}); # has output
27 1503         3780 my $vbool;
28 1503         3801 eval { $vbool = gen_validator(
29 1503         3329 $test->{schema}, {accept_ref=>$ho}) };
30 1503         3153 my $eval_err = $@;
31 1503         7372 if ($test->{dies}) {
32 1503         5128 ok($eval_err, "compile error");
33 1503 100       5676 return;
34 33         182 } else {
35 33         13887 ok(!$eval_err, "compile success") or do {
36             diag $eval_err;
37 1470 50       8051 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       871837 for my $i (0..@{ $test->{valid_inputs} }-1) {
    50          
44             my $data = $test->{valid_inputs}[$i];
45 25         58 ok($vbool->($ho ? \$data : $data), "valid input [$i]");
  25         100  
46 51         9910 }
47 51 50       1231 for my $i (0..@{ $test->{invalid_inputs} }-1) {
48             my $data = $test->{invalid_inputs}[$i];
49 25         9377 ok(!$vbool->($ho ? \$data : $data), "invalid input [$i]");
  25         118  
50 60         12880 }
51 60 50       1309 } 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       5242 if ($ho) {
56 833 100       20536 is_deeply($data, $test->{output}, "output");
57 833 100       331384 }
58 6         43 } else {
59             ok(!$vbool->($ho ? \$data : $data), "invalid (rt=bool_valid)");
60             }
61 612 50       15052 }
62              
63             my $vstr = gen_validator($test->{schema},
64             {return_type=>'str_errmsg'});
65             if (exists $test->{valid}) {
66 1470         259978 if ($test->{valid}) {
67 1470 100       7662 is($vstr->($test->{input}), "", "valid (rt=str_errmsg)");
68 1445 100       5190 } else {
69 833         18793 like($vstr->($test->{input}), qr/\S/, "invalid (rt=str_errmsg)");
70             }
71 612         14125 }
72              
73             my $vfull = gen_validator($test->{schema},
74             {return_type=>'hash_details'});
75             my $res = $vfull->($test->{input});
76 1470         715863 is(ref($res), 'HASH', "validator (rt=hash_details) returns hash");
77 1470         35865 if (exists($test->{errors}) || exists($test->{warnings}) ||
78 1470         9359 exists($test->{valid})) {
79 1470 100 100     809524 my $errors = $test->{errors} // ($test->{valid} ? 0 : 1);
      100        
80             is(scalar(keys %{ $res->{errors} // {} }), $errors, "errors (rt=hash_details)")
81 1445 100 100     9421 or diag explain $res;
82 1445 50 100     3748 my $warnings = $test->{warnings} // 0;
  1445         10978  
83             is(scalar(keys %{ $res->{warnings} // {} }), $warnings,
84 1445   100     590354 "warnings (rt=hash_details)")
85 1445 50 100     3145 or diag explain $res;
  1445         10889  
86             }
87             }, # test_func
88              
89             skip_if => sub {
90             my $t = shift;
91             return 0 unless $t->{tags};
92 1584     1584   3471  
93 1584 50       6653 # 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       9515 for (qw/
100              
101 1556         6218 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       40279  
118             return "properties are not yet implemented"
119             if grep {/^prop:/} @{ $t->{tags} };
120              
121 1524 100       2383 0;
  6162         13041  
  1524         3180  
122             }, # skip_if
123 1503         3816  
124             );
125             }
126 1     1 1 120  
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.914 of Test::Data::Sah::Perl (from Perl distribution Data-Sah), released on 2022-10-19.
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