forked from NovemLinguae/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringFilter.test.js
More file actions
73 lines (63 loc) · 3.36 KB
/
Copy pathStringFilter.test.js
File metadata and controls
73 lines (63 loc) · 3.36 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";
let sf = new StringFilter();
describe('_splitStringUsingMultiplePatterns(string, patterns)', () => {
test('Blank string', () => {
let string = ``;
let patterns = ['<ref', '</ref>', '/>'];
let output = [``];
expect(sf._splitStringUsingMultiplePatterns(string, patterns)).toStrictEqual(output); // toStrictEqual for arrays
});
test('Zero <ref>s', () => {
let string = `Hello. This is a test sentence. This is a test.`;
let patterns = ['<ref', '</ref>', '/>'];
let 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', () => {
let string = `Hello. This is a test sentence. <ref>Test ref</ref>. This is a test.`;
let patterns = ['<ref', '</ref>', '/>'];
let 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', () => {
let string = `<ref>Test ref</ref>. This is a test.`;
let patterns = ['<ref', '</ref>', '/>'];
let output = [`<ref>Test ref`, `</ref>. This is a test.`];
expect(sf._splitStringUsingMultiplePatterns(string, patterns)).toStrictEqual(output); // toStrictEqual for arrays
});
test('One <ref>, at end', () => {
let string = `Hello. This is a test sentence. <ref>Test ref</ref>`;
let patterns = ['<ref', '</ref>', '/>'];
let 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', () => {
let string = `Hello. This is a test sentence. <ref>Test ref</ref>. Test. <ref>Test ref</ref>. This is a test.`;
let patterns = ['<ref', '</ref>', '/>'];
let 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', () => {
let regex = /test/;
let replacement = 'test2';
let haystack = `Hello. This is a test sentence. <ref>test ref</ref>. test. <ref>test ref</ref>. This is a test.`;
let openingTags = ['<ref'];
let closingTags = ['</ref>', '/>'];
let 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', () => {
let regex = /test/;
let replacement = 'test2';
let haystack = `Hello. This is a test sentence. <ref>test ref</ref>. test. <ref>test ref</ref>. This is a test.`;
let openingTags = ['<ref'];
let closingTags = ['</ref>', '/>'];
let 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