Approaches to testing macros

Thanks @rauhul, but I wanted to exactly avoid the approach suggested as the seer amount of combinations supported by MetaCodable will require multiple different samples all with different names. This will also be difficult to maintain as the library scales with more features.

With the new Swift testing framework here is the approach I went with structuring tests:

struct TestCase {
    @Test
    func testErrorsWithMacro() {}

    struct MacroUseCase {
        struct Sample {}

        @Test
        func assertSampleExpansion() {}
    }
}
  • Here TestCase represents a specific macro/use-case scenario I am validating.

  • It can contain multiple testErrorsWithMacro which asserts proper diagnostic errors/warnings generated by the macro in invalid usages.

  • For each usage scenario of the macro one MacroUseCase type can be created.

    • It will contain a simple Sample representing the usage scenario, this will allow verification of any build errors.
    • It will contain one assertSampleExpansion that will assert the expanded code, allowing verification of the usage scenario.
    • Optionally additional tests can be added verifying the generated code behaves as expected.