diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2023-12-08 16:54:52 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-08 16:54:52 +0900 |
commit | d68d1e202285df30893968c8ba71b4a0a769b357 (patch) | |
tree | 40cb0c267c49e9b7e147c92e2f16a304cb2c413c /cli/tools/coverage/util.rs | |
parent | c5c5dea90debcc5ec53b4803ca530558df32e43f (diff) |
feat(coverage): add html reporter (#21495)
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/tools/coverage/util.rs')
-rw-r--r-- | cli/tools/coverage/util.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/cli/tools/coverage/util.rs b/cli/tools/coverage/util.rs new file mode 100644 index 000000000..af986fb23 --- /dev/null +++ b/cli/tools/coverage/util.rs @@ -0,0 +1,103 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use deno_core::url::Url; + +pub fn find_root(urls: Vec<&Url>) -> Option<Url> { + if urls.is_empty() { + return None; + } + + // Gets the common first part of all the urls. + let root = urls[0] + .as_ref() + .chars() + .enumerate() + .take_while(|(i, c)| { + urls.iter().all(|u| u.as_ref().chars().nth(*i) == Some(*c)) + }) + .map(|(_, c)| c) + .collect::<String>(); + + if let Some(index) = root.rfind('/') { + // Removes the basename part if exists. + Url::parse(&root[..index + 1]).ok() + } else { + Url::parse(&root).ok() + } +} + +pub fn percent_to_class(percent: f32) -> &'static str { + match percent { + x if x < 50.0 => "low", + x if x < 80.0 => "medium", + _ => "high", + } +} + +pub fn calc_coverage_display_info( + hit: usize, + miss: usize, +) -> (usize, f32, &'static str) { + let total = hit + miss; + let percent = if total == 0 { + 100.0 + } else { + (hit as f32 / total as f32) * 100.0 + }; + let class = percent_to_class(percent); + (total, percent, class) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_find_root() { + let urls = vec![ + Url::parse("file:///a/b/c/d/e.ts").unwrap(), + Url::parse("file:///a/b/c/d/f.ts").unwrap(), + Url::parse("file:///a/b/c/d/g.ts").unwrap(), + ]; + let urls = urls.iter().collect(); + assert_eq!(find_root(urls), Url::parse("file:///a/b/c/d/").ok()); + } + + #[test] + fn test_find_root_empty() { + let urls = vec![]; + assert_eq!(find_root(urls), None); + } + + #[test] + fn test_find_root_with_similar_filenames() { + let urls = vec![ + Url::parse("file:///a/b/c/d/foo0.ts").unwrap(), + Url::parse("file:///a/b/c/d/foo1.ts").unwrap(), + Url::parse("file:///a/b/c/d/foo2.ts").unwrap(), + ]; + let urls = urls.iter().collect(); + assert_eq!(find_root(urls), Url::parse("file:///a/b/c/d/").ok()); + } + + #[test] + fn test_find_root_with_similar_dirnames() { + let urls = vec![ + Url::parse("file:///a/b/c/foo0/mod.ts").unwrap(), + Url::parse("file:///a/b/c/foo1/mod.ts").unwrap(), + Url::parse("file:///a/b/c/foo2/mod.ts").unwrap(), + ]; + let urls = urls.iter().collect(); + assert_eq!(find_root(urls), Url::parse("file:///a/b/c/").ok()); + } + + #[test] + fn test_percent_to_class() { + assert_eq!(percent_to_class(0.0), "low"); + assert_eq!(percent_to_class(49.9), "low"); + assert_eq!(percent_to_class(50.0), "medium"); + assert_eq!(percent_to_class(79.9), "medium"); + assert_eq!(percent_to_class(80.0), "high"); + assert_eq!(percent_to_class(100.0), "high"); + } +} |