-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathapi_converters.js
223 lines (165 loc) · 11.1 KB
/
api_converters.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var tape = require("tape");
var protobuf = require("..");
tape.test("converters", function(test) {
protobuf.load("tests/data/convert.proto", function(err, root) {
if (err)
return test.fail(err.message);
var Message = root.lookup("Message");
test.test(test.name + " - Message#toObject", function(test) {
test.plan(7);
test.test(test.name + " - called with defaults = true", function(test) {
var obj = Message.toObject(Message.create(), { defaults: true });
test.equal(obj.stringVal, "", "should set stringVal");
test.same(obj.stringRepeated, [], "should set stringRepeated");
test.same(obj.uint64Val, { low: 0, high: 0, unsigned: true }, "should set uint64Val");
test.same(obj.uint64Repeated, [], "should set uint64Repeated");
test.same(obj.bytesVal, protobuf.util.newBuffer(0), "should set bytesVal");
test.same(obj.bytesRepeated, [], "should set bytesRepeated");
test.equal(obj.enumVal, 1, "should set enumVal to the first defined value");
test.same(obj.enumRepeated, [], "should set enumRepeated");
test.same(obj.int64Map, {}, "should set int64Map");
test.end();
});
test.test(test.name + " - called with defaults = true and longs = bigint", function(test) {
var obj = Message.toObject(Message.create(), { defaults: true, longs: BigInt });
test.same(obj.uint64Val, 0n, "should set uint64Val");
test.end();
});
test.test(test.name + " - called with defaults = undefined", function(test) {
var obj = Message.toObject(Message.create());
test.equal(obj.stringVal, undefined, "should not set stringVal");
test.equal(obj.stringRepeated, undefined, "should not set stringRepeated");
test.equal(obj.uint64Val, undefined, "should not set uint64Val");
test.equal(obj.uint64Repeated, undefined, "should not set uint64Repeated");
test.equal(obj.bytesVal, undefined, "should not set bytesVal");
test.equal(obj.bytesRepeated, undefined, "should not set bytesRepeated");
test.equal(obj.enumVal, undefined, "should not set enumVal");
test.equal(obj.enumRepeated, undefined, "should not set enumRepeated");
test.equal(obj.int64Map, undefined, "should not set int64 map");
test.end();
});
test.test(test.name + " - called with arrays = true", function(test) {
var obj = Message.toObject(Message.create(), { arrays: true });
test.equal(obj.stringVal, undefined, "should not set stringVal");
test.same(obj.stringRepeated, [], "should set stringRepeated");
test.equal(obj.uint64Val, undefined, "should not set uint64Val");
test.same(obj.uint64Repeated, [], "should set uint64Repeated");
test.equal(obj.bytesVal, undefined, "should not set bytesVal");
test.same(obj.bytesRepeated, [], "should set bytesRepeated");
test.equal(obj.enumVal, undefined, "should not set enumVal");
test.same(obj.enumRepeated, [], "should set enumRepeated");
test.equal(obj.int64Map, undefined, "should not set int64Map");
test.end();
});
test.test(test.name + " - called with objects = true", function(test) {
var obj = Message.toObject(Message.create(), { objects: true });
test.equal(obj.stringVal, undefined, "should not set stringVal");
test.equal(obj.stringRepeated, undefined, "should not set stringRepeated");
test.equal(obj.uint64Val, undefined, "should not set uint64Val");
test.same(obj.uint64Repeated, undefined, "should not set uint64Repeated");
test.equal(obj.bytesVal, undefined, "should not set bytesVal");
test.same(obj.bytesRepeated, undefined, "should not set bytesRepeated");
test.equal(obj.enumVal, undefined, "should not set enumVal");
test.same(obj.enumRepeated, undefined, "should not set enumRepeated");
test.same(obj.int64Map, {}, "should set int64Map");
test.end();
});
test.test(test.name + " - should convert", function(test) {
var buf = protobuf.util.newBuffer(3);
buf[0] = buf[1] = buf[2] = 49; // "111"
var msg = Message.create({
// This number was chosen to be > 2^63 and < 2^64
uint64Val: protobuf.util.Long.fromString("11000000000000000001", true),
uint64Repeated: [2, 3],
bytesVal: buf,
bytesRepeated: [buf, buf],
enumVal: 2,
enumRepeated: [1, 100, 2],
int64Map: {
// These numbers were chosen to be < Number.MIN_SAFE_INTEGER
a: protobuf.util.Long.fromString("-200000000000000001"),
b: protobuf.util.Long.fromString("-300000000000000001")
},
fixed64Val: protobuf.util.Long.fromString("11000000000000000001", true),
// This number was chosen to be between -2^63 and -2^62
sfixed64Val: protobuf.util.Long.fromString("-9000000000000000001", true),
});
var msgLongsToNumber = Message.toObject(msg, { longs: Number }),
msgLongsToString = Message.toObject(msg, { longs: String }),
msgLongsToBigInt = Message.toObject(msg, { longs: BigInt });
test.same(Message.ctor.toObject(msg, { longs: Number}), msgLongsToNumber, "should convert the same using the static and the instance method");
test.same(Message.ctor.toObject(msg, { longs: String}), msgLongsToString, "should convert the same using the static and the instance method");
test.same(Message.ctor.toObject(msg, { longs: BigInt}), msgLongsToBigInt, "should convert the same using the static and the instance method");
test.equal(msgLongsToNumber.uint64Val, 11000000000000000000, "longs to numbers");
test.equal(msgLongsToString.uint64Val, "11000000000000000001", "longs to strings");
test.equal(msgLongsToBigInt.uint64Val, 11000000000000000001n, "longs to bigints");
test.same(msgLongsToNumber.int64Map, { a: -200000000000000000, b: -300000000000000000}, "long map values to numbers");
test.same(msgLongsToString.int64Map, { a: "-200000000000000001", b: "-300000000000000001"}, "long map values to strings");
test.same(msgLongsToBigInt.int64Map, { a: -200000000000000001n, b: -300000000000000001n}, "long map values to bigints");
test.equal(msgLongsToNumber.fixed64Val, 11000000000000000000, "longs to numbers");
test.equal(msgLongsToString.fixed64Val, "11000000000000000001", "longs to strings");
test.equal(msgLongsToBigInt.fixed64Val, 11000000000000000001n, "longs to bigints");
test.equal(msgLongsToNumber.sfixed64Val, -9000000000000000000, "longs to numbers");
test.equal(msgLongsToString.sfixed64Val, "9446744073709551615", "longs to strings"); // this is wrong
test.equal(msgLongsToBigInt.sfixed64Val, -9000000000000000001n, "longs to bigints");
test.equal(Object.prototype.toString.call(Message.toObject(msg, { bytes: Array }).bytesVal), "[object Array]", "bytes to arrays");
test.equal(Message.toObject(msg, { bytes: String }).bytesVal, "MTEx", "bytes to base64 strings");
if (protobuf.util.isNode)
test.ok(Buffer.isBuffer(Message.toObject(msg, { bytes: Buffer }).bytesVal), "bytes to buffers");
test.equal(Message.toObject(msg, { enums: String }).enumVal, "TWO", "enums to strings");
test.equal(Message.toObject(msg, { enums: String }).enumRepeated[1], 100, "enums to strings does not change unknown values");
test.end();
});
test.test(test.name + " - Message.toObject with empty buffers", function(test) {
var msg = Message.create({
bytesVal: protobuf.util.newBuffer(0),
});
test.equal(Message.toObject(msg, { bytes: String }).bytesVal, "", "bytes to base64 strings");
if (protobuf.util.isNode) {
const bytesVal = Message.toObject(msg, { bytes: Buffer }).bytesVal;
test.ok(Buffer.isBuffer(bytesVal), "bytes to buffers");
test.equal(bytesVal.length, 0, "empty buffer");
}
test.end();
});
});
test.test(test.name + " - Message.fromObject", function(test) {
var obj = {
uint64Val: 1,
uint64Repeated: [1, "2"],
bytesVal: "MTEx",
bytesRepeated: ["MTEx", [49, 49, 49]],
enumVal: "ONE",
enumRepeated: [2, "TWO", 100],
int64Map: {
a: 2,
b: "3"
}
};
var msg = Message.fromObject(obj);
test.same(Message.ctor.fromObject(obj), msg, "should convert the same using the static and the instance method");
test.equal(Message.fromObject(msg), msg, "should just return the object if already a runtime message");
var buf = protobuf.util.newBuffer(3);
buf[0] = buf[1] = buf[2] = 49; // "111"
test.same(msg.uint64Val, { low: 1, high: 0, unsigned: true }, "should set uint64Val from a number");
test.same(msg.uint64Repeated, [ { low: 1, high: 0, unsigned: true}, { low: 2, high: 0, unsigned: true } ], "should set uint64Repeated from a number and a string");
test.same(msg.bytesVal, buf, "should set bytesVal from a base64 string");
test.same(msg.bytesRepeated, [ buf, buf ], "should set bytesRepeated from a base64 string and a plain array");
test.equal(msg.enumVal, 1, "should set enumVal from a string");
test.same(msg.enumRepeated, [ 2, 2, 100 ], "should set enumRepeated from a number and a string and preserve unknown value");
test.same(msg.int64Map, { a: { low: 2, high: 0, unsigned: false }, b: { low: 3, high: 0, unsigned: false } }, "should set int64Map from a number and a string");
test.end();
});
test.test(test.name + " - Message#toJSON", function(test) {
var msg = Message.create();
msg.$type = {
toObject: function(obj, opt) {
test.same(opt, protobuf.util.toJSONOptions, "should use toJSONOptions with toJSON");
test.end();
}
};
msg.toJSON();
});
test.end();
});
});