I want to implement a ValueObjectSharedExampleConfiguration: QuickConfiguration
using Quick
.
class ValueObjectSharedExampleConf: QuickConfiguration {
override class func configure(_ configuration: Configuration) {
sharedExamples("Value Object") {
(context: @escaping SharedExampleContext) in
describe("same objects") {
it("should be equal") {
let obj1a = context()["1a"]
let obj1b = context()["1b"]
expect(obj1a == obj1b).to(beTrue())
}
}
describe("different objects") {
it("should not be equal") {
let obj1 = context()["1a"]
let obj2 = context()["2"]
expect(obj1 == obj2).to(beFalse())
}
}
}
}
}
And then I want to test any classes/structs that conforms to Equatable
with this shared Example like this:
itBehavesLike("Value Object") { [ "obj1a": foo1a, "obj1b": foo1b, "obj2": foo2] }
But the problem is, SharedExampleContext
is actually a closure returns [String: Any]
, so obj1a
, obj1b
, obj2
variables I get in sharedExample
closure are all of type Any
, which doesn't necessarily conform to Equatable
. Thus the code obj1a == obj1b
won't compile.
Actually if I check obj1a is Equatable
it returns true. But I don't know how to cast it to a proper type that compiler will accept. obj1a as! Equatable
won't compile because Equatable
is a generic protocol.
I can't just write obj1a as! Foo
because if there is another class Bar: Equatable
I want my sharedExample also works for that.
The main problem here is: I have two variables cast to Any
, which are guaranteed to be originally of same type that conforms to Equatable
. How should I legally compare these two variables without knowledge of the actual type of them ?