Skip to content

Commit 2dea3b1

Browse files
committed
Add more tests
1 parent c0328a0 commit 2dea3b1

File tree

3 files changed

+458
-53
lines changed

3 files changed

+458
-53
lines changed

Protos/SwiftProtobufTests/test_messages_proto3.proto

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

6262
// Singular
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;
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+
82103

83104
// Repeated
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;
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;
101122

102123
repeated NestedEnum repeated_nested_enum = 51;
103124

@@ -154,12 +175,12 @@ message TestAllTypesProto3 {
154175
repeated google.protobuf.StringValue repeated_string_wrapper = 218;
155176
repeated google.protobuf.BytesValue repeated_bytes_wrapper = 219;
156177

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;
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;
163184
google.protobuf.NullValue optional_null_value = 307;
164185

165186
repeated google.protobuf.Duration repeated_duration = 311;

Tests/SwiftProtobufTests/TestHelpers.swift

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

135135
}
136136

137-
func assertJSONEncode(_ expected: String, extensions: any ExtensionMap = SimpleExtensionMap(), encodingOptions: JSONEncodingOptions = .init(), file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> Void) {
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) {
138138
let empty = MessageTestType()
139139
var configured = empty
140140
configure(&configured)
141-
XCTAssert(configured != empty, "Object should not be equal to empty object", file: file, line: line)
141+
if !allowUnconfigured {
142+
XCTAssert(configured != empty, "Object should not be equal to empty object", file: file, line: line)
143+
}
142144
do {
143145
let encoded = try configured.jsonString(options: encodingOptions)
144146
XCTAssert(expected == encoded, "Did not encode correctly: got \(encoded) but expected \(expected)", file: file, line: line)
145147
do {
146148
let decoded = try MessageTestType(jsonString: encoded, extensions: extensions)
147-
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
149+
if assertEncodeDecodeEqual {
150+
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
151+
}
148152
} catch {
149153
XCTFail("Encode/decode cycle should not throw error decoding: \(encoded), but it threw \(error)", file: file, line: line)
150154
}
@@ -160,7 +164,9 @@ extension PBTestHelpers where MessageTestType: SwiftProtobuf.Message & Equatable
160164
XCTAssert(expected == encodedString, "Did not encode correctly: got \(encodedString)", file: file, line: line)
161165
do {
162166
let decoded = try MessageTestType(jsonUTF8Bytes: encodedData, extensions: extensions)
163-
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
167+
if assertEncodeDecodeEqual {
168+
XCTAssert(decoded == configured, "Encode/decode cycle should generate equal object: \(decoded) != \(configured)", file: file, line: line)
169+
}
164170
} catch {
165171
XCTFail("Encode/decode cycle should not throw error decoding: \(encodedString), but it threw \(error)", file: file, line: line)
166172
}

0 commit comments

Comments
 (0)