ScalaTest: 预定义类型的自定义值输出
•浏览 1
ScalaTest: customized value output for predefined type
说,我有这样的小测试:
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)
}
}
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
1 1 1
2 1 3
1 4 1
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
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)}
class ArrayPrettyPrinter(array : Array[Int]) {
overridedef toString() : String = {
// your pretty print implementation here
}
}
object ArrayPrettyPrinter {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) returnnullreturnnew ArrayPrettyPrinter(array)
}
}
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)
}
}
...失败为:
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)
}
}
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
1 1 1
2 1 3
1 4 1
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
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)}
class ArrayPrettyPrinter(array : Array[Int]) {
overridedef toString() : String = {
// your pretty print implementation here
}
}
object ArrayPrettyPrinter {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) returnnullreturnnew ArrayPrettyPrinter(array)
}
}
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)
}
}
我的 array 代表游戏场,我希望它在测试输出中打印,类似于:
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)
}
}
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
1 1 1
2 1 3
1 4 1
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
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)}
class ArrayPrettyPrinter(array : Array[Int]) {
overridedef toString() : String = {
// your pretty print implementation here
}
}
object ArrayPrettyPrinter {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) returnnullreturnnew ArrayPrettyPrinter(array)
}
}
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)
}
}
... 而不是:
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)
}
}
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
1 1 1
2 1 3
1 4 1
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
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)}
class ArrayPrettyPrinter(array : Array[Int]) {
overridedef toString() : String = {
// your pretty print implementation here
}
}
object ArrayPrettyPrinter {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) returnnullreturnnew ArrayPrettyPrinter(array)
}
}
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)
}
}
有没有办法在 ScalaTest 中做到这一点?
使用的一个选项是WithClue。像这样的
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)
}
}
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
1 1 1
2 1 3
1 4 1
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
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)}
class ArrayPrettyPrinter(array : Array[Int]) {
overridedef toString() : String = {
// your pretty print implementation here
}
}
object ArrayPrettyPrinter {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) returnnullreturnnew ArrayPrettyPrinter(array)
}
}
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)
}
}
在我认为你会寻找覆盖匹配器之间。正如我所看到的,您接下来将比较 ArrayArray 和 ArrayArray,并且您可能会在比较失败的地方寻找非常具体的比较。
这很有趣。
您可能会考虑在 should 之前使用额外的类和伴随对象来package数组。类和同伴必须是这样的,它们不会使您的 null 测试无效,而是提供更漂亮的打印。例如:
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)
}
}
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
1 1 1
2 1 3
1 4 1
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
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)}
class ArrayPrettyPrinter(array : Array[Int]) {
overridedef toString() : String = {
// your pretty print implementation here
}
}
object ArrayPrettyPrinter {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) returnnullreturnnew ArrayPrettyPrinter(array)
}
}
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)
}
}
你的测试应该变成:
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)
}
}
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1)) did not equal null
1 1 1
2 1 3
1 4 1
Array(Array(1, 1, 1), Array(2, 1, 3), Array(1, 4, 1))
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)}
class ArrayPrettyPrinter(array : Array[Int]) {
overridedef toString() : String = {
// your pretty print implementation here
}
}
object ArrayPrettyPrinter {
def apply(array : Array[Int]) : ArrayPrettyPrinter = {
if (array == null) returnnullreturnnew ArrayPrettyPrinter(array)
}
}
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)
}
}