/
api_oneof.js
61 lines (50 loc) · 1.68 KB
/
api_oneof.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
var tape = require("tape");
var protobuf = require("..");
var def = {
oneof: ["a", "b"],
options: {}
};
var proto = "syntax = \"proto3\";\
import \"google/protobuf/descriptor.proto\";\
extend google.protobuf.FileOptions { optional int32 ecs_component_id = 50000;}\
option (ecs_component_id) = 1020;\
message Test {\
oneof kind {\
uint32 a = 1;\
string b = 2;\
}\
bool c = 3;\
}";
tape.test("reflected oneofs", function(test) {
var oneof = protobuf.OneOf.fromJSON("kind", {
oneof: ["a", "b"],
options: {}
});
test.same(oneof.toJSON(), def, "should construct from and convert back to JSON");
var root = protobuf.parse(proto).root;
var Test = root.lookup("Test");
var kind = Test.get("kind");
var field = Test.get("c");
kind.add(field);
test.same(kind.toJSON(), {
oneof: ["a", "b", "c"]
}, "should allow adding fields");
test.ok(Test.get("c"), "should still have the field on the parent");
kind.remove(field);
test.same(kind.toJSON(), {
oneof: ["a", "b"]
}, "should allow removing fields");
test.ok(Test.get("c"), "should still have the field on the parent");
var Test2 = new protobuf.Type("Test2");
root.add(Test2);
Test2.add(field);
kind.add(field);
test.notOk(Test2.get("c"), "should remove the field from the previous parent");
var looseField = new protobuf.Field("d", 4, "float");
kind.add(looseField); // no parent
Test.remove(looseField);
Test.remove(kind);
test.same(Test.fields, {}, "should remove all fields from the parent");
test.same(kind.oneof, ["a","b","c","d"], "should still have the fields on the oneof");
test.end();
});