以下代码无法编译,但说明了我想做的事情:totalTests 应该保存调用 assertEquals() 的次数(assertEquals() 可能应该是一个宏才能实现这一点,但我不熟悉Nim 的这方面呢)。
知道如何修改此代码以使以下代码能够在每个测试报告行的开头打印 [1/2] 和 [2/2] 吗?
from strutils import format
var countTested = 0
var countFailed = 0
var countPassed = 0
let totalTests = 0 # <-- need let or other compile-time type here (?)
# using proc here... macro may be needed to be able to count calls (?)
proc assertEquals*[T](testName: string, expected: T, p: (proc(): T)) =
countTested += 1
totalTests += 1 # <-- compilation error (how can we increase each time it is called?)
write(stdout, format("[$num/$total] $name: ", "num", countTested, "total", totalTests, "name", testName))
var val = p()
if val == expected:
write(stdout, "passed\n")
countPassed += 1
else:
write(stdout, "failed\n")
countFailed += 1
when isMainModule:
assertEquals("test #A", 12, proc(): int = 14-2)
assertEquals("test #B", 12, proc(): int = 12-2)
编辑:在代码中添加问题