summaryrefslogtreecommitdiff
path: root/cli/tools/coverage/util.rs
blob: e9518e1f78e7270b01e56523552c7f1826201e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2018-2024 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");
  }
}