use super::*; #[test] fn handler_navigation_keeps_bindings_separate_and_never_exposes_prototype_calls() { let root = directory("handler-inspect"); let domain = root.join("module/shop/value"); fs::create_dir_all(domain.join("doc")).unwrap(); write_source( domain.join("app/main.dever"), r#" identity(value: Int) (out: Int) { out = value } double(value: Int) (out: Int) { out = value * 2 } apply(value: Int, transform: handler(value: Int) (out: Int)) (out: Int) { out = transform(value) } start(value: Int) (out: Int) { out = apply(value, identity) + apply(value, double) } unused(transform: handler(value: Int) (out: Int)) (out: Int) { out = transform(1) } "#, ) .unwrap(); fs::write(domain.join("doc/app.md"), "## shop.value.app.apply\n\n调用传入的具名计算。\n\n## shop.value.app.unused\n\n未绑定的模板。\n").unwrap(); let load = |name: Option<&str>| -> serde_json::Value { let mut args = vec!["inspect", root.to_str().unwrap(), "--json"]; if let Some(name) = name { args.extend(["--symbol", name, "--depth", "0"]); } serde_json::from_str(&success(cli(&args))).unwrap() }; let index = load(None); assert_eq!(index["diagnostics"], serde_json::json!([])); assert_eq!(index["documentation_errors"], serde_json::json!([])); let symbols = index["symbols"].as_array().unwrap(); let template = symbols .iter() .find(|s| s["name"] == "shop.value.app.apply" && s["kind"] == "template") .unwrap(); let instances = symbols .iter() .filter(|s| s["template"] == template["id"]) .collect::>(); assert_eq!(instances.len(), 2); assert_ne!(instances[0]["name"], instances[1]["name"]); assert_eq!( index["calls"] .as_array() .unwrap() .iter() .filter(|edge| edge["kind"] == "handler") .count(), 2 ); for instance in instances { assert!(instance.get("declarations").is_none()); assert!(instance.get("signatures").is_none()); let name = instance["name"].as_str().unwrap(); assert!(!name.contains("__handler_")); let selected = load(Some(name)); assert_eq!( selected["symbols"][0]["source_name"], "shop.value.app.apply" ); assert!(selected["symbols"][0].get("template").is_none()); assert_eq!( selected["symbols"][0]["declarations"], template["declarations"] ); assert_eq!( selected["symbols"][0]["signatures"][0], template["signatures"][0] .as_str() .unwrap() .replacen("shop.value.app.apply", name, 1) ); let flows = selected["flows"].as_array().unwrap(); assert_eq!(flows.len(), 1); let targets = flows[0]["nodes"] .as_array() .unwrap() .iter() .filter_map(|node| node["target"].as_str()) .collect::>(); assert_eq!(targets.len(), 1); assert!(name.contains(targets[0])); assert_eq!( selected["documentation"][0]["symbol"], "shop.value.app.apply" ); } let all = load(Some("shop.value.app.apply")); assert_eq!(all["flows"].as_array().unwrap().len(), 2); let unused = load(Some("shop.value.app.unused")); assert_eq!(unused["symbols"][0]["kind"], "template"); assert!(unused.get("flows").is_none()); assert_eq!(unused["calls"], serde_json::json!([])); success(cli(&["inspect", root.to_str().unwrap(), "--links"])); success(cli(&[ "inspect", root.to_str().unwrap(), "--links", "--check", ])); let links = fs::read_to_string(domain.join("link.md")).unwrap(); assert!(!links.contains("__handler_")); fs::remove_dir_all(root).unwrap(); }