use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TestLanguage {
Rust,
Go,
Python,
Js,
Swift,
Objc,
Shell,
}
impl TestLanguage {
pub fn as_str(self) -> &'static str {
match self {
Self::Rust => "rust",
Self::Go => "go",
Self::Python => "python",
Self::Js => "js",
Self::Swift => "swift",
Self::Objc => "objc",
Self::Shell => "shell",
}
}
pub fn parse(value: &str) -> Result<Self, String> {
Ok(match value {
"rust" => Self::Rust,
"go" => Self::Go,
"python" => Self::Python,
"js" => Self::Js,
"swift" => Self::Swift,
"objc" => Self::Objc,
"shell" => Self::Shell,
other => return Err(format!("'{other}' is not a language the catalogue knows")),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CataloguedTest {
pub id: String,
pub repo: String,
pub language: TestLanguage,
pub number: Option<i64>,
pub function_name: String,
pub description: String,
pub file_path: String,
pub line_number: i64,
}
impl CataloguedTest {
pub fn location(&self) -> String {
format!("{}:{}", self.file_path, self.line_number)
}
pub fn described_number(&self) -> Option<i64> {
let rest = self.description.trim().strip_prefix("TEST")?;
let digits: String = rest.chars().take_while(char::is_ascii_digit).collect();
if digits.is_empty() {
return None;
}
let after = rest[digits.len()..].trim_start();
if !after.starts_with(':') && !after.starts_with('—') && !after.starts_with('-') {
return None;
}
digits.parse().ok()
}
pub fn is_misnumbered(&self) -> bool {
match (self.number, self.described_number()) {
(Some(name), Some(described)) => name != described,
_ => false,
}
}
}