I overloaded the == operator on a class. Then a child class also overloads the == operator. My understanding is that both == and != are supposed to call the same == function. However, when running tests, == calls the function on the child class, yet != calls the function on the parent class. This is bizarre. I would expect the same behavior for both == and !=.
class Description: Equatable {
var id : Int = IdFactory.id()
static func == (lhs: Description, rhs: Description) -> Bool {
assertionFailure("Child Description must override == method.")
return false
}
}
class LineDescription : Description {
var track = 0
static func == (lhs: LineDescription, rhs: LineDescription) -> Bool {
if lhs.track != rhs.track { return false }
return true
}
}
import XCTest
class LineDescriptionTest: XCTestCase {
func testEquality(){
let ld = LineDescription()
ld.track = 9
let ld2 = LineDescription()
ld2.track = 9
XCTAssert(ld == ld2) //<--Calls child == function
ld2.track = 1
XCTAssert(ld != ld2) //<--Calls parent != function
}
}