root/branches/boomer/t/08-util.t @ 1098

Revision 1098, 6.2 kB (checked in by hachi, 2 years ago)

Branching for boomer from release-19, rev 62318

  • Property svn:mime-type set to text/plain
  • Property svn:keywords set to Author Date Id Revision
Line 
1# $Id$
2
3use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib';
4use Test;
5use MT;
6use MT::Util qw( encode_html decode_html wday_from_ts format_ts dirify
7                 convert_high_ascii encode_xml decode_xml substr_wref
8                 trim ltrim rtrim );
9use MT::I18N qw( encode_text );
10use strict;
11
12my $mt = MT->new;
13$mt->config('NoHTMLEntities', 1);
14
15BEGIN { plan tests => 92 };
16
17ok(substr_wref("Sabado", 0, 3), "Sab"); #1
18ok(substr_wref("Sàbado", 0, 3), "Sàb"); #2
19ok(substr_wref("Sàbado", 0, 6), "Sàbado"); #3
20
21ok(convert_high_ascii("\xd8"), 'O'); #4
22ok(convert_high_ascii("Febr\xf9ary"), 'February'); #5
23
24my $str = '';
25for (my $i = 0; $i < 256; $i++) {
26    $str .= chr($i);
27}
28$mt->config('PublishCharset', 'iso-8859-1');
29ok(dirify($str), '_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyzaaaaaaaeceeeeiiiinoooooouuuuyssaaaaaaaeceeeeiiiinoooooouuuuyy'); #6
30$mt->config('PublishCharset', 'utf-8');
31use bytes;
32my $utf8_str = q{ÀÁÂÃÄÅĀĄĂÆÇĆČĈĊĎĐÈÉÊËĒĘĚĔĖĜĞĠĢĀĊÌÍÎÏĪĚĬĮİIJĎĶŁĜĹĻĿÑŃŇŅŊÒÓÔÕÖØŌŐŎŒŔŘŖŚŠŞŜȘŀŢŊȚÙÚÛÜŪŮŰŬŚŲŎÝŶŞŹŜŻàáâãÀåāąăÊçćčĉċďđÚéêëēęěĕėƒĝğġģĥħìíîïīĩĭįıijĵķĞłğĺČŀñńňņʼnŋòóÎõöÞōőŏœŕřŗśšşŝșťţŧțùúûÌūůűŭũųŵÜÿŷşŌźÞßßſÐð};
33ok(dirify($utf8_str), 'aaaaaaaaaaecccccddeeeeeeeeegggghhiiiiiiiiiijjklllllnnnnnooooooooooerrrsssssttttuuuuuuuuuuwyyyzzzaaaaaaaaaaecccccddeeeeeeeeefgggghhiiiiiiiiiijjkklllllnnnnnnooooooooooerrrsssssttttuuuuuuuuuuwyyyzzzss'); #7
34
35my $ts = '19770908153005';
36ok(format_ts('%a', $ts), 'Thu'); #8
37ok(format_ts('%A', $ts), 'Thursday'); #9
38ok(format_ts('%b', $ts), 'Sep'); #10
39ok(format_ts('%B', $ts), 'September'); #11
40ok(format_ts('%d', $ts), '08'); #12
41ok(format_ts('%e', $ts), ' 8'); #13
42ok(format_ts('%H', $ts), '15'); #14
43ok(format_ts('%I', $ts), '03'); #15
44ok(format_ts('%j', $ts), '251'); #16
45ok(format_ts('%k', $ts), '15'); #17
46ok(format_ts('%l', $ts), ' 3'); #18
47ok(format_ts('%m', $ts), '09'); #19
48ok(format_ts('%M', $ts), '30'); #20
49ok(format_ts('%p', $ts), 'PM'); #21
50ok(format_ts('%S', $ts), '05'); #22
51ok(format_ts('%x', $ts), 'September  8, 1977'); #23
52ok(format_ts('%X', $ts), ' 3:30 PM'); #24
53ok(format_ts('%y', $ts), '77'); #25
54ok(format_ts('%Y', $ts), '1977'); #26
55
56ok(encode_html('<foo>'), '&lt;foo&gt;'); #27
57ok(encode_html('&gt;'), '&gt;'); #28
58ok(encode_html('&gt;', 1), '&amp;gt;'); #29
59ok(encode_html("foo & bar &baz"), "foo &amp; bar &amp;baz"); #30
60ok(decode_html(encode_html('<foo>')), '<foo>'); #31
61ok(encode_html(), ''); #32
62ok(encode_html("&lt;"), "&lt;"); #33
63ok(encode_html("&#x192;"), "&#x192;"); #34
64ok(encode_html("&#X192;"), "&#X192;"); #35
65ok(encode_html("&#192;"), "&#192;"); #36
66ok(encode_html('"'), '&quot;'); #37
67ok(encode_html('&'), '&amp;'); #38
68ok(encode_html('>'), '&gt;'); #39
69ok(encode_html('<'), '&lt;'); #40
70ok(encode_html("<foo>\cM\n"), "&lt;foo&gt;\n"); #41
71
72ok(wday_from_ts(1964,1,3) == 5); #42
73ok(wday_from_ts(1995,11,13) == 1); #43
74ok(wday_from_ts(1995,11,14) == 2); #44
75ok(wday_from_ts(1995,11,15) == 3); #45
76ok(wday_from_ts(1995,11,16) == 4); #46
77ok(wday_from_ts(1995,11,17) == 5); #47
78ok(wday_from_ts(1995,11,18) == 6); #48
79ok(wday_from_ts(1995,11,19) == 0); #49
80ok(wday_from_ts(1995,11,20) == 1); #50
81ok(wday_from_ts(1995,2,28) == 2); #51
82ok(wday_from_ts(1946,12,26) == 4); #52
83
84my %xml_tests = (
85    'foo' => 'foo', #53 #54 #55
86    'x < y' => 'x &lt; y', #56 #57 #58
87    'foo & bar' => 'foo &amp; bar', #59 #60 #61
88    'foo\'s bar' => 'foo&apos;s bar', #62 #63 #64
89    '<title>my title</title>' => #65 #66 #67 #68
90        [ '<![CDATA[<title>my title</title>]]>',
91          '&lt;title&gt;my title&lt;/title&gt;', ],
92    '<foo>]]>' => #69 #70 #71 #72
93        [ '<![CDATA[<foo>]]&gt;]]>',
94          '&lt;foo&gt;]]&gt;', ],
95    'x &lt; y' => #73 #74 #75 #76
96        [ '<![CDATA[x &lt; y]]>',
97          'x &amp;lt; y', ],
98    'foob&aacute;r' => #77 #78 #79 #80
99        [ '<![CDATA[foob&aacute;r]]>',
100          'foob&amp;aacute;r', ],
101);
102 
103for my $test (keys %xml_tests) {
104    if (ref($xml_tests{$test}) eq 'ARRAY') {
105        ok(encode_xml($test), $xml_tests{$test}[0]); #65 #69 #73 #77
106        ok(decode_xml($xml_tests{$test}[0]), $test); #66 #70 #74 #78
107        ok(decode_xml(encode_xml($test)), $test); #67 #71 #75 #79
108        MT::ConfigMgr->instance->NoCDATA(1);
109        ok(encode_xml($test), $xml_tests{$test}[1]); #68 #72 #76 #80
110        MT::ConfigMgr->instance->NoCDATA(0);
111    } else {
112        ok(encode_xml($test), $xml_tests{$test}); #53 #56 #59 #62
113        ok(decode_xml($xml_tests{$test}), $test); #54 #57 #60 #63
114        ok(decode_xml(encode_xml($test)), $test); #55 #58 #61 #64
115    }
116}
117
118### tests for trim
119ok(ltrim(' sunday'), 'sunday'); #81
120ok(ltrim('  sunday monday'), 'sunday monday'); #82
121ok(ltrim(' sunday monday tuesday '), 'sunday monday tuesday '); #83
122ok(ltrim('sunday'), 'sunday'); #84
123ok(rtrim('sunday'), 'sunday'); #85
124ok(rtrim('sunday '), 'sunday'); #86
125ok(rtrim(' sunday monday '), ' sunday monday'); #87
126ok(rtrim('sunday monday tuesday  '), 'sunday monday tuesday'); #88
127ok(trim('sunday'), 'sunday'); #89
128ok(trim(' sunday'), 'sunday'); #90
129ok(trim(' sunday '), 'sunday'); #91
130ok(trim(' sunday monday '), 'sunday monday'); #92
131
132
133=pod
134
135my %dates = (
136          '20021224103045'            => '20021224T10:30:45',
137          '20021224T10:30:45'         => '20021224T10:30:45',
138          '20021224T103045Z'          => '20021224T02:30:45',
139          '20021224T10:30:45-0000'    => '20021224T02:30:45',
140          '20021224T10:30:45+0030'    => '20021224T02:00:45',
141          '2002-12-24T103045+02'      => '20021224T00:30:45',
142          '2002-12-24T10:30:45-08:00' => '20021224T10:30:45',
143          '20020615103045'            => '20020615T10:30:45',
144          '20020615103045-08'         => '20020615T11:30:45',
145          '20020615103045+02'         => '20020615T01:30:45',
146          );
147for my $date (keys %dates) {
148    ok(parse_iso8601_date($date, -8, 'gmtime'), $dates{$date});
149   #for my $tz ((":America/Los_Angeles", ":Europe/Helsinki")) {
150   #    $ENV{"TZ"} = $tz;
151   #    for my $lt (("localtime", "gmtime")) {
152   #        my $fmt = $lt eq "localtime" ? " %18s " : "%20s";
153   #        printf "%-25s -> $fmt %s\n", $i, parseISO8601Date($i, $lt), $tz;
154   #    }
155   #}
156}
157
158=cut
Note: See TracBrowser for help on using the browser.