Skip to content

Commit b6d1b38

Browse files
author
Chris White
committed
Added initial tests of ...Preparse
Also added XML::Axk::Preparse::preparse() for convenience
1 parent 05a24c5 commit b6d1b38

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

lib/XML/Axk/Preparse.pm

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
package XML::Axk::Preparse;
1010
use XML::Axk::Base qw(:all);
1111

12+
use Devel::Peek;
13+
1214
=encoding UTF-8
1315
1416
=head1 NAME
@@ -95,8 +97,11 @@ sub pieces {
9597
next;
9698
}
9799

98-
# Otherwise, normal line
99-
die "Source text can't come before a pragma line" unless @retval;
100+
# Otherwise, normal line.
101+
unless(/^\h*(#|$)/) { # Ignore blanks and comments before the
102+
# first Ln.
103+
die "Source text can't come before a pragma line" unless @retval;
104+
}
100105
$retval[-1]->{text} .= $_;
101106
}
102107
close $fh;
@@ -161,6 +166,25 @@ sub assemble {
161166
return \$retval;
162167
} #assemble()
163168

169+
=head2 preparse
170+
171+
Invokes pieces() and assemble(). Usage:
172+
173+
my $srTextOut = preparse($filename, $textIn);
174+
175+
C<textIn> can be a string or a string ref.
176+
177+
=cut
178+
179+
sub preparse {
180+
my $filename = $_[0] or croak('Need filename');
181+
my $srTextIn = $_[1] or croak('Need text');
182+
$srTextIn = \$_[1] unless ref $srTextIn eq 'SCALAR';
183+
184+
my $hrPieces = pieces($srTextIn);
185+
my $srTextOut = assemble($filename, $hrPieces);
186+
return $srTextOut;
187+
} #preparse()
164188

165189
1;
166190
# vi: set ts=4 sts=4 sw=4 et ai fo-=ro foldmethod=marker: #

t/01-preparse.t

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!perl
2+
# 01-preparse.t
3+
4+
use 5.018;
5+
use strict;
6+
use warnings;
7+
use Test::More;
8+
use Capture::Tiny 'capture_stdout';
9+
use File::Spec;
10+
11+
BEGIN {
12+
use_ok( 'XML::Axk::Preparse' ) || print "Bail out!\n";
13+
}
14+
15+
#sub localpath {
16+
# state $voldir = [File::Spec->splitpath(__FILE__)];
17+
# return File::Spec->catpath($voldir->[0], $voldir->[1], shift)
18+
#}
19+
20+
# Convenient alias
21+
local *pp = sub { goto &XML::Axk::Preparse::preparse; };
22+
23+
my $srTest;
24+
25+
$srTest = pp('foo', <<'EOT');
26+
#!x -L1
27+
EOT
28+
like($$srTest, qr/^use XML::Axk::L::L1/);
29+
like($$srTest, qr/^#line.*foo/m);
30+
31+
$srTest = pp('foo', <<'EOT');
32+
-L1
33+
EOT
34+
like($$srTest, qr/^use XML::Axk::L::L1/);
35+
like($$srTest, qr/^#line.*foo/m);
36+
37+
38+
done_testing();
39+
40+
# vi: set ts=4 sts=4 sw=4 et ai fdm=marker fdl=1: #

0 commit comments

Comments
 (0)