Skip to content

Commit

Permalink
Group tests into a tests module
Browse files Browse the repository at this point in the history
Usually, I prefer to have simple tests (ones that do not involve any
setup) to be close to the code that they are testing, and only put tests
that require some amount of setup into a tests module.
  • Loading branch information
xfbs committed Oct 20, 2023
1 parent fef20b6 commit bd073ee
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,6 @@ impl PackageStore {
}
}

#[test]
fn can_get_proto_path() {
assert_eq!(
PackageStore::new("/tmp".into()).proto_path(),
PathBuf::from("/tmp/proto")
);
assert_eq!(
PackageStore::new("/tmp".into()).proto_vendor_path(),
PathBuf::from("/tmp/proto/vendor")
);
}

/// An in memory representation of a `buffrs` package
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Package {
Expand Down Expand Up @@ -443,35 +431,6 @@ impl fmt::Display for PackageType {
}
}

#[test]
fn can_check_publishable() {
assert!(PackageType::Lib.is_publishable());
assert!(PackageType::Api.is_publishable());
assert!(!PackageType::Impl.is_publishable());
}

#[test]
fn can_check_compilable() {
assert!(PackageType::Lib.is_compilable());
assert!(PackageType::Api.is_compilable());
assert!(!PackageType::Impl.is_compilable());
}

#[test]
fn can_default_package_type() {
assert_eq!(PackageType::default(), PackageType::Impl);
}

#[test]
fn can_parse_package_type() {
let types = [PackageType::Lib, PackageType::Api, PackageType::Impl];
for typ in &types {
let string = typ.to_string();
let parsed: PackageType = string.parse().unwrap();
assert_eq!(parsed, *typ);
}
}

/// A `buffrs` package name for parsing and type safety
#[derive(Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[serde(try_from = "String", into = "String")]
Expand Down Expand Up @@ -542,20 +501,6 @@ impl PackageName {
}
}

#[test]
fn can_parse_package_name() {
assert_eq!(PackageName::new("abc"), Ok(PackageName("abc".into())));
assert_eq!(PackageName::new("a"), Err(PackageNameError::Length(1)));
assert_eq!(
PackageName::new("4abc"),
Err(PackageNameError::InvalidStart('4'))
);
assert_eq!(
PackageName::new("serde_typename"),
Err(PackageNameError::InvalidCharacter('_', 5))
);
}

impl TryFrom<String> for PackageName {
type Error = PackageNameError;

Expand Down Expand Up @@ -591,3 +536,63 @@ impl fmt::Display for PackageName {
self.0.fmt(f)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn can_parse_package_name() {
assert_eq!(PackageName::new("abc"), Ok(PackageName("abc".into())));
assert_eq!(PackageName::new("a"), Err(PackageNameError::Length(1)));
assert_eq!(
PackageName::new("4abc"),
Err(PackageNameError::InvalidStart('4'))
);
assert_eq!(
PackageName::new("serde_typename"),
Err(PackageNameError::InvalidCharacter('_', 5))
);
}

#[test]
fn can_get_proto_path() {
assert_eq!(
PackageStore::new("/tmp".into()).proto_path(),
PathBuf::from("/tmp/proto")
);
assert_eq!(
PackageStore::new("/tmp".into()).proto_vendor_path(),
PathBuf::from("/tmp/proto/vendor")
);
}

#[test]
fn can_check_publishable() {
assert!(PackageType::Lib.is_publishable());
assert!(PackageType::Api.is_publishable());
assert!(!PackageType::Impl.is_publishable());
}

#[test]
fn can_check_compilable() {
assert!(PackageType::Lib.is_compilable());
assert!(PackageType::Api.is_compilable());
assert!(!PackageType::Impl.is_compilable());
}

#[test]
fn can_default_package_type() {
assert_eq!(PackageType::default(), PackageType::Impl);
}

#[test]
fn can_parse_package_type() {
let types = [PackageType::Lib, PackageType::Api, PackageType::Impl];
for typ in &types {
let string = typ.to_string();
let parsed: PackageType = string.parse().unwrap();
assert_eq!(parsed, *typ);
}
}
}

0 comments on commit bd073ee

Please sign in to comment.