Skip to content

Commit db6be11

Browse files
author
Pouya Yarandi
committed
Improve tests
1 parent aae790b commit db6be11

File tree

3 files changed

+78
-48
lines changed

3 files changed

+78
-48
lines changed

Sources/SwiftProtobuf/PathDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ struct PathDecoder<T: Message>: Decoder {
116116
private func setMapValue<K, V>(
117117
_ value: inout Dictionary<K, V>
118118
) throws {
119+
if !nextPath.isEmpty {
120+
throw PathDecodingError.pathNotFound
121+
}
119122
var castedValue: [K: V] = [:]
120123
if self.value != nil {
121124
guard let v = self.value as? Dictionary<K, V> else {

Sources/SwiftProtobuf/PathVisitor.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct PathVisitor<T: Message>: Visitor {
2020
// The path contains parent components
2121
private let prevPath: String?
2222

23-
// Captured value after decoding will be stored in this property
23+
// Captured values after visiting will be stored in this property
2424
private(set) var values: [String: Any] = [:]
2525

2626
internal init(prevPath: String? = nil) {
@@ -114,7 +114,9 @@ struct PathVisitor<T: Message>: Visitor {
114114
}
115115
var visitor = PathVisitor<M>(prevPath: path)
116116
try value.traverse(visitor: &visitor)
117-
values.merge(visitor.values, uniquingKeysWith: { _, new in new })
117+
values.merge(visitor.values, uniquingKeysWith: { _, new in
118+
new
119+
})
118120
}
119121

120122
mutating func visitSingularGroupField<G: Message>(value: G, fieldNumber: Int) throws {

Tests/SwiftProtobufTests/Test_FieldMask.swift

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,15 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
232232
// 2. Valid nested path.
233233
// 3. Invalid primitive path.
234234
// 4, 5. Invalid nested path.
235+
// 6, 7. Invalid path after map and repeated.
235236
func testIsPathValid() {
236237
XCTAssertTrue(SwiftProtoTesting_TestAllTypes.isPathValid("optional_int32"))
237238
XCTAssertTrue(SwiftProtoTesting_TestAllTypes.isPathValid("optional_nested_message.bb"))
238239
XCTAssertFalse(SwiftProtoTesting_TestAllTypes.isPathValid("optional_int"))
239240
XCTAssertFalse(SwiftProtoTesting_TestAllTypes.isPathValid("optional_nested_message.bc"))
240241
XCTAssertFalse(SwiftProtoTesting_TestAllTypes.isPathValid("optional_nested_message.bb.a"))
242+
XCTAssertFalse(SwiftProtoTesting_TestAllTypes.isPathValid("repeatedInt32.a"))
243+
XCTAssertFalse(SwiftProtoTesting_Fuzz_Message.isPathValid("map_bool_int32.a"))
241244
}
242245

243246
// Checks `isValid` func of FieldMask.
@@ -549,51 +552,73 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
549552

550553
// Checks merge could be done for non-optional paths.
551554
func testMergeNonOptionalValues() throws {
552-
let mask = Google_Protobuf_FieldMask(protoPaths: ["value"])
553-
554-
var m1 = Google_Protobuf_DoubleValue(1)
555-
let m2 = Google_Protobuf_DoubleValue()
556-
try m1.merge(with: m2, fieldMask: mask)
557-
XCTAssertEqual(m1.value, m2.value)
558-
559-
var m3 = Google_Protobuf_FloatValue(1)
560-
let m4 = Google_Protobuf_FloatValue()
561-
try m3.merge(with: m4, fieldMask: mask)
562-
XCTAssertEqual(m3.value, m4.value)
563-
564-
var m5 = Google_Protobuf_Int64Value(1)
565-
let m6 = Google_Protobuf_Int64Value()
566-
try m5.merge(with: m6, fieldMask: mask)
567-
XCTAssertEqual(m5.value, m6.value)
568-
569-
var m7 = Google_Protobuf_Int32Value(1)
570-
let m8 = Google_Protobuf_Int32Value()
571-
try m7.merge(with: m8, fieldMask: mask)
572-
XCTAssertEqual(m7.value, m8.value)
573-
574-
var m9 = Google_Protobuf_UInt64Value(1)
575-
let m10 = Google_Protobuf_UInt64Value()
576-
try m9.merge(with: m10, fieldMask: mask)
577-
XCTAssertEqual(m9.value, m10.value)
578-
579-
var m11 = Google_Protobuf_UInt32Value(1)
580-
let m12 = Google_Protobuf_UInt32Value()
581-
try m11.merge(with: m12, fieldMask: mask)
582-
XCTAssertEqual(m11.value, m12.value)
583-
584-
var m13 = Google_Protobuf_BoolValue(true)
585-
let m14 = Google_Protobuf_BoolValue()
586-
try m13.merge(with: m14, fieldMask: mask)
587-
XCTAssertEqual(m13.value, m14.value)
588-
589-
var m15 = Google_Protobuf_StringValue("str")
590-
let m16 = Google_Protobuf_StringValue()
591-
try m15.merge(with: m16, fieldMask: mask)
592-
XCTAssertEqual(m15.value, m16.value)
593-
594-
var m17 = Google_Protobuf_BytesValue("str".data(using: .utf8) ?? .init())
595-
let m18 = Google_Protobuf_BytesValue()
596-
try m17.merge(with: m18, fieldMask: mask)
597-
XCTAssertEqual(m17.value, m18.value)
555+
var m1 = try SwiftProtoTesting_Proto3_TestAllTypes.with { m in
556+
m.optionalInt32 = 1
557+
m.optionalInt64 = 1
558+
m.optionalDouble = 1
559+
m.optionalFloat = 1
560+
m.optionalString = "str"
561+
m.optionalBool = true
562+
m.optionalBytes = try XCTUnwrap("str".data(using: .utf8))
563+
m.optionalUint32 = 1
564+
m.optionalUint64 = 1
565+
m.optionalSint32 = 1
566+
m.optionalSint64 = 1
567+
m.optionalFixed32 = 1
568+
m.optionalFixed64 = 1
569+
m.optionalSfixed32 = 1
570+
m.optionalSfixed64 = 1
571+
m.optionalNestedEnum = .bar
572+
}
573+
let m2 = SwiftProtoTesting_Proto3_TestAllTypes()
574+
try m1.merge(with: m2, fieldMask: .init(protoPaths: [
575+
"optional_int32",
576+
"optional_int64",
577+
"optional_double",
578+
"optional_float",
579+
"optional_string",
580+
"optional_bool",
581+
"optional_bytes",
582+
"optional_uint32",
583+
"optional_uint64",
584+
"optional_sint32",
585+
"optional_sint64",
586+
"optional_fixed32",
587+
"optional_fixed64",
588+
"optional_sfixed32",
589+
"optional_sfixed64",
590+
"optional_nested_enum"
591+
]))
592+
XCTAssertEqual(m1.optionalInt32, m2.optionalInt32)
593+
XCTAssertEqual(m1.optionalInt64, m2.optionalInt64)
594+
XCTAssertEqual(m1.optionalDouble, m2.optionalDouble)
595+
XCTAssertEqual(m1.optionalFloat, m2.optionalFloat)
596+
XCTAssertEqual(m1.optionalString, m2.optionalString)
597+
XCTAssertEqual(m1.optionalBool, m2.optionalBool)
598+
XCTAssertEqual(m1.optionalBytes, m2.optionalBytes)
599+
XCTAssertEqual(m1.optionalUint32, m2.optionalUint32)
600+
XCTAssertEqual(m1.optionalUint64, m2.optionalUint64)
601+
XCTAssertEqual(m1.optionalSint32, m2.optionalSint32)
602+
XCTAssertEqual(m1.optionalSint64, m2.optionalSint64)
603+
XCTAssertEqual(m1.optionalFixed32, m2.optionalFixed32)
604+
XCTAssertEqual(m1.optionalFixed64, m2.optionalFixed64)
605+
XCTAssertEqual(m1.optionalSfixed32, m2.optionalSfixed32)
606+
XCTAssertEqual(m1.optionalSfixed64, m2.optionalSfixed64)
607+
XCTAssertEqual(m1.optionalNestedEnum, m2.optionalNestedEnum)
608+
XCTAssertEqual(m1.optionalSint32, m2.optionalSint32)
609+
}
610+
611+
// Checks if merge works with nested proto messages
612+
func testMergeNestedMessages() throws {
613+
var m1 = SwiftProtoTesting_Fuzz_Message()
614+
let m2 = SwiftProtoTesting_Fuzz_Message.with { m in
615+
m.singularMessage = .with { _m in
616+
_m.singularMessage = .with { __m in
617+
__m.singularInt32 = 1
618+
}
619+
}
620+
}
621+
try m1.merge(with: m2, fieldMask: .init(protoPaths: ["singular_message.singular_message.singular_int32"]))
622+
XCTAssertEqual(m1.singularMessage.singularMessage.singularInt32, Int32(1))
598623
}
599624
}

0 commit comments

Comments
 (0)