forked from mafintosh/protocol-buffers-schema
/
test.mjs
76 lines (76 loc) · 3 KB
/
test.mjs
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
import { parse } from "./parse.mjs";
// shim until promises becomes an export of fs
import { readdirSync as read_dir, readFileSync as read_file } from 'fs';
import { join as path_join, basename } from 'path';
import { deepStrictEqual as deep_strict_equal, rejects } from 'assert';
const fixture = (p) => path_join('test/fixtures', basename(p));
const rjson = async (p) => JSON.parse(await read_file(p, 'utf8'));
const rpbuf = async (p) => parse(await read_file(p, 'utf8'));
const fixtures = async (p) => {
const json = rjson(fixture(p + '.json'));
const schema = rpbuf(fixture(p + '.proto'));
return { json: await json, schema: await schema };
};
const will_error = {
'no-tags': {
name: "SyntaxError",
message: "tag assignment must follow field name [around token 6 \";\"]"
},
'pheromon-trajectories': {
name: "SyntaxError",
message: "Fields of type bytes cannot be declared \[packed=true\]. Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire types) can be declared as \"packed\". See https:\/\/developers.google.com\/protocol-buffers\/docs\/encoding\#optional"
},
'invalid-map-type': {
name: "SyntaxError",
message: `Fields of type map cannot use Free as a key value, please use an enum, integer, or string type (int32, int64, uint32, uint64, sint32, sint64, bool, fixed64, sfixed64, fixed32, sfixed32, string + enum)`
}
};
const verbose = process.argv.includes('--verbose');
async function main() {
let dir = await read_dir('test/fixtures');
let tests = 0;
console.group('Running tests');
for (const file of new Set(dir.map(v => basename(basename(v, '.json'), '.proto')))) {
tests++;
console.group(file);
if (file in will_error) {
let err = null, rp = null;
await rejects(rp = rpbuf(fixture(file + '.proto')), will_error[file]);
try {
await rp;
}
catch (e) {
err = e;
}
if (err)
console.log('Got error correctly: %s', err);
else {
console.error(rp);
throw new ReferenceError('Expected error, got schema instead.');
}
}
else {
const { json, schema } = await fixtures(file);
let schema_json = schema.toJSON();
deep_strict_equal(json, schema_json);
if (verbose) {
console.dir(schema, { depth: null });
console.dir(json, { depth: null });
console.log(schema.toString());
}
else
console.log('passed required tests');
}
console.groupEnd();
}
return tests;
}
Promise.resolve().then(main).then(tests => {
console.info('Passed %d tests successfully; exiting...', tests);
console.groupEnd();
setImmediate(process.exit, 0);
}, e => {
console.error(e);
setImmediate(process.exit, 1);
});
//# sourceMappingURL=test.mjs.map