5

I'm planning a project with multiple modules, and I was looking for a nice solution to run all existing unit tests in the project at once. I came up with the following idea: I can run nim --define:testing main.nim and use the following template as a wrapper for all my unit tests.

# located in some general utils module:
template runUnitTests*(code: stmt): stmt =
  when defined(testing):
    echo "Running units test in ..."
    code

This seems to be working well so far.

As a minor tweak, I was wondering if I can actually print out the file name which is calling the runUnitTests template. Is there any reflection mechanism to get the source file name at compile time?

bluenote10
  • 23,414
  • 14
  • 122
  • 178

2 Answers2

5

instantiationInfo seems to be what you want: http://nim-lang.org/docs/system.html#instantiationInfo,

template filename: string = instantiationInfo().filename

echo filename()
Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
def-
  • 5,275
  • 20
  • 18
  • Thanks, that's it! One minor note: I have to call `instantiationInfo().filename` directly in the `runUnitTests` template. If I create this `filename` template in my `utils.nim` module and call this second template from the `runUnitTests` template, the output is always `utils.nim` (looks like nesting templates affects stack line information). – bluenote10 Apr 01 '15 at 19:54
5

The template currentSourcePath from the system module returns the path of the current source by using a special compiler built-in, called instantiationInfo.

In your case, you need to print the locations of callers of your template, which means you'll have to use instantiationInfo directly with its default stack index argument of -1 (meaning one position up in the stack, or the caller):

# located in some general utils module:
template runUnitTests*(code: stmt): stmt =
  when defined(testing):
    echo "Running units test in ", instantiationInfo(-1).filename
    code

It's worth mentioning that the Nim compiler itself uses a similar technique with this module, which get automatically imported in all other modules by applying a switch in nim.cfg:

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
zah
  • 5,314
  • 1
  • 34
  • 31