-
Notifications
You must be signed in to change notification settings - Fork 3
/
field_test.ts
133 lines (131 loc) · 2.7 KB
/
field_test.ts
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
import { assertNode, assertNodeThrows } from "./testutil.ts";
import { Field } from "./field.ts";
import { Type } from "./type.ts";
import { FieldOption } from "./fieldoption.ts";
import { Constant } from "./constant.ts";
Deno.test("Field", async () => {
const tt: [string, Field, 2 | 3][] = [
[
`optional float F_Ninf = 16;`,
new Field(
new Type(
"float",
[1, 10],
[1, 14],
),
"F_Ninf",
16,
{ optional: true },
[],
[1, 1],
[1, 27],
),
2,
],
[
`.Foo bar = 1;`,
new Field(
new Type(
".Foo",
[1, 1],
[1, 4],
),
"bar",
1,
{},
[],
[1, 1],
[1, 13],
),
2,
],
[
`foo.bar nested_message = 2;`,
new Field(
new Type(
"foo.bar",
[1, 1],
[1, 7],
),
"nested_message",
2,
{},
[],
[1, 1],
[1, 27],
),
3,
],
[
`repeated int32 samples = 4 [packed = true];`,
new Field(
new Type(
"int32",
[1, 10],
[1, 14],
),
"samples",
4,
{ repeated: true },
[
new FieldOption(
["packed"],
false,
new Constant("boolean", "true", [1, 38], [1, 41]),
[1, 29],
[1, 41],
),
],
[1, 1],
[1, 43],
),
3,
],
[
`repeated int32 samples = 4 [(some.nested).key = true];`,
new Field(
new Type(
"int32",
[1, 10],
[1, 14],
),
"samples",
4,
{ repeated: true },
[
new FieldOption(
["some.nested", "key"],
true,
new Constant("boolean", "true", [1, 49], [1, 52]),
[1, 29],
[1, 52],
),
],
[1, 1],
[1, 54],
),
3,
],
];
for (const t of tt) await assertNode(Field, ...t);
});
Deno.test("Field errors", async () => {
const tt: [string, string, 2 | 3][] = [
[
`optional float foo = 1;`,
`unexpected identifier (optional) on line 1, column 1; expected identifier (optional fields are not allowed in Proto3)`,
3,
],
[
`required float foo = 1;`,
`unexpected identifier (required) on line 1, column 1; expected identifier (required fields are not allowed in Proto3)`,
3,
],
[
`float foo.bar = 1;`,
`unexpected token (.) on line 1, column 10; expected identifier`,
3,
],
];
for (const t of tt) await assertNodeThrows(Field, ...t);
});