File Coverage

t/05.examples.t
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 1     1   5 use strict;
  1         2  
  1         35  
4 1     1   6 use warnings;
  1         1  
  1         23  
5 1     1   4 use Carp;
  1         1  
  1         66  
6              
7 1     1   829 use Test::More;
  1         19453  
  1         10  
8              
9 1     1   1049 use lib qw(lib t/lib);
  1         589  
  1         4  
10              
11 1     1   591 use MyTestTools qw(:all);
  1         3  
  1         155  
12 1     1   382 use IkiWiki q(2.0);
  0            
  0            
13              
14             use IO::File;
15             use HTML::Template;
16              
17             my $examples_dir = q(examples/sources);
18             my %examples = (
19             'sarajevo.conf' => 'conf',
20             'smart_comments.pl' => 'perl',
21             'page.tmpl' => 'html',
22             'function.pl' => 'perl',
23             'bash.sh' => 'bash',
24             'fragment.html' => 'html',
25             'text.pod' => 'pod',
26             );
27             my @engines = MyTestTools::Engines();
28             my %options = (
29             linenumbers => 1,
30             bars => 0,
31             );
32              
33             my $html_path = q(examples/example.html);
34             if (not ResultsFile( $html_path )) {
35             plan( skip_all => "Could not write to the ${html_path} file" );
36             }
37             else {
38             my $ntests = ( (scalar keys %examples) * (scalar @engines) ) + 1;
39             plan(tests => $ntests);
40             }
41              
42             use_ok('IkiWiki::Plugin::syntax');
43              
44             ## initialize the output HTML page
45             _init_output();
46              
47             # build the HTML examples
48             EXAMPLES:
49             foreach my $page (keys %examples) {
50             my $html = undef;
51             my $example_path = "${examples_dir}/$page";
52              
53             ## and loop around the engines using the same source
54             ENGINES:
55             foreach my $engine (@engines) {
56             # Setting global parameters
57             $IkiWiki::config{syntax_engine} = $engine;
58             $IkiWiki::config{debug} = 0;
59              
60             # Initialize the plugin
61             eval {
62             IkiWiki::Plugin::syntax::checkconfig();
63             };
64              
65             if ($@) {
66             fail("Engine ${engine} not installed");
67             next ENGINES;
68             }
69             else {
70             eval {
71             $html = IkiWiki::Plugin::syntax::preprocess(
72             file => $example_path,
73             language => $examples{$page},
74             %options );
75             };
76            
77             if ($@) {
78             fail("built page ${example_path} with engine ${engine}");
79             }
80             else {
81             _add_result( engine => $engine, page => $page,
82             language => $examples{$page}, text => $html);
83             pass("syntax highlight from ${page} using engine ${engine}");
84             }
85             }
86             } ## foreach engines
87             } ## foreach pages
88              
89             my $final = HTML::Template->new( filename => q(examples/sources/page.tmpl), no_includes => 1 );
90              
91             $final->param( title => q(Examples page for IkiWiki::Plugin::syntax),
92             results => [ _get_output() ],
93             );
94              
95             # write the final output to a permanent file
96             my $example_html = ResultsFile();
97             $example_html->print($final->output());
98             $example_html->close();
99              
100             {
101              
102             my @blocks = ();
103              
104             sub _init_output {
105             @blocks = ();
106             }
107              
108             sub _get_output {
109             return @blocks;
110             }
111              
112             sub _add_result {
113             my %params = (
114             engine => undef,
115             page => undef,
116             text => undef,
117             @_ );
118             my $description = undef;
119              
120             if (ref $params{text} eq 'SCALAR') {
121             $params{text} = _slurp_page( ${$params{text}} );
122             }
123              
124             my $html = HTML::Template->new( filename => q(examples/results.tmpl),
125             no_includes => 1 );
126              
127             if (not $params{engine}) {
128             $description = sprintf 'Source from %s', $params{page};
129             }
130             else {
131             $description = sprintf 'Source %s using %s engine', $params{page},
132             $params{engine};
133             }
134              
135             $html->param( description => $description,
136             text => $params{text} );
137              
138             push(@blocks, { output => $html->output() } );
139             }
140              
141             sub _slurp_page {
142             my $path = shift;
143             my $content = undef;
144              
145             if (my $fh = IO::File->new( $path )) {
146             local $/;
147            
148             $content = <$fh>;
149              
150             $fh->close;
151             }
152             else {
153             croak "could not open ${path} - ${!}";
154             }
155              
156             return $content;
157             }
158             }