Files
rust/library/coretests/tests/panic/location.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.2 KiB
Rust
Raw Normal View History

2022-09-27 19:09:32 +00:00
use core::panic::Location;
// Note: Some of the following tests depend on the source location,
// so please be careful when editing this file.
mod file_a;
mod file_b;
mod file_c;
// A small shuffled set of locations for testing, along with their true order.
const LOCATIONS: [(usize, &'static Location<'_>); 9] = [
(7, file_c::two()),
(0, file_a::one()),
(3, file_b::one()),
(5, file_b::three()),
(8, file_c::three()),
(6, file_c::one()),
(2, file_a::three()),
(4, file_b::two()),
(1, file_a::two()),
];
2022-09-27 19:09:32 +00:00
#[test]
fn location_const_caller() {
2022-09-27 19:40:53 +00:00
const _CALLER_REFERENCE: &Location<'static> = Location::caller();
const _CALLER: Location<'static> = *Location::caller();
2022-09-27 19:09:32 +00:00
}
#[test]
fn location_const_file() {
2022-09-27 19:40:53 +00:00
const CALLER: &Location<'static> = Location::caller();
const FILE: &str = CALLER.file();
2022-10-08 11:48:53 +00:00
assert_eq!(FILE, file!());
2022-09-27 19:09:32 +00:00
}
#[test]
fn location_const_line() {
2022-09-27 19:40:53 +00:00
const CALLER: &Location<'static> = Location::caller();
const LINE: u32 = CALLER.line();
assert_eq!(LINE, 38);
2022-09-27 19:09:32 +00:00
}
#[test]
fn location_const_column() {
2022-09-27 19:40:53 +00:00
const CALLER: &Location<'static> = Location::caller();
const COLUMN: u32 = CALLER.column();
assert_eq!(COLUMN, 40);
2022-09-27 19:23:52 +00:00
}
2025-06-11 18:28:17 +02:00
#[test]
fn location_file_lifetime<'x>() {
// Verify that the returned `&str`s lifetime is derived from the generic
// lifetime 'a, not the lifetime of `&self`, when calling `Location::file`.
// Test failure is indicated by a compile failure, not a runtime panic.
let _: for<'a> fn(&'a Location<'x>) -> &'x str = Location::file;
}
2025-06-11 18:28:17 +02:00
#[test]
fn location_debug() {
let f = format!("{:?}", Location::caller());
assert!(f.contains(&format!("{:?}", file!())));
2025-08-02 17:34:59 -04:00
assert!(f.contains("60"));
2025-06-11 18:28:17 +02:00
assert!(f.contains("29"));
}
#[test]
fn location_eq() {
for (i, a) in LOCATIONS {
for (j, b) in LOCATIONS {
if i == j {
assert_eq!(a, b);
} else {
assert_ne!(a, b);
}
}
}
}
#[test]
fn location_ord() {
let mut locations = LOCATIONS.clone();
locations.sort_by_key(|(_o, l)| **l);
for (correct, (order, _l)) in locations.iter().enumerate() {
assert_eq!(correct, *order);
}
}