File: Simple.pm

package info (click to toggle)
libtest-inline-perl 2.214-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 596 kB
  • sloc: perl: 2,016; makefile: 2
file content (217 lines) | stat: -rw-r--r-- 4,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package Test::Inline::Content::Simple;
# ABSTRACT: Simple templating Content Handler

#pod =pod
#pod
#pod =head1 SYNOPSIS
#pod
#pod   In your inline2test.tpl
#pod   ----------------------
#pod   #!/usr/bin/perl -w
#pod   
#pod   use strict;
#pod   use Test::More [% plan %];
#pod   $| = 1;
#pod   
#pod   
#pod   
#pod   [% tests %]
#pod   
#pod   
#pod   
#pod   1;
#pod
#pod =head1 DESCRIPTION
#pod
#pod It is relatively common to want to customise the contents of the generated
#pod test files to set up custom environment things on an all-scripts basis,
#pod rather than file by file (using =begin SETUP blocks).
#pod
#pod C<Test::Inline::Content::Simple> lets you use a very simple Template Toolkit
#pod style template to define this information.
#pod
#pod It contains only two tags, C<plan> and C<tests>.
#pod
#pod The C<plan> tag will be inserted as either C<tests => 123> or C<'no_plan'>.
#pod
#pod The C<tests> tag will be replaced by the actual testing code.
#pod
#pod =head1 METHODS
#pod
#pod =cut

use strict;
use Path::Tiny ();
use Params::Util qw{_INSTANCE};
use Test::Inline::Content ();

our $VERSION = '2.214';
our @ISA     = 'Test::Inline::Content';





#####################################################################
# Constructor and Accessors

#pod =pod
#pod
#pod =head2 new $filename
#pod
#pod Manually create a new C<Test::Inline::Content::Simple> object. Takes as
#pod parameter a single filename which should contain the template code.
#pod
#pod Returns a new C<Test::Inline::Content::Simple> object, or C<undef> on error.
#pod
#pod =cut

sub new {
	my $class = ref $_[0] ? ref shift : shift;
	my $file  = (defined $_[0] and -r $_[0]) ? shift : return undef;
	
	# Create the object
	my $self  = $class->SUPER::new() or return undef;

	# Load, check and add the file
	my $template = Path::Tiny::path($file)->slurp or return undef;
	$template =~ /\[%\s+tests\s+\%\]/              or return undef;
	# $template =~ /\[\%\s+plan\s+\%\]/              or return undef;
	$self->{template} = $template;

	$self;
}

#pod =pod
#pod
#pod =head2 template
#pod
#pod The C<template> accessor returns the template content for the object
#pod
#pod =cut

sub template { $_[0]->{template} }





#####################################################################
# Test::Inline::Content Methods

#pod =pod
#pod
#pod =head2 process $Inline, $Script
#pod
#pod The C<process> method is unchanged from C<Test::Inline::Content>.
#pod
#pod =cut

sub process {
	my $self   = shift;
	my $Inline = _INSTANCE(shift, 'Test::Inline')         or return undef;
	my $Script = _INSTANCE(shift, 'Test::Inline::Script') or return undef;

	# Get the merged content
	my $content = $Script->merged_content;
	return undef unless defined $content;

	# Determine a plan
	my $tests = $Script->tests;
	my $plan  = defined $tests
		? "tests => $tests"
		: "'no_plan'";

	# Replace the two values
	my $script = $self->{template};
	$script =~ s/\[%\s+tests\s+\%\]/$content/;
	$script =~ s/\[\%\s+plan\s+\%\]/$plan/;

	$script;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test::Inline::Content::Simple - Simple templating Content Handler

=head1 VERSION

version 2.214

=head1 SYNOPSIS

  In your inline2test.tpl
  ----------------------
  #!/usr/bin/perl -w
  
  use strict;
  use Test::More [% plan %];
  $| = 1;
  
  
  
  [% tests %]
  
  
  
  1;

=head1 DESCRIPTION

It is relatively common to want to customise the contents of the generated
test files to set up custom environment things on an all-scripts basis,
rather than file by file (using =begin SETUP blocks).

C<Test::Inline::Content::Simple> lets you use a very simple Template Toolkit
style template to define this information.

It contains only two tags, C<plan> and C<tests>.

The C<plan> tag will be inserted as either C<tests => 123> or C<'no_plan'>.

The C<tests> tag will be replaced by the actual testing code.

=head1 METHODS

=head2 new $filename

Manually create a new C<Test::Inline::Content::Simple> object. Takes as
parameter a single filename which should contain the template code.

Returns a new C<Test::Inline::Content::Simple> object, or C<undef> on error.

=head2 template

The C<template> accessor returns the template content for the object

=head2 process $Inline, $Script

The C<process> method is unchanged from C<Test::Inline::Content>.

=head1 SUPPORT

See the main L<SUPPORT|Test::Inline/SUPPORT> section.

Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Test-Inline>
(or L<bug-Test-Inline@rt.cpan.org|mailto:bug-Test-Inline@rt.cpan.org>).

=head1 AUTHOR

Adam Kennedy <adamk@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2003 by Adam Kennedy.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut