-
Notifications
You must be signed in to change notification settings - Fork 4
/
tmgmt_zanata.test
497 lines (448 loc) · 18.7 KB
/
tmgmt_zanata.test
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* @file
* Unit tests.
*
* Note that much of the functionality requires communication with
* a Zanata server, so cannot be easily unit tested.
*/
/**
* Note: all tests should start with 'test' to be auto-detected.
*/
class TmgmtZanataSegmenterRegexTestCase extends DrupalUnitTestCase {
// Return values from preg_match.
const NO_MATCH = 0;
const MATCH = 1;
const MATCH_ERROR = FALSE;
/**
* {@inheritdoc}
*/
public static function getInfo() {
// Note: do not translate getInfo strings.
return array(
'name' => 'TMGMT Zanata Tests',
'description' => 'Unit tests for TMGMT Zanata.',
// This is a global group in the Testing config page.
'group' => 'TMGMT Zanata',
);
}
/**
* Load the code for TMGMT Zanata into the test environment.
*/
public function setUp() {
$module_path = drupal_get_path('module', 'tmgmt_zanata');
include_once $module_path . '/tmgmt_zanata.segmenter.inc';
parent::setUp();
}
/**
* I was having some issues loading classes in tests.
*
* This just makes sure I have done setup properly. If any of
* these fail, other tests will likely have errors.
*/
public function testClassesAreLoaded() {
$this->assertTrue(class_exists('TmgmtZanataSegmenter', FALSE),
'class TmgmtZanataSegmenter should be available.', 'Test setup');
}
/**
* Ensure sentences are segmented as expected.
*/
public function testSentenceSegmentationRegex() {
$sentence_regex = TmgmtZanataSegmenter::SENTENCE_REGEX;
$matched = preg_match($sentence_regex, 'No sentence terminator');
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected when there are no sentence terminators.',
'Sentence regex');
$matched = preg_match($sentence_regex,
'Has a sentence terminator but no space after.');
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for a single sentence ending on the last character.',
'Sentence regex');
$matched = preg_match($sentence_regex,
'This sentence has ... an ellipsis');
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for ellipsis.', 'Sentence regex');
$matched = preg_match($sentence_regex,
'This sentence ends with a period. ');
$this->assertIdentical($matched, $this::MATCH,
'Match expected for a period followed by whitespace (space).',
'Sentence regex');
$matched = preg_match($sentence_regex,
"This sentence ends with a period.\n");
$this->assertIdentical($matched, $this::MATCH,
'Match expected for a period followed by whitespace (newline).',
'Sentence regex');
$matched = preg_match($sentence_regex,
"This sentence ends with a period.\r\n");
$this->assertIdentical($matched, $this::MATCH,
'Match expected for a period followed by whitespace (Windows newline).',
'Sentence regex');
$matched = preg_match($sentence_regex,
"This sentence ends with a period.\t");
$this->assertIdentical($matched, $this::MATCH,
'Match expected for a period followed by whitespace (tab).',
'Sentence regex');
$matched = preg_match($sentence_regex,
"The example is e.g. an example");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for the abbreviation e.g.',
'Sentence regex');
$matched = preg_match($sentence_regex,
"The example is i.e. an example");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for the abbreviation i.e.',
'Sentence regex');
$matched = preg_match($sentence_regex,
"It is 32.45 degrees");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for decimal point in a number',
'Sentence regex');
$matched = preg_match($sentence_regex,
'Is this sentence a query? ');
$this->assertIdentical($matched, $this::MATCH,
'Match expected for a question mark followed by whitespace (space).',
'Sentence regex');
$matched = preg_match($sentence_regex,
'This sentence is an exclamation! ');
$this->assertIdentical($matched, $this::MATCH,
'Match expected for an exclamation mark followed by whitespace (space).',
'Sentence regex');
$matched = preg_match($sentence_regex,
'This sentence is an exclamation! ');
$this->assertIdentical($matched, $this::MATCH,
'Match expected for an exclamation mark followed by whitespace (space).',
'Sentence regex');
$matched = preg_match($sentence_regex,
'This sentence, has a comma; a colon is present');
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for comma or semicolon.',
'Sentence regex');
preg_match($sentence_regex,
'Period with 2 spaces after. Then next sentence',
$matches, PREG_OFFSET_CAPTURE);
$full_match = $matches[0][0];
$this->assertIdentical($full_match, ' ',
'Punctuation at end of sentence should not be included in the match.',
'Sentence regex');
$match_pos = $matches[0][1];
$this->assertIdentical($match_pos, 27,
'Match should immediately follow punctuation at end of sentence.',
'Sentence regex');
}
/**
* Ensure sentences are segmented as expected.
*/
public function testParagraphSegmentationRegex() {
$paragraph_regex = TmgmtZanataSegmenter::PARAGRAPH_REGEX;
$matched = preg_match($paragraph_regex, "Line 1\nLine 2");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for single newline.',
'Paragraph regex');
$matched = preg_match($paragraph_regex, "Line 1\r\nLine 2");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for single Windows newline.',
'Paragraph regex');
$matched = preg_match($paragraph_regex,
"Sentence 1? Sentence 2. Sentence 3! Sentence 4.");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for sentence terminators.',
'Paragraph regex');
$matched = preg_match($paragraph_regex, "<p>Paragraph 1<p>Paragraph 2",
$matches, PREG_OFFSET_CAPTURE);
$this->assertIdentical($matched, $this::MATCH,
'Match expected before opening paragraph tag that is not the start of '
. 'the string.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0][0];
$this->assertIdentical($full_match, '',
'Match before opening paragraph tag should not include the tag itself.',
'Paragraph regex');
$match_pos = $matches[0][1];
$this->assertIdentical($match_pos, 14,
'Match for opening paragraph tag should be immediately before the tag.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"<p>Paragraph 1\n <p>\n Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected before opening paragraph tag that is not the start of '
. 'the string and is preceded by whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\n ",
'Match before opening paragraph tag should include leading whitespace, '
. 'but not the tag itself or any trailing whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex, "Paragraph 1\n\nParagraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for 2 newlines in a row.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\n\n",
'Match for 2 newlines should include both newlines.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex, "Paragraph 1\n\n\nParagraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for more than 2 newlines in a row.',
'Paragraph regex');
$full_match = $matches[0];
$this->assertIdentical($full_match, "\n\n\n",
'Match for 2 or more newlines should include any adjacent newlines.',
'Paragraph regex');
$matched = preg_match($paragraph_regex, "Paragraph 1\n \t \nParagraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for 2 newlines with only whitespace between them.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\n \t \n",
'Match for 2 newlines with whitespace between should include both '
. 'newlines and the intervening space.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex, "Paragraph 1\r\n\r\nParagraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for 2 Windows newlines in a row.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\r\n\r\n",
'Match for 2 Windows newlines should include both newlines.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1\r\n\r\n\r\nParagraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for more than 2 Windows newlines in a row.',
'Paragraph regex');
$full_match = $matches[0];
$this->assertIdentical($full_match, "\r\n\r\n\r\n",
'Match for 2 or more Windows newlines should include adjacent newlines.',
'Paragraph regex');
$matched = preg_match($paragraph_regex,
"Paragraph 1\r\n \t \r\nParagraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for 2 Windows newlines with only whitespace between them.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\r\n \t \r\n",
'Match for 2 Windows newlines with whitespace between should include '
. ' both newlines and the intervening space.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex, "Paragraph 1 \n\n\tParagraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for 2 newlines in a row with surrounding whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, " \n\n\t",
'Match for 2 newlines should include surrounding whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex, "Paragraph 1\t\r\n\r\n Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for 2 Windows newlines in a row with surrounding '
. 'whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\t\r\n\r\n ",
'Match for 2 Windows newlines should include surrounding whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1</p>Paragraph 2",
$matches, PREG_OFFSET_CAPTURE);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for closing paragraph tag.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0][0];
$this->assertIdentical($full_match, '',
'Match for closing paragraph tag should not include the tag iteslf.',
'Paragraph regex');
$match_pos = $matches[0][1];
$this->assertIdentical($match_pos, 15,
'Match position for closing paragraph tag should be immediately before '
. 'the next non-whitespace character.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"<p>Paragraph 1");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for opening paragraph tag that is at the start of the '
. 'string.',
'Paragraph regex');
$matched = preg_match($paragraph_regex,
"\n<p>Paragraph 1",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for opening paragraph tag that is preceded only by '
. 'whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\n",
'Match before paragraph tag at start of string should include all '
. 'preceding whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"\n <p>Paragraph 1",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for opening paragraph tag that is preceded only by '
. 'whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "\n ",
'Match before paragraph tag at start of string should include all '
. 'preceding whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"<p>Paragraph 1</p>");
$this->assertIdentical($matched, $this::NO_MATCH,
'No match expected for closing paragraph tag that is at the end of the '
. 'string.',
'Paragraph regex');
$matched = preg_match($paragraph_regex,
"Paragraph 1</p>\n Paragraph 2",
$matches, PREG_OFFSET_CAPTURE);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for closing paragraph tag followed by whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0][0];
$this->assertIdentical($full_match, "\n ",
'Match for closing paragraph tag should include whitespace after the '
. 'tag.',
'Paragraph regex');
$match_pos = $matches[0][1];
$this->assertIdentical($match_pos, 15,
'Match position for closing paragraph tag should be immediately before '
. 'the next non-whitespace character.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1<br>Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for linebreak tag.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "<br>",
'Match for linebreak tag should include the matched tag.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1 <br>\n Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for linebreak tag with surrounding whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, " <br>\n ",
'Match for linebreak tag should include surrounding whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1 <br/>\n Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for self-closing linebreak tag.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, " <br/>\n ",
'Match for self-closing linebreak tag should include surrounding '
. 'whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1 <br />\n Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for wide self-closing linebreak tag.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, " <br />\n ",
'Match for wide self-closing linebreak tag should include surrounding '
. 'whitespace.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1<br><br><br>Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for multiple adjacent linebreak tags.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "<br><br><br>",
'Match for multiple adjacent linebreak tags should include all '
. 'adjacent linebreak tags.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1<br/><br><br />Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for mixed types of adjacent linebreak tags.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "<br/><br><br />",
'Match for mixed types of adjacent linebreak tags should include all '
. 'adjacent linebreak tags.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1<br> <br>\n<br>Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for multiple linebreak tags separated by only whitespace.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "<br> <br>\n<br>",
'Match for multiple linebreak tags separated by only whitespace should '
. 'include all the whitespace and linebreak tags.',
'Paragraph regex');
}
$matched = preg_match($paragraph_regex,
"Paragraph 1</p><br>\n<br>Paragraph 2",
$matches);
$this->assertIdentical($matched, $this::MATCH,
'Match expected for closing paragraph tag followed by linebreak tags.',
'Paragraph regex');
if ($matched) {
$full_match = $matches[0];
$this->assertIdentical($full_match, "<br>\n<br>",
'Match for closing paragraph tag should include any mix of linebreak '
. 'tags and whitespace that follow, but not the closing paragraph tag '
. 'itself.',
'Paragraph regex');
}
}
}