Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 264 lines (217 sloc) 6.606 kB
850d474 @abw *** empty log message ***
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/date.t
4 #
5 # Tests the 'Date' plugin.
6 #
82cbea8 @abw version 2.00
authored
7 # Written by Andy Wardley <abw@kfs.org>
850d474 @abw *** empty log message ***
authored
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 );
20 use Template;
21 use Template::Test;
22 use Template::Plugin::Date;
23 use POSIX;
24 $^W = 1;
25
f4df3b6 @abw approach v2.03
authored
26 eval "use Date::Calc";
27
28 my $got_date_calc = 0;
29 $got_date_calc++ unless $@;
30
31
850d474 @abw *** empty log message ***
authored
32 $Template::Test::DEBUG = 0;
33
34 my $format = {
35 'default' => $Template::Plugin::Date::FORMAT,
36 'time' => '%H:%M:%S',
37 'date' => '%d-%b-%Y',
38 'timeday' => 'the time is %H:%M:%S on %A',
39 };
40
41 my $time = time;
42 my @ltime = localtime($time);
43
44 my $params = {
45 time => $time,
46 format => $format,
47 timestr => &POSIX::strftime($format->{ time }, @ltime),
48 datestr => &POSIX::strftime($format->{ date }, @ltime),
49 daystr => &POSIX::strftime($format->{ timeday }, @ltime),
50 defstr => &POSIX::strftime($format->{ default }, @ltime),
51 now => sub {
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
52 &POSIX::strftime(shift || $format->{ default }, localtime(time));
850d474 @abw *** empty log message ***
authored
53 },
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
54 time_locale => \&time_locale,
55 date_locale => \&date_locale,
56 date_calc => $got_date_calc,
57 };
f231acb @abw * fixed t/date.t to look for locales with different file suffixes,
authored
58
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
59 sub time_locale {
60 my ($time, $format, $locale) = @_;
61 my $old_locale = &POSIX::setlocale(&POSIX::LC_ALL);
62
63 # some systems expect locales to have a particular suffix
64 for my $suffix ('', @Template::Plugin::Date::LOCALE_SUFFIX) {
65 my $try_locale = $locale.$suffix;
50600af @abw fixed undef warnings
authored
66 my $setlocale = &POSIX::setlocale(&POSIX::LC_ALL, $try_locale);
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
67 if (defined $setlocale && $try_locale eq $setlocale) {
68 $locale = $try_locale;
69 last;
f231acb @abw * fixed t/date.t to look for locales with different file suffixes,
authored
70 }
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
71 }
72 my $datestr = &POSIX::strftime($format, localtime($time));
73 &POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
74 return $datestr;
75 }
76
77 sub date_locale {
78 my ($time, $format, $locale) = @_;
79 my @date = (split(/(?:\/| |:|-)/, $time))[2,1,0,3..5];
80 return (undef, Template::Exception->new('date',
81 "bad time/date string: expects 'h:m:s d:m:y' got: '$time'"))
82 unless @date >= 6 && defined $date[5];
83 $date[4] -= 1; # correct month number 1-12 to range 0-11
84 $date[5] -= 1900; # convert absolute year to years since 1900
85 $time = &POSIX::mktime(@date);
86 return time_locale($time, $format, $locale);
87 }
88
850d474 @abw *** empty log message ***
authored
89
74c8840 @abw * added sleep 1 to tickle and bugs not exposed in the case of the script
authored
90 # force second to rollover so that we reliably see any tests failing.
91 # lesson learnt from 2.07b where I broke the Date plugin's handling of a
92 # 'time' parameter, but which didn't immediately come to light because the
93 # script could run before the second rolled over and not expose the bug
94
95 sleep 1;
96
850d474 @abw *** empty log message ***
authored
97 test_expect(\*DATA, { POST_CHOMP => 1 }, $params);
98
99
100 #------------------------------------------------------------------------
101 # test input
102 #
103 # NOTE: these tests check that the Date plugin is behaving as expected
104 # but don't attempt to validate that the output returned from strftime()
105 # is semantically correct. It's a closed loop (aka "vicious circle" :-)
106 # in which we compare what date.format() returns to what we get by
107 # calling strftime() directly. Despite this, we can rest assured that
108 # the plugin is correctly parsing the various parameters and passing
109 # them to strftime() as expected.
110 #------------------------------------------------------------------------
111
112 __DATA__
113 -- test --
114 [% USE date %]
115 Let's hope the year doesn't roll over in between calls to date.format()
116 and now()...
117 Year: [% date.format(format => '%Y') %]
118
119 -- expect --
120 -- process --
121 Let's hope the year doesn't roll over in between calls to date.format()
122 and now()...
123 Year: [% now('%Y') %]
124
125 -- test --
126 [% USE date(time => time) %]
127 default: [% date.format %]
128
129 -- expect --
130 -- process --
131 default: [% defstr %]
132
133 -- test --
134 [% USE date(time => time) %]
135 [% date.format(format => format.timeday) %]
136
137 -- expect --
138 -- process --
139 [% daystr %]
140
141 -- test --
142 [% USE date(time => time, format = format.date) %]
143 Date: [% date.format %]
144
145 -- expect --
146 -- process --
147 Date: [% datestr %]
148
149 -- test --
150 [% USE date(format = format.date) %]
151 Time: [% date.format(time, format.time) %]
152
153 -- expect --
154 -- process --
155 Time: [% timestr %]
156
157 -- test --
158 [% USE date(format = format.date) %]
159 Time: [% date.format(time, format = format.time) %]
160
161 -- expect --
162 -- process --
163 Time: [% timestr %]
164
165
166 -- test --
167 [% USE date(format = format.date) %]
168 Time: [% date.format(time = time, format = format.time) %]
169
170 -- expect --
171 -- process --
172 Time: [% timestr %]
173
174 -- test --
175 [% USE english = date(format => '%A', locale => 'en_GB') %]
176 [% USE french = date(format => '%A', locale => 'fr_FR') %]
177 In English, today's day is: [% english.format +%]
178 In French, today's day is: [% french.format +%]
179
180 -- expect --
181 -- process --
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
182 In English, today's day is: [% time_locale(time, '%A', 'en_GB') +%]
183 In French, today's day is: [% time_locale(time, '%A', 'fr_FR') +%]
850d474 @abw *** empty log message ***
authored
184
185 -- test --
186 [% USE english = date(format => '%A') %]
187 [% USE french = date() %]
188 In English, today's day is:
189 [%- english.format(locale => 'en_GB') +%]
190 In French, today's day is:
191 [%- french.format(format => '%A', locale => 'fr_FR') +%]
192
193 -- expect --
194 -- process --
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
195 In English, today's day is: [% time_locale(time, '%A', 'en_GB') +%]
196 In French, today's day is: [% time_locale(time, '%A', 'fr_FR') +%]
850d474 @abw *** empty log message ***
authored
197
198 -- test --
199 [% USE date %]
fea9b8d @abw Updated various tests while fixing bugs
authored
200 [% date.format('4:20:00 13-6-2000', '%H') %]
201 -- expect --
202 04
850d474 @abw *** empty log message ***
authored
203
fea9b8d @abw Updated various tests while fixing bugs
authored
204 -- test --
205 [% USE date %]
206 [% date.format('2000-6-13 4:20:00', '%H') %]
850d474 @abw *** empty log message ***
authored
207 -- expect --
208 04
209
210 -- test --
3b64ab5 @abw * fixed bug in Monday/Tuesday/Wednesday test
authored
211 -- name September 13th 2000 --
850d474 @abw *** empty log message ***
authored
212 [% USE day = date(format => '%A', locale => 'en_GB') %]
3b64ab5 @abw * fixed bug in Monday/Tuesday/Wednesday test
authored
213 [% day.format('4:20:00 13-9-2000') %]
850d474 @abw *** empty log message ***
authored
214
215 -- expect --
e64b054 @abw * applied patch from Axel to fix Tuesday
authored
216 -- process --
0cffbc2 @abw * added time_locale() and date_locale() to (hopefully) fix the bug in…
authored
217 [% date_locale('4:20:00 13-9-2000', '%A', 'en_GB') %]
850d474 @abw *** empty log message ***
authored
218
e6332cd @abw prep for version 2.12
authored
219
850d474 @abw *** empty log message ***
authored
220 -- test --
221 [% TRY %]
222 [% USE date %]
223 [% date.format('some stupid date') %]
224 [% CATCH date %]
225 Bad date: [% e.info %]
226 [% END %]
227 -- expect --
228 Bad date: bad time/date string: expects 'h:m:s d:m:y' got: 'some stupid date'
0b63aae @abw *** empty log message ***
authored
229
230 -- test --
231 [% USE date %]
d52480d @abw *** empty log message ***
authored
232 [% template.name %] [% date.format(template.modtime, format='%Y') %]
233 -- expect --
234 -- process --
235 input text [% now('%Y') %]
236
f4df3b6 @abw approach v2.03
authored
237 -- test --
238 [% IF date_calc -%]
239 [% USE date; calc = date.calc; calc.Monday_of_Week(22, 2001).join('/') %]
240 [% ELSE -%]
241 not testing
242 [% END -%]
243 -- expect --
244 -- process --
245 [% IF date_calc -%]
246 2001/5/28
247 [% ELSE -%]
248 not testing
249 [% END -%]
250
2a7f967 @abw *** empty log message ***
authored
251 -- test --
252 [% USE date;
253 date.format('12:59:00 30/09/2001', '%H:%M')
254 -%]
255 -- expect --
256 12:59
257
fea9b8d @abw Updated various tests while fixing bugs
authored
258 -- test --
259 [% USE date;
260 date.format('2001/09/30 12:59:00', '%H:%M')
261 -%]
262 -- expect --
263 12:59
Something went wrong with that request. Please try again.