案例列表中的 Scalatest 双等价 类
Scalatest Double equivalency in list of case classes
我有 Double
个值,它们的值相似,但不完全相同。通常我会这样做:
val a: Double = ???
val b: Double = ???
a shouldEqual b +- 0.25
如果我只是比较一个案例 class 我会这样做:
case class Data(label: String, value: Double)
val a: Data = ???
val b: Data = ???
a.value shouldEqual b.value +- 0.25
在我的例子中,我有一个案例列表 class 实例,并且想将它们与它们的 value
属性的公差进行比较:
val output = Seq(Data("a", 1.1), Data("b", 1.2))
val expected = Seq(Data("a", 0.9), Data("b", 1.1))
output should contain theSameElementsInOrderAs expected
当然,那会归档,因为 value
属性不完全匹配。我需要的是这样的:
output should contain theSameElementsInOrderAs expected +- 0.25
你可以选择
forAll(output.zipAll(expected, null, null)) {
case (a, b) => a.value shouldEqual b.value +- 0.25
}
使用 zipAll
因为当长度不匹配时 zip
会错误地成功,但是您通过这种方式获得的错误消息不太好。
我最终为我的 Seq[Data]
类型定义了自定义 Matcher
:
trait CustomMatcher {
class SeqDataContainsTheSameElementsInOrderAs(expected: Seq[Data]) {
override def apply(left: Seq[Data]): MatchResult = {
// ... do other checks like comparing the length and such
val bad = left.zip(expected).filter(t => {
val difference = Math.abs(t._1.value - t._2.value)
difference > TOLERANCE // Declare this somewhere
})
// Return the MatchResult, you will probably want to give better error messages than this
MatchResult(
bad.isEmpty,
s"""Some of the values were not equal""",
s"""Everything was equal"""
)
}
def customContainTheSameElementsInOrderAs(expected: Seq[Data]) = new SeqDataContainsTheSameElementsInOrderAs(expected)
}
}
然后我这样使用它:
output should customContainTheSameElementsInOrderAs(expected)
我有 Double
个值,它们的值相似,但不完全相同。通常我会这样做:
val a: Double = ???
val b: Double = ???
a shouldEqual b +- 0.25
如果我只是比较一个案例 class 我会这样做:
case class Data(label: String, value: Double)
val a: Data = ???
val b: Data = ???
a.value shouldEqual b.value +- 0.25
在我的例子中,我有一个案例列表 class 实例,并且想将它们与它们的 value
属性的公差进行比较:
val output = Seq(Data("a", 1.1), Data("b", 1.2))
val expected = Seq(Data("a", 0.9), Data("b", 1.1))
output should contain theSameElementsInOrderAs expected
当然,那会归档,因为 value
属性不完全匹配。我需要的是这样的:
output should contain theSameElementsInOrderAs expected +- 0.25
你可以选择
forAll(output.zipAll(expected, null, null)) {
case (a, b) => a.value shouldEqual b.value +- 0.25
}
使用 zipAll
因为当长度不匹配时 zip
会错误地成功,但是您通过这种方式获得的错误消息不太好。
我最终为我的 Seq[Data]
类型定义了自定义 Matcher
:
trait CustomMatcher {
class SeqDataContainsTheSameElementsInOrderAs(expected: Seq[Data]) {
override def apply(left: Seq[Data]): MatchResult = {
// ... do other checks like comparing the length and such
val bad = left.zip(expected).filter(t => {
val difference = Math.abs(t._1.value - t._2.value)
difference > TOLERANCE // Declare this somewhere
})
// Return the MatchResult, you will probably want to give better error messages than this
MatchResult(
bad.isEmpty,
s"""Some of the values were not equal""",
s"""Everything was equal"""
)
}
def customContainTheSameElementsInOrderAs(expected: Seq[Data]) = new SeqDataContainsTheSameElementsInOrderAs(expected)
}
}
然后我这样使用它:
output should customContainTheSameElementsInOrderAs(expected)