/
api_root.js
118 lines (105 loc) · 4.08 KB
/
api_root.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
var tape = require("tape");
var protobuf = require("..");
var Root = protobuf.Root;
var def = {
nested: {},
options: { javaPackage: "com.something" }
};
tape.test("reflected roots", function(test) {
test.test(test.name + " - construct", function(test) {
var root = Root.fromJSON(def);
test.ok(root instanceof Root, "should construct from JSON");
var root2 = Root.fromJSON(def, root);
test.equal(root2, root, "should construct from JSON and reuse specified Root");
test.end();
});
if (typeof Promise !== "undefined")
test.test(test.name + " - promise", function(test) {
var root = new Root();
var promise = root.load("tests/data/common.proto");
test.ok(promise instanceof Promise, "should return a Promise when loading without a callback");
promise
.then(function() {
test.pass("should resolve");
test.end();
})
.catch(function() {
test.fail("should not reject");
});
});
test.test(test.name + " - json", function(test) {
var root = new Root();
test.plan(3);
root.load("tests/data/common.json", function(err) {
if (err)
return test.fail("should not return an error when loading JSON files: " + err.message);
test.ok(root.lookupType("google.protobuf.Any"), "should load JSON files");
root.load("tests/data/common.json", function(err) {
test.same(root.files, [ "tests/data/common.json" ], "should not attempt to load the same file twice");
test.notOk(err, "should not return an error when loading files twice");
test.end();
});
});
});
test.test(test.name + " - weak", function(test) {
var root = new Root();
test.plan(1);
root.load(["tests/data/weak.proto"], function (err) {
test.notOk(err, "should ignore missing weak imports");
test.end();
});
});
test.test(test.name + " - missing", function(test) {
var root = new Root();
test.plan(1);
root.load("tests/data/NOTFOUND", function(err) {
test.ok(err, "should return an error when trying to load missing protos");
test.end();
});
});
test.test(test.name + " - missing import", function(test) {
var root = new Root();
test.plan(2);
root.load("tests/data/badimport.proto", function(err) {
test.ok(err, "should return an error when an imported file does not exist");
test.match(err.toString(), /nonexistent\.proto/, "should mention the file name which was not found");
test.end();
});
});
test.test(test.name + " - missing import, sync load", function(test) {
var root = new Root();
test.plan(2);
try {
root.loadSync("tests/data/badimport.proto");
root.resolveAll();
} catch (err) {
test.ok(err, "should return an error when an imported file does not exist");
test.match(err.toString(), /nonexistent\.proto/, "should mention the file name which was not found");
}
test.end();
});
test.test(test.name + " - skipped", function(test) {
var root = new Root();
root.resolvePath = function() {
return null;
};
test.plan(1);
root.load("tests/data/NOTFOUND2", function(err) {
test.notOk(err, "should skip files without error when resolvePath returns null");
test.end();
});
});
test.test(test.name + " - skipped import", function(test) {
var root = new Root();
root.resolvePath = function(origin, target) {
if (/weak\.proto$/.test(target))
return protobuf.util.path.resolve(origin, target);
return null;
};
test.plan(1);
root.load("tests/data/weak.proto", function(err) {
test.notOk(err, "should skip files without error when resolvePath returns null");
test.end();
});
});
});