I have used @compileError for doing validations during compilation, basically to catch errors early on. What’s a good approach to test those validations? Currently I put in the calls that would trigger the @compileError in tests, and test them by doing manual compilation. Then comment them out afterward, with a note left behind saying to uncomment this to test such and such @compileError.
Frankly, I think the test case is when it fails to compile.
While I am confident there are many other use-cases for it beyond validation of types (i.e. anytype that would often cause a complication error down the road anyways) , I mainly use it as a means of providing a helpful error message at the error location instead of some other nebulous compilation error later on with a less helpful message. So instead of a message about a struct not supporting an addition operator in some random function, a consumer gets a message explicitly stating a signed integer or float is required here.
I don’t know if “testing” them really makes a lot of sense within the context of their purpose. i do agree that their could be some cases where it would be helpful, but it is beyond my reasoning how it would even be possible to test such a thing.
Point taken.
In my case, I’m writing a library and want to make sure the user of the library is using it correctly. Some of the validations are @compileError checks. They become part of the UX of the library. It would be nice to add them to the tests.
It is a accepted proposal.
Good to know it’s in the pipeline.
You can pull off this kind of testing without any help from the language.
With the Zig build system you can expect a given step to fail and you can capture its stdout/stderr output and compare it against a known good value.
As a similar example, this is how I do snapshot testing with Zine: zine/build.zig at 03734aa552eba662db4d4d166a4220b132b302f9 · kristoff-it/zine · GitHub
In your case you would want to invoke the Zig compiler itself, ask it to build a test project (if the code that you want to test is dependency free, you can even just zig build-exe a single file) and then expect that build to fail in the build system.
Coincidentally, this is also how some Zig compiler tests work.
Related post: Dead Simple Snapshot Testing In Zig | Loris Cro's Blog
Doing it at the build process would work. Thanks!