File Coverage

blib/lib/JMAP/Tester/Sugar.pm
Criterion Covered Total %
statement 39 39 100.0
branch 5 6 83.3
condition n/a
subroutine 10 10 100.0
pod 0 3 0.0
total 54 58 93.1


line stmt bran cond sub pod time code
1             package JMAP::Tester::Sugar 0.109;
2              
3 3     3   363403 use v5.20.0;
  3         37  
4 3     3   16 use warnings;
  3         11  
  3         203  
5              
6 3     3   1141 use experimental 'signatures';
  3         6928  
  3         22  
7              
8 3     3   2154 use Sub::Exporter -setup => [ qw( jset jcreate json_literal ) ];
  3         32767  
  3         26  
9              
10 8     8 0 584368 sub jset ($type, $arg, $call_id = undef) {
  8         20  
  8         20  
  8         17  
  8         30  
11 8         70 my %method_arg = %$arg;
12 8 50       66 if (my $create_spec = delete $method_arg{create}) {
13 8 100       81 unless (ref $create_spec eq 'ARRAY') {
14 4         14 $create_spec = [ $create_spec ];
15             }
16              
17 8         22 $method_arg{create} = {};
18              
19 8         18 my $i = 0;
20 8         25 for my $creation (@$create_spec) {
21 12         56 $method_arg{create}{"$type-" . $i++} = $creation;
22             }
23             }
24              
25             return [
26 8 100       120 "$type/set",
27             \%method_arg,
28             (defined $call_id ? $call_id : ()),
29             ];
30             }
31              
32 4     4 0 32068 sub jcreate ($type, $create, $call_id = undef) {
  4         14  
  4         8  
  4         8  
  4         7  
33 4         45 return jset($type, { create => $create }, $call_id);
34             }
35              
36             package JMAP::Tester::JSONLiteral 0.109 {
37             sub new {
38 2     2   6 my ($class, $bytes) = @_;
39              
40 2         16 bless { _bytes => $bytes }, $class;
41             }
42              
43 1     1   6 sub bytes { return $_[0]{_bytes} }
44              
45             sub TO_JSON {
46             # Some day, somebody is going to think that they can do this:
47             # $tester->request([[ json_literal(...), {...} ]]);
48             #
49             # ...but they can't, because you can't supply the JSON encoder a hunk of
50             # bytes to stick in the middle. We can only decline to encode *at all*.
51             # Because TO_JSON can't really die to abort JSON encoding, we just put some
52             # obvious "you did it wrong" text into the output, and then we hope that
53             # the user reads the logging! -- rjbs, 2025-12-11
54 1     1   26 return "ERROR: a JMAP::Tester json_literal was passed to a JSON encoder"
55             }
56             }
57              
58 2     2 0 5093 sub json_literal ($bytes) {
  2         6  
  2         4  
59 2         18 return JMAP::Tester::JSONLiteral->new($bytes);
60             }
61              
62             1;
63              
64             __END__