Skip to content

Commit

Permalink
test: add cases for format_location
Browse files Browse the repository at this point in the history
Because this function uses a regex, it's better to add some tests to make sure we don't break anything if we want to change the regex.
  • Loading branch information
Rustin170506 committed Apr 9, 2024
1 parent e176a7a commit be6c973
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tokio-console/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,35 @@ fn pb_duration(dur: prost_types::Duration) -> Duration {
let nanos = u64::try_from(dur.nanos).expect("duration should not be negative!");
Duration::from_secs(secs) + Duration::from_nanos(nanos)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_format_location() {
let mut location1 = proto::Location::default();
location1.file = Some(
"/home/user/.cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/tokio-1.0.1/src/lib.rs"
.to_string(),
);
let mut location2 = proto::Location::default();
location2.file = Some("/home/user/.cargo/git/checkouts/tokio-1.0.1/src/lib.rs".to_string());
let mut location3 = proto::Location::default();
location3.file = Some("/home/user/projects/tokio-1.0.1/src/lib.rs".to_string());

assert_eq!(
format_location(Some(location1)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location2)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location3)),
"/home/user/projects/tokio-1.0.1/src/lib.rs"
);
assert_eq!(format_location(None), "<unknown location>");
}
}

0 comments on commit be6c973

Please sign in to comment.