use hurl_core::input::Input;
use crate::report::ReportError;
use crate::runner::HurlResult;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Testcase {
pub(crate) description: String,
pub(crate) success: bool,
}
impl Testcase {
pub fn from(hurl_result: &HurlResult, filename: &Input) -> Testcase {
let description = filename.to_string();
let success = hurl_result.errors().is_empty();
Testcase {
description,
success,
}
}
pub fn parse(line: &str) -> Result<Testcase, ReportError> {
let mut line = line;
let success = if line.starts_with("ok") {
line = &line[2..];
true
} else if line.starts_with("not ok") {
line = &line[6..];
false
} else {
return Err(ReportError::from_string(&format!(
"Invalid TAP line <{line}> - must start with ok or nok"
)));
};
let description = match line.find('-') {
None => {
return Err(ReportError::from_string(&format!(
"Invalid TAP line <{line}> - missing '-' separator"
)));
}
Some(index) => {
if line.split_at(index).0.trim().parse::<usize>().is_err() {
return Err(ReportError::from_string(&format!(
"Invalid TAP line <{line}> - missing test number"
)));
}
line.split_at(index).1[1..].trim().to_string()
}
};
Ok(Testcase {
description,
success,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_tap_test_line() {
assert_eq!(
Testcase::parse("toto").err().unwrap().to_string(),
"Invalid TAP line <toto> - must start with ok or nok".to_string()
);
assert_eq!(
Testcase::parse("ok 1 - tests_ok/test.1.hurl").unwrap(),
Testcase {
description: "tests_ok/test.1.hurl".to_string(),
success: true
}
);
}
}