/
cli.js
155 lines (129 loc) · 5.51 KB
/
cli.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// A minimal test for pbjs tool targets.
var tape = require("tape");
var path = require("path");
var Module = require("module");
var protobuf = require("..");
function cliTest(test, testFunc) {
// pbjs does not seem to work with Node v4, so skip this test if we're running on it
if (process.versions.node.match(/^4\./)) {
test.end();
return;
}
// Alter the require cache to make the cli/targets/static work since it requires "protobufjs"
// and we don't want to mess with "npm link"
var savedResolveFilename = Module._resolveFilename;
Module._resolveFilename = function(request, parent) {
if (request.startsWith("protobufjs")) {
return request;
}
return savedResolveFilename(request, parent);
};
require.cache.protobufjs = require.cache[path.resolve("index.js")];
try {
testFunc();
} finally {
// Rollback all the require() related mess we made
delete require.cache.protobufjs;
Module._resolveFilename = savedResolveFilename;
}
}
tape.test("pbjs generates static code", function(test) {
cliTest(test, function() {
var root = protobuf.loadSync("tests/data/cli/test.proto");
root.resolveAll();
var staticTarget = require("../cli/targets/static");
staticTarget(root, {
create: true,
decode: true,
encode: true,
convert: true,
}, function(err, jsCode) {
test.error(err, 'static code generation worked');
// jsCode is the generated code; we'll eval it
// (since this is what we normally does with the code, right?)
// This is a test code. Do not use this in production.
var $protobuf = protobuf;
eval(jsCode);
var OneofContainer = protobuf.roots.default.OneofContainer;
var Message = protobuf.roots.default.Message;
test.ok(OneofContainer, "type is loaded");
test.ok(Message, "type is loaded");
// Check that fromObject and toObject work for plain object
var obj = {
messageInOneof: {
value: 42,
},
regularField: "abc",
};
var obj1 = OneofContainer.toObject(OneofContainer.fromObject(obj));
test.deepEqual(obj, obj1, "fromObject and toObject work for plain object");
// Check that dynamic fromObject and toObject work for static instance
var root = protobuf.loadSync("tests/data/cli/test.proto");
var OneofContainerDynamic = root.lookup("OneofContainer");
var instance = new OneofContainer();
instance.messageInOneof = new Message();
instance.messageInOneof.value = 42;
instance.regularField = "abc";
var instance1 = OneofContainerDynamic.toObject(OneofContainerDynamic.fromObject(instance));
test.deepEqual(instance, instance1, "fromObject and toObject work for instance of the static type");
test.end();
});
});
});
tape.test("without null-defaults, absent optional fields have zero values", function(test) {
cliTest(test, function() {
var root = protobuf.loadSync("tests/data/cli/null-defaults.proto");
root.resolveAll();
var staticTarget = require("../cli/targets/static");
staticTarget(root, {
create: true,
decode: true,
encode: true,
convert: true,
}, function(err, jsCode) {
test.error(err, 'static code generation worked');
// jsCode is the generated code; we'll eval it
// (since this is what we normally does with the code, right?)
// This is a test code. Do not use this in production.
var $protobuf = protobuf;
eval(jsCode);
var OptionalFields = protobuf.roots.default.OptionalFields;
test.ok(OptionalFields, "type is loaded");
// Check default values
var msg = OptionalFields.fromObject({});
test.equal(msg.a, null, "default submessage is null");
test.equal(msg.b, "", "default string is empty");
test.equal(msg.c, 0, "default integer is 0");
test.end();
});
});
});
tape.test("with null-defaults, absent optional fields have null values", function(test) {
cliTest(test, function() {
var root = protobuf.loadSync("tests/data/cli/null-defaults.proto");
root.resolveAll();
var staticTarget = require("../cli/targets/static");
staticTarget(root, {
create: true,
decode: true,
encode: true,
convert: true,
"null-defaults": true,
}, function(err, jsCode) {
test.error(err, 'static code generation worked');
// jsCode is the generated code; we'll eval it
// (since this is what we normally does with the code, right?)
// This is a test code. Do not use this in production.
var $protobuf = protobuf;
eval(jsCode);
var OptionalFields = protobuf.roots.default.OptionalFields;
test.ok(OptionalFields, "type is loaded");
// Check default values
var msg = OptionalFields.fromObject({});
test.equal(msg.a, null, "default submessage is null");
test.equal(msg.b, null, "default string is null");
test.equal(msg.c, null, "default integer is null");
test.end();
});
});
});