-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcoverage.rs
More file actions
169 lines (144 loc) · 4.67 KB
/
Copy pathcoverage.rs
File metadata and controls
169 lines (144 loc) · 4.67 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/// The line in a file that was covered by a mantra macro.
#[cfg_attr(feature = "extract", derive(Debug, Clone, PartialEq, Eq))]
pub struct LineCoverage {
line: u32,
#[cfg(not(feature = "extract"))]
file: &'static str,
#[cfg(feature = "extract")]
file: String,
}
impl LineCoverage {
/// Create a new line coverage preferably using `line!()` and `file!()`.
#[cfg(not(feature = "extract"))]
pub fn new(line: u32, file: &'static str) -> Self {
Self { line, file }
}
/// Create a new line coverage preferably using `line!()` and `file!()`.
#[cfg(feature = "extract")]
pub fn new(line: u32, file: impl Into<String>) -> Self {
Self {
line,
file: file.into(),
}
}
pub fn line(&self) -> u32 {
self.line
}
pub fn file(&self) -> &str {
#[cfg(not(feature = "extract"))]
return self.file;
#[cfg(feature = "extract")]
&self.file
}
pub fn log(&self) {
#[cfg(feature = "defmt")]
defmt::println!("{}", self);
}
/// Returns `true` if the given string starts like a mantra coverage log message.
///
/// `mantra coverage at line='`
pub fn potential_log(msg: &str) -> bool {
msg.starts_with("mantra coverage at line='")
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for LineCoverage {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "{}", defmt::Display2Format(self));
}
}
impl core::fmt::Display for LineCoverage {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::write!(
f,
"mantra coverage at line='{}' in file: {}",
self.line,
self.file()
)
}
}
#[cfg(feature = "extract")]
static MANTRA_LINE_COV_REGEX: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
regex::Regex::new(r"mantra coverage at line='(?<line>\d+)' in file: (?<file>.+)").unwrap()
});
#[cfg(feature = "extract")]
const FILE_MATCH_NAME: &str = "file";
#[cfg(feature = "extract")]
const LINE_MATCH_NAME: &str = "line";
#[cfg(feature = "extract")]
impl core::str::FromStr for LineCoverage {
type Err = ExtractError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match MANTRA_LINE_COV_REGEX.captures(s) {
Some(captures) => {
let file_match = captures
.name(FILE_MATCH_NAME)
.ok_or(ExtractError::BadFile)?;
let file = file_match.as_str();
let line_match = captures
.name(LINE_MATCH_NAME)
.ok_or(ExtractError::BadLine)?;
let line: u32 = line_match
.as_str()
.parse()
.map_err(|_| ExtractError::BadLine)?;
Ok(LineCoverage::new(line, file))
}
None => Err(ExtractError::Unmatched),
}
}
}
#[cfg(feature = "extract")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExtractError {
Unmatched,
BadLine,
BadFile,
}
#[cfg(all(test, feature = "extract"))]
mod tests {
use core::{assert_eq, str::FromStr};
use crate::coverage::{ExtractError, LineCoverage};
#[test]
fn extract_valid_coverage() {
let coverage = LineCoverage::new(1, "some-file");
let extracted = LineCoverage::from_str(&coverage.to_string()).unwrap();
assert_eq!(
coverage, extracted,
"Extracted (right) from orig (left) differ"
);
}
#[test]
fn extract_valid_coverage_longer_filepath() {
let coverage = LineCoverage::new(1, "path/path/path/path/some-longer-nested-filepath");
let extracted = LineCoverage::from_str(&coverage.to_string()).unwrap();
assert_eq!(
coverage, extracted,
"Extracted (right) from orig (left) differ"
);
}
#[test]
fn unmatched_msg() {
let msg = "mantra coverage at line='1";
let err = LineCoverage::from_str(msg).unwrap_err();
assert!(
LineCoverage::potential_log(msg),
"Message does not start like a mantra coverage log"
);
assert_eq!(
err,
ExtractError::Unmatched,
"Message is not a valid mantra coverage log"
)
}
#[test]
fn bad_line() {
let msg = "mantra coverage at line='123456789000' in file: some-file";
let err = LineCoverage::from_str(msg).unwrap_err();
assert!(
LineCoverage::potential_log(msg),
"Message does not start like a mantra coverage log"
);
assert_eq!(err, ExtractError::BadLine, "Line number exceeds u32")
}
}