File: test.pl

package info (click to toggle)
delimmatch 1.06a-4
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, lenny, squeeze, stretch, wheezy
  • size: 68 kB
  • ctags: 24
  • sloc: perl: 631; makefile: 2
file content (213 lines) | stat: -rw-r--r-- 5,751 bytes parent folder | download | duplicates (5)
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
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..47\n"; }
END {print "not ok 1\n" unless $loaded;}
use Text::DelimMatch;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

$mc = new Text::DelimMatch '"';

# test: simple delimited text, fast
&test (2, 'pre "match" post', 'pre ', '"match"', ' post');
&test (3, '"match" post', '', '"match"', ' post');
&test (4, 'pre "match"', 'pre ', '"match"', '');
&test (5, 'no match" post');
&test (6, 'pre "no match');

# test: simple delimited text, slow
$mc->slow(1);
&test (7, 'pre "match" post', 'pre ', '"match"', ' post');
&test (8, '"match" post', '', '"match"', ' post');
&test (9, 'pre "match"', 'pre ', '"match"', '');
&test (10, 'no match" post');
&test (11, 'pre "no match');

# test: delimited text, fast
$mc->slow(0);
$mc->delim("\\(", "\\)");

&test (12, 'pre (m(a(t)c)h) post', 'pre ', '(m(a(t)c)h)', ' post');
&test (13, '(m(a(t)c)h) post', '', '(m(a(t)c)h)', ' post');
&test (14, 'pre (m(a(t)c)h)', 'pre ', '(m(a(t)c)h)', '');
&test (15, '(no match post');
&test (16, 'no match) post');
&test (17, 'pre (no match');
&test (18, 'pre no match)');

# test: delimited text, slow
$mc->slow(1);
&test (19, 'pre (m(a(t)c)h) post', 'pre ', '(m(a(t)c)h)', ' post');
&test (20, '(m(a(t)c)h) post', '', '(m(a(t)c)h)', ' post');
&test (21, 'pre (m(a(t)c)h)', 'pre ', '(m(a(t)c)h)', '');
&test (22, '(no match post');
&test (23, 'no match) post');
&test (24, 'pre (no match');
&test (25, 'pre no match)');

# test: delimited text, skipping quotes
$mc->slow(0);
$mc->quote('"');

&test (26, 'pre "(no match)" "(no" "match)" (match) post', 
       'pre "(no match)" "(no" "match)" ', '(match)', ' post');
&test (27, '"(no match)" pre (match) post', 
       '"(no match)" pre ', '(match)', ' post');

# test: delimited text, complex quotes
$mc->quote();
$mc->quote('<!--', '-->');
&test (28, 'pre <!-- ( --> (match) post', 
       'pre <!-- ( --> ', '(match)', ' post');

# test: delimited text, escaped characters
$mc->quote();
$mc->escape('\\');
&test (29, 'pre \(no match) (match) post',
       'pre \(no match) ', '(match)', ' post');

&test (30, 'pre (match \) this) post',
       'pre ', '(match \) this)', ' post');

# test: delimited text, doubled quotes
$mc->quote ('"');
$mc->escape();
&test (31, 'pre "(no match)" (no match)" (match) post',
       'pre "(no match)" ', '(no match)', '" (match) post');

$mc->double_escape ('"');
&test (32, 'pre "(no match)"" (no match)" (match) post',
       'pre "(no match)"" (no match)" ', '(match)', ' post');

# test: a little of everything

$mc->escape('[;\\]');
&test (33, ';(no \(match)\) \(no match) \"(m(a\)tc)h)',
       ';(no \(match)\) \(no match) \"', '(m(a\)tc)h)', '');

&test (34, '"(no match) "" (no match) \""\((m"a)t"ch)',
       '"(no match) "" (no match) \""\(', '(m"a)t"ch)', '');

# test: repeated matching

&test (35, 'pre (match) (match2) post',
       'pre ', '(match)', ' (match2) post');

&test (36, undef,
       ' ', '(match2)', ' post');

# test: case sensitivity

$mc->delim('S', 'E');

&test (37, "pre SmastechE post", "pre ", "SmastechE", " post");
&test (38, "pre smaStEche post", "pre ", "smaStEche", " post");

$mc->case_sensitive(1);

&test (39, "snomatche SmastechE post", "snomatche ", "SmastechE", " post");
&test (40, "snomatche smaStEche post", "snomatche sma", "StE", "che post");

# test: backward compatability functions

if (&Text::DelimMatch::nested_match ("pre (match) ", "\\(", "\\)") eq "(match)") {
    print "ok 41\n";
} else {
    print "not ok 41\n";
}

if (&Text::DelimMatch::skip_nested_match (" (match)post", "\\(", "\\)") eq "post") {
    print "ok 42\n";
} else {
    print "not ok 42\n";
}

# test: turning off repeated matching

$mc->keep(0);

$mc->delim("\\(", "\\)");

&test (43, 'pre (match) (match2) post',
       'pre ', '(match)', ' (match2) post');

&test (44, undef);

# test: strip delimiters

$mc = new Text::DelimMatch '\(', '\)';
$mc->returndelim(1);

$match = $mc->match("test ((this is) a test) so there");

if ($match eq '((this is) a test)') {
    print "ok 45\n";
} else {
    print "not ok 45\n";
}

$mc->returndelim(0);

$match = $mc->match("test ((this is) a test) so there");

if ($match eq '(this is) a test') {
    print "ok 46\n";
} else {
    print "not ok 46 ($match)\n";
}

# test: multi-line matching with delimiter stripping

$match = $mc->match("test ((this is)\na test) so there");

if ($match eq "(this is)\na test") {
    print "ok 47\n";
} else {
    print "not ok 47 ($match)\n";
}

exit;

sub test {
    my ($tnum, $string, $okpre, $okmatch, $okpost) = @_;
    my ($pre, $match, $post) = $mc->match($string);

    # check this first, to avoid -w errors
    if (!defined($match) && !defined($okmatch)) {
	print "ok $tnum\n";
	return;
    }

#    print "1: ", defined($pre), " ";
#    print "2: ", defined($okpre), " ";
#    print "3: ", defined($match), "($match) ";
#    print "4: ", defined($okmatch), " ";
#    print "5: ", defined($post), " ";
#    print "6: ", defined($okpost), "\n";

    if (($pre ne $okpre)
	|| ($match ne $okmatch)
	|| ($post ne $okpost)) {
	print "not ok $tnum\n";
	print "\t\"$pre\" =?= \"$okpre\"\n";
	print "\t\"$match\" =?= \"$okmatch\"\n";
	print "\t\"$post\" =?= \"$okpost\"\n";
	$mc->dump();
	exit 1;
    } else {
	print "ok $tnum\n";
    }
}