Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,408 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn flow_formatting_resolves_kinds_and_navigation_uses_the_same_steps() {
|
||||
let root = directory("flow-kinds");
|
||||
let path = root.join("main.dever");
|
||||
let source = "rule fee(remote: Bool) (amount: Int)\n| remote | amount |\n| true | 15 |\n| false | 8 |\nflow quote(remote: Bool, count: Int) (total: Int)\n| step | kind | do |\n| - | - | count > 0 else Invalid |\n| amount | - | fee(remote) |\n| total | - | amount * count |\nflow main() (out: Int)\n| step | kind | do |\n| out | - | quote(false, 2) |\n";
|
||||
fs::write(&path, source).unwrap();
|
||||
let file = path.to_str().unwrap();
|
||||
success(cli(&["fmt", file]));
|
||||
let formatted = fs::read_to_string(&path).unwrap();
|
||||
success(cli(&["fmt", file, "--check"]));
|
||||
assert_eq!(formatted, fs::read_to_string(&path).unwrap());
|
||||
success(cli(&["check", file]));
|
||||
assert_eq!(success(cli(&["run", file])).trim(), "16");
|
||||
let index: serde_json::Value = serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
file,
|
||||
"--json",
|
||||
"--symbol",
|
||||
"main.main.app.quote",
|
||||
"--depth",
|
||||
"0",
|
||||
])))
|
||||
.unwrap();
|
||||
assert_eq!(index["symbols"][0]["kind"], "flow");
|
||||
let nodes = index["flows"][0]["nodes"].as_array().unwrap();
|
||||
assert!(
|
||||
nodes
|
||||
.iter()
|
||||
.any(|node| node["label"] == "rule · amount = fee(remote)")
|
||||
);
|
||||
assert!(nodes.iter().any(|node| node["label"] == "传播原错误"));
|
||||
assert!(
|
||||
nodes
|
||||
.iter()
|
||||
.any(|node| node["target"] == "main.main.app.fee")
|
||||
);
|
||||
let invalid = source.replace("fee(remote)", "missing(remote)");
|
||||
fs::write(&path, &invalid).unwrap();
|
||||
assert!(!cli(&["fmt", file]).status.success());
|
||||
assert_eq!(
|
||||
fs::read_to_string(&path).unwrap(),
|
||||
invalid,
|
||||
"unknown targets must not be filled as calc"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workflow_follows_argument_order_rule_branches_and_short_circuiting() {
|
||||
let root = directory("workflow");
|
||||
let path = root.join("app.dever");
|
||||
fs::write(
|
||||
&path,
|
||||
r#"first() (n: Int) { n = 2 }
|
||||
guarded(value: Int) (n: Int) { check value >= 1 else Invalid
|
||||
n = value }
|
||||
nested() (n: Int) { n = guarded(first()) }
|
||||
positive(value: Int) (ok: Bool) { ok = guarded(value) >= 1 }
|
||||
short(flag: Bool, value: Int) (ok: Bool) { ok = flag and positive(value) }
|
||||
rule choose(flag: Bool, value: Int) (out: Int)
|
||||
| flag | value | out |
|
||||
| true | - | guarded(value) |
|
||||
| false | - | 0 |
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
let load = |name: &str| -> serde_json::Value {
|
||||
serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
path.to_str().unwrap(),
|
||||
"--json",
|
||||
"--symbol",
|
||||
name,
|
||||
"--depth",
|
||||
"0",
|
||||
])))
|
||||
.unwrap()
|
||||
};
|
||||
let nested = load("main.main.app.nested");
|
||||
let targets = nested["flows"][0]["nodes"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter_map(|node| node["target"].as_str())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(targets, ["main.main.app.first", "main.main.app.guarded"]);
|
||||
let choice = load("main.main.app.choose");
|
||||
let flow = &choice["flows"][0];
|
||||
let nodes = flow["nodes"].as_array().unwrap();
|
||||
let edges = flow["edges"].as_array().unwrap();
|
||||
let branch = nodes
|
||||
.iter()
|
||||
.position(|node| node["kind"] == "branch")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
edges.iter().filter(|edge| edge["from"] == branch).count(),
|
||||
2
|
||||
);
|
||||
assert!(nodes.iter().any(|node| node["label"] == "flag: true"));
|
||||
assert!(nodes.iter().any(|node| node["label"] == "flag: false"));
|
||||
assert!(nodes.iter().any(|node| node["label"] == "传播原错误"));
|
||||
let short = load("main.main.app.short");
|
||||
let flow = &short["flows"][0];
|
||||
let nodes = flow["nodes"].as_array().unwrap();
|
||||
let branch = nodes
|
||||
.iter()
|
||||
.position(|node| node["kind"] == "branch")
|
||||
.unwrap();
|
||||
let alternatives = flow["edges"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|edge| edge["from"] == branch)
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(alternatives.len(), 2);
|
||||
let skipped = alternatives
|
||||
.iter()
|
||||
.find(|edge| edge["label"] == "不成立,短路")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
nodes[skipped["to"].as_u64().unwrap() as usize]["label"],
|
||||
"布尔结果"
|
||||
);
|
||||
assert!(
|
||||
nodes
|
||||
.iter()
|
||||
.any(|node| node["target"] == "main.main.app.positive")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspect_uses_checked_calls_and_utf16_positions_without_cargo() {
|
||||
let root = directory("inspect");
|
||||
let path = root.join("app.dever");
|
||||
let source = "sink(text: Text, value: Int) (n: Int) { n = value }\nhelper() (n: Int) { n = 3 }\nmain() (n: Int) { n = sink(\"😀\", helper()) }";
|
||||
fs::write(&path, source).unwrap();
|
||||
let output = Command::new(env!("CARGO_BIN_EXE_dever"))
|
||||
.args(["inspect", path.to_str().unwrap(), "--json"])
|
||||
.env("PATH", "")
|
||||
.output()
|
||||
.unwrap();
|
||||
let index: serde_json::Value = serde_json::from_str(&success(output)).unwrap();
|
||||
assert_eq!(index["symbols"].as_array().unwrap().len(), 3);
|
||||
assert_eq!(index["calls"].as_array().unwrap().len(), 2);
|
||||
let helper = index["calls"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|call| call["callee"] == 1)
|
||||
.unwrap();
|
||||
assert_eq!(helper["site"][1], 2);
|
||||
let column = source
|
||||
.lines()
|
||||
.nth(2)
|
||||
.unwrap()
|
||||
.split("helper")
|
||||
.next()
|
||||
.unwrap()
|
||||
.encode_utf16()
|
||||
.count();
|
||||
assert_eq!(helper["site"][2], column);
|
||||
let before = index["files"][0]["sha256"].clone();
|
||||
fs::write(&path, source.replace("n = 3", "n = 4")).unwrap();
|
||||
let index: serde_json::Value = serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
path.to_str().unwrap(),
|
||||
"--json",
|
||||
"--symbol",
|
||||
"main.main.app.helper",
|
||||
"--depth",
|
||||
"1",
|
||||
])))
|
||||
.unwrap();
|
||||
assert_ne!(index["files"][0]["sha256"], before);
|
||||
assert_eq!(index["symbols"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(index["calls"].as_array().unwrap().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspect_keeps_independent_checked_definitions_after_local_errors() {
|
||||
let root = directory("partial-inspect");
|
||||
let path = root.join("app.dever");
|
||||
fs::write(&path, "broken() (n: Int) { n = true }\ndependent() (n: Int) { n = broken() }\nhelper() (n: Int) { n = 3 }\nvalid() (n: Int) { n = helper() }\nmalformed() (n: Int) { n = }\ncmd good = valid\ncmd bad = broken").unwrap();
|
||||
let index: serde_json::Value = serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
path.to_str().unwrap(),
|
||||
"--json",
|
||||
])))
|
||||
.unwrap();
|
||||
let names = index["symbols"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|symbol| symbol["name"].as_str().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
names,
|
||||
[
|
||||
"main.main.app.helper",
|
||||
"main.main.app.valid",
|
||||
"main.main.good"
|
||||
]
|
||||
);
|
||||
assert_eq!(index["calls"].as_array().unwrap().len(), 2);
|
||||
assert!(index["diagnostics"].as_array().unwrap().len() >= 4);
|
||||
assert!(!cli(&["check", path.to_str().unwrap()]).status.success());
|
||||
let selected: serde_json::Value = serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
path.to_str().unwrap(),
|
||||
"--json",
|
||||
"--symbol",
|
||||
"main.main.app.helper",
|
||||
"--depth",
|
||||
"1",
|
||||
])))
|
||||
.unwrap();
|
||||
assert_eq!(selected["symbols"].as_array().unwrap().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspect_excludes_invalid_types_cycles_duplicates_and_their_dependents() {
|
||||
let root = directory("invalid-inspect");
|
||||
let path = root.join("app.dever");
|
||||
let cases = [
|
||||
"lexical() (n: Int) { n = @ }",
|
||||
"lexical() (n: Int) { if true }",
|
||||
"lexical() (n: Text) { n = \"bad\\q\" }",
|
||||
"lexical() (n: Text) { n = \"unclosed\n}",
|
||||
"lexical() (n: Text) { n = \"bad\\\n}",
|
||||
"lexical() (n: Text) { n = \"bad\\u{ffff\n}",
|
||||
"type Broken { value: Missing }\nuseBroken(value: Broken) () {}",
|
||||
"type Cycle { next: Cycle? }\nuseCycle(value: Cycle) () {}",
|
||||
"cycle() (n: Int) { n = cycle() }\nuseCycle() (n: Int) { n = cycle() }",
|
||||
"duplicate() (n: Int) { n = 1 }\nduplicate() (n: Int) { n = 2 }\nuseDuplicate() (n: Int) { n = duplicate() }",
|
||||
"type Duplicate { value: Int }\ntype Duplicate { value: Bool }\nuseDuplicate(value: Duplicate) () {}",
|
||||
"cmd duplicate = valid\ncmd duplicate = valid",
|
||||
];
|
||||
for source in cases {
|
||||
fs::write(&path, format!("{source}\nvalid() (n: Int) {{ n = 3 }}")).unwrap();
|
||||
let index: serde_json::Value = serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
path.to_str().unwrap(),
|
||||
"--json",
|
||||
])))
|
||||
.unwrap();
|
||||
assert_eq!(index["symbols"].as_array().unwrap().len(), 1, "{index}");
|
||||
assert_eq!(index["symbols"][0]["name"], "main.main.app.valid");
|
||||
assert!(index["calls"].as_array().unwrap().is_empty());
|
||||
assert!(!index["diagnostics"].as_array().unwrap().is_empty());
|
||||
}
|
||||
let order = root.join("module/demo/order");
|
||||
let stock = root.join("module/demo/stock");
|
||||
let other = root.join("module/demo/other");
|
||||
for directory in [&order, &stock, &other] {
|
||||
fs::create_dir_all(directory).unwrap();
|
||||
}
|
||||
write_source(
|
||||
order.join("app/main.dever"),
|
||||
"one() (n: Int) { n = stock.two() }\nthree() (n: Int) { n = 3 }",
|
||||
)
|
||||
.unwrap();
|
||||
write_source(
|
||||
stock.join("app/main.dever"),
|
||||
"two() (n: Int) { n = 2 }\nfour() (n: Int) { n = order.three() }",
|
||||
)
|
||||
.unwrap();
|
||||
write_source(other.join("app/main.dever"), "valid() (n: Int) { n = 3 }").unwrap();
|
||||
let index: serde_json::Value = serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
root.to_str().unwrap(),
|
||||
"--json",
|
||||
])))
|
||||
.unwrap();
|
||||
assert_eq!(index["symbols"].as_array().unwrap().len(), 1, "{index}");
|
||||
assert_eq!(index["symbols"][0]["name"], "demo.other.app.valid");
|
||||
assert!(index["calls"].as_array().unwrap().is_empty());
|
||||
assert!(
|
||||
index["diagnostics"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|error| error["code"] == "C010")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inspect_binds_markdown_to_checked_symbols_and_rejects_ambiguous_descriptions() {
|
||||
let root = directory("documentation");
|
||||
let domain = root.join("module/demo/article");
|
||||
fs::create_dir_all(domain.join("doc")).unwrap();
|
||||
write_source(
|
||||
domain.join("app/main.dever"),
|
||||
"title() (text: Text) { text = \"标题\" }",
|
||||
)
|
||||
.unwrap();
|
||||
let doc = domain.join("doc/article.md");
|
||||
fs::write(&doc, "# 文章\n\n```markdown\n## ignored.example\n```\n\n## `demo.article.app.title`\n\n读取标题。\n\n## demo.article.app.missing\n不存在的能力。").unwrap();
|
||||
let load = || -> serde_json::Value {
|
||||
serde_json::from_str(&success(cli(&[
|
||||
"inspect",
|
||||
root.to_str().unwrap(),
|
||||
"--json",
|
||||
])))
|
||||
.unwrap()
|
||||
};
|
||||
let first = load();
|
||||
assert_eq!(first["documentation"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(
|
||||
first["documentation"][0]["symbol"],
|
||||
"demo.article.app.title"
|
||||
);
|
||||
assert_eq!(first["documentation"][0]["markdown"], "读取标题。");
|
||||
assert_eq!(first["documentation_errors"].as_array().unwrap().len(), 1);
|
||||
fs::write(&doc, "## demo.article.app.title\n新版说明。").unwrap();
|
||||
let second = load();
|
||||
assert_ne!(
|
||||
first["documentation_files"][0]["sha256"],
|
||||
second["documentation_files"][0]["sha256"]
|
||||
);
|
||||
assert_eq!(second["documentation"][0]["markdown"], "新版说明。");
|
||||
fs::write(
|
||||
domain.join("doc/duplicate.md"),
|
||||
"## demo.article.app.title\n重复说明。",
|
||||
)
|
||||
.unwrap();
|
||||
let duplicate = load();
|
||||
assert!(duplicate["documentation"].as_array().unwrap().is_empty());
|
||||
assert_eq!(
|
||||
duplicate["documentation_errors"].as_array().unwrap().len(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn links_preserve_user_files_and_track_current_source_and_descriptions() {
|
||||
let root = directory("links");
|
||||
let domain = root.join("module/demo/reader");
|
||||
fs::create_dir_all(domain.join("doc")).unwrap();
|
||||
let source = domain.join("app/main.dever");
|
||||
write_source(
|
||||
&source,
|
||||
"helper() (n: Int) { n = 3 }\nmain() (n: Int) { n = helper() }",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(domain.join("api.dever"), "cmd read = app.main").unwrap();
|
||||
let root = root.to_str().unwrap();
|
||||
let index = domain.join("link.md");
|
||||
let inspect = |check: bool| {
|
||||
if check {
|
||||
cli(&["inspect", root, "--links", "--check"])
|
||||
} else {
|
||||
cli(&["inspect", root, "--links"])
|
||||
}
|
||||
};
|
||||
assert!(!inspect(true).status.success());
|
||||
success(inspect(false));
|
||||
success(inspect(true));
|
||||
let page = fs::read_to_string(&index).unwrap();
|
||||
assert!(page.contains("flowchart TD"));
|
||||
assert!(page.contains("子流程 [demo.reader.app.helper]"));
|
||||
assert!(page.contains("click n"));
|
||||
assert!(
|
||||
page.find("## demo.reader.read").unwrap() < page.find("## demo.reader.app.main").unwrap()
|
||||
);
|
||||
let modified = fs::metadata(&index).unwrap().modified().unwrap();
|
||||
success(inspect(false));
|
||||
assert_eq!(modified, fs::metadata(&index).unwrap().modified().unwrap());
|
||||
fs::write(
|
||||
domain.join("doc/reader.md"),
|
||||
"## demo.reader.app.main\n读取结果。",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!inspect(true).status.success());
|
||||
success(inspect(false));
|
||||
assert!(fs::read_to_string(&index).unwrap().contains("读取结果。"));
|
||||
write_source(
|
||||
&source,
|
||||
"helper() (n: Int) { n = 4 }\nmain() (n: Int) { n = helper() }",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!inspect(true).status.success());
|
||||
success(inspect(false));
|
||||
let before = fs::read(&index).unwrap();
|
||||
write_source(&source, "broken() (n: Int) { n = true }").unwrap();
|
||||
assert!(!inspect(false).status.success());
|
||||
assert_eq!(before, fs::read(&index).unwrap());
|
||||
write_source(
|
||||
&source,
|
||||
"helper() (n: Int) { n = 3 }\nmain() (n: Int) { n = helper() }",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(&index, "用户维护的文档").unwrap();
|
||||
assert!(!inspect(false).status.success());
|
||||
assert_eq!(fs::read_to_string(&index).unwrap(), "用户维护的文档");
|
||||
for flags in [
|
||||
vec!["--check"],
|
||||
vec!["--links", "--json"],
|
||||
vec!["--links", "--symbol", "demo.reader.app.main"],
|
||||
] {
|
||||
let mut arguments = vec!["inspect", root];
|
||||
arguments.extend(flags);
|
||||
assert!(!cli(&arguments).status.success());
|
||||
}
|
||||
}
|
||||
|
||||
#[path = "inspect/handler.rs"]
|
||||
mod handler;
|
||||
Reference in new issue
Block a user