Nimble icon indicating copy to clipboard operation
Nimble copied to clipboard

expect(arrayOfStrings).to(equal([implicitlyUnwrappedOptionalString])) crashes

Open otaran opened this issue 10 years ago • 9 comments

Hi,

Following iOS Unit Test crashes on Xcode 7.3.1 and Nimble 4.1.0 (installed by CocoaPods 1.0.1):

import XCTest
import Nimble

class ExampleTests: XCTestCase {

    func testExample() {
        let array: [String] = [""]
        let item: String! = ""
        expect(array).to(equal([String]([item]))) // this doesn't crash
        expect(array).to(equal([item])) // fatal error: can't unsafeBitCast between types of different sizes
    }
}

Crash log says fatal error: can't unsafeBitCast between types of different sizes.

Sample project: Example.zip

otaran avatar Jul 08 '16 14:07 otaran

This is a most excellent bug report. Thank you!! ❤️

I ran:

$ sudo gem install cocoapods
$ pod repo update
$ cd Example
Example $ pod install

I then opened Example.xcworkspace to run the tests, and I also encountered the error you described:

Thread 1Queue : com.apple.main-thread (serial)
#0  0x000000010798c108 in specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt) -> () ()
#1  0x000000010783d308 in _arrayConditionalBridgeElements<A, B> ([A]) -> [B]? ()
#2  0x000000010783bf67 in _arrayForceCast<A, B> ([A]) -> [B] ()
#3  0x00000001111f99d9 in ExampleTests.(testExample() -> ()).(implicit closure #1) at /Users/bgesiak/Downloads/Example/ExampleTests/ExampleTests.swift:18
#4  0x00000001111f9a17 in thunk ()
#5  0x00000001111f9581 in partial apply for thunk ()
#6  0x000000011124cd27 in (memoizedClosure<A> (() throws -> A) -> (Bool) throws -> A).(closure #1) at /Users/bgesiak/Downloads/Example/Pods/Nimble/Sources/Nimble/Expression.swift:9
#7  0x000000011124bf5d in partial apply for (memoizedClosure<A> (() throws -> A) -> (Bool) throws -> A).(closure #1) ()
#8  0x000000011124c6bf in Expression.evaluate() throws -> A? at /Users/bgesiak/Downloads/Example/Pods/Nimble/Sources/Nimble/Expression.swift:84
#9  0x000000011124130d in (equal<A where ...> ([A?]) -> NonNilMatcherFunc<[A?]>).(closure #1) at /Users/bgesiak/Downloads/Example/Pods/Nimble/Sources/Nimble/Matchers/Equal.swift:60
#10 0x000000011123c45c in partial apply for (equal<A where ...> ([A?]) -> NonNilMatcherFunc<[A?]>).(closure #1) ()
#11 0x0000000111254a58 in NonNilMatcherFunc.matches(Expression<A>, failureMessage : FailureMessage) throws -> Bool at /Users/bgesiak/Downloads/Example/Pods/Nimble/Sources/Nimble/Matchers/MatcherFunc.swift:47
#12 0x0000000111255373 in protocol witness for Matcher.matches(Expression<A.ValueType>, failureMessage : FailureMessage) throws -> Bool in conformance <A> NonNilMatcherFunc<A> at /Users/bgesiak/Downloads/Example/Pods/Nimble/Sources/Nimble/Matchers/MatcherFunc.swift:46
#13 0x0000000111249ad2 in expressionMatches<A, B where ...> (Expression<A>, matcher : B, to : String, description : String?) -> (Bool, FailureMessage) at /Users/bgesiak/Downloads/Example/Pods/Nimble/Sources/Nimble/Expectation.swift:8
#14 0x000000011124ad78 in Expectation.to<A where ...> (A1, description : String?) -> () at /Users/bgesiak/Downloads/Example/Pods/Nimble/Sources/Nimble/Expectation.swift:46
#15 0x00000001111f940f in ExampleTests.testExample() -> () at /Users/bgesiak/Downloads/Example/ExampleTests/ExampleTests.swift:18
#16 0x00000001111f95b2 in @objc ExampleTests.testExample() -> () ()
#17 0x000000010552b5cc in __invoking___ ()
#18 0x000000010552b41e in -[NSInvocation invoke] ()
#19 0x0000000110df9ef3 in __24-[XCTestCase invokeTest]_block_invoke_2 ()
#20 0x0000000110e2e613 in -[XCTestContext performInScope:] ()
#21 0x0000000110df9d78 in -[XCTestCase invokeTest] ()
#22 0x0000000110dfa3a2 in -[XCTestCase performTest:] ()
#23 0x0000000110df7cf7 in -[XCTestSuite performTest:] ()
#24 0x0000000110df7cf7 in -[XCTestSuite performTest:] ()
#25 0x0000000110df7cf7 in -[XCTestSuite performTest:] ()
#26 0x0000000110de4b10 in __25-[XCTestDriver _runSuite]_block_invoke ()
#27 0x0000000110e05b4c in -[XCTestObservationCenter _observeTestExecutionForBlock:] ()
#28 0x0000000110de4a55 in -[XCTestDriver _runSuite] ()
#29 0x0000000110de57d1 in -[XCTestDriver _checkForTestManager] ()
#30 0x0000000110e2fa9a in _XCTestMain ()
#31 0x00000001055682ec in __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ ()
#32 0x000000010555df75 in __CFRunLoopDoBlocks ()
#33 0x000000010555d6d2 in __CFRunLoopRun ()
#34 0x000000010555d0f8 in CFRunLoopRunSpecific ()
#35 0x0000000109d0cad2 in GSEventRunModal ()
#36 0x0000000105e6cf09 in UIApplicationMain ()
#37 0x000000010546d692 in main at /Users/bgesiak/Downloads/Example/Example/AppDelegate.swift:12
#38 0x0000000107eaa92d in start ()
#39 0x0000000107eaa92d in start ()

modocache avatar Jul 08 '16 14:07 modocache

I am glad to help. Is there anything I can do to help fix this issue (apart from actually fixing it - I am not good at Swift Voodoo 😄 )

otaran avatar Jul 08 '16 14:07 otaran

Is there a workaround? Maybe try the following, and report back the results?

expect(array).to(haveCount(1))
expect(array).to(contain(item))

modocache avatar Jul 08 '16 15:07 modocache

Yes, the workaround I have found is to create a copy of [item]:

expect(array).to(equal([String]([item]))) // this doesn't crash

otaran avatar Jul 08 '16 15:07 otaran

Yes,

expect(array).to(haveCount(1))
expect(array).to(contain(item))

doesn't crash.

otaran avatar Jul 08 '16 15:07 otaran

I've tried this in Xcode 8 & Nimble/swift-3 without any error. It seems like it was fixed in swift?

jeffh avatar Sep 13 '16 05:09 jeffh

@jeffh , do you have a sample project compatible with Xcode 8? I tried to convert my project, but failed.

otaran avatar Sep 13 '16 16:09 otaran

@otaran You can just change the Podfile dependency to the latest commit, Or, I just create a sample repo which shipped with Xcode 8. https://github.com/wongzigii/CrashSample

Make sure to run pod install before build and test.

Everything looks good in Xcode 8. (Hum, I actually have converted 1-2 files to met the Swift 3 syntax manually, though).

wongzigii avatar Sep 13 '16 17:09 wongzigii

Thanks, @wongzigii ! The code doesn't crash in Xcode 8, everything seems to be good.

otaran avatar Sep 13 '16 17:09 otaran