From 360f75cb1a942f36a5ed1cbf9741d0477517ed99 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 14 Jul 2025 20:10:28 +0300 Subject: [PATCH] test: add unittests --- Cargo.lock | 293 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 + src/from_file.rs | 181 ++++++++++++++++++++++++++--- src/lib.rs | 2 +- 4 files changed, 462 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee3d027..d10029b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,18 +8,91 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "claims" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bba18ee93d577a8428902687bcc2b6b45a56b1981a1f6d779731c86cc4c5db18" + +[[package]] +name = "errno" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad" +dependencies = [ + "libc", + "windows-sys 0.60.2", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + [[package]] name = "filecaster" version = "0.1.0" dependencies = [ + "claims", "merge", "proc-macro-error", "proc-macro2", "quote", "serde", + "serde_json", "syn 2.0.104", + "tempfile", ] +[[package]] +name = "getrandom" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "libc" +version = "0.2.174" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" + +[[package]] +name = "linux-raw-sys" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" + +[[package]] +name = "memchr" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" + [[package]] name = "merge" version = "0.2.0" @@ -51,6 +124,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -115,6 +194,31 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rustix" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.59.0", +] + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + [[package]] name = "serde" version = "1.0.219" @@ -135,6 +239,18 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + [[package]] name = "syn" version = "1.0.109" @@ -156,6 +272,19 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a64e3985349f2441a1a9ef0b853f869006c3855f2cda6862a94d26ebb9d6a1" +dependencies = [ + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + [[package]] name = "unicode-ident" version = "1.0.18" @@ -167,3 +296,167 @@ name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.2", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c66f69fcc9ce11da9966ddb31a40968cad001c5bedeb5c2b82ede4253ab48aef" +dependencies = [ + "windows_aarch64_gnullvm 0.53.0", + "windows_aarch64_msvc 0.53.0", + "windows_i686_gnu 0.53.0", + "windows_i686_gnullvm 0.53.0", + "windows_i686_msvc 0.53.0", + "windows_x86_64_gnu 0.53.0", + "windows_x86_64_gnullvm 0.53.0", + "windows_x86_64_msvc 0.53.0", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] diff --git a/Cargo.toml b/Cargo.toml index 3697198..6cbb28e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,3 +39,8 @@ proc-macro = true pedantic = "warn" nursery = "warn" unwrap_used = "warn" + +[dev-dependencies] +claims = "0.8.0" +serde_json = "1.0.140" +tempfile = "3.20.0" diff --git a/src/from_file.rs b/src/from_file.rs index c79641a..23f3b46 100644 --- a/src/from_file.rs +++ b/src/from_file.rs @@ -24,7 +24,7 @@ pub fn impl_from_file(input: &DeriveInput) -> Result { Ok(quote! { #derive_clause - #vis struct #file_ident { + #vis struct #file_ident #where_clause { #(#file_fields),* } @@ -75,7 +75,7 @@ fn process_fields( .ok_or_else(|| Error::new_spanned(field, "Expected named fields"))?; let ty = &field.ty; - let default_expr = parse_default_attrs(&field.attrs)?; + let default_expr = parse_from_file_default_attr(&field.attrs)?; let field_attrs = if WITH_MERGE { quote! { @@ -146,30 +146,175 @@ fn add_trait_bouds(mut generics: Generics) -> Generics { generics } -fn parse_default_attrs(attrs: &[Attribute]) -> Result> { +/// Parses attributes for `#[from_file(default = ...)]` +fn parse_from_file_default_attr(attrs: &[Attribute]) -> Result> { for attr in attrs { - if let Some(expr) = parse_default_attr(attr)? { - return Ok(Some(expr)); + if !attr.path().is_ident("from_file") { + continue; // Not a #[from_file] attribute, skip it } + + // Parse the content inside the parentheses of #[from_file(...)] + return match &attr.meta { + Meta::List(meta_list) => { + let mut default_expr = None; + meta_list.parse_nested_meta(|meta| { + if meta.path.is_ident("default") { + let value = meta.value()?; + let expr = value.parse::()?; + default_expr = Some(expr); + } + Ok(()) + })?; + Ok(default_expr) + } + _ => Err(Error::new_spanned( + attr, + "Expected #[from_file(default = \"literal\")] or similar", + )), + }; } Ok(None) } -fn parse_default_attr(attr: &Attribute) -> Result> { - if !attr.path().is_ident("default") { - return Ok(None); +#[cfg(test)] +mod tests { + use claims::{assert_err, assert_none}; + use quote::ToTokens; + + use super::*; + + #[test] + fn extract_named_fields_success() { + let input: DeriveInput = parse_quote! { + struct S { x: i32, y: String } + }; + let fields = extract_named_fields(&input).unwrap(); + let names = fields + .named + .iter() + .map(|f| f.ident.as_ref().unwrap().to_string()) + .collect::>(); + assert_eq!(names, vec!["x", "y"]); } - let meta = attr.parse_args::()?; - let Meta::NameValue(name_value) = meta else { - return Err(Error::new_spanned(attr, "Expected #[default = \"value\"]")); - }; + #[test] + fn extract_named_fields_err_on_enum() { + let input: DeriveInput = parse_quote! { + enum E { A, B } + }; + assert_err!(extract_named_fields(&input)); + } - match name_value.value { - Expr::Lit(expr_lit) => Ok(Some(Expr::Lit(expr_lit))), - _ => Err(Error::new_spanned( - &name_value.value, - "Default value must be a literal", - )), + #[test] + fn extract_named_fields_err_on_tuple_struct() { + let input: DeriveInput = parse_quote! { + struct T(i32, String); + }; + assert_err!(extract_named_fields(&input)); + } + + #[test] + fn parse_default_attrs_picks_first_default() { + let attrs: Vec = vec![ + parse_quote!(#[foo]), + parse_quote!(#[from_file(default = "bar")]), + parse_quote!(#[from_file(default = "baz")]), + ]; + let expr = parse_from_file_default_attr(&attrs).unwrap().unwrap(); + // should pick the first default attribute + assert_eq!(expr, parse_quote!("bar")); + } + + #[test] + fn parse_default_attrs_none() { + let attrs: Vec = vec![parse_quote!(#[foo])]; + assert_none!(parse_from_file_default_attr(&attrs).unwrap()); + } + + #[test] + fn process_fields_mixed() { + let fields: FieldsNamed = parse_quote! { + { + #[from_file(default = 1)] + a: u32, + b: String, + } + }; + let (assign, file_fields, bounds) = process_fields(&fields).unwrap(); + // two fields + assert_eq!(assign.len(), 2); + assert_eq!(file_fields.len(), 2); + // a uses unwrap_or_else + assert!( + assign[0] + .to_string() + .contains("a : file . a . unwrap_or_else") + ); + // b uses unwrap_or_default + assert!( + assign[1] + .to_string() + .contains("b : file . b . unwrap_or_default") + ); + // default-bound should only mention String + assert_eq!(bounds.len(), 1); + assert!(bounds[0].to_string().contains("String : Default")); + } + + #[test] + fn build_where_clause_to_new() { + let bounds = vec![quote! { A: Default }, quote! { B: Default }]; + let wc = build_where_clause(None, bounds).unwrap().unwrap(); + let s = wc.to_token_stream().to_string(); + assert!(s.contains("where A : Default , B : Default")); + } + + #[test] + fn build_where_clause_append_existing() { + let orig: WhereClause = parse_quote!(where X: Clone); + let bounds = vec![quote! { Y: Default }]; + let wc = build_where_clause(Some(orig.clone()), bounds) + .unwrap() + .unwrap(); + let preds: Vec<_> = wc + .predicates + .iter() + .map(|p| p.to_token_stream().to_string()) + .collect(); + assert!(preds.contains(&"X : Clone".to_string())); + assert!(preds.contains(&"Y : Default".to_string())); + } + + #[test] + fn build_where_clause_no_bounds_keeps_original() { + let orig: WhereClause = parse_quote!(where Z: Eq); + let wc = build_where_clause(Some(orig.clone()), vec![]) + .unwrap() + .unwrap(); + let preds: Vec<_> = wc + .predicates + .iter() + .map(|p| p.to_token_stream().to_string()) + .collect(); + assert_eq!(preds, vec!["Z : Eq".to_string()]); + } + + #[test] + fn build_derive_clause_defaults() { + let derive_ts = build_derive_clause(); + let s = derive_ts.to_string(); + dbg!(&s); + assert!(s.contains( + "derive (Debug , Clone , Default , serde :: Deserialize , serde :: Serialize)" + )); + } + + #[test] + fn add_trait_bouds_appends_default() { + let gens: Generics = parse_quote!(); + let new = add_trait_bouds(gens); + let s = new.to_token_stream().to_string(); + assert!(s.contains("T : Default")); + assert!(s.contains("U : Default")); } } diff --git a/src/lib.rs b/src/lib.rs index 72d7ae4..8f08b12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ mod from_file; -use from_file::impl_from_file; +pub(crate) use from_file::impl_from_file; use proc_macro::TokenStream; use proc_macro_error::proc_macro_error; use syn::{DeriveInput, parse_macro_input};