@@ -106,7 +106,8 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
106
106
XCTAssertThrowsError ( try m. jsonString ( ) )
107
107
}
108
108
}
109
-
109
+
110
+ // Checks merge functionality for field masks.
110
111
func testMergeFieldsOfMessage( ) throws {
111
112
var message = SwiftProtoTesting_TestAllTypes . with { model in
112
113
model. optionalInt32 = 1
@@ -122,15 +123,18 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
122
123
}
123
124
}
124
125
126
+ // Checks nested message merge
125
127
try message. merge ( to: secondMessage, fieldMask: . init( protoPaths: " optional_nested_message.bb " ) )
126
128
XCTAssertEqual ( message. optionalInt32, 1 )
127
129
XCTAssertEqual ( message. optionalNestedMessage. bb, 3 )
128
130
131
+ // Checks primitive type merge
129
132
try message. merge ( to: secondMessage, fieldMask: . init( protoPaths: " optional_int32 " ) )
130
133
XCTAssertEqual ( message. optionalInt32, 2 )
131
134
XCTAssertEqual ( message. optionalNestedMessage. bb, 3 )
132
135
}
133
136
137
+ // Checks trim functionality for field masks.
134
138
func testTrimFieldsOfMessage( ) throws {
135
139
var message = SwiftProtoTesting_TestAllTypes . with { model in
136
140
model. optionalInt32 = 1
@@ -139,28 +143,41 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
139
143
}
140
144
}
141
145
146
+ // Checks trim to be successful.
142
147
let r1 = message. trim ( fieldMask: . init( protoPaths: " optional_nested_message.bb " ) )
143
148
XCTAssertTrue ( r1)
144
149
XCTAssertEqual ( message. optionalInt32, 0 )
145
150
XCTAssertEqual ( message. optionalNestedMessage. bb, 2 )
146
151
152
+ // Checks trim should does nothing with an empty fieldMask.
147
153
let r2 = message. trim ( fieldMask: . init( ) )
148
154
XCTAssertFalse ( r2)
149
155
156
+ // Checks trim should return false if nothing has been changed.
150
157
let r3 = message. trim ( fieldMask: . init( protoPaths: " optional_nested_message.bb " ) )
151
158
XCTAssertFalse ( r3)
152
159
160
+ // Checks trim to be unsuccessful with an invalid fieldMask.
153
161
let r4 = message. trim ( fieldMask: . init( protoPaths: " invalid_path " ) )
154
162
XCTAssertFalse ( r4)
155
163
}
156
164
165
+ // Checks `isPathValid` func
166
+ // 1. Valid primitive path should be valid.
167
+ // 2. Valid nested path should be valid.
168
+ // 3. Invalid primitive path should be valid.
169
+ // 4. Invalid nested path should be valid.
157
170
func testIsPathValid( ) {
158
171
XCTAssertTrue ( SwiftProtoTesting_TestAllTypes . isPathValid ( " optional_int32 " ) )
159
172
XCTAssertTrue ( SwiftProtoTesting_TestAllTypes . isPathValid ( " optional_nested_message.bb " ) )
160
173
XCTAssertFalse ( SwiftProtoTesting_TestAllTypes . isPathValid ( " optional_int " ) )
161
174
XCTAssertFalse ( SwiftProtoTesting_TestAllTypes . isPathValid ( " optional_nested_message.bc " ) )
162
175
}
163
176
177
+ // Checks `isValid` func of FieldMask.
178
+ // 1. Empty field mask is always valid.
179
+ // 2, 3. Valid field masks.
180
+ // 4, 5. Invalid field masks.
164
181
func testIsFieldMaskValid( ) {
165
182
let m1 = Google_Protobuf_FieldMask ( )
166
183
let m2 = Google_Protobuf_FieldMask ( protoPaths: [
@@ -186,15 +203,23 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
186
203
XCTAssertFalse ( m5. isValid ( for: SwiftProtoTesting_TestAllTypes . self) )
187
204
}
188
205
206
+ // Checks canonincal form of field mask.
207
+ // 1. Sub-message with parent in the paths should be excluded.
208
+ // 2. Canonincal form should be sorted.
209
+ // 3. More nested levels of paths.
189
210
func testCanonicalFieldMask( ) {
190
- let m1 = Google_Protobuf_FieldMask ( protoPaths: [ " a.b " , " b " , " a " ] )
211
+ let m1 = Google_Protobuf_FieldMask ( protoPaths: [ " a.b " , " a " , " b " ] )
191
212
XCTAssertEqual ( m1. canonical. paths, [ " a " , " b " ] )
192
- let m2 = Google_Protobuf_FieldMask ( protoPaths: [ " a " , " b " ] )
213
+ let m2 = Google_Protobuf_FieldMask ( protoPaths: [ " b " , " a " ] )
193
214
XCTAssertEqual ( m2. canonical. paths, [ " a " , " b " ] )
194
215
let m3 = Google_Protobuf_FieldMask ( protoPaths: [ " c " , " a.b.c " , " a.b " , " a.b.c.d " ] )
195
216
XCTAssertEqual ( m3. canonical. paths, [ " a.b " , " c " ] )
196
217
}
197
218
219
+ // Checks `addPath` func of fieldMask with:
220
+ // - Valid primitive path should be added.
221
+ // - Valid nested path should be added.
222
+ // - Invalid path should throw error.
198
223
func testAddPathToFieldMask( ) throws {
199
224
var mask = Google_Protobuf_FieldMask ( )
200
225
XCTAssertNoThrow ( try mask. addPath ( " optional_int32 " , of: SwiftProtoTesting_TestAllTypes . self) )
@@ -204,6 +229,12 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
204
229
XCTAssertThrowsError ( try mask. addPath ( " optional_int " , of: SwiftProtoTesting_TestAllTypes . self) )
205
230
}
206
231
232
+ // Check `contains` func of fieldMask.
233
+ // 1. Parent contains sub-message.
234
+ // 2. Path contains itself.
235
+ // 3. Sub-message does not contain its parent.
236
+ // 4. Two different paths does not contain each other.
237
+ // 5. Two different sub-paths does not contain each other.
207
238
func testPathContainsInFieldMask( ) {
208
239
let m1 = Google_Protobuf_FieldMask ( protoPaths: [ " a " ] )
209
240
XCTAssertTrue ( m1. contains ( " a.b " ) )
@@ -217,6 +248,9 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
217
248
XCTAssertFalse ( m5. contains ( " a.c " ) )
218
249
}
219
250
251
+ // Checks inits of fieldMask with:
252
+ // - All fields of a message type.
253
+ // - Particular field numbers of a message type.
220
254
func testFieldPathMessageInits( ) throws {
221
255
let m1 = Google_Protobuf_FieldMask ( allFieldsOf: SwiftProtoTesting_TestAny . self)
222
256
XCTAssertEqual ( m1. paths. sorted ( ) , [ " any_value " , " int32_value " , " repeated_any_value " , " text " ] )
@@ -225,6 +259,7 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
225
259
XCTAssertThrowsError ( try Google_Protobuf_FieldMask ( fieldNumbers: [ 10 ] , of: SwiftProtoTesting_TestAny . self) )
226
260
}
227
261
262
+ // Checks `union` func of fieldMask.
228
263
func testUnionFieldMasks( ) throws {
229
264
let m1 = Google_Protobuf_FieldMask ( protoPaths: [ " a " , " b " ] )
230
265
let m2 = Google_Protobuf_FieldMask ( protoPaths: [ " b " , " c " ] )
@@ -243,6 +278,7 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
243
278
XCTAssertEqual ( m7. union ( m8) . paths, [ " a " , " b " ] )
244
279
}
245
280
281
+ // Checks `intersect` func of fieldMask.
246
282
func testIntersectFieldMasks( ) throws {
247
283
let m1 = Google_Protobuf_FieldMask ( protoPaths: [ " a " , " b " ] )
248
284
let m2 = Google_Protobuf_FieldMask ( protoPaths: [ " b " , " c " ] )
@@ -261,6 +297,7 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
261
297
XCTAssertEqual ( m7. intersect ( m8) . paths, [ " a " , " b " ] )
262
298
}
263
299
300
+ // Checks `substract` func of fieldMask.
264
301
func testSubtractFieldMasks( ) throws {
265
302
let m1 = Google_Protobuf_FieldMask ( protoPaths: [ " a " , " b " ] )
266
303
let m2 = Google_Protobuf_FieldMask ( protoPaths: [ " b " , " c " ] )
0 commit comments