1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! Test utilities for the zyn proc macro framework.
//!
//! Provides assertion macros for comparing token streams and inspecting
//! diagnostics produced by [`Output`](crate::Output). Since `zyn!` returns
//! `Output`, all macros work directly on template expansion results.
//!
//! # Assertions
//!
//! ## Setting up tests
//!
//! Element integration tests require an `input` variable in scope. Use
//! [`parse!`](crate::parse) to create one:
//!
//! ```ignore
//! let input: zyn::Input = zyn::parse!("struct Test;" => zyn::syn::DeriveInput)
//! .unwrap()
//! .into();
//! ```
//!
//! ## Comparing token output
//!
//! Use [`assert_tokens!`] to compare the full output of a template against
//! an expected `quote!` expression:
//!
//! ```ignore
//! #[test]
//! fn generates_function() {
//! let input: zyn::Input = zyn::parse!("struct Test;" => zyn::syn::DeriveInput)
//! .unwrap()
//! .into();
//! let output = zyn::zyn!(fn {{ name }}() {});
//! let expected = zyn::quote::quote!(fn hello() {});
//! zyn::assert_tokens!(output, expected);
//! }
//! ```
//!
//! For partial matching, use [`assert_tokens_contain!`]:
//!
//! ```ignore
//! zyn::assert_tokens_contain!(output, "fn hello");
//! // ✓ "fn hello" found in raw token output
//! ```
//!
//! To verify empty output (e.g., after `bail!`):
//!
//! ```ignore
//! zyn::assert_tokens_empty!(output);
//! ```
//!
//! ## Asserting diagnostics
//!
//! When an element emits errors, warnings, or other diagnostics, they are
//! carried in the [`Output`](crate::Output). Assert on them by level and message:
//!
//! ```ignore
//! #[zyn::element]
//! fn validated(name: zyn::syn::Ident) -> zyn::TokenStream {
//! if name == "forbidden" {
//! bail!("reserved identifier `{}`", name);
//! }
//! zyn::zyn!(fn {{ name }}() {})
//! }
//!
//! #[test]
//! fn rejects_forbidden_name() {
//! let input: zyn::Input = zyn::parse!("struct Test;" => zyn::syn::DeriveInput)
//! .unwrap()
//! .into();
//! let output = zyn::zyn!(@validated(name = zyn::format_ident!("forbidden")));
//! zyn::assert_diagnostic_error!(output, "reserved identifier");
//! zyn::assert_tokens_empty!(output);
//! }
//!
//! #[test]
//! fn accepts_valid_name() {
//! let input: zyn::Input = zyn::parse!("struct Test;" => zyn::syn::DeriveInput)
//! .unwrap()
//! .into();
//! let output = zyn::zyn!(@validated(name = zyn::format_ident!("hello")));
//! zyn::assert_tokens_contain!(output, "fn hello");
//! }
//! ```
//!
//! Warnings and notes don't block output — both tokens and diagnostics are present:
//!
//! ```ignore
//! #[zyn::element]
//! fn deprecated_el(name: zyn::syn::Ident) -> zyn::TokenStream {
//! warn!("this element is deprecated");
//! zyn::zyn!(fn {{ name }}() {})
//! }
//!
//! #[test]
//! fn warning_does_not_block_output() {
//! let input: zyn::Input = zyn::parse!("struct Test;" => zyn::syn::DeriveInput)
//! .unwrap()
//! .into();
//! let output = zyn::zyn!(@deprecated_el(name = zyn::format_ident!("hello")));
//! zyn::assert_tokens_contain!(output, "fn hello");
//! zyn::assert_diagnostic_warning!(output, "deprecated");
//! }
//! ```
//!
//! ## Pretty assertions
//!
//! With the `pretty` feature, use [`assert_tokens_pretty!`] and
//! [`assert_tokens_contain_pretty!`] to compare against `prettyplease`-formatted output:
//!
//! ```ignore
//! zyn::assert_tokens_pretty!(output, expected);
//! zyn::assert_tokens_contain_pretty!(output, "fn validate");
//! ```
//!
//! ## Assertion macro reference
//!
//! | Macro | Purpose |
//! |-------|---------|
//! | [`assert_tokens!`] | Compare two token streams (raw-formatted diff on failure) |
//! | [`assert_tokens_empty!`] | Assert no tokens produced |
//! | [`assert_tokens_contain!`] | Check for substring in raw token output |
//! | [`assert_diagnostic!`] | Assert diagnostic at a specific level with message |
//! | [`assert_diagnostic_error!`] | Assert error diagnostic with message |
//! | [`assert_diagnostic_warning!`] | Assert warning diagnostic with message |
//! | [`assert_diagnostic_note!`] | Assert note diagnostic with message |
//! | [`assert_diagnostic_help!`] | Assert help diagnostic with message |
//! | [`assert_compile_error!`] | Alias for [`assert_diagnostic_error!`] |
//!
//! With the `pretty` feature:
//!
//! | Macro | Purpose |
//! |-------|---------|
//! | [`assert_tokens_pretty!`] | Compare using `prettyplease`-formatted output |
//! | [`assert_tokens_contain_pretty!`] | Substring check on pretty-printed output |
//!
//! # Debugging
//!
//! Add the `debug` argument to any zyn attribute macro to inspect generated code
//! at compile time. Output is emitted as a compiler `note` diagnostic.
//!
//! Two conditions must be met:
//!
//! 1. Add `debug` (or `debug = "pretty"`) to the macro attribute
//! 2. Set the `ZYN_DEBUG` environment variable to match the generated type name
//!
//! ```ignore
//! #[zyn::element(debug)]
//! fn greeting(name: zyn::syn::Ident) -> zyn::TokenStream {
//! zyn::zyn!(fn {{ name }}() {})
//! }
//! ```
//!
//! ```bash
//! ZYN_DEBUG="*" cargo build
//! ```
//!
//! ```text
//! note: zyn::element ─── Greeting
//!
//! struct Greeting { pub name : zyn :: syn :: Ident , } impl :: zyn :: Render
//! for Greeting { fn render (& self , input : & :: zyn :: Input) -> :: zyn ::
//! proc_macro2 :: TokenStream { ... } }
//! ```
//!
//! With the `pretty` feature, use `debug = "pretty"` for formatted output:
//!
//! ```ignore
//! #[zyn::element(debug = "pretty")]
//! fn greeting(name: zyn::syn::Ident) -> zyn::TokenStream {
//! zyn::zyn!(fn {{ name }}() {})
//! }
//! ```
//!
//! ```text
//! note: zyn::element ─── Greeting
//!
//! struct Greeting {
//! pub name: zyn::syn::Ident,
//! }
//! impl ::zyn::Render for Greeting { ... }
//! ```
//!
//! All macros support `debug`: `#[zyn::element(debug)]`, `#[zyn::pipe(debug)]`,
//! `#[zyn::derive("Name", debug)]`, `#[zyn::attribute(debug)]`.
//!
//! ## ZYN_DEBUG patterns
//!
//! | Pattern | Matches |
//! |---------|---------|
//! | `*` | Everything |
//! | `Greeting` | Exact match only |
//! | `Greet*` | `Greeting`, `GreetingElement`, etc. |
//! | `*Element` | `FieldElement`, `GreetingElement`, etc. |
//! | `Greeting,Shout` | Multiple patterns |
//!
//! ## Pipeline API
//!
//! The debug module exposes a pipeline API via the `DebugExt` trait:
//!
//! ```ignore
//! use zyn::debug::DebugExt;
//!
//! let raw: String = tokens.debug().raw();
//! #[cfg(feature = "pretty")]
//! let pretty: String = tokens.debug().pretty();
//! ```