Files
marqdo/tests/occlusion.rs
T
cflmy 6dd0ce47b1 feat(check): M4 L1 引用一致性 — 描述里的反引号实体必须可解析(label.ref_dangling)
规则:描述里的 `名字` = 断言,必须能解析 ——
要么 () 的实现用到它,要么文档定义了它(函数/参数/import 绑定/frontmatter 键),
要么是内置函数。否则报 label.ref_dangling(error)。

只认反引号(可证伪原则):普通汉字不参与判定,避免海量假阳性;
代价是作者不写反引号就得不到保护,这是刻意的——机器只校验它看得懂的断言。

实现:
- refs(label):扫描成对反引号取内部(未闭合/空串忽略,不 panic)
- payload_idents(payload):Var / callee / path 段 / receiver / [k] 变量键 / 插值变量
- module_symbols(module):函数与子函数、参数、import/use 绑定、frontmatter 键
- 内置函数用 crate::host::HostFn::from_name 判定
- OcclusionSite 增加 payload_idents,collect 阶段一次算好

实测:
  悬空 → "描述引用了不存在的实体:`税率` ..." 1 error, exit 1
  可解析(payload 变量 / len 内置 / `> 加 1 2` 行内调用)→ 零诊断

测试:3 个单测(抽取/多引用与散文混排/未闭合不 panic)
+ 2 个集成(悬空 exit 1;三类可解析零诊断)
全量 cargo test --release:583 passed / 0 failed(M2.5 后 578 + 5)

文档:doc/design/change-m4-label-refs.md
2026-10-22 15:14:50 +08:00

102 lines
3.5 KiB
Rust

//! `<TAB>` M2: occlusion syntax end-to-end (CLI + `MARQDO_MARKUP=occlusion`).
//!
//! The mode flag keeps `[键](集合)` (classic index) intact until M7 migrates the
//! corpus; these tests pin both sides of the switch.
use std::process::Command;
fn run(path: &str, markup: Option<&str>) -> (i32, String, String) {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_marqdo"));
cmd.args(["run", path]);
if let Some(m) = markup {
cmd.env("MARQDO_MARKUP", m);
}
let out = cmd.output().expect("run marqdo");
(
out.status.code().unwrap_or(1),
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
}
#[test]
fn occlusion_value_is_payload() {
let (code, stdout, stderr) = run("tests/occlusion/value.mq.md", Some("occlusion"));
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(stdout.trim_end(), "a=2\nb=42\nc=True");
}
#[test]
fn occlusion_off_keeps_classic_index() {
let (code, stdout, stderr) = run("tests/occlusion/classic-index.mq.md", None);
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(stdout.trim_end(), "值");
}
#[test]
fn occlusion_mode_rejects_legacy_index_shape() {
// In occlusion mode `[键](集合)` is read as an occlusion whose payload is the
// collection — i.e. it returns the *whole* map, not the member. This is why
// M7 must migrate before flipping the default; here we only pin that the
// shape no longer means "index".
let (code, stdout, stderr) = run("tests/occlusion/classic-index.mq.md", Some("occlusion"));
assert_eq!(code, 0, "stderr={stderr}");
assert_ne!(stdout.trim_end(), "值", "occlusion mode must not index");
}
// ---- M2.5: label diagnostics (`label.missing` / `label.empty_payload`) ----
fn check(path: &str, markup: &str) -> (i32, String) {
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args(["check", path])
.env("MARQDO_MARKUP", markup)
.output()
.expect("run marqdo check");
(
out.status.code().unwrap_or(1),
format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
),
)
}
#[test]
fn label_missing_is_an_error() {
let (code, text) = check("tests/occlusion/empty-label.mq.md", "occlusion");
assert_eq!(code, 1, "text={text}");
assert!(text.contains("遮蔽缺少描述"), "text={text}");
}
#[test]
fn label_empty_payload_is_a_warning() {
let (code, text) = check("tests/occlusion/empty-payload.mq.md", "occlusion");
assert_eq!(code, 0, "warnings must not fail the build: text={text}");
assert!(text.contains("空载荷遮蔽"), "text={text}");
}
#[test]
fn label_ok_has_no_diagnostics() {
let (code, text) = check("tests/occlusion/clean.mq.md", "occlusion");
assert_eq!(code, 0, "text={text}");
assert!(!text.contains("空载荷"), "text={text}");
assert!(!text.contains("缺少描述"), "text={text}");
}
// ---- M4: L1 reference consistency (`label.ref_dangling`) ----
#[test]
fn label_ref_dangling_is_an_error() {
let (code, text) = check("tests/occlusion/ref-dangling.mq.md", "occlusion");
assert_eq!(code, 1, "text={text}");
assert!(text.contains("描述引用了不存在的实体"), "text={text}");
}
#[test]
fn label_refs_that_resolve_are_fine() {
let (code, text) = check("tests/occlusion/ref-ok.mq.md", "occlusion");
assert_eq!(code, 0, "text={text}");
assert!(!text.contains("不存在的实体"), "text={text}");
}