-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
75 lines (66 loc) · 2.77 KB
/
test.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
const assert = require("assert");
const fs = require("fs");
const { promisify } = require("util");
const streamToGoogleQuery = require("./lib.js");
run()
.then(() => process.exit(0))
.catch(err => {
console.error(err);
process.exit(1);
});
async function run() {
const readFile = promisify(fs.readFile);
const unlink = promisify(fs.unlink);
return runAllTests();
async function runAllTests() {
await test("five-sentences-text", {});
await test("short-text", {});
await test("text-with-filtered-sentences", {});
await test("text-with-filtered-sentences", {});
await test("twelve-sentences-text", { maximumNumberOfSentences: 12 });
await test("filter-by-chars", { minimumLengthOfSentence: 53 });
await test("five-sentences-text-prefix", { prefix: "Prefix!" });
await test("five-sentences-text-suffix", { suffix: '%20!g"' });
await test("raw-text-with-filtered-sentences", { prefix: "", raw: true, suffix: "" });
await testIncludedButNotAll("twelve-sentences-text", {});
}
async function testIncludedButNotAll(name, options) {
return assertAgainstOutput(name, options, (expectedOutput, testOutput) => {
const possibilities = expectedOutput.split(/\n/).filter(line => line !== "");
const results = testOutput.split(/\n/).filter(line => line !== "");
for (let line of results) {
const countedTimes = results.reduce((count, possibility) => (line === possibility ? count + 1 : count), 0);
assert(countedTimes === 1, `counted the same line twice in the results`);
assert(possibilities.includes(line), `line is not in possibilities!\n${line}`);
}
assert(
results.length < possibilities.length,
`results should be less than possibliities!\n${results.length} >= ${possibilities.length}`
);
});
}
async function test(name, options) {
return assertAgainstOutput(name, options, (expectedOutput, testOutput) =>
assert(
testOutput === expectedOutput,
`output does not match expectation in test ${name}:\n${testOutput}\n!==\n${expectedOutput}`
)
);
}
async function assertAgainstOutput(name, options, assertions) {
const expectedOutput = await readFile(`__test__/${name}.expected.txt`, "utf8");
const tempTestOutputFile = `test-output-${name}.tmp.txt`;
await new Promise((resolve, reject) => {
const stream = streamToGoogleQuery({
inputStream: fs.createReadStream(`__test__/${name}.txt`),
outputStream: fs.createWriteStream(tempTestOutputFile),
...options
});
stream.on("error", reject);
stream.on("close", resolve);
});
const testOutput = await readFile(tempTestOutputFile, "utf8");
await assertions(expectedOutput, testOutput);
await unlink(tempTestOutputFile);
}
}