plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
2b84e73
— Peter John
2026-11-10T20:07:36+05:30
Errors in a test name the test under plum test too
compiler/checker/tests/checker/review_tests.rs
CHANGED
|
@@ -162,3 +162,19 @@ fun main() -> Int =\n a: Thing<Int> := Box(id: 1, w: 2)\n b: Thing<Str> := Box
|
|
|
162
162
|
}
|
|
163
163
|
assert_eq!(plum_test_support::runInt("open_order", src), 150);
|
|
164
164
|
}
|
|
165
|
+
|
|
166
|
+
/// Under `plum test` a test is checked as the fn it is lowered to: its errors still name
|
|
167
|
+
/// the test, and not the hidden locals an `assert a == b` binds.
|
|
168
|
+
#[test]
|
|
169
|
+
fn a_lowered_tests_errors_name_the_test() {
|
|
170
|
+
let source = plum_test_support::load(
|
|
171
|
+
"test_names",
|
|
172
|
+
"import std/Int\nimport std/Str\nimport std/Option\n\nfun a(o: Option<Int>) -> Int = o.unwrapOr(1)\n\ntest \"adds up\"\n assert a(Some(\"x\")) == 1\n",
|
|
173
|
+
)
|
|
174
|
+
.expect("loads");
|
|
175
|
+
let program = plum_core::tests_lower::lowerTests(&source);
|
|
176
|
+
let es = plum_checker::checkProgram(&program.source).0.expect_err("a type error");
|
|
177
|
+
let m = &es[0].message;
|
|
178
|
+
assert!(m.contains("test \"adds up\""), "{m}");
|
|
179
|
+
assert!(!m.contains("__"), "{m}");
|
|
180
|
+
}
|
compiler/core/src/diag.rs
CHANGED
|
@@ -72,10 +72,38 @@ impl fmt::Display for Diagnostic {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/// `fn '__test_N'` as the test it was lowered from (`test "adds"`), and no `assign '__lhs_N': `
|
|
76
|
+
/// for the hidden locals an `assert a == b` binds its operands to.
|
|
77
|
+
fn testNames(text: &str) -> String {
|
|
78
|
+
if !text.contains("'__") {
|
|
79
|
+
return text.to_string();
|
|
80
|
+
}
|
|
81
|
+
let mut out = text.to_string();
|
|
82
|
+
while let Some(at) = out.find("fn '__test_") {
|
|
83
|
+
let name_start = at + "fn '".len();
|
|
84
|
+
let Some(len) = out[name_start..].find('\'') else { break };
|
|
85
|
+
let internal = out[name_start..name_start + len].to_string();
|
|
86
|
+
let Some(shown) = crate::tests_lower::testDisplayName(&internal) else { break };
|
|
87
|
+
out.replace_range(at..name_start + len + 1, &shown);
|
|
88
|
+
}
|
|
89
|
+
for side in ["__lhs_", "__rhs_"] {
|
|
90
|
+
while let Some(at) = out.find(&format!("assign '{side}")) {
|
|
91
|
+
let digits = at + "assign '".len() + side.len();
|
|
92
|
+
let end = digits + out[digits..].chars().take_while(|c| c.is_ascii_digit()).count();
|
|
93
|
+
if !out[end..].starts_with("': ") {
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
out.replace_range(at..end + 3, "");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
out
|
|
100
|
+
}
|
|
101
|
+
|
|
75
102
|
/// `text` with the module mark of renamed definitions removed (`clamp@mathx` -> `clamp`), and
|
|
76
103
|
/// the suffix of a renamed match binder (`n'0` -> `n`, see `binders`), so messages show the
|
|
77
104
|
/// names as written in the source (see `modules`).
|
|
78
105
|
pub fn sourceName(text: &str) -> String {
|
|
106
|
+
let text = &testNames(text);
|
|
79
107
|
let fresh = crate::binders::FRESH_MARK;
|
|
80
108
|
if !text.contains(crate::modules::MODULE_MARK) && !text.contains(fresh) {
|
|
81
109
|
return text.to_string();
|
compiler/core/src/tests_lower.rs
CHANGED
|
@@ -8,9 +8,25 @@ pub struct TestProgram {
|
|
|
8
8
|
pub names: Vec<String>,
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
thread_local! {
|
|
12
|
+
/// The fn each test became -> the test's name, for messages (`testDisplayName`).
|
|
13
|
+
static TEST_FNS: std::cell::RefCell<std::collections::BTreeMap<String, String>> = Default::default();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/// `test "name"` for the fn a test was lowered to (`__test_N`), as the user wrote it.
|
|
17
|
+
pub fn testDisplayName(internal: &str) -> Option<String> {
|
|
18
|
+
TEST_FNS.with(|t| t.borrow().get(internal).map(|n| format!("test \"{n}\"")))
|
|
19
|
+
}
|
|
20
|
+
|
|
11
21
|
/// Temp names (`__lhs_N`) restart at 0 on every call.
|
|
12
22
|
pub fn lowerTests(source: &ast::Source) -> TestProgram {
|
|
13
23
|
let (mut lowered, extraExports) = desugarTestsToFns(source);
|
|
24
|
+
TEST_FNS.with(|t| {
|
|
25
|
+
*t.borrow_mut() = extraExports
|
|
26
|
+
.iter()
|
|
27
|
+
.map(|(internal, export)| (internal.clone(), export["test#".len()..].to_string()))
|
|
28
|
+
.collect()
|
|
29
|
+
});
|
|
14
30
|
// The desugaring builds nodes (assert messages, temps) with no id.
|
|
15
31
|
ast::numberIds(&mut lowered);
|
|
16
32
|
let names = extraExports
|