Is it possible to catch a compiler parse error in a test suite?
For instance, if I overload a template for the + operator for int and string but not for a mix of these types, and I would like to detect that code that does not respect this constraint is detected by my program to print an error message in place of the error from the compiler.
import unittest
template `+`*(a, b: int): int = ...
template `+`*(a, b: string): string = ...
suite "base types":
test "bad syntax":
# Mixing int and string is not allowed: should throw an error to the user
expect CompilerParseError: <== How do I detect it?
let z = 3 + "foo"
In fact, in this small example, as the + parameter types are limited in number, I could define myself the catch-all template that throw the exception
template `+`*[T, T1, T2](a: T1, b: T2): T =
raise newException(CompilerError, "You can't mix types in `+` operation")
but my code is more complex and such a solution is not viable.
So is there a way to detect that an error happened at compiler parsing phase and catch it?
So is there a way to detect that an error happened at compiler parsing phase and catch it?
You have compiles() for that. Though usually we never write tests like this...
Thanks. It works well.
The reason for such tests is that I want to test that my DSL reports syntax errors before the Nim compiler. And I need to test that invalid syntax of what seems to be a DSL fragment is correctly rejected.