跳到正文
cccskills
免费在 GitHub 公开中文介绍

Toolchain tests

本指南介绍如何用 `file_test` 编写、组织和运行 Carbon 工具链测试,涵盖文件头、拆分用例、失败前缀、筛选 SemIR 输出,以及通过更新脚本重新生成预期结果。

原文查看原文说明

Instructions for authoring, structuring, and running toolchain tests using the file_test infrastructure.

carbon-language/carbon-lang星标 33,929(GitHub 33,929 + cccskills 0)

实测评分 未实测

查看安装方法

适用场景

  • 编写工具链 file_test
  • 将测试拆分为多个用例
  • 编写验证失败的测试
  • 重新生成预期输出

中文介绍

功能

本技能介绍如何使用 file_test 为 Carbon 工具链创建、组织和运行测试。内容包括标准文件头、配置注释、最小化前置文件,以及在一个物理文件中拆分多个测试用例。它说明如何使用 fail_、todo_ 等前缀标记预期失败或当前行为不正确的测试。还会介绍如何用标记将 SemIR 输出限制在指定声明附近,以及如何运行 ./toolchain/autoupdate_testdata.py 重新生成预期的标准输出和标准错误。

适用场景

新增或修改 Carbon 编译器功能并需要编写对应测试时,可以参考本指南。它适用于区分通过用例和验证失败的用例、控制捕获输出的范围,以及编写稳定的常量求值测试,包括边界值测试。

使用示例

  • “请为这个功能编写 file_test,并分别设置通过和失败用例。”
  • “请让这个测试只显示目标声明的 SemIR 输出。”
  • “请更新测试的预期输出,并说明发生了哪些变化。”

注意事项

本指南针对 Carbon 工具链测试。不要手动编辑 // CHECK:STDOUT: 或 // CHECK:STDERR: 行;应运行 ./toolchain/autoupdate_testdata.py 更新,再确认新输出是否体现了预期行为。语言服务器测试采用不同规则,应参考对应技能。

本介绍由 AI(OpenAI Codex)根据公开的 SKILL.md 撰写。准确的规格请查看下方原文。

文档检查

AI 根据 SKILL.md 的内容检查了需要注意的操作和使用前提。评分只依据实际运行的结果(实测对比)。

安全性发送数据、删除文件、执行命令等是否有说明
B部分不足更新脚本会改写预期输出,资料要求更新后检查其内容。
安装难易度所需工具和 API 密钥是否少且有说明
B部分不足需要Carbon代码库及其更新脚本,但没有提供安装步骤。

不计入评分。 由 AI(OpenAI Codex)于 2026年10月26日 评估。仅供参考,安装前请同时查看原文。

如果您认为评估有误,请申请复核

请写明哪一项有误以及原因。无需登录。我们会对照 SKILL.md 重新评估,并在此处更新结果。

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

包含的文件(1)

  • SKILL.md8.2 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Toolchain tests

<!-- Part of the Carbon Language project, under the Apache License v2.0 with LLVM Exceptions. See /LICENSE for license information. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -->

Introduction

This skill provides guidelines and patterns for creating and updating tests for the Carbon toolchain, especially file tests in toolchain/*/testdata/ (for example, toolchain/check/testdata/).

Toolchain tests evaluate Carbon source files through Lexing, Parsing, Checking, and optionally Lowering. Output (for example SemIR dumps, Clang errors) is captured and validated using inline CHECK records.

Language server tests also use file_test, but with quite different conventions (an LSP message stream, AUTOUPDATE-SPLIT, FROM_FILE_SPLIT). Refer to the Language server skill (SKILL.md) for those.

Structure and Authoring

File Layout and Headers

Test files must start with the standard Carbon license, followed by configuration comments. Separate sections with blank comment lines (//).

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/...
//
// AUTOUPDATE
  • // AUTOUPDATE is mandatory for files using CHECK markers.
  • // TIP: lines are automatically generated by the autoupdater. You do not need to hand-write them. It is harmless to add them, but the script will handle it.

Minimized Preludes

When writing tests entirely unrelated to the Core package, specify a minimal prelude file using // INCLUDE-FILE. Usually, include toolchain/testing/testdata/min_prelude/ scripts, such as int.carbon or primitives.carbon. This significantly speeds up execution and minimizes STDOUT noise.

  • Builtin Primitive Testing: Standard operators (such as +, -, /, <, etc.) are not imported or available inside minimized preludes. To write tests with a minimal prelude footprint, call primitive builtins directly (e.g., float.negate, float.div) inside your test code to build expressions.

Split Tests and [[@TEST_NAME]]

A single physical file can test multiple scenarios using split constraints:

// --- passing_case.carbon
library "[[@TEST_NAME]]";

// ...

// --- fail_bad_case.carbon
library "[[@TEST_NAME]]";
// ...
  • Always use library "[[@TEST_NAME]]"; in each split rather than hardcoding the library name. This prevents name conflicts, avoids redefining the default library, and keeps the test code clean and templateable.
  • Exactly [[@TEST_NAME]] (including the brackets) must be used. The test infrastructure automatically replaces it with the split's filename minus todo_ and fail_ prefixes.
  • Do not put code that is expected to pass and code that is expected to fail into the same split. Validation relies on non-failing splits producing absolutely no errors and failing splits producing the correct compiler errors independently.

File Prefixing: fail_ and todo_

Expected failures must be differentiated from unexpected failures (and from bugs). Include prefixes to name individual split files or the main test:

  • fail_...: The test should and does produce compiler errors.
  • todo_fail_...: The test should produce errors but currently does not.
  • fail_todo_...: The test does produce errors or crashes, but it shouldn't (or produces the wrong errors or otherwise misbehaves with errors).
  • todo_...: The test has some incorrect behavior, but doesn't produce errors currently, and shouldn't.

Main File Naming: The main test file (and any split-files) must have a fail_ prefix if they have an associated error. Exception: The main file may omit fail_ if it contains a least one split that has a fail_ prefix.

Both the fail_ and todo_ prefixes are stripped from filename properties like [[@TEST_NAME]].

Constant Evaluation Validation

When testing constant evaluation in semantic checker tests, follow these conventions to ensure diagnostic stability and accuracy:

  • Literal Spelling Canonicalization: In Semantic IR, real literals (floating-point constants) with identical mathematical values can be assigned distinct internal representation identifiers based on spelling variations in source code. To completely prevent literal spelling mismatches in expected output checks, validation tests must be performed using canonical comparison methods (for example, passing converted values through an Expect(X as f64) function).
  • Generic Parameters Validation: To bypass compile-time constraints where local runtime variables are rejected as generic function arguments, test generic type conversions at runtime, and validate compile-time conversions by passing static literal values directly into primitive builtin calls.
  • Exhaustive Edge Case Verification: For complex mathematical algorithms (such as floating-point to integer truncation and rounding), map and execute test constraints covering every code branch, conditional exit, and fallback evaluation path.
  • Rounding Threshold Boundaries: Test cases that land extremely close to mathematical boundaries (for example, floating-point literals representing a tiny fraction above 1.0, such as $2^{30} \times 2^{-30}$ or $10^{10} \times 10^{-10}$, verifying correct exact truncation down to 1 or 0).
  • Precise Float Literal Spelling: Spell floating-point literals in test code with exact mathematical precision targeting target thresholds. For example, if testing the smallest fractional increment above 1.0, use the exact hex fractional representation (e.g. 0x1.0000000000001p0) or a highly precise decimal fractional spelling (e.g. 1.0000000000000001) instead of coarse fractions like 1.1 to ensure correct boundary assertions.
  • Representation Capacity Boundaries: Explicitly target edge cases near representation limits of target types. Test combinations of mantissas and exponents that yield values exactly on, just below, or just above the capacity limits of fixed-size destination types (e.g. signed/unsigned targets like i32 or u32).
  • Zero-Value Sizing Bounds: Verify boundary inputs of 0 and 0.0 explicitly. Assert that zero inputs are sized and simplified correctly without triggering calculation underflows, division-by-zero errors, or underestimating required bit allocations.

Test Code Comments

  • No agent thinking: Do not include comments describing your reasoning or "train of thought" (for example, "Wait, but...") inside the test files. Any comments left in tests should be concise and describe what the test itself is validating for human readers.

SemIR Dumps and Minimizing Output

Limit STDOUT checks to the logic under test. Always use //@dump-sem-ir-begin and //@dump-sem-ir-end around the specific declarations/blocks where SemIR output is desired. Only use these markers and not --dump-sem-ir-ranges=if-present or similar extra args—new tests use //@dump-sem-ir... to naturally filter output to the highlighted segments based on the default behavior.

//@dump-sem-ir-begin
fn F(x:? form(ref i32));
//@dump-sem-ir-end

Creating/Updating the Output

AI tools should never hand-write or manually touch // CHECK:STDOUT: or // CHECK:STDERR: comments.

Write your Carbon test code, headers, and // AUTOUPDATE then run the test updater:

./toolchain/autoupdate_testdata.py toolchain/PATH/TO/YOUR/TEST.carbon

Review the updated test outputs (for example, by way of git diff). Ensure logic paths are correctly tested rather than producing massive boilerplate blocks.

Autoupdating makes the tests pass whether or not the new behavior is correct, so deciding that the new output is the output you wanted is a separate step. See the Review testdata changes skill.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

本技能介绍如何使用命令行`gh`只读查看GitHub issue正文和评论,支持通过编号或URL定位,也可指定其他仓库,并说明如何提取大量评论。

  • 查看issue正文和评论
  • 通过URL读取issue
  • 查看其他仓库的issue
carbon-language/carbon-lang3.4万2026年10月26日 更新

Agent tools

免费有简介

本技能为在 Carbon 代码库中工作的 AI 助手规定文件查看、搜索和编辑工具,并说明临时文件的存放与清理规则,以及何时可以编写和运行自定义程序。

  • 按规定工具搜索Carbon文件
  • 为文件编辑选择专用工具
  • 确认临时文件的存放与清理规则
carbon-language/carbon-lang3.4万2026年10月26日 更新

Bazel usage

免费有简介

了解如何在 Carbon 项目中通过 Bazelisk 构建、测试和运行程序,包括指定目标、启用 ASan,以及系统依赖变更后重建缓存。

  • 构建 Carbon 代码
  • 运行与修改相关的测试
  • 启动 Bazel 构建的程序
carbon-language/carbon-lang3.4万2026年10月26日 更新

Builtin functions

免费有简介

本技能介绍如何为Carbon编译器添加内置函数,包括类型注册、签名验证、常量求值、运行时LLVM IR生成、Prelude绑定,以及使用最小Prelude编写针对性测试。

  • 注册内置函数
  • 实现常量求值
  • 添加LLVM IR生成
carbon-language/carbon-lang3.4万2026年10月26日 更新

Code style

免费有简介

了解 Carbon 工具链对 Bazel、C++、Carbon、Markdown 和 Python 文件的格式规范,并查看注释、文档、命名、设计及提交说明的写作要求。

  • 整理 Carbon 代码格式
  • 检查注释和文档
  • 确认名称是否符合实际含义
carbon-language/carbon-lang3.4万2026年10月26日 更新

Diagnostics

免费有简介

本技能介绍如何在Carbon工具链中添加或修改错误和警告,包括注册诊断类型、用带类型的参数构造消息、为源码位置添加标签和上下文,以及检查措辞规范和测试覆盖。

  • 添加诊断消息
  • 为源码位置添加标签
  • 统一诊断措辞
carbon-language/carbon-lang3.4万2026年10月26日 更新

查看 carbon-language 的全部技能

报告此技能的问题