Parsing tokens

In the previous exercise, both #[vanilla_test] and the default #[test] macro had to be specified on top of the test function. Without adding #[test], the annotated function is not picked up by the test runner.

Detecting existing attributes

You'll augment #[vanilla_test]:

  • If the annotated function has been annotated with #[test], it should emit the code unchanged.
  • If the annotated function has not been annotated with #[test], it should add #[test] to the function.

This is how #[googletest::test] works, for example.

The toolkit

When the macro game is serious, you can't get by using the built-in proc_macro crate.
Almost all macros written in Rust are built on top of three ecosystem crates:

  • syn for parsing tokens into abstract syntax tree nodes (AST node)
  • quote for expressing the generated code with a println!-style syntax
  • proc-macro2, a wrapper around proc_macro's types

Exercise

The exercise for this section is located in 08_macros/02_test