Skip to content

Commit

Permalink
Test: add general assertDebugDescription message
Browse files Browse the repository at this point in the history
  • Loading branch information
dflems committed Jul 7, 2022
1 parent f2b48be commit eecbb4b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 71 deletions.
20 changes: 20 additions & 0 deletions Tests/SwiftProtobufTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@ extension PBTestHelpers where MessageTestType: SwiftProtobuf.Message & Equatable
// Yay! It failed!
}
}

func assertDebugDescription(_ expected: String, file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> ()) {
// `assertDebugDescription` is a no-op in release as `debugDescription` is unavailable.
#if DEBUG
var m = MessageTestType()
configure(&m)
let actual = m.debugDescription
XCTAssertEqual(actual, expected, file: file, line: line)
#endif
}
}

extension XCTestCase {
func assertDebugDescription(_ expected: String, _ m: SwiftProtobuf.Message, fmt: String? = nil, file: XCTestFileArgType = #file, line: UInt = #line) {
// `assertDebugDescription` is a no-op in release as `debugDescription` is unavailable.
#if DEBUG
let actual = m.debugDescription
XCTAssertEqual(actual, expected, fmt ?? "debugDescription did not match", file: file, line: line)
#endif
}
}

/// Protocol to help write visitor for testing. It provides default implementaions
Expand Down
37 changes: 15 additions & 22 deletions Tests/SwiftProtobufTests/Test_AllTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// -----------------------------------------------------------------------------

import Foundation
import SwiftProtobuf
import XCTest

class Test_AllTypes: XCTestCase, PBTestHelpers {
Expand All @@ -39,13 +40,6 @@ class Test_AllTypes: XCTestCase, PBTestHelpers {
}
}

func assertDebugDescription(_ expected: String, file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> ()) {
var m = MessageTestType()
configure(&m)
let actual = m.debugDescription
XCTAssertEqual(actual, expected, file: file, line: line)
}

//
// Unknown field
//
Expand Down Expand Up @@ -1849,7 +1843,7 @@ class Test_AllTypes: XCTestCase, PBTestHelpers {
t.defaultInt32 = 4
t.clearDefaultInt32()
XCTAssertEqual(t.defaultInt32, 41)
XCTAssertEqual(t.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\n", t)

// The default is still not serialized
let s = try t.serializedBytes()
Expand Down Expand Up @@ -2294,7 +2288,7 @@ class Test_AllTypes: XCTestCase, PBTestHelpers {

var m = MessageTestType()
m.oneofUint32 = 77
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_uint32: 77\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_uint32: 77\n", m)
var m2 = MessageTestType()
m2.oneofUint32 = 78
XCTAssertNotEqual(m.hashValue, m2.hashValue)
Expand Down Expand Up @@ -2343,7 +2337,7 @@ class Test_AllTypes: XCTestCase, PBTestHelpers {
nested1.bb = 1
var m = MessageTestType()
m.oneofNestedMessage = nested1
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_nested_message {\n bb: 1\n}\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_nested_message {\n bb: 1\n}\n", m)
var nested2 = MessageTestType.NestedMessage()
nested2.bb = 2
var m2 = MessageTestType()
Expand Down Expand Up @@ -2422,7 +2416,7 @@ class Test_AllTypes: XCTestCase, PBTestHelpers {

var m = MessageTestType()
m.oneofString = "abc"
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_string: \"abc\"\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_string: \"abc\"\n", m)
var m2 = MessageTestType()
m2.oneofString = "def"
XCTAssertNotEqual(m.hashValue, m2.hashValue)
Expand Down Expand Up @@ -2496,43 +2490,42 @@ class Test_AllTypes: XCTestCase, PBTestHelpers {
var m = MessageTestType()
m.oneofBytes = Data([1, 2, 3])

XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_bytes: \"\\001\\002\\003\"\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noneof_bytes: \"\\001\\002\\003\"\n", m)
var m2 = MessageTestType()
m2.oneofBytes = Data([4, 5, 6])
XCTAssertNotEqual(m.hashValue, m2.hashValue)
}

func testDebugDescription() {
var m = MessageTestType()
let d = m.debugDescription
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\n", d)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\n", m)
m.optionalInt32 = 7
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noptional_int32: 7\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noptional_int32: 7\n", m)
m.repeatedString = ["a", "b"]
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noptional_int32: 7\nrepeated_string: \"a\"\nrepeated_string: \"b\"\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllTypes:\noptional_int32: 7\nrepeated_string: \"a\"\nrepeated_string: \"b\"\n", m)
}

func testDebugDescription2() {
// Message with only one field
var m = ProtobufUnittest_ForeignMessage()
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\n", m)
m.c = 3
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\nc: 3\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\nc: 3\n", m)
}

func testDebugDescription3() {
// Message with only a single oneof
var m = ProtobufUnittest_TestOneof()
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\n", m)
m.fooInt = 1
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_int: 1\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_int: 1\n", m)
m.fooString = "a"
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_string: \"a\"\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_string: \"a\"\n", m)
var g = ProtobufUnittest_TestOneof.FooGroup()
g.a = 7
g.b = "b"
m.fooGroup = g
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nFooGroup {\n a: 7\n b: \"b\"\n}\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nFooGroup {\n a: 7\n b: \"b\"\n}\n", m)
}

func testDebugDescription4() {
Expand Down
34 changes: 13 additions & 21 deletions Tests/SwiftProtobufTests/Test_AllTypes_Proto3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ class Test_AllTypes_Proto3: XCTestCase, PBTestHelpers {
}
}

func assertDebugDescription(_ expected: String, file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> ()) {
var m = MessageTestType()
configure(&m)
let actual = m.debugDescription
XCTAssertEqual(actual, expected, file: file, line: line)
}

//
// Singular types
//
Expand Down Expand Up @@ -1229,7 +1222,7 @@ class Test_AllTypes_Proto3: XCTestCase, PBTestHelpers {

var m = MessageTestType()
m.oneofUint32 = 77
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_uint32: 77\n")
assertDebugDescription("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_uint32: 77\n", m)
var m2 = MessageTestType()
m2.oneofUint32 = 78
XCTAssertNotEqual(m.hashValue, m2.hashValue)
Expand Down Expand Up @@ -1276,7 +1269,7 @@ class Test_AllTypes_Proto3: XCTestCase, PBTestHelpers {
var m = MessageTestType()
m.oneofNestedMessage = MessageTestType.NestedMessage()
m.oneofNestedMessage.bb = 1
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_nested_message {\n bb: 1\n}\n")
assertDebugDescription("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_nested_message {\n bb: 1\n}\n", m)
var m2 = MessageTestType()
m2.oneofNestedMessage = MessageTestType.NestedMessage()
m2.oneofNestedMessage.bb = 2
Expand Down Expand Up @@ -1337,7 +1330,7 @@ class Test_AllTypes_Proto3: XCTestCase, PBTestHelpers {

var m = MessageTestType()
m.oneofString = "abc"
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_string: \"abc\"\n")
assertDebugDescription("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_string: \"abc\"\n", m)
var m2 = MessageTestType()
m2.oneofString = "def"
XCTAssertNotEqual(m.hashValue, m2.hashValue)
Expand Down Expand Up @@ -1407,43 +1400,42 @@ class Test_AllTypes_Proto3: XCTestCase, PBTestHelpers {
var m = MessageTestType()
m.oneofBytes = Data([1, 2, 3])

XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_bytes: \"\\001\\002\\003\"\n")
assertDebugDescription("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noneof_bytes: \"\\001\\002\\003\"\n", m)
var m2 = MessageTestType()
m2.oneofBytes = Data([4, 5, 6])
XCTAssertNotEqual(m.hashValue, m2.hashValue)
}

func testDebugDescription() {
var m = MessageTestType()
let d = m.debugDescription
XCTAssertEqual("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\n", d)
assertDebugDescription("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\n", m)
m.optionalInt32 = 7
XCTAssertEqual("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noptional_int32: 7\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noptional_int32: 7\n", m)
m.repeatedString = ["a", "b"]
XCTAssertEqual("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noptional_int32: 7\nrepeated_string: \"a\"\nrepeated_string: \"b\"\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.Proto3Unittest_TestAllTypes:\noptional_int32: 7\nrepeated_string: \"a\"\nrepeated_string: \"b\"\n", m)
}

func testDebugDescription2() {
// Message with only one field
var m = ProtobufUnittest_ForeignMessage()
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\n", m)
m.c = 3
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\nc: 3\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_ForeignMessage:\nc: 3\n", m)
}

func testDebugDescription3() {
// Message with only a optional oneof
var m = ProtobufUnittest_TestOneof()
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\n", m)
m.fooInt = 1
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_int: 1\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_int: 1\n", m)
m.fooString = "a"
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_string: \"a\"\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nfoo_string: \"a\"\n", m)
var g = ProtobufUnittest_TestOneof.FooGroup()
g.a = 7
g.b = "b"
m.fooGroup = g
XCTAssertEqual("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nFooGroup {\n a: 7\n b: \"b\"\n}\n", m.debugDescription)
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestOneof:\nFooGroup {\n a: 7\n b: \"b\"\n}\n", m)
}
}

7 changes: 0 additions & 7 deletions Tests/SwiftProtobufTests/Test_AllTypes_Proto3_Optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ class Test_AllTypes_Proto3_Optional: XCTestCase, PBTestHelpers {
}
}

func assertDebugDescription(_ expected: String, file: XCTestFileArgType = #file, line: UInt = #line, configure: (inout MessageTestType) -> ()) {
var m = MessageTestType()
configure(&m)
let actual = m.debugDescription
XCTAssertEqual(actual, expected, file: file, line: line)
}

//
// Optional Singular types
//
Expand Down
14 changes: 7 additions & 7 deletions Tests/SwiftProtobufTests/Test_Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Test_Extensions: XCTestCase, PBTestHelpers {
m2.ProtobufUnittest_optionalInt32Extension = 18
XCTAssertNotEqual(m1, m2)

XCTAssertEqual(m2.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.optional_int32_extension]: 18\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.optional_int32_extension]: 18\n", m2)
XCTAssertNotEqual(m1.hashValue, m2.hashValue)
}

Expand Down Expand Up @@ -143,7 +143,7 @@ class Test_Extensions: XCTestCase, PBTestHelpers {

var m1 = ProtobufUnittest_TestAllExtensions()
m1.ProtobufUnittest_optionalStringExtension = "ab"
XCTAssertEqual(m1.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.optional_string_extension]: \"ab\"\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.optional_string_extension]: \"ab\"\n", m1)
}

func test_repeatedInt32Extension() throws {
Expand All @@ -164,7 +164,7 @@ class Test_Extensions: XCTestCase, PBTestHelpers {
XCTAssertNotEqual(m.ProtobufUnittest_repeatedInt32Extension, [7, 8])
XCTAssertEqual(m.ProtobufUnittest_repeatedInt32Extension, [7, 9])

XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.repeated_int32_extension]: 7\n[protobuf_unittest.repeated_int32_extension]: 9\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.repeated_int32_extension]: 7\n[protobuf_unittest.repeated_int32_extension]: 9\n", m)

XCTAssertFalse(m.ProtobufUnittest_repeatedInt32Extension.isEmpty)
m.ProtobufUnittest_repeatedInt32Extension = []
Expand All @@ -175,16 +175,16 @@ class Test_Extensions: XCTestCase, PBTestHelpers {
var m = ProtobufUnittest_TestAllExtensions()
XCTAssertEqual(m.ProtobufUnittest_defaultInt32Extension, 41)
XCTAssertEqual(try m.serializedBytes(), [])
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n", m)
m.ProtobufUnittest_defaultInt32Extension = 100
XCTAssertEqual(try m.serializedBytes(), [232, 3, 100])
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.default_int32_extension]: 100\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.default_int32_extension]: 100\n", m)
m.clearProtobufUnittest_defaultInt32Extension()
XCTAssertEqual(try m.serializedBytes(), [])
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n", m)
m.ProtobufUnittest_defaultInt32Extension = 41 // Default value
XCTAssertEqual(try m.serializedBytes(), [232, 3, 41])
XCTAssertEqual(m.debugDescription, "SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.default_int32_extension]: 41\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_TestAllExtensions:\n[protobuf_unittest.default_int32_extension]: 41\n", m)

assertEncode([232, 3, 17]) { (o: inout MessageTestType) in
o.ProtobufUnittest_defaultInt32Extension = 17
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftProtobufTests/Test_FieldMask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Test_FieldMask: XCTestCase, PBTestHelpers {
func testDebugDescription() {
var m = Google_Protobuf_FieldMask()
m.paths = ["foo", "bar"]
XCTAssertEqual(m.debugDescription, "SwiftProtobuf.Google_Protobuf_FieldMask:\npaths: \"foo\"\npaths: \"bar\"\n")
assertDebugDescription("SwiftProtobuf.Google_Protobuf_FieldMask:\npaths: \"foo\"\npaths: \"bar\"\n", m)
}

func testConvenienceInits() {
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftProtobufTests/Test_Required.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Test_Required: XCTestCase, PBTestHelpers {
if !expectedTextFormat.isEmpty {
expected += expectedTextFormat + "\n"
}
XCTAssertEqual(msg.debugDescription, expected, "While decoding \(bytes)", file: file, line: line)
assertDebugDescription(expected, msg, fmt: "While decoding \(bytes)", file: file, line: line)
} catch let e {
XCTFail("Decoding \(bytes) failed with error: \(e)", file: file, line: line)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ class Test_Required: XCTestCase, PBTestHelpers {
allTextFormattedField.append("\n")
}
let fullMsg = try ProtobufUnittest_TestAllRequiredTypes(serializedData: allBytesData)
XCTAssertEqual(fullMsg.debugDescription, allTextFormattedField)
assertDebugDescription(allTextFormattedField, fullMsg)
}

// Helper to assert encoding fails with a not initialized error.
Expand Down Expand Up @@ -372,7 +372,7 @@ class Test_SmallRequired: XCTestCase, PBTestHelpers {
if !expectedTextFormat.isEmpty {
expected += expectedTextFormat + "\n"
}
XCTAssertEqual(msg.debugDescription, expected, "While decoding \(bytes)", file: file, line: line)
assertDebugDescription(expected, msg, fmt: "While decoding \(bytes)", file: file, line: line)
} catch let e {
XCTFail("Decoding \(bytes) failed with error: \(e)", file: file, line: line)
}
Expand Down Expand Up @@ -406,7 +406,7 @@ class Test_SmallRequired: XCTestCase, PBTestHelpers {
allTextFormattedField.append("\n")
}
let fullMsg = try ProtobufUnittest_TestSomeRequiredTypes(serializedData: allBytesData)
XCTAssertEqual(fullMsg.debugDescription, allTextFormattedField)
assertDebugDescription(allTextFormattedField, fullMsg)
}

// Helper to assert encoding fails with a not initialized error.
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftProtobufTests/Test_Reserved.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Test_Reserved: XCTestCase {
msg.hashValue_p = "bar"
msg.debugDescription_p = 5

XCTAssertEqual(msg.debugDescription, "SwiftProtobufTests.ProtobufUnittest_SwiftReservedTest:\nproto_message_name: 1\nproto_package_name: 2\nany_type_prefix: 3\nany_type_url: 4\nis_initialized: \"foo\"\nhash_value: \"bar\"\ndebug_description: 5\n")
assertDebugDescription("SwiftProtobufTests.ProtobufUnittest_SwiftReservedTest:\nproto_message_name: 1\nproto_package_name: 2\nany_type_prefix: 3\nany_type_url: 4\nis_initialized: \"foo\"\nhash_value: \"bar\"\ndebug_description: 5\n", msg)

msg.clearIsInitialized_p()
msg.clearHashValue_p()
Expand Down
Loading

0 comments on commit eecbb4b

Please sign in to comment.