summaryrefslogtreecommitdiff
path: root/tests/it.t
blob: 67160d0d9f4d3ac2500da2d106f454ee1cf1cd69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/www/server/bin/php -qC
<?php

# Tests for it.class

function match( $regex, $string, $exp, $name )
{
	$GLOBALS['TEST_MORE_LEVEL'] = 1;
	$pass = is( it::match( $regex, $string ), $exp, $name );
	if( !$pass ) {
		diag( "        regex given: $regex" );
		diag( "    regex converted: " . it::convertregex( $regex ) );
	} 
	$GLOBALS['TEST_MORE_LEVEL'] = 0;
}

match(
	'b', 'aaaabaaaa',
	'b',
	'simple regex'
	);
match(
	'a/b', '   a/b   ',
	'a/b',
	'regex with /'
);
match(
	'aa(bb)aa(cc)aa(dd)qq', 'aabbaaccaaddqq',
	array( 'bb', 'cc', 'dd' ),
	'return array of captures'
	);
match(
	'\bblah\b', ' blah ',
	'blah',
	'match \b at spaces'
	);
match(
	'\bblah\b', 'blah',
	'blah',
	'match \b at end of string'
	);
match(
	'\bblah\b', 'ablahc',
	false,
	'don\'t match \b at word chars'
	);
match(
	'\bblah\b', 'Üblahä',
	false,
	'don\'t match \b at umlaute in latin1'
	);
match(
	'\Bblah\B', ' blah ',
	false,
	'don\'t match \B at spaces'
	);
match(
	'\Bblah\B', 'blah',
	false,
	'don\'t match \B at end of string'
	);
match(
	'\Bblah\B', 'ablahc',
	'blah',
	'match \B at word chars'
	);
match(
	'\Bblah\B', 'Üblahä',
	'blah',
	'match \B at umlaute in latin1'
	);
match(
	'\w+', '  |#Üblahä   ',
	'Üblahä',
	'include umlaute in \w'
	);
match(
	'[[:alpha:]]+', '  |#blahä   ',
	'blahä',
	'include umlaute in [[:alpha:]]'
	);
match(
	'\W+', '  |#Üblahä  ',
	'  |#',
	'don\'t include umlaute in \W'
	);
match(
	'\ba', 'äa',
	'',
	'\b must know umlauts'
	);

eval( '$escapedwordregex = "' . it::convertregex( '\w' ) . '";' );
$escapedwordregex = preg_replace( '|[\\\\/]|', '', $escapedwordregex );

match(
	'\\\\w+',  $escapedwordregex,
	false,
	'don\'t parse \w in \\\\w at beginning (no match)'
	);
match(
	'aaa\\\\w+', '   aaa\www  ',
	'aaa\www',
	'don\'t parse \w in \\\\w at beginning (match)'
	);
match(
	'aaa\\\\w+', 'aaa' . $escapedwordregex,
	false,
	'don\'t parse \w in \\\\w after chars (no match)'
	);
match(
	'aaa\\\\w+', '   aaa\www  ',
	'aaa\www',
	'don\'t parse \w in \\\\w after chars (match)'
	);
match(
	'\\\\\\\\w+', '\\' . $escapedwordregex,
	false,
	'don\'t parse \w in \\\\\\\w (no match)'
	);
match(
	'\\\\\\\\w+', '  \\\\www  ',
	'\\\\www',
	'don\'t parse \\\\\\\\w as \w (match)'
	);
match(
	'[\w]+', '[[[]]]---',
	false,
	'replace \w in [\w] correctly (no match)'
	);
match(
	'[\w]+', '  \\\\aword[[[]]]   ',
	'aword',
	'replace \w in [\w] correctly (match)'
	);
match(
	'[\\\\w]+', ' blabergna ',
	false,
	'don\'t parse \w in [\\\\w] (no match)'
	);
match(
	'[\\\\w]+', '  \\\\worda[[[]',
	'\\\\w',
	'don\'t parse \w in [\\\\w] (match)'
	);
match(
	'[a\W]+', 'bbbbbbb a a%$+ accccc',
	' a a%$+ a',
	'\W in []'
	);
match(
	'\\\\\\w+', '  \Üblahä  ',
	'\Üblahä',
	'parse \w in \\\\\\w at beginning'
	);
match(
	'aaa\\\\\\w+', '  aaa\Üblahä  ',
	'aaa\Üblahä',
	'parse \w in \\\\\\w after chars'
	);
is(
	it::replace(
		array(
			'regex1' => 'repl1',
			'regex2' => 'repl2',
			'regex3' => 'repl3' ),
		'regex2 regex1 regex3' ),
	'repl2 repl1 repl3',
	'test tr regex function'
	);
is(
	it::match( '\w+', 'word1 wörd2 word_3', array('all' => true )),
	array( 'word1', 'wörd2', 'word_3' ),
	"test match_all function"
	);
match(
	'aBcD', '  aBcD  ',
	'aBcD',
	"caseinsensitive is default"
	);
match(
	'ö', 'Ö',
	'Ö',
	'match umlaute in latin1 case insensitive'
	);
is(
	it::match( 'abc', "aBc", array('casesensitive' => 1 )),
	false,
	"set case sensitivity by parameter"
	);

is(
	it::match( '\w+', 'word1 wörd2 word_3', array('all' => 1 )),
	array( 'word1', 'wörd2', 'word_3' ),
	"test all=>1 without captures"
	);
is(
	it::match( '\w+\s+(\d+)', 'word1 12 wörd2 3 word_3 4', array('all' => 1 )),
	array( '12', '3', '4' ),
	"test all=>1 with one capture"
	);
is(
	it::match( '(\w+)\s+(\d+)', 'word1 12 wörd2 3 word_3 4', array('all' => 1 )),
	array( array( 'word1', '12' ), array( 'wörd2', '3' ), array( 'word_3', '4' ) ),
	"test all=>1 with captures"
	);
is(
	it::match( '(\w+)\s+(\d+)', 'word1 12 wörd2 3 word_3 4', array('all' => 1, 'pattern_order' => 1 )),
	array( array( 'word1', 'wörd2', 'word_3' ), array( '12', '3', '4' ) ),
	"test all=>1,pattern_order=>1"
	);

is(it::replace(array('a' => "1", 'b' => "2"), "ab"), "12");
is(it::replace(array('!' => "x"), "!"), "x");
is(it::replace(array('\w' => "x"), "oö"), "xx");
is(it::replace(array('[[:alpha:]]' => "x"), "ö"), "x");
is(it::replace(array('\w' => "x", '#' => "!"), "#ö"), "!x");
is(it::replace(array('#' => "!", '\w' => "x"), "#ö"), "!x");
is(it::replace(array('ö' => "x"), "Ö"), "x");
is(it::replace(array('a' => "1"), "aaa", array('limit' => 1)), "1aa");

# it::filter_keys tests

$data = array('a' => 1, 'b' => 2, 'c' => 3);
is(it::filter_keys($data, 'a'),             array('a' => 1),           "select one key");
is(it::filter_keys($data, array('a', 'b')), array('a' => 1, 'b' => 2), "select two keys with array");
is(it::filter_keys($data, 'a,b'),           array('a' => 1, 'b' => 2), "select two keys with string");
is(
	array_keys(it::filter_keys($data, 'b,a')),
	array('a', 'b'),
	"keep order of data array per default");
is(
	array_keys(it::filter_keys($data, 'b,a', array('reorder' => true))),
	array('b', 'a'),
	"reorder with given key order");

# it::date tests

is(it::date('date', '2011-10-25'), '25.10.2011', 'parse date string with strtotime');
is(it::date('date', '2011-10-25 + 3 days'), '28.10.2011', 'some date arithmetic');
is(it::date('datetime', time()), it::date('datetime'), 'recognize int as timestamp');
is(it::date('datetime', time()*1.0), it::date('datetime'), 'recognize float as timestamp');
is(it::date('datetime', time() . ''), it::date('datetime'), 'recognize digit string as timestamp');
is(it::date('datetime', '@' . time()), it::date('datetime'), 'recognize strtotime timestamp format');
is(it::date('datetime', 10), it::date('datetime', "10"), 'numeric and string give same result');
is(it::date('datetime', 10.0), it::date('datetime', "10"), '... as long as num is properly truncated');
is(it::date('datetime', 10.5), it::date('datetime', "10"), '... with one digit after point');
is(it::date('datetime', 10.56), it::date('datetime', "10"), '... with two digits after point');
is(it::date('datetime', 1000000), it::date('datetime', "1000000"), '... large nummer');
is(it::date('datetime', 1000000.543), it::date('datetime', "1000000"), '... large nummer and point');
is(it::date('time', "10.5"), "10:05", 'interpret string with points with strtotime');
is(it::date('time', "10.05"), "10:05", 'interpret string with points with strtotime');

?>