From b5907101528f8b2a42cbd44d21f1b8e0436c7ccb Mon Sep 17 00:00:00 2001 From: SkyfallWasTaken Date: Tue, 30 Jul 2024 09:43:29 +0100 Subject: [PATCH] Add `author_variants`, `repository_variants`, `test_parse` tests --- Cargo.lock | 24 +++++++ crates/dinopkg-package-json/Cargo.toml | 4 ++ crates/dinopkg-package-json/src/lib.rs | 88 +++++++++++++++++++++++++- 3 files changed, 113 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index be8b205..318910d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -367,6 +367,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "dinopkg-cli" version = "0.1.0" @@ -404,6 +410,8 @@ dependencies = [ name = "dinopkg-package-json" version = "0.3.0" dependencies = [ + "maplit", + "pretty_assertions", "serde", "serde_json", "serde_with", @@ -1400,6 +1408,16 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -2453,6 +2471,12 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "zeroize" version = "1.8.1" diff --git a/crates/dinopkg-package-json/Cargo.toml b/crates/dinopkg-package-json/Cargo.toml index baa0420..57126be 100644 --- a/crates/dinopkg-package-json/Cargo.toml +++ b/crates/dinopkg-package-json/Cargo.toml @@ -9,3 +9,7 @@ serde_json = "1.0.120" serde_with = "3.9.0" thiserror = "1.0.61" tokio = { version = "1.38.0", features = ["fs"], optional = true } + +[dev-dependencies] +maplit = "1.0.2" +pretty_assertions = "1.4.0" diff --git a/crates/dinopkg-package-json/src/lib.rs b/crates/dinopkg-package-json/src/lib.rs index f4bff2e..e4465f6 100644 --- a/crates/dinopkg-package-json/src/lib.rs +++ b/crates/dinopkg-package-json/src/lib.rs @@ -8,7 +8,7 @@ mod util; #[serde_as] #[skip_serializing_none] -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Default)] #[serde(rename_all = "camelCase")] pub struct PackageJson { pub name: String, @@ -28,14 +28,14 @@ pub struct PackageJson { pub dev_dependencies: Option, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(untagged)] pub enum AuthorVariant { Author { name: String, url: Option }, String(String), } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] #[serde(untagged)] pub enum RepositoryVariant { Repository { r#type: String, url: Option }, @@ -91,3 +91,85 @@ impl PackageJson { Ok((Self::parse(&file)?, path)) } } + +#[cfg(test)] +mod tests { + use super::*; + use maplit::hashmap; + use pretty_assertions::assert_eq; + + #[test] + fn test_parse() { + let json = r#"{ + "name": "dinopkg-package-json", + "version": "0.1.0", + "author": "Skyfall", + "dependencies": { + "express": "^4.17.1" + } + }"#; + let package_json = PackageJson::parse(json).unwrap(); + assert_eq!( + package_json, + PackageJson { + name: "dinopkg-package-json".into(), + version: "0.1.0".into(), + author: Some(AuthorVariant::String("Skyfall".into())), + dependencies: Some(hashmap! { + "express".into() => "^4.17.1".into(), + }), + ..Default::default() + } + ) + } + + #[test] + fn author_variants() { + let json = r#"{ + "name": "dinopkg-package-json", + "version": "0.1.0", + "author": { + "name": "Skyfall", + "url": "https://skyfall.dev" + } + }"#; + let package_json = PackageJson::parse(json).unwrap(); + assert_eq!( + package_json, + PackageJson { + name: "dinopkg-package-json".into(), + version: "0.1.0".into(), + author: Some(AuthorVariant::Author { + name: "Skyfall".into(), + url: Some("https://skyfall.dev".into()) + }), + ..Default::default() + } + ) + } + + #[test] + fn repository_variants() { + let json = r#"{ + "name": "dinopkg-package-json", + "version": "0.1.0", + "repository": { + "type": "git", + "url": "git+https://github.com/SkyfallWasTaken/choco.git" + } + }"#; + let package_json = PackageJson::parse(json).unwrap(); + assert_eq!( + package_json, + PackageJson { + name: "dinopkg-package-json".into(), + version: "0.1.0".into(), + repository: Some(RepositoryVariant::Repository { + r#type: "git".into(), + url: Some("git+https://github.com/SkyfallWasTaken/choco.git".into()) + }), + ..Default::default() + } + ) + } +}