-
Notifications
You must be signed in to change notification settings - Fork 4
/
ecoji-orig.test.js
96 lines (72 loc) · 2.66 KB
/
ecoji-orig.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* JavaScript/AVA implementation
* @see https://github.com/keith-turner/ecoji/blob/main/test_scripts/ecoji_test.sh
*/
import { readFile, readdir } from "fs/promises";
import { Ecoji } from "base-ex";
import test from "ava";
// macros
const enDecodeTest = test.macro((t, input, expected, ecoji) => {
const output = ecoji.encode(input);
t.is(output, expected);
t.deepEqual(ecoji.decode(output), input);
});
const decodeTest = test.macro((t, input, expected, ecoji, ...args) => {
t.is(ecoji.decode(input, ...args), expected);
});
const errorTest = test.macro((t, input, ecoji) => {
t.throws(() => ecoji.decode(input));
});
// declarations
const path = "./test/fixtures/ecoji/";
const files = await (readdir(path));
const ecojiV1 = new Ecoji("emojis_v1", "uint8");
const ecojiV2 = new Ecoji("emojis_v2", "uint8");
// test group file lists
const plainFilesA = files.filter(f => (/\.plain$/).test(f));
const aLen = plainFilesA.length;
const plainFilesB = files.filter(f => (/\.plaind$/).test(f));
const bLen = plainFilesB.length;
const garbage = files.filter(f => (/\.garbage$/).test(f));
const gLen = garbage.length;
// test data generation
const dataA = await Promise.all(
plainFilesA.map(async plainFile => {
const bareName = plainFile.slice(0, -6);
const input = new Uint8Array(await readFile(`${path}${plainFile}`));
const expectedV1 = await readFile(`${path}${bareName}.ev1`, "utf-8");
const expectedV2 = await readFile(`${path}${bareName}.ev2`, "utf-8");
return {
input,
expectedV1,
expectedV2
}
})
);
const dataB = await Promise.all(
plainFilesB.map(async plainFile => {
const bareName = plainFile.slice(0, -7);
const input = await readFile(`${path}${bareName}.enc`, "utf-8");
const expected = await readFile(`${path}${plainFile}`, "utf-8");
return {
input,
expected
}
})
);
const dataC = await Promise.all(
garbage.map(
async plainFile => readFile(`${path}${plainFile}`)
)
);
// tests
dataA.forEach((data, i) => {
test(`En- and decoding for Ecoji Version 1, Test: [${i+1}|${aLen}]`, enDecodeTest, data.input, data.expectedV1, ecojiV1);
test(`En- and decoding for Ecoji Version 2, Test: [${i+1}|${aLen}]`, enDecodeTest, data.input, data.expectedV2, ecojiV2);
});
dataB.forEach((data, i) => {
test(`Decode sample data for both versions, Test: [${i+1}|${bLen}]`, decodeTest, data.input, data.expected, ecojiV2, "str");
});
dataC.forEach((data, i) => {
test(`Garbage input > throw Error, Test: [${i+1}|${gLen}]`, errorTest, data, ecojiV2);
});