#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
use core::{fmt, str::FromStr};
use std::error::Error;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum GoTestError {
EmptyName,
InvalidTestName,
InvalidBenchmarkName,
InvalidFuzzTestName,
InvalidExampleName,
EmptyFileName,
InvalidTestFileName,
UnknownLabel,
}
impl fmt::Display for GoTestError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyName => formatter.write_str("Go test name cannot be empty"),
Self::InvalidTestName => formatter.write_str("Go test name must start with `Test`"),
Self::InvalidBenchmarkName => {
formatter.write_str("Go benchmark name must start with `Benchmark`")
}
Self::InvalidFuzzTestName => {
formatter.write_str("Go fuzz test name must start with `Fuzz`")
}
Self::InvalidExampleName => {
formatter.write_str("Go example name must start with `Example`")
}
Self::EmptyFileName => formatter.write_str("Go test file name cannot be empty"),
Self::InvalidTestFileName => {
formatter.write_str("Go test file name should end in `_test.go`")
}
Self::UnknownLabel => formatter.write_str("unknown Go test metadata label"),
}
}
}
impl Error for GoTestError {}
macro_rules! prefixed_name_type {
($name:ident, $prefix:literal, $error:ident) => {
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct $name(String);
impl $name {
pub fn new(value: impl AsRef<str>) -> Result<Self, GoTestError> {
let trimmed = value.as_ref().trim();
if trimmed.is_empty() {
return Err(GoTestError::EmptyName);
}
if !trimmed.starts_with($prefix) {
return Err(GoTestError::$error);
}
Ok(Self(trimmed.to_string()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for $name {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for $name {
type Err = GoTestError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Self::new(value)
}
}
impl TryFrom<&str> for $name {
type Error = GoTestError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
};
}
prefixed_name_type!(GoTestName, "Test", InvalidTestName);
prefixed_name_type!(GoBenchmarkName, "Benchmark", InvalidBenchmarkName);
prefixed_name_type!(GoFuzzTestName, "Fuzz", InvalidFuzzTestName);
prefixed_name_type!(GoExampleName, "Example", InvalidExampleName);
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct GoTestFileName(String);
impl GoTestFileName {
pub fn new(value: impl AsRef<str>) -> Result<Self, GoTestError> {
let trimmed = value.as_ref().trim();
if trimmed.is_empty() {
return Err(GoTestError::EmptyFileName);
}
if !trimmed.ends_with("_test.go") {
return Err(GoTestError::InvalidTestFileName);
}
Ok(Self(trimmed.to_string()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AsRef<str> for GoTestFileName {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for GoTestFileName {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for GoTestFileName {
type Err = GoTestError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Self::new(value)
}
}
impl TryFrom<&str> for GoTestFileName {
type Error = GoTestError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum GoTestOutcome {
Passed,
Failed,
Skipped,
Panicked,
TimedOut,
}
impl GoTestOutcome {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Passed => "passed",
Self::Failed => "failed",
Self::Skipped => "skipped",
Self::Panicked => "panicked",
Self::TimedOut => "timed-out",
}
}
}
impl fmt::Display for GoTestOutcome {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for GoTestOutcome {
type Err = GoTestError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match normalized_label(value)?.as_str() {
"passed" | "pass" => Ok(Self::Passed),
"failed" | "fail" => Ok(Self::Failed),
"skipped" | "skip" => Ok(Self::Skipped),
"panicked" | "panic" => Ok(Self::Panicked),
"timed-out" | "timed_out" | "timed out" | "timeout" => Ok(Self::TimedOut),
_ => Err(GoTestError::UnknownLabel),
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum GoTestKind {
Test,
Benchmark,
Fuzz,
Example,
}
impl GoTestKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Test => "test",
Self::Benchmark => "benchmark",
Self::Fuzz => "fuzz",
Self::Example => "example",
}
}
}
impl fmt::Display for GoTestKind {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for GoTestKind {
type Err = GoTestError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match normalized_label(value)?.as_str() {
"test" => Ok(Self::Test),
"benchmark" | "bench" => Ok(Self::Benchmark),
"fuzz" => Ok(Self::Fuzz),
"example" => Ok(Self::Example),
_ => Err(GoTestError::UnknownLabel),
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum GoTestPackageMode {
Package,
ExternalPackage,
}
impl GoTestPackageMode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Package => "package",
Self::ExternalPackage => "external-package",
}
}
}
impl fmt::Display for GoTestPackageMode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for GoTestPackageMode {
type Err = GoTestError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match normalized_label(value)?.as_str() {
"package" => Ok(Self::Package),
"external-package" | "external_package" | "external package" => {
Ok(Self::ExternalPackage)
}
_ => Err(GoTestError::UnknownLabel),
}
}
}
fn normalized_label(value: &str) -> Result<String, GoTestError> {
let trimmed = value.trim();
if trimmed.is_empty() {
Err(GoTestError::UnknownLabel)
} else {
Ok(trimmed.to_ascii_lowercase())
}
}
#[cfg(test)]
mod tests {
use super::{
GoBenchmarkName, GoExampleName, GoFuzzTestName, GoTestError, GoTestFileName, GoTestKind,
GoTestName, GoTestOutcome, GoTestPackageMode,
};
#[test]
fn validates_test_names() -> Result<(), GoTestError> {
assert_eq!(GoTestName::new("TestHandler")?.as_str(), "TestHandler");
assert_eq!(
GoBenchmarkName::new("BenchmarkServe")?.as_str(),
"BenchmarkServe"
);
assert_eq!(GoFuzzTestName::new("FuzzParser")?.as_str(), "FuzzParser");
assert_eq!(
GoExampleName::new("ExampleClient")?.as_str(),
"ExampleClient"
);
assert_eq!(
GoTestName::new("Handler"),
Err(GoTestError::InvalidTestName)
);
assert_eq!(
GoBenchmarkName::new("BenchServe"),
Err(GoTestError::InvalidBenchmarkName)
);
Ok(())
}
#[test]
fn validates_test_file_names() -> Result<(), GoTestError> {
let file = GoTestFileName::new("handler_test.go")?;
assert_eq!(file.as_str(), "handler_test.go");
assert_eq!(GoTestFileName::new(""), Err(GoTestError::EmptyFileName));
assert_eq!(
GoTestFileName::new("handler.go"),
Err(GoTestError::InvalidTestFileName)
);
Ok(())
}
#[test]
fn parses_test_enums() -> Result<(), GoTestError> {
assert_eq!("timeout".parse::<GoTestOutcome>()?, GoTestOutcome::TimedOut);
assert_eq!("bench".parse::<GoTestKind>()?, GoTestKind::Benchmark);
assert_eq!(
"external package".parse::<GoTestPackageMode>()?,
GoTestPackageMode::ExternalPackage
);
assert_eq!(GoTestKind::Test.to_string(), "test");
Ok(())
}
}