← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.4.e.1.a.1.a.1 · 生成模型:GPT-6

白盒断言宏展开输出

理解等级:高级

白盒测试可以在实现同文件的测试子模块中直接触达私有函数。用 quote! 造输入后,可以按想保护的约束选择断言。

断言 能发现什么 代价
输出文本含名称 某个标识符缺失 名称出现于错误位置仍可能通过
输出与预期 token 流的字符串一致 输出片段和次序变化 等义改写也可能触发失败
用 syn::parse2 读回 AST 解析失败或目标节点结构不符合预期 多个输出项可能需要额外提取逻辑

令牌流缺少直接的相等比较,书中先转字符串。解析 AST 也只验证语法结构,真实类型检查和行为由调用端测试覆盖。

相关卡片 [[黑盒验证宏的调用行为]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Testing a builder macro 白盒验证输出 · 查看原书依据

      This is very basic, but it already gives us some assurance about our code. For example, we now know that the code is returning the builder name somewhere in its output. But would it not be better to check that the entire output matches our expectations? We can do that by comparing the output with a TokenStream of our own. That does mean we need to somehow manually build that expected output, and there are several ways to do this.

    参考来源

    - 位置:《Write Powerful Rust Macros》第06章《Testing a builder macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-6.xhtml#p42) - 结论依据:清单 6.5–6.7 对同一个空结构体生成器分别断言名称、完整 token 文本和解析后的 AST,6.4.1 说明新增实现块会改变后两种测试的维护成本。 - 原始内容:This is very basic, but it already gives us some assurance about our code. For example, we now know that the code is returning the builder name somewhere in its output. But would it not be better to check that the entire output matches our expectations? We can do that by comparing the output with a TokenStream of our own. That does mean we need to somehow manually build that expected output, and there are several ways to do this.