blib/lib/Template/Plugin/Assert.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 72 CC 5 SCAR 18.9
CC
5
Cov
0%
AUTOLOAD
12.0
AUTOLOAD
12.0
load
2.0
CRAP 6.6
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Assert
4 #
5 # DESCRIPTION
6 # Template Toolkit plugin module which allows you to assert that
7 # items fetches from the stash are defined.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2008-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Plugin::Assert;
21 use base 'Template::Plugin';
22 use strict;
23 use warnings;
24 use Template::Exception;
25  
26 our $VERSION = '3.105';
27 our $MONAD = 'Template::Monad::Assert';
28 our $EXCEPTION = 'Template::Exception';
29 our $AUTOLOAD;
30  
31 sub load {
32 my $class = shift;
33 my $context = shift;
34 my $stash = $context->stash;
35 my $vmethod = sub {
36 $MONAD->new($stash, shift);
37 };
38  
39 # define .assert vmethods for hash and list objects
40 $context->define_vmethod( hash => assert => $vmethod );
41 $context->define_vmethod( list => assert => $vmethod );
42  
43 return $class;
44 }
45  
46 sub new {
47 my ($class, $context, @args) = @_;
48 # create an assert plugin object which will handle simple variable
49 # lookups.
50 return bless { _CONTEXT => $context }, $class;
51 }
52  
53 sub AUTOLOAD {
54 my ($self, @args) = @_;
55 my $item = $AUTOLOAD;
56 $item =~ s/.*:://;
57 return if $item eq 'DESTROY';
58  
59 # lookup the named values
60 my $stash = $self->{ _CONTEXT }->stash;
61 my $value = $stash->dotop($stash, $item, \@args);
62  
63 if (! defined $value) {
64 die $EXCEPTION->new( assert => "undefined value for $item" );
65 }
66 return $value;
67 }
68  
69  
70 package Template::Monad::Assert;
71  
72 our $EXCEPTION = 'Template::Exception';
73 our $AUTOLOAD;
74  
75 sub new {
76 my ($class, $stash, $this) = @_;
77 bless [$stash, $this], $class;
78 }
79  
80 sub AUTOLOAD {
81 my ($self, @args) = @_;
82 my ($stash, $this) = @$self;
83 my $item = $AUTOLOAD;
84 $item =~ s/.*:://;
85 return if $item eq 'DESTROY';
86  
87 my $value = $stash->dotop($this, $item, \@args);
88  
89 if (! defined $value) {
90 die $EXCEPTION->new( assert => "undefined value for $item" );
91 }
92 return $value;
93 }
94  
95 1;
96  
97 __END__