1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00

core: add Deps::to_json() (#2223)

This commit is contained in:
Greg Altman 2019-04-27 13:04:09 -04:00 committed by Ryan Dahl
parent 40d8ef1ec9
commit 1d4b14e306

View file

@ -500,6 +500,22 @@ impl Deps {
})
}
}
pub fn to_json(&self) -> String {
let mut children = "[".to_string();
if let Some(ref deps) = self.deps {
for d in deps {
children.push_str(&d.to_json());
if !d.is_last {
children.push_str(",");
}
}
}
children.push_str("]");
format!("[\"{}\",{}]", self.name, children)
}
}
impl fmt::Display for Deps {
@ -918,4 +934,22 @@ mod tests {
assert_eq!(bar_deps.name, "bar");
assert_eq!(bar_deps.deps, Some(vec![]));
}
#[test]
fn test_deps_to_json() {
let mut modules = Modules::new();
modules.register(1, "foo");
modules.register(2, "bar");
modules.register(3, "baz");
modules.register(4, "zuh");
modules.add_child(1, "bar");
modules.add_child(1, "baz");
modules.add_child(3, "zuh");
let maybe_deps = modules.deps("foo");
assert!(maybe_deps.is_some());
assert_eq!(
"[\"foo\",[[\"bar\",[]],[\"baz\",[[\"zuh\",[]]]]]]",
maybe_deps.unwrap().to_json()
);
}
}