|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Test - string func |
|
4
|
|
|
* |
|
5
|
|
|
* @package fwolflib |
|
6
|
|
|
* @subpackage func.test |
|
7
|
|
|
* @copyright Copyright 2004-2012, Fwolf |
|
8
|
|
|
* @author Fwolf <[email protected]> |
|
9
|
|
|
* @since 2008-05-08 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Define like this, so test can run both under eclipse and web alone. |
|
13
|
|
|
// {{{ |
|
14
|
|
|
if (! defined('SIMPLE_TEST')) { |
|
15
|
|
|
define('SIMPLE_TEST', 'simpletest/'); |
|
16
|
|
|
require_once(SIMPLE_TEST . 'autorun.php'); |
|
17
|
|
|
} |
|
18
|
|
|
// Then set output encoding |
|
19
|
|
|
//header('Content-Type: text/html; charset=utf-8'); |
|
20
|
|
|
// }}} |
|
21
|
|
|
|
|
22
|
|
|
// Require library define file which need test |
|
23
|
|
|
require_once('fwolflib/func/ecl.php'); |
|
24
|
|
|
require_once('fwolflib/func/request.php'); |
|
25
|
|
|
require_once('fwolflib/func/string.php'); |
|
26
|
|
|
|
|
27
|
|
|
class TestFuncString extends UnitTestCase { |
|
28
|
|
|
|
|
29
|
|
|
function TestAddslashesRecursive () { |
|
|
|
|
|
|
30
|
|
|
$x = array( |
|
31
|
|
|
"It's 1.", |
|
32
|
|
|
"It's 2." => "It's 3.", |
|
33
|
|
|
2012, |
|
34
|
|
|
"It's 4." => array( |
|
35
|
|
|
"It's 5." => array( |
|
36
|
|
|
"It's 6." => "It's 7.", |
|
37
|
|
|
), |
|
38
|
|
|
'end', |
|
39
|
|
|
), |
|
40
|
|
|
); |
|
41
|
|
|
$y = array( |
|
42
|
|
|
"It\\'s 1.", |
|
43
|
|
|
"It\\'s 2." => "It\\'s 3.", |
|
44
|
|
|
2012, |
|
45
|
|
|
"It\\'s 4." => array( |
|
46
|
|
|
"It\\'s 5." => array( |
|
47
|
|
|
"It\\'s 6." => "It\\'s 7.", |
|
48
|
|
|
), |
|
49
|
|
|
"end", |
|
50
|
|
|
), |
|
51
|
|
|
); |
|
52
|
|
|
$this->assertEqual($y, AddslashesRecursive($x)); |
|
|
|
|
|
|
53
|
|
|
} // end of func TestAddslashesRecursive |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
function TestJsonEncodeHex () { |
|
|
|
|
|
|
57
|
|
|
$x = true; |
|
58
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
59
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
60
|
|
|
|
|
61
|
|
|
$x = 3.86; |
|
62
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
63
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
64
|
|
|
|
|
65
|
|
|
$x = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); |
|
66
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
67
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
68
|
|
|
|
|
69
|
|
|
$x = array(5 => 'a',4 => 'b', 3 => 'c'); |
|
70
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
71
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
72
|
|
|
|
|
73
|
|
|
$x = array(1 => 'a',2 => 'b', 3 => 'c'); |
|
74
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
75
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
76
|
|
|
|
|
77
|
|
|
$x = array('<foo>',"'bar'",'"baz"','&blong&', true); |
|
78
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
79
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
80
|
|
|
|
|
81
|
|
|
$x = array('<foo>' => "'bar'",'"baz"' => '&blong&', 3.86); |
|
82
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
83
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
84
|
|
|
|
|
85
|
|
|
$x = array('<foo>' => "'bar'",'"baz"' => '&blong&', 2 => 'a我d'); |
|
86
|
|
|
$y = JsonEncodeHex($x); |
|
|
|
|
|
|
87
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
88
|
|
|
|
|
89
|
|
|
} // end of func TestJsonEncodeHex |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
function TestJsonEncodeUnicode () { |
|
|
|
|
|
|
93
|
|
|
$x = array('中文', array('中' => '文')); |
|
94
|
|
|
$y = '["中文",{"中":"文"}]'; |
|
95
|
|
|
$this->assertEqual($y, JsonEncodeUnicode($x)); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
$this->assertEqual($x, json_decode($y, true)); |
|
98
|
|
|
} // end of func TestJsonEncodeUnicode |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
function TestMatchWildcard () { |
|
|
|
|
|
|
102
|
|
|
$s = 'abcdefg'; |
|
103
|
|
|
$this->assertEqual(true, MatchWildcard($s, 'a*e?g')); |
|
|
|
|
|
|
104
|
|
|
$this->assertEqual(true, MatchWildcard($s, '?b*e*')); |
|
|
|
|
|
|
105
|
|
|
$this->assertEqual(false, MatchWildcard($s, '?b*e?')); |
|
|
|
|
|
|
106
|
|
|
$s = 'abc'; |
|
107
|
|
|
$this->assertEqual(true, MatchWildcard($s, 'a*')); |
|
|
|
|
|
|
108
|
|
|
} // end of func TestMatchWildcard |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
function TestOrgCodeGen () { |
|
|
|
|
|
|
112
|
|
|
$x = 'D2143569'; |
|
113
|
|
|
$y = 'D2143569-X'; |
|
114
|
|
|
$this->assertEqual(OrgCodeGen($x), $y); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$x = 'd2143569'; |
|
117
|
|
|
$y = 'D2143569-X'; |
|
118
|
|
|
$this->assertEqual(OrgCodeGen($x), $y); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$y = OrgCodeGen($x); |
|
|
|
|
|
|
121
|
|
|
$x = substr($x, 0, 8); |
|
122
|
|
|
$this->assertEqual(OrgCodeGen($x), $y); |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
$x = 'D214356'; |
|
125
|
|
|
$y = ''; |
|
126
|
|
|
$this->assertEqual(OrgCodeGen($x), $y); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
$x = 'D214356-'; |
|
129
|
|
|
$y = ''; |
|
130
|
|
|
$this->assertEqual(OrgCodeGen($x), $y); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
} // end of func TestOrgCodeGen |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
function TestPin15To18() { |
|
|
|
|
|
|
136
|
|
|
$x = '340524800101001'; |
|
137
|
|
|
$y = '34052419800101001X'; |
|
138
|
|
|
$this->assertEqual(Pin15To18($x), $y); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
$x = '410881790605552'; |
|
141
|
|
|
$y = '410881197906055527'; |
|
142
|
|
|
$this->assertEqual(Pin15To18($x), $y); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
} // end of func TestPin15To18 |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
function TestRfc2047Decode() { |
|
|
|
|
|
|
148
|
|
|
$x = 'Re: =?utf-8?B?5aiB5Y6/55Sz5oql5Yi25bqm?='; |
|
149
|
|
|
$y = 'Re: 威县申报制度'; |
|
150
|
|
|
$this->assertEqual(Rfc2047Decode($x), $y); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
$x = '=?gbk?B?wLTX1HNqemxiekBzaW5hLmNvbbXE19S2r7vYuLQg?='; |
|
153
|
|
|
$y = '来自[email protected]的自动回复 '; // Without tailing ' ', will error. |
|
154
|
|
|
$this->assertEqual(Rfc2047Decode($x), $y); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
} // end of func TestRfc2047Decode |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
function TestStrToArray () { |
|
|
|
|
|
|
160
|
|
|
$x = ' blah '; |
|
161
|
|
|
$y = array('blah'); |
|
162
|
|
|
$y2 = array(' blah '); |
|
163
|
|
|
$this->assertEqual(StrToArray($x), $y); |
|
|
|
|
|
|
164
|
|
|
$this->assertEqual(StrToArray($x, '|', false), $y2); |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
$x = ', a, b, c , d , '; |
|
167
|
|
|
$this->assertEqual(StrToArray($x, ','), array('a', 'b', 'c', 'd')); |
|
|
|
|
|
|
168
|
|
|
$this->assertEqual(StrToArray($x, ' '), array(',', 'a,', 'b,', 'c', ',', 'd', ',')); |
|
|
|
|
|
|
169
|
|
|
$this->assertEqual(StrToArray($x, ',', true, false), |
|
|
|
|
|
|
170
|
|
|
array('', 'a', 'b', 'c', 'd', '')); |
|
171
|
|
|
$this->assertEqual(StrToArray($x, ',', false, true), |
|
|
|
|
|
|
172
|
|
|
array(' a', ' b', ' c ', ' d ', ' ')); |
|
173
|
|
|
} // end of func TestStrToArray |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
function TestSubstrIgnHtml() { |
|
|
|
|
|
|
177
|
|
|
$x = '测试12<4测试'; |
|
178
|
|
|
$x = SubstrIgnHtml($x, 11, '...'); |
|
|
|
|
|
|
179
|
|
|
$y = '测试12<4...'; |
|
180
|
|
|
$this->assertEqual($x, $y); |
|
181
|
|
|
|
|
182
|
|
|
$x = '测<b><i><br / >试</i></b>"<b>234测试</b>'; |
|
183
|
|
|
$x = SubstrIgnHtml($x, 9, '...'); |
|
|
|
|
|
|
184
|
|
|
$y = '测<b><i><br / >试</i></b>"<b>2...</b>'; |
|
185
|
|
|
$this->assertEqual($x, $y); |
|
186
|
|
|
|
|
187
|
|
|
$x = '`reStructuredText 中文示例 <?f=20101113-restructuredtext-example.rst>`_'; |
|
188
|
|
|
$y = SubstrIgnHtml($x, 71, ''); |
|
|
|
|
|
|
189
|
|
|
$this->assertEqual($x, $y); |
|
190
|
|
|
|
|
191
|
|
|
} // end of func TestSubstrIgnHtml |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
} // end of class TestFuncString |
|
195
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
// Change output charset in this way. |
|
198
|
|
|
// {{{ |
|
199
|
|
|
$s_url = GetSelfUrl(false); |
|
|
|
|
|
|
200
|
|
|
$s_url = substr($s_url, strrpos($s_url, '/') + 1); |
|
201
|
|
|
if ('string.test.php' == $s_url) { |
|
202
|
|
|
$test = new TestFuncString(); |
|
203
|
|
|
$test->run(new HtmlReporter('utf-8')); |
|
204
|
|
|
} |
|
205
|
|
|
// }}} |
|
206
|
|
|
?> |
|
|
|
|
|
|
207
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.