gpt4 book ai didi

nim-lang - Nim 中的编译时调用计数

转载 作者:行者123 更新时间:2023-12-05 00:22:16 26 4
gpt4 key购买 nike

以下代码无法编译,但说明了我想做的事情: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)

编辑:在代码中添加问题

最佳答案

这是一种方法。您可以使用宏或 static 在编译时执行代码。陈述。请注意,仍然无法在多个模块中可靠地计算这些。

import macros, strutils

proc beginTests()

var countTested = 0
var countFailed = 0
var countPassed = 0
var totalTests = 0
var totalTestsCT {.compiletime.} = 0

macro endTests(): stmt =
quote do:
proc beginTests() =
totalTests = `totalTestsCT`

proc assertEqualsImpl*[T](testName: string, expected: T, p: (proc(): T)) =
countTested += 1
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

macro assertEquals*[T](testName: string, expected: T, p: (proc(): T)): stmt =
totalTestsCT += 1
quote do:
assertEqualsImpl(`testName`, `expected`, `p`)

when isMainModule:
beginTests()
assertEquals("test #A", 12, proc(): int = 14-2)
assertEquals("test #B", 12, proc(): int = 12-2)
endTests()

另一种实现是将测试嵌入到自定义 block 语句中,例如
testSuite:
assertEquals("test #A", 12, proc(): int = 14-2)
assertEquals("test #B", 12, proc(): int = 12-2)
testSuite然后宏将计算嵌入代码中的断言并相应地初始化变量。

另一种解决方案是不直接执行测试,而是将它们存储在一个列表中并仅在最后执行它们。

关于nim-lang - Nim 中的编译时调用计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30200676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com