-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStringFilter.test.js
More file actions
73 lines (63 loc) · 3.52 KB
/
Copy pathStringFilter.test.js
File metadata and controls
73 lines (63 loc) · 3.52 KB
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
import { StringFilter } from '../modules/StringFilter.js';
const sf = new StringFilter();
describe( '_splitStringUsingMultiplePatterns(string, patterns)', () => {
test( 'Blank string', () => {
const string = '';
const patterns = [ '<ref', '</ref>', '/>' ];
const output = [ '' ];
expect( sf._splitStringUsingMultiplePatterns( string, patterns ) ).toStrictEqual( output ); // toStrictEqual for arrays
} );
test( 'Zero <ref>s', () => {
const string = 'Hello. This is a test sentence. This is a test.';
const patterns = [ '<ref', '</ref>', '/>' ];
const output = [ 'Hello. This is a test sentence. This is a test.' ];
expect( sf._splitStringUsingMultiplePatterns( string, patterns ) ).toStrictEqual( output ); // toStrictEqual for arrays
} );
test( 'One <ref>, in middle', () => {
const string = 'Hello. This is a test sentence. <ref>Test ref</ref>. This is a test.';
const patterns = [ '<ref', '</ref>', '/>' ];
const output = [ 'Hello. This is a test sentence. ', '<ref>Test ref', '</ref>. This is a test.' ];
expect( sf._splitStringUsingMultiplePatterns( string, patterns ) ).toStrictEqual( output ); // toStrictEqual for arrays
} );
test( 'One <ref>, at start', () => {
const string = '<ref>Test ref</ref>. This is a test.';
const patterns = [ '<ref', '</ref>', '/>' ];
const output = [ '<ref>Test ref', '</ref>. This is a test.' ];
expect( sf._splitStringUsingMultiplePatterns( string, patterns ) ).toStrictEqual( output ); // toStrictEqual for arrays
} );
test( 'One <ref>, at end', () => {
const string = 'Hello. This is a test sentence. <ref>Test ref</ref>';
const patterns = [ '<ref', '</ref>', '/>' ];
const output = [ 'Hello. This is a test sentence. ', '<ref>Test ref', '</ref>' ];
expect( sf._splitStringUsingMultiplePatterns( string, patterns ) ).toStrictEqual( output ); // toStrictEqual for arrays
} );
test( 'Two <ref>s', () => {
const string = 'Hello. This is a test sentence. <ref>Test ref</ref>. Test. <ref>Test ref</ref>. This is a test.';
const patterns = [ '<ref', '</ref>', '/>' ];
const output = [ 'Hello. This is a test sentence. ', '<ref>Test ref', '</ref>. Test. ', '<ref>Test ref', '</ref>. This is a test.' ];
expect( sf._splitStringUsingMultiplePatterns( string, patterns ) ).toStrictEqual( output ); // toStrictEqual for arrays
} );
} );
describe( 'surgicalReplaceOutsideTags(regex, replacement, haystack, openingTags, closingTags)', () => {
test( 'Basic', () => {
const regex = /test/;
const replacement = 'test2';
const haystack = 'Hello. This is a test sentence. <ref>test ref</ref>. test. <ref>test ref</ref>. This is a test.';
const openingTags = [ '<ref' ];
const closingTags = [ '</ref>', '/>' ];
const output = 'Hello. This is a test2 sentence. <ref>test ref</ref>. test2. <ref>test ref</ref>. This is a test2.';
expect( sf.surgicalReplaceOutsideTags( regex, replacement, haystack, openingTags, closingTags ) ).toBe( output );
} );
} );
describe( 'surgicalReplaceInsideTags(regex, replacement, haystack, openingTags, closingTags)', () => {
test( 'Basic', () => {
const regex = /test/;
const replacement = 'test2';
const haystack = 'Hello. This is a test sentence. <ref>test ref</ref>. test. <ref>test ref</ref>. This is a test.';
const openingTags = [ '<ref' ];
const closingTags = [ '</ref>', '/>' ];
const output = 'Hello. This is a test sentence. <ref>test2 ref</ref>. test. <ref>test2 ref</ref>. This is a test.';
expect( sf.surgicalReplaceInsideTags( regex, replacement, haystack, openingTags, closingTags ) ).toBe( output );
} );
} );
// TODO: handle nesting