6 Testing a builder macro
This chapter covers
- Writing a derive macro that will generate a builder for structs
- Creating white-box tests to verify the behavior of functions within your macro
- Using black-box tests that take an outside view of your code
- Deciding what types of tests are most useful for your macro
The builder pattern is a very convenient, fluent way of constructing structs. Because of that, it is omnipresent in Rust code. Often, though, the code required to write a builder is boilerplate—boilerplate that we can automate away! In this chapter, we will write a macro to do just that. Because we are not touching the original struct, we can use a derive macro (remember, go for the simplest option). In an implementation block, we ...
Get Write Powerful Rust Macros now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.