mirror of
https://github.com/kristoferssolo/filecaster.git
synced 2025-10-21 19:00:34 +00:00
test(file): add tests with tempfile
This commit is contained in:
parent
45efc67894
commit
df9f7bf7fc
77
Cargo.lock
generated
77
Cargo.lock
generated
@ -26,6 +26,12 @@ version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bba18ee93d577a8428902687bcc2b6b45a56b1981a1f6d779731c86cc4c5db18"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.13"
|
||||
@ -55,6 +61,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"syn",
|
||||
"tempfile",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -69,6 +76,22 @@ dependencies = [
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.15"
|
||||
@ -227,6 +250,15 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40734c41988f7306bb04f0ecf60ec0f3f1caa34290e4e8ea471dcd3346483b83"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.104"
|
||||
@ -251,6 +283,45 @@ dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed0aee96c12fa71097902e0bb061a5e1ebd766a6636bb605ba401c45c1650eac"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_parser",
|
||||
"toml_writer",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bade1c3e902f58d73d3f294cd7f20391c1cb2fbcb643b73566bc773971df91e3"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_parser"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97200572db069e74c512a14117b296ba0a80a30123fbbb5aa1f4a348f639ca30"
|
||||
dependencies = [
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_writer"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fcc842091f2def52017664b53082ecbbeb5c7731092bad69d2c63050401dfd64"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
@ -412,6 +483,12 @@ version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.7.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3edebf492c8125044983378ecb5766203ad3b4c2f7a922bd7dd207f6d443e95"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.39.0"
|
||||
|
||||
11
Cargo.toml
11
Cargo.toml
@ -25,6 +25,12 @@ syn = { version = "2.0", features = ["extra-traits", "parsing"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
merge = { version = "0.2", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
claims = "0.8"
|
||||
serde_json = "1.0"
|
||||
tempfile = "3.20"
|
||||
toml = "0.9"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
@ -32,8 +38,3 @@ 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"
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
use filecaster::FromFile;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, FromFile)]
|
||||
struct Simple {
|
||||
pub struct Simple {
|
||||
x: i32,
|
||||
#[from_file(default = "hello")]
|
||||
y: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, FromFile)]
|
||||
struct NumericDefault {
|
||||
pub struct NumericDefault {
|
||||
a: i32,
|
||||
#[from_file(default = 42)]
|
||||
b: i32,
|
||||
|
||||
141
tests/from_file_io.rs
Normal file
141
tests/from_file_io.rs
Normal file
@ -0,0 +1,141 @@
|
||||
use filecaster::FromFile;
|
||||
use std::{fs::File, io::Write};
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, FromFile)]
|
||||
pub struct Simple {
|
||||
x: i32,
|
||||
#[from_file(default = "hello")]
|
||||
y: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, FromFile)]
|
||||
pub struct NumericDefault {
|
||||
a: i32,
|
||||
#[from_file(default = 42)]
|
||||
b: i32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_tempfile_full() {
|
||||
let json = r#"{"x": 2, "y": "temp"}"#;
|
||||
let mut tmp = NamedTempFile::new().unwrap();
|
||||
write!(tmp.as_file_mut(), "{}", json).unwrap();
|
||||
|
||||
let file = File::open(tmp.path()).unwrap();
|
||||
let file_struct: SimpleFile = serde_json::from_reader(file).unwrap();
|
||||
let s = Simple::from_file(Some(file_struct));
|
||||
|
||||
assert_eq!(
|
||||
s,
|
||||
Simple {
|
||||
x: 2,
|
||||
y: "temp".to_string()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_tempfile_partial() {
|
||||
let json = r#"{"x": 5}"#;
|
||||
let mut tmp = NamedTempFile::new().unwrap();
|
||||
write!(tmp.as_file_mut(), "{}", json).unwrap();
|
||||
|
||||
let file = File::open(tmp.path()).unwrap();
|
||||
let file_struct: SimpleFile = serde_json::from_reader(file).unwrap();
|
||||
let s = Simple::from_file(Some(file_struct));
|
||||
|
||||
assert_eq!(
|
||||
s,
|
||||
Simple {
|
||||
x: 5,
|
||||
y: "hello".to_string()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_toml_tempfile_full() {
|
||||
let toml_str = r#"
|
||||
x = 7
|
||||
y = "toml_test"
|
||||
"#;
|
||||
let mut tmp = NamedTempFile::new().unwrap();
|
||||
write!(tmp.as_file_mut(), "{}", toml_str).unwrap();
|
||||
|
||||
let content = std::fs::read_to_string(tmp.path()).unwrap();
|
||||
let file_struct: SimpleFile = toml::from_str(&content).unwrap();
|
||||
let s = Simple::from_file(Some(file_struct));
|
||||
|
||||
assert_eq!(
|
||||
s,
|
||||
Simple {
|
||||
x: 7,
|
||||
y: "toml_test".to_string()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_toml_tempfile_partial() {
|
||||
let toml_str = r#"x = 15"#;
|
||||
let mut tmp = NamedTempFile::new().unwrap();
|
||||
write!(tmp.as_file_mut(), "{}", toml_str).unwrap();
|
||||
|
||||
let content = std::fs::read_to_string(tmp.path()).unwrap();
|
||||
let file_struct: SimpleFile = toml::from_str(&content).unwrap();
|
||||
let s = Simple::from_file(Some(file_struct));
|
||||
|
||||
assert_eq!(
|
||||
s,
|
||||
Simple {
|
||||
x: 15,
|
||||
y: "hello".to_string()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_numeric_default_toml() {
|
||||
let toml_str = r#"a = 100"#;
|
||||
let mut tmp = NamedTempFile::new().unwrap();
|
||||
write!(tmp.as_file_mut(), "{}", toml_str).unwrap();
|
||||
|
||||
let content = std::fs::read_to_string(tmp.path()).unwrap();
|
||||
let file_struct: NumericDefaultFile = toml::from_str(&content).unwrap();
|
||||
let n = NumericDefault::from_file(Some(file_struct));
|
||||
|
||||
assert_eq!(n, NumericDefault { a: 100, b: 42 });
|
||||
}
|
||||
|
||||
#[cfg(feature = "merge")]
|
||||
#[test]
|
||||
fn test_merge_from_toml() {
|
||||
use merge::Merge;
|
||||
let toml1 = r#"x = 1"#;
|
||||
let toml2 = r#"y = "merged""#;
|
||||
|
||||
let mut tmp1 = NamedTempFile::new().unwrap();
|
||||
write!(tmp1.as_file_mut(), "{}", toml1).unwrap();
|
||||
let content1 = std::fs::read_to_string(tmp1.path()).unwrap();
|
||||
let mut f1: SimpleFile = toml::from_str(&content1).unwrap();
|
||||
|
||||
let mut tmp2 = NamedTempFile::new().unwrap();
|
||||
write!(tmp2.as_file_mut(), "{}", toml2).unwrap();
|
||||
let content2 = std::fs::read_to_string(tmp2.path()).unwrap();
|
||||
let f2: SimpleFile = toml::from_str(&content2).unwrap();
|
||||
|
||||
f1.merge(f2);
|
||||
assert_eq!(f1.x, Some(1));
|
||||
assert_eq!(f1.y, Some("merged".to_string()));
|
||||
|
||||
// Finally convert into the real struct
|
||||
let s = Simple::from_file(Some(f1));
|
||||
assert_eq!(
|
||||
s,
|
||||
Simple {
|
||||
x: 1,
|
||||
y: "merged".to_string()
|
||||
}
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user