-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathgenerate-code.test.ts
More file actions
239 lines (226 loc) · 7.36 KB
/
generate-code.test.ts
File metadata and controls
239 lines (226 loc) · 7.36 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2021-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { describe, test, expect } from "@jest/globals";
import type { Int32Value, StringValue } from "@bufbuild/protobuf/wkt";
import { hasExtension } from "@bufbuild/protobuf";
import type { GenDescService } from "@bufbuild/protobuf/codegenv1";
import * as proto2_ts from "./gen/ts/extra/proto2_pb.js";
import * as proto2_js from "./gen/js/extra/proto2_pb.js";
import * as proto3_ts from "./gen/ts/extra/proto3_pb.js";
import * as proto3_js from "./gen/js/extra/proto3_pb.js";
import * as edition2023_ts from "./gen/ts/extra/edition2023_pb.js";
import * as edition2023_js from "./gen/js/extra/edition2023_pb.js";
import * as nameclash_ts from "./gen/ts/extra/name-clash_pb.js";
import * as nameclash_js from "./gen/js/extra/name-clash_pb.js";
import * as service_ts from "./gen/ts/extra/service-all_pb.js";
import * as service_js from "./gen/js/extra/service-all_pb.js";
import * as test_messages_proto2_ts from "./gen/ts/google/protobuf/test_messages_proto2_pb.js";
import * as test_messages_proto2_js from "./gen/js/google/protobuf/test_messages_proto2_pb.js";
import * as test_messages_proto3_ts from "./gen/ts/google/protobuf/test_messages_proto3_pb.js";
import * as test_messages_proto3_js from "./gen/js/google/protobuf/test_messages_proto3_pb.js";
import * as options_ts from "./gen/ts/extra/options_pb.js";
import * as option_usage_ts from "./gen/ts/extra/option-usage_pb.js";
test("source retention options are unavailable in generated code", () => {
const fileOptions = option_usage_ts.fileDesc_extra_option_usage.proto.options;
expect(
!!fileOptions &&
hasExtension(fileOptions, options_ts.file_option_retention_source),
).toBe(false);
const messageOptions = option_usage_ts.MessageWithOptionsDesc.proto.options;
expect(
!!messageOptions &&
hasExtension(messageOptions, options_ts.message_option_retention_source),
).toBe(false);
const fieldOptions =
option_usage_ts.MessageWithOptionsDesc.fields[0].proto.options;
expect(
!!fieldOptions &&
hasExtension(fieldOptions, options_ts.field_option_retention_source),
).toBe(false);
const oneofOptions =
option_usage_ts.MessageWithOptionsDesc.oneofs[0].proto.options;
expect(
!!oneofOptions &&
hasExtension(oneofOptions, options_ts.oneof_option_retention_source),
).toBe(false);
const enumOptions = option_usage_ts.EnumWithOptionsDesc.proto.options;
expect(
!!enumOptions &&
hasExtension(enumOptions, options_ts.enum_option_retention_source),
).toBe(false);
const enumValueOptions =
option_usage_ts.EnumWithOptionsDesc.values[0].proto.options;
expect(
!!enumValueOptions &&
hasExtension(
enumValueOptions,
options_ts.enum_value_option_retention_source,
),
).toBe(false);
const serviceOptions = option_usage_ts.ServiceWithOptions.proto.options;
expect(
!!serviceOptions &&
hasExtension(serviceOptions, options_ts.service_option_retention_source),
).toBe(false);
const methodOptions =
option_usage_ts.ServiceWithOptions.methods[0].proto.options;
expect(
!!methodOptions &&
hasExtension(methodOptions, options_ts.method_option_retention_source),
).toBe(false);
});
test("ts generated code is assignable to js", () => {
expect([
function f(ts: proto2_ts.Proto2Enum, js: proto2_js.Proto2Enum) {
ts = js;
js = ts;
return [ts, js];
},
function f(ts: proto2_ts.Proto2Message, js: proto2_js.Proto2Message) {
ts = js;
js = ts;
return [ts, js];
},
function f(ts: proto3_ts.Proto3Enum, js: proto3_js.Proto3Enum) {
ts = js;
js = ts;
return [ts, js];
},
function f(ts: proto3_ts.Proto3Message, js: proto3_js.Proto3Message) {
ts = js;
js = ts;
return [ts, js];
},
function f(
ts: edition2023_ts.Edition2023EnumOpen,
js: edition2023_js.Edition2023EnumOpen,
) {
ts = js;
js = ts;
return [ts, js];
},
function f(
ts: edition2023_ts.Edition2023Message,
js: edition2023_js.Edition2023Message,
) {
ts = js;
js = ts;
return [ts, js];
},
function f(
ts: test_messages_proto2_ts.TestAllTypesProto2,
js: test_messages_proto2_js.TestAllTypesProto2,
) {
ts = js;
js = ts;
return [ts, js];
},
function f(
ts: test_messages_proto3_ts.TestAllTypesProto3,
js: test_messages_proto3_js.TestAllTypesProto3,
) {
ts = js;
js = ts;
return [ts, js];
},
function f(
ts: typeof service_ts.ServiceAll,
js: typeof service_js.ServiceAll,
) {
ts = js;
js = ts;
return [ts, js];
},
]).toBeDefined();
});
test("service generates as expected", () => {
type Expected = {
unary: {
kind: "unary";
I: StringValue;
O: Int32Value;
};
serverStream: {
kind: "server_streaming";
I: StringValue;
O: Int32Value;
};
clientStream: {
kind: "client_streaming";
I: StringValue;
O: Int32Value;
};
bidi: {
kind: "bidi_streaming";
I: StringValue;
O: Int32Value;
};
};
type Actual<T> = T extends GenDescService<infer Shape> ? Shape : never;
function f(expected: Expected, actual: Actual<typeof service_js.ServiceAll>) {
expected = actual;
actual = expected;
return [expected, actual];
}
expect(f).toBeDefined();
});
describe("ts generated code is equal to js generated code", () => {
test("proto2", () => {
expect(toPlain(proto2_ts)).toStrictEqual(toPlain(proto2_js));
});
test("proto3", () => {
expect(toPlain(proto3_ts)).toStrictEqual(toPlain(proto3_js));
});
test("edition2023", () => {
expect(toPlain(edition2023_ts)).toStrictEqual(toPlain(edition2023_js));
});
test("nameclash", () => {
expect(toPlain(nameclash_ts)).toStrictEqual(toPlain(nameclash_js));
});
test("test_messages_proto3", () => {
expect(toPlain(test_messages_proto3_ts)).toStrictEqual(
toPlain(test_messages_proto3_js),
);
});
test("service", () => {
expect(toPlain(service_ts)).toStrictEqual(toPlain(service_js));
});
/**
* Convert to plain object for comparison with Jest.
* Replaces cyclic references.
*/
function toPlain(obj: unknown): unknown {
const seen = new Map<object, string>();
return JSON.parse(
JSON.stringify(obj, (_, value: unknown): unknown => {
if (typeof value == "bigint") {
return value.toString() + "n";
}
if (typeof value == "object" && value !== null) {
let id = seen.get(value);
if (id !== undefined) {
return id;
}
if ("toString" in value) {
id = String(value) + "@" + seen.size;
} else {
id = "unknown@" + seen.size;
}
seen.set(value, id);
}
return value;
}),
);
}
});