有断言错误 return 各自的列表位置而不是对象?

Have assertion error return respective list position instead of the object?

test("Comparison") {
  val list: List[String] = List("Thing", "Entity", "Variable")
  val expected: List[String] = List("Thingg", "Entityy", "Variablee")
  var expectedPosition = 0
  for (item <- list) {
    assert(list(expectedPosition) == expected(expectedPosition))
    expectedPosition += 1
  }
}

在 Scala 中,为了使我的低级代码测试更具可读性,我认为只使用一个断言并让它遍历一个循环并在最后增加一个累加器是个好主意。 这是为了在多个输入或多或少具有相似属性时一次测试多个输入。当断言失败时,它返回为“Thing[]”不等于“Thing[g]”。与其报告列表中失败的项目,有没有办法让它直接说明列表位置,而无需在断言之前连接列表位置或使用 returns 列表位置的条件语句?就像我宁愿将它全部包含在 assert() 错误报告中一样。

val expected: LazyList[String] = ...

expected.zip(list)
        .zipWithIndex
        .foreach{case ((exp,itm),idx) =>
          assert(exp == itm, s"off at index $idx")
        }

//java.lang.AssertionError: assertion failed: off at index 0

expected 是惰性的,因此即使压缩两次也只遍历一次。