-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathapi_load-sync.js
More file actions
61 lines (46 loc) · 2.21 KB
/
api_load-sync.js
File metadata and controls
61 lines (46 loc) · 2.21 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
var tape = require("tape");
var protobuf = require("../..");
tape.test("load sync", function(test) {
var root = protobuf.loadSync("tests/data/common.proto");
test.ok(root.lookup("Something"), "should parse message Something");
test.throws(function() {
protobuf.loadSync("tests/data/__NOTFOUND__", root);
}, Error, "should throw if not found");
var isNode = protobuf.util.isNode;
try {
protobuf.util.isNode = false;
test.throws(function() {
protobuf.loadSync("tests/data/common.proto");
}, "should throw when not running under node");
} finally {
protobuf.util.isNode = isNode;
}
test.throws(function() {
protobuf.loadSync("tests/data/invalid.proto");
}, Error, "should throw when trying to load an invalid proto");
test.throws(function() {
protobuf.loadSync("tests/data/invalid.json");
}, Error, "should throw when trying to load invalid json");
root = protobuf.loadSync("tests/data/weak.proto");
test.ok(root.files.indexOf("tests/data/NOT_FOUND") > -1, "should ignore missing weak protos and remember them");
test.ok(root.files.indexOf("google/protobuf/any.proto") > -1, "should still load other protos when ignoring weak protos");
test.end();
});
tape.test("load sync resolves features", function(test) {
var root = protobuf.loadSync("tests/data/test.proto");
var Complex = root.lookupType("jspb.test.Complex");
var Simple1 = root.lookupType("jspb.test.Simple1");
test.notOk(Complex.fields.aNestedMessage.resolved, "should not resolve field types eagerly");
test.ok(Simple1.fields.aString.required, "should resolve field features");
root.resolveAll();
test.ok(Complex.fields.aNestedMessage.resolved, "should resolve field types explicitly");
test.end();
});
tape.test("should load bundled definitions even if resolvePath method was overrided", function(test) {
var protoFilePath = "tests/data/common.proto";
var root = new protobuf.Root();
root.resolvePath = (origin, target) => origin === "" && target === protoFilePath ? target : null;
root.loadSync(protoFilePath);
test.ok(root.lookup("Something"), "should parse message Something");
test.end();
});