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”) {
val array = Array(
Array(1, 1, 1),
Array(2, 1, 3),
Array(1, 4, 1)
)
array should equal (null)
}
}
|
…失败为:
1
|
Array (Array (1, 1, 1), Array (2, 1, 3), Array (1, 4, 1)) did not equal null
|
我的 array 代表游戏场,我希望它在测试输出中打印,类似于:
… 而不是:
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 {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) return null
return new ArrayPrettyPrinter(array)
}
}
|
你的测试应该变成:
1 2 3 4 5 6 7 8 9 10 11 12
|
class Test extends FunSuite with Matchers {
test(“test”) {
val array = Array(
Array(1, 1, 1),
Array(2, 1, 3),
Array(1, 4, 1)
)
ArrayPrettyPrinter(array) should equal (null)
}
}
|
来源:https://www.codenong.com/31139741/
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9028.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57