Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 200 lines (158 sloc) 5.296 kB
0de0538 @abw * added map.t and math.t
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/map.t
4 #
5 # Test the Template::Map module.
6 #
7 # Written by Andy Wardley <abw@kfs.org>
8 #
9 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
10 #
11 # This is free software; you can redistribute it and/or modify it
12 # under the same terms as Perl itself.
13 #
14 # $Id$
15 #
16 #========================================================================
17
18 use strict;
19 use lib qw( ./lib ../lib );
d10da64 @abw * additional tests for Template::Map
authored
20 use Test::More tests => 28;
0de0538 @abw * added map.t and math.t
authored
21 $^W = 1;
22
23 # just testing
24 ok(1);
25
26 use Template::Map;
27
28 #$Template::Map::DEBUG = 1;
29 #$Template::Test::DEBUG = 0;
30 #$Template::Parser::DEBUG = 1;
31 #$Template::Directive::PRETTY = 1;
c942c1b @abw Version 2.15
authored
32 #$Template::Test::PRESERVE = 1;
0de0538 @abw * added map.t and math.t
authored
33
d10da64 @abw * additional tests for Template::Map
authored
34 #------------------------------------------------------------------------
35 package Some::Kind::Of::Noodle;
36 sub new { bless { }, $_[0] }
37 sub TT_name { return 'noodle' }
38 #------------------------------------------------------------------------
0de0538 @abw * added map.t and math.t
authored
39
d10da64 @abw * additional tests for Template::Map
authored
40 package main;
0de0538 @abw * added map.t and math.t
authored
41
d10da64 @abw * additional tests for Template::Map
authored
42 my $pkg = 'Template::Map';
43 my $map = $pkg->new() || die $pkg->error();
44 ok( $map, 'created a default map' );
45 is( $map->name('foo'), 'text', "'foo' is text" );
46 is( $map->name(['foo']), 'list', "['foo'] is list" );
47 is( $map->name({ foo => 'bar' }), 'hash', "{ foo => 'bar' } is hash" );
48 is( $map->name(bless { }, 'Thingy'), 'Thingy', 'Thingy is a Thingy' );
49 is( $map->name(Some::Kind::Of::Noodle->new()), 'noodle', 'some noodles' );
50
51
52 $map = $pkg->new( map => { TEXT => 'string',
53 ARRAY => 'array',
54 'Some::Kind::Of::Noodle' => 'egg_noodle',
55 'Foo::Bar' => 'fubar' } ) || die $pkg->error();
56
57 ok( $map, 'created a custom map' );
58 is( $map->name('foo'), 'string', "'foo' is string" );
59 is( $map->name(['foo']), 'array', "['foo'] is array" );
60 is( $map->name({ foo => 'bar' }), 'hash', "{ foo => 'bar' } is still a hash" );
61 is( $map->name(bless { }, 'Thingy'), 'Thingy', 'Thingy is a Thingy' );
62 is( $map->name(bless { }, 'Foo::Bar'), 'fubar', 'Foo::Bar is fubar' );
63 is( $map->name(Some::Kind::Of::Noodle->new()), 'egg_noodle', 'egg noodle' );
64
65
66 my $names = $map->names('foo') || die $map->error();
67 ok( $names, 'got names for foo' );
68 is( ref $names, 'ARRAY', 'an array' );
69 is( scalar @$names, 1, 'one item in array' );
70 is( $names->[0], 'foo', 'name is foo' );
71
72 $map = $pkg->new( format => 'x/%s.tt' );
73 $names = $map->names('foo') || die $map->error();
74 ok( $names, 'got format names for foo' );
75 is( $names->[0], 'x/foo.tt', 'first name is x/foo.tt' );
76
77 $map = $pkg->new( format => [ 'x/%s.tt', 'y/%s.tt' ] );
78 $names = $map->names('bar') || die $map->error();
79 ok( $names, 'got names for bar' );
80 is( $names->[0], 'x/bar.tt', 'first name is x/bar.tt' );
81 is( $names->[1], 'y/bar.tt', 'second name is y/bar.tt' );
82
83 $map = $pkg->new( prefix => 'z/', suffix => '.tt' );
84 $names = $map->names('baz') || die $map->error();
85 ok( $names, 'got names for baz' );
86 is( $names->[0], 'z/baz.tt', 'name is z/baz.tt' );
87
88 $map = $pkg->new( default => 'pong' );
89 $names = $map->names('ping') || die $map->error();
90 ok( $names, 'got names for ping' );
91 is( $names->[0], 'ping', 'ping' );
92 is( $names->[1], 'pong', 'pong' );
0de0538 @abw * added map.t and math.t
authored
93
d10da64 @abw * additional tests for Template::Map
authored
94 __END__
0de0538 @abw * added map.t and math.t
authored
95
96 $map = Template::Map->new( default => 'bar' );
97 assert( $map );
98
99 $res = $map->map('foo');
100 assert( $res );
101 assert( ref $res eq 'ARRAY' );
102 match( scalar @$res, 2 );
103 match( $res->[0], 'foo' );
104 match( $res->[1], 'bar' );
105
106
107 $map = Template::Map->new( prefix => 'my/', default => 'bar' );
108 assert( $map );
109
110 $res = $map->map('foo');
111 assert( $res );
112 assert( ref $res eq 'ARRAY' );
113 match( scalar @$res, 2 );
114 match( $res->[0], 'my/foo' );
115 match( $res->[1], 'bar' );
116
117
118 $map = Template::Map->new( {
119 prefix => 'my/',
120 suffix => '.tt2',
121 default => 'bar',
122 } );
123 assert( $map );
124
125 $res = $map->map('foo');
126 assert( $res );
127 assert( ref $res eq 'ARRAY' );
128 match( scalar @$res, 2 );
129 match( $res->[0], 'my/foo.tt2' );
130 match( $res->[1], 'bar' );
131
132
133 $map = Template::Map->new( {
134 format => 'try/%s/first',
135 prefix => 'my/',
136 suffix => '.tt2',
137 default => 'bar',
138 } );
139 assert( $map );
140
141 $res = $map->map('foo');
142 assert( $res );
143 assert( ref $res eq 'ARRAY' );
144 match( scalar @$res, 3 );
145 match( $res->[0], 'try/foo/first' );
146 match( $res->[1], 'my/foo.tt2' );
147 match( $res->[2], 'bar' );
148
149
150 $map = Template::Map->new( {
151 format => [ 'try/%s/first', 'then/%s/next' ],
152 prefix => 'my/',
153 suffix => '.tt2',
154 default => 'bar',
155 } );
156 assert( $map );
157
158 $res = $map->map('foo');
159 assert( $res );
160 assert( ref $res eq 'ARRAY' );
161 match( scalar @$res, 4 );
162 match( $res->[0], 'try/foo/first' );
163 match( $res->[1], 'then/foo/next' );
164 match( $res->[2], 'my/foo.tt2' );
165 match( $res->[3], 'bar' );
166
167
168 $map = Template::Map->new( {
169 format => [ 'zeroth', 'try/%s/first', 'then/%s/next' ],
170 default => 'bar',
171 } );
172 assert( $map );
173
174 $res = $map->map('foo');
175 assert( $res );
176 assert( ref $res eq 'ARRAY' );
177 match( scalar @$res, 5 );
178 match( $res->[0], 'zeroth' );
179 match( $res->[1], 'try/foo/first' );
180 match( $res->[2], 'then/foo/next' );
181 match( $res->[3], 'foo' );
182 match( $res->[4], 'bar' );
183
184 match( $map->name('foo'), 'foo' );
185 match( $map->name([]), 'list' );
186 match( $map->name({}), 'hash' );
187 match( $map->name(bless {}, 'Foo::Bar'), 'Foo_Bar' );
188
189 package Foo::Bar::Baz;
190
191 sub TT_name {
192 return 'floozy';
193 }
194
195 package main;
196
197 match( $map->name(bless {}, 'Foo::Bar::Baz'), 'floozy' );
198
199
Something went wrong with that request. Please try again.