Nim中的编译时呼叫计数

时间:2015-05-12 20:19:45

标签: nim

下面的代码没有编译,但说明了我想做的事情:totalTests应该保持调用assertEquals()的时间(assertEquals()应该是一个宏,这是可能的,但我&#39 ;我不熟悉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)

编辑:在代码中添加了问题

2 个答案:

答案 0 :(得分:4)

这是一种方法。您可以使用宏或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() 语句在编译时执行代码。请注意,仍然无法在多个模块中可靠地计算这些数据。

testSuite:
  assertEquals("test #A", 12, proc(): int = 14-2)
  assertEquals("test #B", 12, proc(): int = 12-2)

另一种实现方式是将测试嵌入到自定义块语句中,例如

testSuite

{{1}}宏将计算嵌入代码中的断言并相应地初始化变量。

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

答案 1 :(得分:2)

这是Reimer的第三个建议的实现,这对我来说效果最好。

import macros, strutils

type 
  TestSuiteObj = object
    countTested: int
    countFailed: int
    countPassed: int
    totalTests: int
    tests: seq[(proc (self: TestSuite))]
  TestSuite* = ref TestSuiteObj


proc newTestSuite*(): TestSuite =
  new(result)
  result.countTested = 0
  result.countFailed = 0
  result.countPassed = 0
  result.totalTests = 0
  result.tests = @[]

proc assertEquals*[T](self: TestSuite, testName: string, expected: T, p: (proc(): T)) =
  self.totalTests += 1

  var testProc = proc(self: TestSuite) =
    self.countTested += 1
    write(stdout, format("[$num/$total] $name: ", "num", self.countTested, "total", self.totalTests, "name", testName))
    var val = p()
    if val == expected:
      write(stdout, "passed\n")
      self.countPassed += 1
    else:
      write(stdout, "failed\n")
      self.countFailed += 1

  self.tests.add(testProc)


proc run*(self: TestSuite) =
  self.totalTests = self.tests.len
  for p in self.tests:
    p(self)

  var verdict = case (self.countTested == self.countPassed)
  of true: "PASSED"
  of false: "FAILED"
  echo format("$verdict. Passed [$passed/$total] tests.", "verdict", verdict, "passed", self.countPassed, "total", self.countTested)

  # Sanity
  assert(self.countTested == (self.countFailed+self.countPassed))
  assert(self.countTested == self.totalTests)


when isMainModule:
  var suite = newTestSuite() 
  suite.assertEquals("test #A", 12, proc(): int = 14-2)
  suite.assertEquals("test #B", 12, proc(): int = 12-2)
  suite.run()