/
convert.js
104 lines (76 loc) · 4.35 KB
/
convert.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
var tape = require("tape");
var protobuf = require("..");
tape.test("convert", 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("Message#asJSON", function(test) {
test.test("called with defaults = true", function(test) {
var obj = Message.create().asJSON({ 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, [], "should set bytesVal");
test.same(obj.bytesRepeated, [], "should set bytesRepeated");
test.equal(obj.enumVal, 0, "should set enumVal");
test.same(obj.enumRepeated, [], "should set enumRepeated");
test.end();
});
test.test("called with defaults = undefined", function(test) {
var obj = Message.create().asJSON();
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.end();
});
test.test("should convert", function(test) {
var buf = protobuf.util.newBuffer(3);
buf[0] = buf[1] = buf[2] = 49; // "111"
var msg = Message.create({
uint64Val: protobuf.util.Long.fromNumber(1),
uint64Repeated: [2, 3],
bytesVal: buf,
bytesRepeated: [buf, buf],
enumVal: 1,
enumRepeated: [1, 2]
});
test.equal(msg.asJSON({ long: Number }).uint64Val, 1, "longs to numbers");
test.equal(msg.asJSON({ long: String }).uint64Val, "1", "longs to strings");
test.equal(Object.prototype.toString.call(msg.asJSON({ bytes: Array }).bytesVal), "[object Array]", "bytes to arrays");
test.equal(msg.asJSON({ bytes: String }).bytesVal, "MTEx", "bytes to base64 strings");
if (protobuf.util.isNode)
test.ok(Buffer.isBuffer(msg.asJSON({ bytes: Buffer }).bytesVal), "bytes to buffers");
test.equal(msg.asJSON({ enum: String }).enumVal, "ONE", "enums to strings");
test.end();
});
test.end();
});
test.test("Message.from", function(test) {
var msg = Message.from({
uint64Val: 1,
uint64Repeated: [1, "2"],
bytesVal: "MTEx",
bytesRepeated: ["MTEx", [49, 49, 49]],
enumVal: "ONE",
enumRepeated: [2, "TWO"]
});
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 ], "should set enumRepeated from a number and a string");
test.end();
});
test.end();
});
});