我正在测试Swift中的一系列功能。
我需要比较一个复杂的元组类型(并且应该在单个XCTAssertEqual
语句中,而不是比较所有的元组组成类型。
为简单起见,我尝试在此最小示例中将自己的已知答案与自己进行比较:
let answer: (question: [String], correctAnswerIndex: [String], givenAnswers: [String], givenAnswerIndex: [String]) = (question:[], correctAnswerIndex: ["0"], givenAnswers: ["0"], givenAnswerIndex: ["0"])
XCTAssertEqual(answer,answer)
可以预料的是,这会导致错误类型类型不等于
全局函数'XCTAssertEqual(::__:file:line :)'要求 '(问题:[String],correctAnswerIndex:[String],givenAnswers: [String],givenAnswerIndex:[String])'符合“ Equatable”
因此,我希望在测试中进行单独的扩展以使它们相等,我可以看到的最接近的答案涉及通用参数,而实际上我想使其尽可能简单...
答案 0 :(得分:0)
代替Equatable
,有一种变通方法,它可以为==
元组编写一个Answer
函数:
typealias Answer = (question: [String], correctAnswerIndex: [String], givenAnswers: [String], givenAnswerIndex: [String])
func ==(answer: Answer, anotherAnswer: Answer) -> Bool {
// Implement your own equation logic here!
return answer.question == anotherAnswer.question
&& answer.correctAnswerIndex == anotherAnswer.correctAnswerIndex
&& answer.givenAnswers == anotherAnswer.givenAnswers
&& answer.givenAnswerIndex == anotherAnswer.givenAnswerIndex
}
let answer: Answer = (question:[], correctAnswerIndex: ["0"], givenAnswers: ["0"], givenAnswerIndex: ["0"])
XCTAssertTrue(answer == answer)