File Coverage

blib/lib/Test/Returns.pm
Criterion Covered Total %
statement 36 43 83.7
branch 5 10 50.0
condition 2 4 50.0
subroutine 9 9 100.0
pod 4 4 100.0
total 56 70 80.0


line stmt bran cond sub pod time code
1             package Test::Returns;
2              
3 1     1   206407 use strict;
  1         2  
  1         41  
4 1     1   5 use warnings;
  1         1  
  1         54  
5              
6 1     1   524 use parent 'Exporter';
  1         332  
  1         7  
7              
8 1     1   91 use Test::Builder;
  1         4  
  1         33  
9 1     1   624 use Return::Set qw(set_return);
  1         69831  
  1         483  
10              
11             our @EXPORT = qw(returns_ok returns_not_ok returns_is returns_isnt);
12             our $VERSION = '0.02';
13              
14             my $Test = Test::Builder->new();
15              
16             =head1 NAME
17              
18             Test::Returns - Verify that a method's output agrees with its specification
19              
20             =head1 SYNOPSIS
21              
22             use Test::More;
23             use Test::Returns;
24              
25             returns_ok(42, { type => 'integer' }, 'Returns valid integer');
26             returns_ok([], { type => 'arrayref' }, 'Returns valid arrayref');
27             returns_not_ok("bad", { type => 'arrayref' }, 'Fails (expected arrayref)');
28              
29             =head1 DESCRIPTION
30              
31             Exports the function C, which asserts that a value satisfies a schema as defined in L.
32             Integrates with L for use alongside L and friends.
33              
34             =head1 METHODS
35              
36             =head2 returns_is($value, $schema, $test_name)
37              
38             Passes if C<$value> satisfies C<$schema> using C.
39             Fails otherwise.
40              
41             =cut
42              
43             sub returns_is {
44 5     5 1 177682 my ($value, $schema, $test_name) = @_;
45              
46 5         9 my $ok;
47             my $error;
48              
49             eval {
50 5 50       13 if($value) {
51 5         15 $ok = set_return($value, $schema) eq $value;
52             } else {
53 0         0 set_return(undef, $schema);
54 0         0 $ok = 1;
55             }
56 5         720 1;
57 5 50       10 } or do {
58 0         0 $error = $@;
59 0         0 $ok = 0;
60             };
61              
62 5   50     11 $test_name ||= 'Value matches schema';
63              
64 5 50       10 if($ok) {
65 5         16 $Test->ok(1, $test_name);
66             } else {
67 0         0 $Test->ok(0, $test_name);
68 0         0 $Test->diag("Validation failed: $error");
69             }
70              
71 5         1580 return $ok;
72             }
73              
74             =head2 returns_isnt
75              
76             Opposite of returns_is
77              
78             =cut
79              
80             sub returns_isnt
81             {
82 4     4 1 25 my ($value, $schema, $test_name) = @_;
83              
84 4         7 my $ok;
85              
86             eval {
87 4         12 $ok = defined(set_return($value, $schema));
88 4 50       7 } or do {
89 4         15289 $ok = 0;
90             };
91              
92 4   50     13 $test_name ||= 'Value does not match schema';
93              
94 4 50       11 if($ok) {
95 0         0 $Test->ok(0, $test_name); # Value matched schema — test fails
96             } else {
97 4         21 $Test->ok(1, $test_name); # Value did not match — test passes
98             }
99              
100 4         1613 return !$ok;
101             }
102              
103             =head2 returns_ok($value, $schema, $test_name)
104              
105             Alias for C.
106             Provided for naming symmetry and clarity.
107              
108             =cut
109              
110             sub returns_ok
111             {
112 3     3 1 23 return returns_is(@_);
113             }
114              
115             =head2 returns_not_ok
116              
117             Synonym of returns_isnt
118              
119             =cut
120              
121             sub returns_not_ok
122             {
123 1     1 1 11 return returns_isnt(@_);
124             }
125              
126             =head1 AUTHOR
127              
128             Nigel Horne
129              
130             =head1 SEE ALSO
131              
132             L, L, L
133              
134             =head1 SUPPORT
135              
136             This module is provided as-is without any warranty.
137              
138             =head1 LICENCE AND COPYRIGHT
139              
140             Copyright 2025 Nigel Horne.
141              
142             Usage is subject to licence terms.
143              
144             The licence terms of this software are as follows:
145              
146             =over 4
147              
148             =item * Personal single user, single computer use: GPL2
149              
150             =item * All other users (including Commercial, Charity, Educational, Government)
151             must apply in writing for a licence for use from Nigel Horne at the
152             above e-mail.
153              
154             =back
155              
156             =cut
157              
158             1;