Skip to content

Commit

Permalink
Fix regression in beAnInstanceOf (#1174)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
younata authored Nov 6, 2024
1 parent 0e445aa commit b0cee1b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/Nimble/Matchers/BeAnInstanceOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public func beAnInstanceOf<T, U>(_ expectedType: T.Type) -> Matcher<U> {
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)
Expand Down
13 changes: 11 additions & 2 deletions Tests/NimbleTests/Matchers/BeAKindOfTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Error> = .success(1)
expect(result).to(beSuccess(beAKindOf(Int.self)))
expect(Result<Int, Error>.success(1)).to(beSuccess(beAKindOf(Int.self)))
expect(Result<TestProtocol, Error>.success(TestClassConformingToProtocol())).to(beSuccess(beAKindOf(TestClassConformingToProtocol.self)))
expect(Result<TestProtocol, Error>.success(TestClassConformingToProtocol())).to(beSuccess(beAKindOf(TestProtocol.self)))
}

func testFailureMessages() {
Expand Down
13 changes: 11 additions & 2 deletions Tests/NimbleTests/Matchers/BeAnInstanceOfTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Error> = .success(1)
expect(result).to(beSuccess(beAnInstanceOf(Int.self)))
expect(Result<Int, Error>.success(1)).to(beSuccess(beAnInstanceOf(Int.self)))
expect(Result<TestProtocol, Error>.success(TestClassConformingToProtocol())).to(beSuccess(beAnInstanceOf(TestClassConformingToProtocol.self)))
expect(Result<TestProtocol, Error>.success(TestClassConformingToProtocol())).toNot(beSuccess(beAnInstanceOf(TestProtocol.self)))
}

func testFailureMessages() {
Expand Down

0 comments on commit b0cee1b

Please sign in to comment.