What is a Macro
A macro is a compile-time code transformation tool. Unlike functions that process values at runtime, macros take program code itself as both input and output. Macros can read Token sequences from source code, generate new code fragments, which are then compiled and executed.
Functions can only obtain the runtime value of parameters, not the parameter expression itself. For example, a function f(x + y) only receives the computed result of x + y, not the expression x + y. Macros can access the expression itself, enabling capabilities that functions cannot achieve.
Cangjie provides built-in test macros @Test, @TestCase, and @Assert. The @Assert macro checks if two values are equal - if equal, the test passes; otherwise, it reports failure. These macros expand into test framework code at compile time, simplifying test writing.
The code example on the right demonstrates the basic usage of test macros. @Test marks test classes, @TestCase marks test methods, and @Assert(a + b, 5) asserts that the value of a + b equals 5.
@ symbol as a prefix to distinguish them from function calls.Run the code, then change @Assert(a + b, 5) to @Assert(a + b, 99) and observe the test failure effect.