Add author_variants, repository_variants, test_parse tests

This commit is contained in:
SkyfallWasTaken 2024-07-30 09:43:29 +01:00
parent f72f4d372c
commit b590710152
3 changed files with 113 additions and 3 deletions

24
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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<Dependencies>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum AuthorVariant {
Author { name: String, url: Option<String> },
String(String),
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum RepositoryVariant {
Repository { r#type: String, url: Option<String> },
@ -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()
}
)
}
}