From b0cee1b31b58e46a8443593a97f5a69256e76558 Mon Sep 17 00:00:00 2001 From: Rachel Brindle Date: Tue, 5 Nov 2024 17:24:52 -0800 Subject: [PATCH] Fix regression in beAnInstanceOf (#1174) beAnInstanceOf was not correctly matching when an exact type as was expected was given. This fixes that by emulating the previous typing of beAnInstanceOf while still working when used as a submatcher --- Sources/Nimble/Matchers/BeAnInstanceOf.swift | 2 +- Tests/NimbleTests/Matchers/BeAKindOfTest.swift | 13 +++++++++++-- Tests/NimbleTests/Matchers/BeAnInstanceOfTest.swift | 13 +++++++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Sources/Nimble/Matchers/BeAnInstanceOf.swift b/Sources/Nimble/Matchers/BeAnInstanceOf.swift index 9d55b9efa..f36d6689e 100644 --- a/Sources/Nimble/Matchers/BeAnInstanceOf.swift +++ b/Sources/Nimble/Matchers/BeAnInstanceOf.swift @@ -5,7 +5,7 @@ public func beAnInstanceOf(_ expectedType: T.Type) -> Matcher { let errorMessage = "be an instance of \(String(describing: expectedType))" return Matcher.define { actualExpression in let instance = try actualExpression.evaluate() - guard let validInstance = instance else { + guard let validInstance: Any = instance else { return MatcherResult( status: .doesNotMatch, message: .expectedActualValueTo(errorMessage) diff --git a/Tests/NimbleTests/Matchers/BeAKindOfTest.swift b/Tests/NimbleTests/Matchers/BeAKindOfTest.swift index 3d8fa1609..f7c2b0dd6 100644 --- a/Tests/NimbleTests/Matchers/BeAKindOfTest.swift +++ b/Tests/NimbleTests/Matchers/BeAKindOfTest.swift @@ -28,16 +28,25 @@ final class BeAKindOfSwiftTest: XCTestCase { expect(testProtocolClass).to(beAKindOf(TestProtocol.self)) expect(testProtocolClass).toNot(beAKindOf(TestStructConformingToProtocol.self)) + expect(testProtocolClass as TestProtocol).to(beAKindOf(TestClassConformingToProtocol.self)) + expect(testProtocolClass as TestProtocol).to(beAKindOf(TestProtocol.self)) + expect(testProtocolClass as TestProtocol).toNot(beAKindOf(TestStructConformingToProtocol.self)) + let testProtocolStruct = TestStructConformingToProtocol() expect(testProtocolStruct).to(beAKindOf(TestStructConformingToProtocol.self)) expect(testProtocolStruct).to(beAKindOf(TestProtocol.self)) expect(testProtocolStruct).toNot(beAKindOf(TestClassConformingToProtocol.self)) + + expect(testProtocolStruct as TestProtocol).to(beAKindOf(TestStructConformingToProtocol.self)) + expect(testProtocolStruct as TestProtocol).to(beAKindOf(TestProtocol.self)) + expect(testProtocolStruct as TestProtocol).toNot(beAKindOf(TestClassConformingToProtocol.self)) } func testNestedMatchers() { // This test is successful if it even compiles. - let result: Result = .success(1) - expect(result).to(beSuccess(beAKindOf(Int.self))) + expect(Result.success(1)).to(beSuccess(beAKindOf(Int.self))) + expect(Result.success(TestClassConformingToProtocol())).to(beSuccess(beAKindOf(TestClassConformingToProtocol.self))) + expect(Result.success(TestClassConformingToProtocol())).to(beSuccess(beAKindOf(TestProtocol.self))) } func testFailureMessages() { diff --git a/Tests/NimbleTests/Matchers/BeAnInstanceOfTest.swift b/Tests/NimbleTests/Matchers/BeAnInstanceOfTest.swift index 393fba1ae..0907c0796 100644 --- a/Tests/NimbleTests/Matchers/BeAnInstanceOfTest.swift +++ b/Tests/NimbleTests/Matchers/BeAnInstanceOfTest.swift @@ -30,16 +30,25 @@ final class BeAnInstanceOfTest: XCTestCase { expect(testProtocolClass).toNot(beAnInstanceOf(TestProtocol.self)) expect(testProtocolClass).toNot(beAnInstanceOf(TestStructConformingToProtocol.self)) + expect(testProtocolClass as TestProtocol).to(beAnInstanceOf(TestClassConformingToProtocol.self)) + expect(testProtocolClass as TestProtocol).toNot(beAnInstanceOf(TestProtocol.self)) + expect(testProtocolClass as TestProtocol).toNot(beAnInstanceOf(TestStructConformingToProtocol.self)) + let testProtocolStruct = TestStructConformingToProtocol() expect(testProtocolStruct).to(beAnInstanceOf(TestStructConformingToProtocol.self)) expect(testProtocolStruct).toNot(beAnInstanceOf(TestProtocol.self)) expect(testProtocolStruct).toNot(beAnInstanceOf(TestClassConformingToProtocol.self)) + + expect(testProtocolStruct as TestProtocol).to(beAnInstanceOf(TestStructConformingToProtocol.self)) + expect(testProtocolStruct as TestProtocol).toNot(beAnInstanceOf(TestProtocol.self)) + expect(testProtocolStruct as TestProtocol).toNot(beAnInstanceOf(TestClassConformingToProtocol.self)) } func testNestedMatchers() { // This test is successful if it even compiles. - let result: Result = .success(1) - expect(result).to(beSuccess(beAnInstanceOf(Int.self))) + expect(Result.success(1)).to(beSuccess(beAnInstanceOf(Int.self))) + expect(Result.success(TestClassConformingToProtocol())).to(beSuccess(beAnInstanceOf(TestClassConformingToProtocol.self))) + expect(Result.success(TestClassConformingToProtocol())).toNot(beSuccess(beAnInstanceOf(TestProtocol.self))) } func testFailureMessages() {