Skip to content

Commit 9e48ebc

Browse files
committed
Create dedicated message for json encoding tests
1 parent ca55901 commit 9e48ebc

File tree

6 files changed

+2607
-546
lines changed

6 files changed

+2607
-546
lines changed

Protos/SwiftProtobufTests/test_messages_proto3.proto

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -60,65 +60,44 @@ message TestAllTypesProto3 {
6060
}
6161

6262
// Singular
63-
optional int32 optional_int32 = 1;
64-
optional int64 optional_int64 = 2;
65-
optional uint32 optional_uint32 = 3;
66-
optional uint64 optional_uint64 = 4;
67-
optional sint32 optional_sint32 = 5;
68-
optional sint64 optional_sint64 = 6;
69-
optional fixed32 optional_fixed32 = 7;
70-
optional fixed64 optional_fixed64 = 8;
71-
optional sfixed32 optional_sfixed32 = 9;
72-
optional sfixed64 optional_sfixed64 = 10;
73-
optional float optional_float = 11;
74-
optional double optional_double = 12;
75-
optional bool optional_bool = 13;
76-
optional string optional_string = 14;
77-
optional bytes optional_bytes = 15;
78-
79-
optional NestedMessage optional_nested_message = 16;
80-
81-
optional NestedEnum optional_nested_enum = 17;
82-
83-
int32 singular_int32 = 18;
84-
int64 singular_int64 = 19;
85-
uint32 singular_uint32 = 20;
86-
uint64 singular_uint64 = 21;
87-
sint32 singular_sint32 = 22;
88-
sint64 singular_sint64 = 23;
89-
fixed32 singular_fixed32 = 24;
90-
fixed64 singular_fixed64 = 25;
91-
sfixed32 singular_sfixed32 = 26;
92-
sfixed64 singular_sfixed64 = 27;
93-
float singular_float = 28;
94-
double singular_double = 29;
95-
bool singular_bool = 30;
96-
string singular_string = 31;
97-
bytes singular_bytes = 32;
98-
99-
NestedMessage singular_nested_message = 33;
100-
101-
NestedEnum singular_nested_enum = 34;
102-
63+
int32 optional_int32 = 1;
64+
int64 optional_int64 = 2;
65+
uint32 optional_uint32 = 3;
66+
uint64 optional_uint64 = 4;
67+
sint32 optional_sint32 = 5;
68+
sint64 optional_sint64 = 6;
69+
fixed32 optional_fixed32 = 7;
70+
fixed64 optional_fixed64 = 8;
71+
sfixed32 optional_sfixed32 = 9;
72+
sfixed64 optional_sfixed64 = 10;
73+
float optional_float = 11;
74+
double optional_double = 12;
75+
bool optional_bool = 13;
76+
string optional_string = 14;
77+
bytes optional_bytes = 15;
78+
79+
NestedMessage optional_nested_message = 18;
80+
81+
NestedEnum optional_nested_enum = 21;
10382

10483
// Repeated
105-
repeated int32 repeated_int32 = 35;
106-
repeated int64 repeated_int64 = 36;
107-
repeated uint32 repeated_uint32 = 37;
108-
repeated uint64 repeated_uint64 = 38;
109-
repeated sint32 repeated_sint32 = 39;
110-
repeated sint64 repeated_sint64 = 40;
111-
repeated fixed32 repeated_fixed32 = 41;
112-
repeated fixed64 repeated_fixed64 = 42;
113-
repeated sfixed32 repeated_sfixed32 = 43;
114-
repeated sfixed64 repeated_sfixed64 = 44;
115-
repeated float repeated_float = 45;
116-
repeated double repeated_double = 46;
117-
repeated bool repeated_bool = 47;
118-
repeated string repeated_string = 48;
119-
repeated bytes repeated_bytes = 49;
120-
121-
repeated NestedMessage repeated_nested_message = 50;
84+
repeated int32 repeated_int32 = 31;
85+
repeated int64 repeated_int64 = 32;
86+
repeated uint32 repeated_uint32 = 33;
87+
repeated uint64 repeated_uint64 = 34;
88+
repeated sint32 repeated_sint32 = 35;
89+
repeated sint64 repeated_sint64 = 36;
90+
repeated fixed32 repeated_fixed32 = 37;
91+
repeated fixed64 repeated_fixed64 = 38;
92+
repeated sfixed32 repeated_sfixed32 = 39;
93+
repeated sfixed64 repeated_sfixed64 = 40;
94+
repeated float repeated_float = 41;
95+
repeated double repeated_double = 42;
96+
repeated bool repeated_bool = 43;
97+
repeated string repeated_string = 44;
98+
repeated bytes repeated_bytes = 45;
99+
100+
repeated NestedMessage repeated_nested_message = 48;
122101

123102
repeated NestedEnum repeated_nested_enum = 51;
124103

@@ -175,12 +154,12 @@ message TestAllTypesProto3 {
175154
repeated google.protobuf.StringValue repeated_string_wrapper = 218;
176155
repeated google.protobuf.BytesValue repeated_bytes_wrapper = 219;
177156

178-
optional google.protobuf.Duration optional_duration = 301;
179-
optional google.protobuf.Timestamp optional_timestamp = 302;
180-
optional google.protobuf.FieldMask optional_field_mask = 303;
181-
optional google.protobuf.Struct optional_struct = 304;
182-
optional google.protobuf.Any optional_any = 305;
183-
optional google.protobuf.Value optional_value = 306;
157+
google.protobuf.Duration optional_duration = 301;
158+
google.protobuf.Timestamp optional_timestamp = 302;
159+
google.protobuf.FieldMask optional_field_mask = 303;
160+
google.protobuf.Struct optional_struct = 304;
161+
google.protobuf.Any optional_any = 305;
162+
google.protobuf.Value optional_value = 306;
184163
google.protobuf.NullValue optional_null_value = 307;
185164

186165
repeated google.protobuf.Duration repeated_duration = 311;
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
// https://developers.google.com/protocol-buffers/
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above
12+
// copyright notice, this list of conditions and the following disclaimer
13+
// in the documentation and/or other materials provided with the
14+
// distribution.
15+
// * Neither the name of Google Inc. nor the names of its
16+
// contributors may be used to endorse or promote products derived from
17+
// this software without specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
syntax = "proto3";
32+
33+
package swift_proto_testing.test3;
34+
35+
import "google/protobuf/any.proto";
36+
import "google/protobuf/duration.proto";
37+
import "google/protobuf/field_mask.proto";
38+
import "google/protobuf/struct.proto";
39+
import "google/protobuf/timestamp.proto";
40+
import "google/protobuf/wrappers.proto";
41+
42+
// This proto includes every type of field in singular and repeated
43+
// and optional forms.
44+
message TestJsonAllTypesProto3 {
45+
message NestedMessage {
46+
int32 a = 1;
47+
TestJsonAllTypesProto3 corecursive = 2;
48+
}
49+
50+
enum NestedEnum {
51+
FOO = 0;
52+
BAR = 1;
53+
BAZ = 2;
54+
NEG = -1; // Intentionally negative.
55+
}
56+
57+
// Singular
58+
optional int32 optional_int32 = 1;
59+
optional int64 optional_int64 = 2;
60+
optional uint32 optional_uint32 = 3;
61+
optional uint64 optional_uint64 = 4;
62+
optional sint32 optional_sint32 = 5;
63+
optional sint64 optional_sint64 = 6;
64+
optional fixed32 optional_fixed32 = 7;
65+
optional fixed64 optional_fixed64 = 8;
66+
optional sfixed32 optional_sfixed32 = 9;
67+
optional sfixed64 optional_sfixed64 = 10;
68+
optional float optional_float = 11;
69+
optional double optional_double = 12;
70+
optional bool optional_bool = 13;
71+
optional string optional_string = 14;
72+
optional bytes optional_bytes = 15;
73+
74+
optional NestedMessage optional_nested_message = 16;
75+
76+
optional NestedEnum optional_nested_enum = 17;
77+
78+
int32 singular_int32 = 18;
79+
int64 singular_int64 = 19;
80+
uint32 singular_uint32 = 20;
81+
uint64 singular_uint64 = 21;
82+
sint32 singular_sint32 = 22;
83+
sint64 singular_sint64 = 23;
84+
fixed32 singular_fixed32 = 24;
85+
fixed64 singular_fixed64 = 25;
86+
sfixed32 singular_sfixed32 = 26;
87+
sfixed64 singular_sfixed64 = 27;
88+
float singular_float = 28;
89+
double singular_double = 29;
90+
bool singular_bool = 30;
91+
string singular_string = 31;
92+
bytes singular_bytes = 32;
93+
94+
NestedMessage singular_nested_message = 33;
95+
96+
NestedEnum singular_nested_enum = 34;
97+
98+
99+
// Repeated
100+
repeated int32 repeated_int32 = 35;
101+
repeated int64 repeated_int64 = 36;
102+
repeated uint32 repeated_uint32 = 37;
103+
repeated uint64 repeated_uint64 = 38;
104+
repeated sint32 repeated_sint32 = 39;
105+
repeated sint64 repeated_sint64 = 40;
106+
repeated fixed32 repeated_fixed32 = 41;
107+
repeated fixed64 repeated_fixed64 = 42;
108+
repeated sfixed32 repeated_sfixed32 = 43;
109+
repeated sfixed64 repeated_sfixed64 = 44;
110+
repeated float repeated_float = 45;
111+
repeated double repeated_double = 46;
112+
repeated bool repeated_bool = 47;
113+
repeated string repeated_string = 48;
114+
repeated bytes repeated_bytes = 49;
115+
116+
repeated NestedMessage repeated_nested_message = 50;
117+
118+
repeated NestedEnum repeated_nested_enum = 51;
119+
120+
// Map
121+
map<int32, int32> map_int32_int32 = 56;
122+
map<int64, int64> map_int64_int64 = 57;
123+
map<uint32, uint32> map_uint32_uint32 = 58;
124+
map<uint64, uint64> map_uint64_uint64 = 59;
125+
map<sint32, sint32> map_sint32_sint32 = 60;
126+
map<sint64, sint64> map_sint64_sint64 = 61;
127+
map<fixed32, fixed32> map_fixed32_fixed32 = 62;
128+
map<fixed64, fixed64> map_fixed64_fixed64 = 63;
129+
map<sfixed32, sfixed32> map_sfixed32_sfixed32 = 64;
130+
map<sfixed64, sfixed64> map_sfixed64_sfixed64 = 65;
131+
map<int32, float> map_int32_float = 66;
132+
map<int32, double> map_int32_double = 67;
133+
map<bool, bool> map_bool_bool = 68;
134+
map<string, string> map_string_string = 69;
135+
map<string, bytes> map_string_bytes = 70;
136+
map<string, NestedMessage> map_string_nested_message = 71;
137+
map<string, NestedEnum> map_string_nested_enum = 73;
138+
139+
oneof oneof_field {
140+
uint32 oneof_uint32 = 111;
141+
NestedMessage oneof_nested_message = 112;
142+
string oneof_string = 113;
143+
bytes oneof_bytes = 114;
144+
bool oneof_bool = 115;
145+
uint64 oneof_uint64 = 116;
146+
float oneof_float = 117;
147+
double oneof_double = 118;
148+
NestedEnum oneof_enum = 119;
149+
google.protobuf.NullValue oneof_null_value = 120;
150+
}
151+
152+
// Well-known types
153+
google.protobuf.BoolValue optional_bool_wrapper = 201;
154+
google.protobuf.Int32Value optional_int32_wrapper = 202;
155+
google.protobuf.Int64Value optional_int64_wrapper = 203;
156+
google.protobuf.UInt32Value optional_uint32_wrapper = 204;
157+
google.protobuf.UInt64Value optional_uint64_wrapper = 205;
158+
google.protobuf.FloatValue optional_float_wrapper = 206;
159+
google.protobuf.DoubleValue optional_double_wrapper = 207;
160+
google.protobuf.StringValue optional_string_wrapper = 208;
161+
google.protobuf.BytesValue optional_bytes_wrapper = 209;
162+
163+
repeated google.protobuf.BoolValue repeated_bool_wrapper = 211;
164+
repeated google.protobuf.Int32Value repeated_int32_wrapper = 212;
165+
repeated google.protobuf.Int64Value repeated_int64_wrapper = 213;
166+
repeated google.protobuf.UInt32Value repeated_uint32_wrapper = 214;
167+
repeated google.protobuf.UInt64Value repeated_uint64_wrapper = 215;
168+
repeated google.protobuf.FloatValue repeated_float_wrapper = 216;
169+
repeated google.protobuf.DoubleValue repeated_double_wrapper = 217;
170+
repeated google.protobuf.StringValue repeated_string_wrapper = 218;
171+
repeated google.protobuf.BytesValue repeated_bytes_wrapper = 219;
172+
173+
optional google.protobuf.Duration optional_duration = 301;
174+
optional google.protobuf.Timestamp optional_timestamp = 302;
175+
optional google.protobuf.FieldMask optional_field_mask = 303;
176+
optional google.protobuf.Struct optional_struct = 304;
177+
optional google.protobuf.Any optional_any = 305;
178+
optional google.protobuf.Value optional_value = 306;
179+
optional google.protobuf.NullValue optional_null_value = 307;
180+
181+
repeated google.protobuf.Duration repeated_duration = 311;
182+
repeated google.protobuf.Timestamp repeated_timestamp = 312;
183+
repeated google.protobuf.FieldMask repeated_fieldmask = 313;
184+
repeated google.protobuf.Struct repeated_struct = 324;
185+
repeated google.protobuf.Any repeated_any = 315;
186+
repeated google.protobuf.Value repeated_value = 316;
187+
repeated google.protobuf.ListValue repeated_list_value = 317;
188+
189+
// Test field-name-to-JSON-name convention.
190+
// (protobuf says names can be any valid C/C++ identifier.)
191+
int32 fieldname1 = 401;
192+
int32 field_name2 = 402;
193+
int32 _field_name3 = 403;
194+
int32 field__name4_ = 404;
195+
int32 field0name5 = 405;
196+
int32 field_0_name6 = 406;
197+
int32 fieldName7 = 407;
198+
int32 FieldName8 = 408;
199+
int32 field_Name9 = 409;
200+
int32 Field_Name10 = 410;
201+
int32 FIELD_NAME11 = 411;
202+
int32 FIELD_name12 = 412;
203+
int32 __field_name13 = 413;
204+
int32 __Field_name14 = 414;
205+
int32 field__name15 = 415;
206+
int32 field__Name16 = 416;
207+
int32 field_name17__ = 417;
208+
int32 Field_name18__ = 418;
209+
210+
// Reserved for testing unknown fields
211+
reserved 501 to 510;
212+
}

Tests/SwiftProtobufTests/TestHelpers.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,17 @@ extension PBTestHelpers where MessageTestType: SwiftProtobuf.Message & Equatable
134134

135135
}
136136

137-
func assertJSONEncode(_ expected: String, extensions: any ExtensionMap = SimpleExtensionMap(), encodingOptions: JSONEncodingOptions = .init(), allowUnconfigured: Bool = false, assertEncodeDecodeEqual: Bool = true, file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> Void) {
137+
func assertJSONEncode(_ expected: String, extensions: any ExtensionMap = SimpleExtensionMap(), encodingOptions: JSONEncodingOptions = .init(), file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> Void) {
138138
let empty = MessageTestType()
139139
var configured = empty
140140
configure(&configured)
141-
if !allowUnconfigured {
142-
XCTAssert(configured != empty, "Object should not be equal to empty object", file: file, line: line)
143-
}
141+
XCTAssert(configured != empty, "Object should not be equal to empty object", file: file, line: line)
144142
do {
145143
let encoded = try configured.jsonString(options: encodingOptions)
146144
XCTAssert(expected == encoded, "Did not encode correctly: got \(encoded) but expected \(expected)", file: file, line: line)
147145
do {
148146
let decoded = try MessageTestType(jsonString: encoded, extensions: extensions)
149-
if assertEncodeDecodeEqual {
150-
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
151-
}
147+
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
152148
} catch {
153149
XCTFail("Encode/decode cycle should not throw error decoding: \(encoded), but it threw \(error)", file: file, line: line)
154150
}
@@ -164,9 +160,7 @@ extension PBTestHelpers where MessageTestType: SwiftProtobuf.Message & Equatable
164160
XCTAssert(expected == encodedString, "Did not encode correctly: got \(encodedString)", file: file, line: line)
165161
do {
166162
let decoded = try MessageTestType(jsonUTF8Bytes: encodedData, extensions: extensions)
167-
if assertEncodeDecodeEqual {
168-
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
169-
}
163+
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
170164
} catch {
171165
XCTFail("Encode/decode cycle should not throw error decoding: \(encodedString), but it threw \(error)", file: file, line: line)
172166
}

0 commit comments

Comments
 (0)