1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4######################### We start with some black magic to print on failure.
5
6# Change 1..1 below to 1..last_test_to_print .
7# (It may become useful if the test is moved to ./t subdirectory.)
8
9BEGIN { $| = 1; print "1..25\n"; }
10END {print "not ok 1\n" unless $loaded;}
11use Text::FillIn;
12$loaded = 1;
13&report(1);
14
15######################### End of black magic.
16
17# Insert your test code below (better if it prints "ok 13"
18# (correspondingly "not ok 13") depending on the success of chunk 13
19# of the test code):
20
21sub report {
22	$TEST_NUM++;
23	print ( $_[0] ? "ok $TEST_NUM\n" : "not ok $TEST_NUM\n" );
24}
25
26# The variables for interpolation
27$TVars{'var'} = 'text';
28$TVars{'nestedtext'} = 'coconuts';
29$TVars{'more_var'} = 'donuts';
30$TVars{'var2'} = 'nested';
31$TVars{'text\\]]'} = 'garbage';
32
33# 2,3
34&test_both('some [[$var]] and so on' => 'some text and so on');
35
36# 4,5
37&test_both('some [[ $nested[[$var]] ]] flambe' => 'some coconuts flambe');
38
39# 6,7
40&test_both('[[$var]]' => 'text');
41
42# 8,9
43&test_both('[[ $var ]]' => 'text');
44
45# 10,11
46&test_both('an example of [[$var]] and [[$more_var]] together' =>
47             'an example of text and donuts together');
48
49# 12,13
50&test_both('some [[$[[$var2]][[$var]]]] and some \\[[ text \\]]' =>
51             'some coconuts and some [[ text ]]');
52
53# 14,15
54&test_both('some [[$[[$var2]][[$var]]]] and some [[ $text\\]] ]]' =>
55             'some coconuts and some garbage');
56
57# 16,17
58&test_both('some [[&func1()]]?' => 'some snails?');
59
60# 18,19
61&test_both('some [[&func2(star,studded)]] SNAILS?' => 'some STAR*STUDDED SNAILS?');
62
63# 20,21
64&test_both('Pi is about [[&add_numbers(3,.1,.04,.001,.0006)]]' => 'Pi is about 3.1416');
65
66# 22,23,24
67{
68	my $t = new Text::FillIn('some [[$var} unbalanced');
69	$t->Rdelim('}');
70	&should_equal($t->interpret(), 'some text unbalanced');
71
72	$t = new Text::FillIn('some {$var]] unbalanced');
73	$t->Ldelim('{');
74	&should_equal($t->interpret(), 'some text unbalanced');
75
76	Text::FillIn->Ldelim('(');
77	$t = new Text::FillIn('some ($var]] unbalanced');
78	&should_equal($t->interpret(), 'some text unbalanced');
79	Text::FillIn->Ldelim('[[');
80	Text::FillIn->Rdelim(']]');
81}
82
83# 25
84{
85	my $obj = bless {thing=>5}, 'MyPack';
86	sub MyPack::find_value { my $s = shift; $s->{shift()} }
87
88	my $t = new Text::FillIn('some [[$thing]] is 5');
89	$t->object($obj);
90	&should_equal($t->interpret(), 'some 5 is 5');
91}
92
93###################################################################
94
95sub test_both {
96   &test_interpret(@_);
97   &test_print(@_);
98}
99
100sub test_interpret {
101   my ($raw_text, $cooked_text) = @_;
102   my $result = Text::FillIn->new($raw_text)->interpret();
103
104   &should_equal($result, $cooked_text);
105}
106
107sub test_print {
108   my $debug = 0;
109   my ($raw_text, $cooked_text) = @_;
110   my $template = new Text::FillIn($raw_text);
111   my $file = 'output_test';
112
113   open (TEMP, ">$file") or die "Couldn't create $file: $!";
114   my $prev_select = select TEMP;
115   $template->interpret_and_print();
116   close TEMP;
117   select $prev_select;
118
119   my $result = `cat $file`;
120   unlink $file or die $!;
121
122   &should_equal($result, $cooked_text);
123}
124
125sub should_equal {
126	my ($one, $two) = @_;
127	&report($one eq $two);
128	if ($one ne $two  and  $ENV{TEST_VERBOSE}) {
129		print STDERR "$one =/= $two\n";
130	}
131}
132
133sub TExport::func1 {
134   return "snails";
135}
136
137sub TExport::func2 {
138   return join '*', map {uc} @_;
139}
140
141sub TExport::add_numbers {
142  my $result;
143  foreach (@_) {
144     $result += $_;
145  }
146  return $result;
147}
148
149