-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.js
171 lines (150 loc) · 6.01 KB
/
tests.js
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
var stringReplaceString = require('./string-replace-stream');
var expect = require('expect.js');
var concat = require('concat-stream');
describe('string-replace-stream', function () {
// First test KNP table building internal function
describe('_buildTable', function () {
it('should build knp table', function () {
expect(stringReplaceString._buildTable("ABCDABD")).eql([-1, 0, 0, 0, 0, 1, 2]);
expect(stringReplaceString._buildTable("PARTICIPATE IN PARACHUTE")).eql([
-1,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,1,2,3,0,0,0,0,0
]);
});
});
// Ruining poetry with node.js
["string", "buffer"].forEach(function (type) {
function testReplacement(haystack, search, replace, result, done) {
replaceStream = stringReplaceString(search, replace);
replaceStream.pipe(concat({encoding: 'string'}, complete));
haystack.forEach(function (h) {
if (type === "buffer") {
replaceStream.write(new Buffer(h, 'utf8'));
} else {
replaceStream.write(h);
}
});
replaceStream.end();
function complete(output) {
expect(output).to.equal(result);
done();
}
}
describe('with input ' + type, function () {
it('should replace one instance of the search string with the replacement string inside a chunk', function (done) {
testReplacement(
["'Twas brillig, and the slithy toves"],
"slithy",
"slimy",
"'Twas brillig, and the slimy toves",
done
);
});
it('should replace search string with replacement string spanning 2 chunks', function (done) {
testReplacement(
["'Twas brillig, and the slithy toves\n", "Did gyre and gimble in the wabe;"],
"toves\nDid gyre",
"badgers\nDid rotate",
"'Twas brillig, and the slithy badgers\nDid rotate and gimble in the wabe;",
done
);
});
it('should replace search string with replacement string spanning 3 chunks', function (done) {
testReplacement(
["'Twas brillig, and the slithy toves\n", "Did gyre and gimble in the wabe;\n", "All mimsy were the borogoves,"],
"toves\nDid gyre and gimble in the wabe;\nAll mimsy",
"badgers\nDid rotate and bore in the side of the hill;\nAll flimsy and miserable",
"'Twas brillig, and the slithy badgers\nDid rotate and bore in the side of the hill;\nAll flimsy and miserable were the borogoves,",
done
);
});
it('should replace multiple occurrences of search string with replacment string', function (done) {
testReplacement(
["'Twas brillig, and the slithy toves\n", "Did gyre and gimble in the wabe;\n", "All mimsy were the borogoves,"],
"the",
"that",
"'Twas brillig, and that slithy toves\nDid gyre and gimble in that wabe;\nAll mimsy were that borogoves,",
done
);
});
it('should replace multiple occurances of search string next to each other with replacement string', function (done) {
testReplacement(
["testtestte","sttesttesttes","tHELLOtest"],
"test",
"badger",
"badgerbadgerbadgerbadgerbadgerbadgerHELLObadger",
done
);
});
it('should replace the whole lot if it matches', function (done) {
testReplacement(
["'Twas brillig, and the slithy toves\n", "Did gyre and gimble in the wabe;\n", "All mimsy were the borogoves,"],
"'Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe;\nAll mimsy were the borogoves,",
"It was four in the afternoon, and the slimy badgers were rotating and boring into the hill. Some birds were miserable and a bit flimsy",
"It was four in the afternoon, and the slimy badgers were rotating and boring into the hill. Some birds were miserable and a bit flimsy",
done
);
});
it('should handle a prefix of the search string overlapping the actual search', function (done) {
testReplacement(
["abcabcabcabcdtesttest"],
"abcabcd",
"badger",
"abcabcbadgertesttest",
done
);
});
it('should handle match no matter where it falls on chunk break', function (done) {
var haystack = "the Jabberwock son!";
var todo = 0;
function doneOne() {
todo--;
if (todo === 0) done();
}
for (var i = 1; i < haystack.length-1; i++) {
for (var k = i + 1; k < haystack.length-1; k++) {
todo++;
testReplacement(
[haystack.slice(0, i), haystack.slice(i, k), haystack.slice(k) + k],
"Jabber",
"XMPP",
"the XMPPwock son!" + k,
doneOne
);
}
}
});
it('should handle a prefix of the search string overlapping the actual search broken into chunks', function (done) {
testReplacement(
["abcabcabc","abcdtest","test"],
"abcabcd",
"badger",
"abcabcbadgertesttest",
done
);
});
it('should handle "dangling tails" where the stream ends in a prefix of the search string', function (done) {
testReplacement(
["abcefg","hijklmnopqrstu","vw"],
"uvwxyz",
"test",
"abcefghijklmnopqrstuvw",
done
);
});
});
});
it('should handle unicode characters split between blocks', function (done) {
var input = new Buffer("My ☃ cost £100 🙁!", "utf-8");
var replaceStream = stringReplaceString("☃ cost £100 🙁", "snowman cost £100 :(");
replaceStream.pipe(concat({encoding: 'string'}, complete));
// One byte at a time, doesn't get any more split than that
for (var i = 0; i < input.length; i++) {
replaceStream.write(input.slice(i,i+1));
}
replaceStream.end();
function complete(output) {
expect(output).to.equal(("My snowman cost £100 :(!"));
done();
}
});
});