关于 scala:ScalaTest: 预定义类型的自定义值输出
ScalaTest: customized value output for predefined type
说,我有这样的小测试:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 |
class Test extends FunSuite with Matchers {
test("test") { array should equal (null) } |
...失败为:
|
1
|
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
|
我的 array 代表游戏场,我希望它在测试输出中打印,类似于:
|
1
2 3 |
1 1 1
2 1 3 1 4 1 |
... 而不是:
|
1
|
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
|
有没有办法在 ScalaTest 中做到这一点?
相关讨论
- 使用 class GameField(field: Array[Array[Int]]) { override def toString = ... } 而不是直接使用嵌套数组。
- @sschaef 感谢您的建议,但这不是我的选择。package字段数组是我不想要的复杂情况。
使用的一个选项是WithClue。像这样的
|
1
2 3 |
val a = Array( Array(1,2,3), Array(4,5,6),Array(7,8,9))
def p (x:Array[Array[Int]]) = x.foreach( x=> {x.foreach(print);println;} ) withClue(p(a) ) {a should equal (null)} |
在我认为你会寻找覆盖匹配器之间。正如我所看到的,您接下来将比较 ArrayArray 和 ArrayArray,并且您可能会在比较失败的地方寻找非常具体的比较。
这很有趣。
您可能会考虑在 should 之前使用额外的类和伴随对象来package数组。类和同伴必须是这样的,它们不会使您的 null 测试无效,而是提供更漂亮的打印。例如:
|
1
2 3 4 5 6 7 8 9 10 11 12 |
class ArrayPrettyPrinter(array : Array[Int]) {
override def toString() : String = { // your pretty print implementation here } } object ArrayPrettyPrinter { |
你的测试应该变成:
|
1
2 3 4 5 6 7 8 9 10 11 12 |
class Test extends FunSuite with Matchers {
test("test") { ArrayPrettyPrinter(array) should equal (null) |