Martin Mlostek
Martin Mlostek

Reputation: 2970

Nimble matching of struct via "identical to"

I am using nimble in unit test expectation mapping and having a question about comparing structs.

What i am observing is that the matched .to(be(x)) does not work at all with structs. So this following unit test fails:

func someTest() {
    struct Struct {
        let a: String
        let b: String
    }
    let structure = Struct(a: "a", b: "b")
    expect(structure).to(be(structure))
}

Does this mean that the copy on write mechanism kicks in here and we are looking on 2 copies? Why is that test failing?

Upvotes: 2

Views: 1176

Answers (1)

David Pasztor
David Pasztor

Reputation: 54716

The be() function actually calls beIdenticalTo, which uses pointer equality checking, so it only works for reference types. See the BeIdenticalTo source code.

You should make Struct conform to Equatable and use equal instead.

func someTest() {
    struct Struct: Equatable {
        let a: String
        let b: String
    }
    let structure = Struct(a: "a", b: "b")
    expect(structure).to(equal(structure))
}

Upvotes: 4

Related Questions