feat(stdlib): format all value types and expose handle identity members
- Add read-only FunctionRef.module/name and PluginHandle.plugin/export members; PluginHandle.id stays private (ADR-0029). - Extend stdlib/format.mq.md to Formula/Code/FunctionRef/PluginHandle/ Memory/Resource/Unit, with UTF-8 byte length for Code. - Fix two-digit ordered-list body indentation so arms 10+ stay inside their branch (they were previously top-level statements). - Extend stdlib demo to Formula/Code/FunctionRef and assert all three io.写值/打印 pairs match byte-for-byte. - Add runtime member observation test, ADR-0029, spec/status/changelog/evidence. Gate: fmt, clippy -D warnings, 416 tests passed.
This commit is contained in:
1 parent
1377af1601
commit
e119727c5d
14 files changed
+334
-15
No files matched your search
@@ -30,14 +30,23 @@ fn mqmd_stdlib_print_output_matches_builtin_print() {
|
||||
assert_eq!(output.status.code(), Some(0), "{}", stderr(&output));
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let lines: Vec<&str> = stdout.lines().collect();
|
||||
assert_eq!(lines.len(), 4, "{stdout:?}");
|
||||
assert_eq!(lines[0], lines[1], "list formatting differs from 打印");
|
||||
assert_eq!(lines[2], lines[3], "table formatting differs from 打印");
|
||||
assert_eq!(lines.len(), 6, "{stdout:?}");
|
||||
for pair in [(0, 1), (2, 3), (4, 5)] {
|
||||
assert_eq!(
|
||||
lines[pair.0], lines[pair.1],
|
||||
"stdlib formatting differs from 打印 at line {}",
|
||||
pair.0
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
lines[0],
|
||||
r#"["文本", 2, 1.5, [3, true], {a: 1, b: "x"}, None]"#
|
||||
);
|
||||
assert_eq!(lines[2], "Table[名称 | 分数]{甲 | 87; 乙 | 92}");
|
||||
assert_eq!(
|
||||
lines[4],
|
||||
r#"[Formula("a = 1"), Code("text", 9 bytes), FunctionRef(root:default::demo.mq.md::演示函数)]"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1019,6 +1019,24 @@ fn member_of(value: Value, name: &str, span: Option<Span>) -> Result<Value, Runt
|
||||
format!("Code has no field `{name}`"),
|
||||
)),
|
||||
},
|
||||
Value::FunctionRef(reference) => match name {
|
||||
"module" => Ok(Value::Text(reference.module)),
|
||||
"name" => Ok(Value::Text(reference.name)),
|
||||
_ => Err(RuntimeError::new(
|
||||
ErrorCategory::UnknownField,
|
||||
span,
|
||||
format!("FunctionRef has no field `{name}`"),
|
||||
)),
|
||||
},
|
||||
Value::PluginHandle(handle) => match name {
|
||||
"plugin" => Ok(Value::Text(handle.plugin)),
|
||||
"export" => Ok(Value::Text(handle.export)),
|
||||
_ => Err(RuntimeError::new(
|
||||
ErrorCategory::UnknownField,
|
||||
span,
|
||||
format!("PluginHandle has no field `{name}`"),
|
||||
)),
|
||||
},
|
||||
other => Err(RuntimeError::new(
|
||||
ErrorCategory::UnknownField,
|
||||
span,
|
||||
|
||||
@@ -171,6 +171,21 @@ fn ambiguous_ref_resource_is_an_error() {
|
||||
assert_eq!(error.code(), "ref.ambiguous");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_ref_and_resource_members_are_readable() {
|
||||
let source = "## 目标\n\n*1*\n\n## main\n\n### 公式\n\n$$a = 1$$\n\n### 代码\n\n```text\nx\n```\n\n**f = ref(\"#目标\")**\n\n**g = ref(\"#公式\")**\n\n**c = ref(\"#代码\")**\n\n*[f.module, f.name, g.latex, g.display, c.language, c.source]*\n";
|
||||
let value = run_named(source, "main", &[]).unwrap();
|
||||
let Value::List(items) = value else {
|
||||
panic!("expected list");
|
||||
};
|
||||
assert!(matches!(&items[0], Value::Text(module) if module.ends_with("index.mq.md")));
|
||||
assert_eq!(items[1], Value::text("目标"));
|
||||
assert_eq!(items[2], Value::text("a = 1"));
|
||||
assert_eq!(items[3], Value::Bool(true));
|
||||
assert_eq!(items[4], Value::text("text"));
|
||||
assert_eq!(items[5], Value::text("x\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn memory_region_is_shared_by_handle_identity() {
|
||||
let source = "## f\n\n**m = memory.new(1, 2)**\n\n**n = m**\n\n**`m[3] = 7`**\n\n*[n[3], m == n, memory.size(n)]*\n";
|
||||
|
||||
Reference in new issue
Block a user