File: 050_Opt_base_dir.t

package info (click to toggle)
libpetal-perl 2.19-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,076 kB
  • ctags: 166
  • sloc: perl: 4,732; xml: 726; makefile: 43
file content (151 lines) | stat: -rw-r--r-- 3,282 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl
##############################################################################
# Tests the 'data_dir' option to Petal->new and related functionality.
# Uses t/data/namespaces.xml and t/data/include/index_xinclude.xml
#

use Test::More tests => 16;

use warnings;
use lib 'lib';

use Petal;
use File::Spec;

$Petal::DISK_CACHE   = 0;
$Petal::MEMORY_CACHE = 0;

my $data_dir = File::Spec->catdir('t', 'data');
my $file     = 'namespaces.xml';

my @args = (
  replace   => 'Replacement Text',
  content   => 'Content Here',
  attribute => 'An attribute',
  elements  => [ 'one', 'two' ],
);


# Test template not found if path not specified

my $template = new Petal (file => $file);

is_deeply([ $template->base_dir ], [ '.' ],
          "\@Petal::BASE_DIR is used as default base_dir");

$@ = '';
my $output = eval {
  $template->process(@args);
};

ok($@, "Template not found (as expected)");

like($@, qr{
  Cannot\sfind\snamespaces\.xml\sin
}x, "Error message is correct");

# Confirm $Petal::BASE_DIR used as default if defined

{
  local($Petal::BASE_DIR) = 'dummy_dir';

  $template = new Petal (file => $file);
  ok (1);
#  is_deeply([ $template->base_dir ], [ 'dummy_dir', '.' ], 
#            "\$Petal::BASE_DIR is in default base_dir");

  $@ = '';
  $output = eval {
    $template->process(@args);
  };

  ok($@, "Template still not found (as expected)");
  like($@, qr{
    Cannot\sfind\snamespaces\.xml\sin
  }x, "Error message is correct");
}


# Confirm base_dir option as arrayref works

$template = new Petal (file => $file, base_dir => ['dummy1', 'dummy2']);

is_deeply([ $template->base_dir ], [ 'dummy1', 'dummy2' ], 
          "base_dir option specified as arrayref works");

$@ = '';
$output = eval {
  $template->process(@args);
};

ok($@, "Template still not found (as expected)");
like($@, qr{
  Cannot\sfind\snamespaces\.xml\sin
}x, "Error message is correct");


# Confirm base_dir option as scalar works

$template = new Petal (file => $file, base_dir => 'dummy');

is_deeply([ $template->base_dir ], [ 'dummy' ], 
          "base_dir option specified as scalar works");

$@ = '';
$output = eval {
  $template->process(@args);
};

ok($@, "Template still not found (as expected)");
like($@, qr{
  Cannot\sfind\snamespaces\.xml\sin
}x, "Error message is correct");


# Now specify a valid base_dir option and process a template

$template = new Petal (file => $file, base_dir => $data_dir);

$@ = '';
$output = eval {
  $template->process(@args);
};

ok(!$@, "Template was found");
like($output, qr{^\s*
  <body\s*>\s+
  Replacement\sText\s+
  <p\s*>Content\sHere</p>\s+
  <p\s+attribute=(['"])An\sattribute\1\s*>yo</p>\s+
  <ul>\s*
  <li>one</li>\s*
  <li>two</li>\s*
  </ul>\s+
  </body>
}x, "Output is correct");


# Try processing a template with an include (data_dir is required for included
# template too).

$data_dir = File::Spec->catdir('t', 'data', 'include');
$file     = 'index_xinclude.xml';

$template = new Petal (file => $file, base_dir => $data_dir);

$@ = '';
$output = eval {
  $template->process(@args);
};

ok(!$@, "Template with includes successfully processed");
like($output, qr{^\s*
  <xml>\s+
  <div>\s+
  <p>__INCLUDED__</p>\s+
  <p>__INCLUDED__</p>\s+
  <p></p>\s+
  </div>\s+
  </xml>
}x, "Output is correct");