about summary refs log tree commit diff
path: root/library/coretests/tests/panic/location.rs
blob: a7db05a15c68f3b97aecfa613d708350f27f1bcf (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
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()),
];

#[test]
fn location_const_caller() {
    const _CALLER_REFERENCE: &Location<'static> = Location::caller();
    const _CALLER: Location<'static> = *Location::caller();
}

#[test]
fn location_const_file() {
    const CALLER: &Location<'static> = Location::caller();
    const FILE: &str = CALLER.file();
    assert_eq!(FILE, file!());
}

#[test]
fn location_const_line() {
    const CALLER: &Location<'static> = Location::caller();
    const LINE: u32 = CALLER.line();
    assert_eq!(LINE, 38);
}

#[test]
fn location_const_column() {
    const CALLER: &Location<'static> = Location::caller();
    const COLUMN: u32 = CALLER.column();
    assert_eq!(COLUMN, 40);
}

#[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;
}

#[test]
fn location_debug() {
    let f = format!("{:?}", Location::caller());
    assert!(f.contains(&format!("{:?}", file!())));
    assert!(f.contains("60"));
    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);
    }
}