From e8e17647cb545b49d783a39dce2ee27706f2d3fc Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 4 Jan 2024 17:00:49 +0100 Subject: [PATCH] Finish testing --- micronfig_macros/src/lib.rs | 25 ++++----- .../tests/sources/example_angybot.rs | 53 +++++++++++++++++++ .../sources/example_distributedarcade.rs | 17 ++++++ .../tests/sources/example_patchedporobot.rs | 14 ++--- .../tests/sources/from_single_custom.rs | 4 +- .../tests/sources/parse_single_custom.rs | 4 +- .../tests/sources/parse_single_pathbuf.rs | 2 +- .../sources/parse_single_u64_optional.rs | 8 +++ .../tests/sources/string_multi_explicit.rs | 14 +++++ .../tests/sources/tryfrom_single_custom.rs | 4 +- .../wrong_conversion_longfatarrow.stderr | 6 +-- .../wrong_conversion_longthinarrow.stderr | 6 +-- .../wrong_conversion_tildearrow.stderr | 6 +-- .../tests/sources/wrong_start.stderr | 8 +-- .../tests/sources/wrong_syntax_colon.stderr | 6 +-- .../tests/sources/wrong_syntax_type.stderr | 6 +-- micronfig_macros/tests/tests.rs | 3 +- 17 files changed, 141 insertions(+), 45 deletions(-) create mode 100644 micronfig_macros/tests/sources/example_angybot.rs create mode 100644 micronfig_macros/tests/sources/example_distributedarcade.rs create mode 100644 micronfig_macros/tests/sources/parse_single_u64_optional.rs create mode 100644 micronfig_macros/tests/sources/string_multi_explicit.rs diff --git a/micronfig_macros/src/lib.rs b/micronfig_macros/src/lib.rs index fb2b9d6..3448e7a 100644 --- a/micronfig_macros/src/lib.rs +++ b/micronfig_macros/src/lib.rs @@ -29,8 +29,6 @@ enum Conversion { } -const VALID_INITIAL_TYPES: [&'static str; 2] = ["String", "std::string::String"]; - impl Parse for ConfigItem { fn parse(input: ParseStream) -> syn::Result { let identifier = input.parse::()?; @@ -47,8 +45,13 @@ impl Parse for ConfigItem { .expect("this token to be parsed correctly, as it has been previously peeked"); let string_type = input.parse::()?; - if !VALID_INITIAL_TYPES.contains(&&*string_type.to_token_stream().to_string()) { - return Err(syn::Error::new_spanned(string_type, "first type of a conversion chain should always be `String` or `std::string::String`, type aliases are not allowed")) + if &*string_type.to_token_stream().to_string() != "String" { + return Err( + syn::Error::new_spanned( + string_type, + "first type of a conversion chain should always be literally `String`, other aliases are not allowed" + ) + ); } let mut types = Vec::new(); @@ -130,19 +133,17 @@ pub fn config(input: TokenStream) -> TokenStream { match (conversion, item.optional) { (Conversion::From, true) => quote! { let value: Option<#typ> = value - .map(std::convert::Into::into); + .map(|v| v.into()); }, (Conversion::TryFrom, true) => quote! { let value: Option<#typ> = value - .map(std::convert::TryInto::try_into) - .map(|v| v.expect(&format!("to be able to convert {}", #identifier_string)) - ); + .map(|v| v.try_into()) + .map(|v| v.expect(&format!("to be able to convert {}", #identifier_string))); }, (Conversion::FromStr, true) => quote! { let value: Option<#typ> = value - .map(str::parse) - .map(|v| v.expect(&format!("to be able to parse {}", #identifier_string)) - ); + .map(|v| v.parse()) + .map(|v| v.expect(&format!("to be able to parse {}", #identifier_string))); }, (Conversion::From, false) => quote! { let value: #typ = value @@ -182,7 +183,7 @@ pub fn config(input: TokenStream) -> TokenStream { pub(crate) fn #identifier() -> &'static #type_final_option { #identifier::_lock.get_or_init(|| { let key: std::ffi::OsString = #identifier_string.into(); - let value: Option = _cache().get(&key); + let value: Option = _cache().get(&key); #require_code #conversion_code diff --git a/micronfig_macros/tests/sources/example_angybot.rs b/micronfig_macros/tests/sources/example_angybot.rs new file mode 100644 index 0000000..fcd4148 --- /dev/null +++ b/micronfig_macros/tests/sources/example_angybot.rs @@ -0,0 +1,53 @@ +use std::env; + +#[derive(Debug, PartialEq, Eq)] +struct GuildId(u64); + +#[derive(Debug, PartialEq, Eq)] +struct UserId(u64); + +impl From for GuildId { + fn from(value: u64) -> Self { + Self(value) + } +} + +impl From for UserId { + fn from(value: u64) -> Self { + Self(value) + } +} + +micronfig::config! { + ANGY_TOKEN: String, + ANGY_APPID: String > u64, + ANGY_PLEX_SERVER: String, + ANGY_PLEX_TOKEN: String, + ANGY_PLEX_LIBRARY: String, + ANGY_PLEX_REPLACE_FROM: String, + ANGY_PLEX_REPLACE_TO: String, + ANGY_DEV_GUILD_ID?: String > u64 -> crate::GuildId, + ANGY_DEV_USER_ID?: String > u64 -> crate::UserId, +} + +fn main() { + env::set_var("ANGY_TOKEN", "abcdef"); + env::set_var("ANGY_APPID", "1234"); + env::set_var("ANGY_PLEX_SERVER", "example.org"); + env::set_var("ANGY_PLEX_TOKEN", "123456dada"); + env::set_var("ANGY_PLEX_LIBRARY", "beta"); + env::set_var("ANGY_PLEX_REPLACE_FROM", "sus"); + env::set_var("ANGY_PLEX_REPLACE_TO", "sos"); + env::set_var("ANGY_DEV_GUILD_ID", "4567"); + env::set_var("ANGY_DEV_USER_ID", "5678"); + + assert_eq!(ANGY_TOKEN(), "abcdef"); + assert_eq!(ANGY_APPID(), &1234); + assert_eq!(ANGY_PLEX_SERVER(), "example.org"); + assert_eq!(ANGY_PLEX_TOKEN(), "123456dada"); + assert_eq!(ANGY_PLEX_LIBRARY(), "beta"); + assert_eq!(ANGY_PLEX_REPLACE_FROM(), "sus"); + assert_eq!(ANGY_PLEX_REPLACE_TO(), "sos"); + assert_eq!(ANGY_DEV_GUILD_ID(), &Some(GuildId(4567))); + assert_eq!(ANGY_DEV_USER_ID(), &Some(UserId(5678))); +} diff --git a/micronfig_macros/tests/sources/example_distributedarcade.rs b/micronfig_macros/tests/sources/example_distributedarcade.rs new file mode 100644 index 0000000..bba66be --- /dev/null +++ b/micronfig_macros/tests/sources/example_distributedarcade.rs @@ -0,0 +1,17 @@ +use std::net::{SocketAddr, SocketAddrV4, Ipv4Addr}; + +micronfig::config! { + REDIS_CONN: String, + AXUM_HOST: String > std::net::SocketAddr, + CREATE_TOKEN: String, +} + +fn main() { + std::env::set_var("REDIS_CONN", "redis://garas"); + std::env::set_var("AXUM_HOST", "127.0.0.1:12345"); + std::env::set_var("CREATE_TOKEN", "tokennnnn"); + + assert_eq!(REDIS_CONN(), "redis://garas"); + assert_eq!(AXUM_HOST(), &SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 12345)), "127.0.0.1:12345"); + assert_eq!(CREATE_TOKEN(), "tokennnnn"); +} diff --git a/micronfig_macros/tests/sources/example_patchedporobot.rs b/micronfig_macros/tests/sources/example_patchedporobot.rs index 43399b8..f624c52 100644 --- a/micronfig_macros/tests/sources/example_patchedporobot.rs +++ b/micronfig_macros/tests/sources/example_patchedporobot.rs @@ -1,11 +1,13 @@ use std::str::FromStr; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub struct CommaSeparatedStrings(Vec); impl FromStr for CommaSeparatedStrings { + type Err = (); + fn from_str(s: &str) -> Result { - Self(s.split(',').collect()) + Ok(Self(s.split(',').map(|v| v.to_string()).collect())) } } @@ -25,8 +27,8 @@ fn main() { std::env::remove_var("SERENITY_DEV_GUILD_ID"); assert_eq!(DATA_DRAGON_LOCALE(), "it_IT"); - assert_eq!(DATA_DRAGON_SET_CODES(), CommaSeparatedStrings(vec!["set1", "set2abc"])); - assert_eq!(POROXY_KEY, "abcdef"); - assert_eq!(POROXY_SALT, "abcdef"); - assert_eq!(SERENITY_DEV_GUILD_ID, &None); + assert_eq!(DATA_DRAGON_SET_CODES(), &CommaSeparatedStrings(vec!["set1".to_string(), "set2abc".to_string()])); + assert_eq!(POROXY_KEY(), "abcdef"); + assert_eq!(POROXY_SALT(), "abcdef"); + assert_eq!(SERENITY_DEV_GUILD_ID(), &None); } diff --git a/micronfig_macros/tests/sources/from_single_custom.rs b/micronfig_macros/tests/sources/from_single_custom.rs index a041260..e2a7efa 100644 --- a/micronfig_macros/tests/sources/from_single_custom.rs +++ b/micronfig_macros/tests/sources/from_single_custom.rs @@ -1,4 +1,4 @@ -#[derive(std::fmt::Debug, Eq)] +#[derive(Debug, PartialEq, Eq)] struct MyCustomStruct(String); impl From for MyCustomStruct { @@ -13,5 +13,5 @@ micronfig::config! { fn main() { std::env::set_var("GARASAUTO", "baba"); - assert_eq!(GARASAUTO(), &MyCustomStruct("baba")); + assert_eq!(GARASAUTO(), &MyCustomStruct("baba".to_string())); } diff --git a/micronfig_macros/tests/sources/parse_single_custom.rs b/micronfig_macros/tests/sources/parse_single_custom.rs index ec93fc0..0e6ac05 100644 --- a/micronfig_macros/tests/sources/parse_single_custom.rs +++ b/micronfig_macros/tests/sources/parse_single_custom.rs @@ -1,4 +1,4 @@ -#[derive(std::fmt::Debug, Eq)] +#[derive(Debug, PartialEq, Eq)] struct MyCustomStruct(String); impl std::str::FromStr for MyCustomStruct { @@ -15,5 +15,5 @@ micronfig::config! { fn main() { std::env::set_var("GARASAUTO", "keke"); - assert_eq!(GARASAUTO(), &MyCustomStruct("keke")); + assert_eq!(GARASAUTO(), &MyCustomStruct("keke".to_string())); } diff --git a/micronfig_macros/tests/sources/parse_single_pathbuf.rs b/micronfig_macros/tests/sources/parse_single_pathbuf.rs index 8b32658..b102b21 100644 --- a/micronfig_macros/tests/sources/parse_single_pathbuf.rs +++ b/micronfig_macros/tests/sources/parse_single_pathbuf.rs @@ -4,5 +4,5 @@ micronfig::config! { fn main() { std::env::set_var("GARASAUTO", "./garas"); - assert_eq!(GARASAUTO(), std::path::PathBuf::from("./garas")); + assert_eq!(GARASAUTO(), &std::path::PathBuf::from("./garas")); } diff --git a/micronfig_macros/tests/sources/parse_single_u64_optional.rs b/micronfig_macros/tests/sources/parse_single_u64_optional.rs new file mode 100644 index 0000000..235eb8b --- /dev/null +++ b/micronfig_macros/tests/sources/parse_single_u64_optional.rs @@ -0,0 +1,8 @@ +micronfig::config! { + GARASAUTO?: String > u64, +} + +fn main() { + std::env::remove_var("GARASAUTO"); + assert_eq!(GARASAUTO(), &None); +} diff --git a/micronfig_macros/tests/sources/string_multi_explicit.rs b/micronfig_macros/tests/sources/string_multi_explicit.rs new file mode 100644 index 0000000..aa322a7 --- /dev/null +++ b/micronfig_macros/tests/sources/string_multi_explicit.rs @@ -0,0 +1,14 @@ +micronfig::config! { + GARAS: String, + AUTO: String, + BUS: String, +} + +fn main() { + std::env::set_var("GARAS", "garas"); + std::env::set_var("AUTO", "auto"); + std::env::set_var("BUS", "bus"); + assert_eq!(GARAS(), "garas"); + assert_eq!(AUTO(), "auto"); + assert_eq!(BUS(), "bus"); +} diff --git a/micronfig_macros/tests/sources/tryfrom_single_custom.rs b/micronfig_macros/tests/sources/tryfrom_single_custom.rs index 19c52d2..04e87d8 100644 --- a/micronfig_macros/tests/sources/tryfrom_single_custom.rs +++ b/micronfig_macros/tests/sources/tryfrom_single_custom.rs @@ -1,4 +1,4 @@ -#[derive(std::fmt::Debug, Eq)] +#[derive(Debug, PartialEq, Eq)] struct MyCustomStruct(String); impl std::convert::TryFrom for MyCustomStruct { @@ -15,5 +15,5 @@ micronfig::config! { fn main() { std::env::set_var("GARASAUTO", "me"); - assert_eq!(GARASAUTO(), &MyCustomStruct("me")); + assert_eq!(GARASAUTO(), &MyCustomStruct("me".to_string())); } diff --git a/micronfig_macros/tests/sources/wrong_conversion_longfatarrow.stderr b/micronfig_macros/tests/sources/wrong_conversion_longfatarrow.stderr index 5e062cb..3394f2d 100644 --- a/micronfig_macros/tests/sources/wrong_conversion_longfatarrow.stderr +++ b/micronfig_macros/tests/sources/wrong_conversion_longfatarrow.stderr @@ -5,7 +5,7 @@ error: expected `,` | ^ error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope - --> tests/sources/wrong_conversion_longfatarrow.rs:7:19 + --> tests/sources/wrong_conversion_longfatarrow.rs:7:20 | -7 | println!("{:?}", GARASAUTO()) - | ^^^^^^^^^ not found in this scope +7 | println!("{:#?}", GARASAUTO()); + | ^^^^^^^^^ not found in this scope diff --git a/micronfig_macros/tests/sources/wrong_conversion_longthinarrow.stderr b/micronfig_macros/tests/sources/wrong_conversion_longthinarrow.stderr index e9c64f7..6e4043b 100644 --- a/micronfig_macros/tests/sources/wrong_conversion_longthinarrow.stderr +++ b/micronfig_macros/tests/sources/wrong_conversion_longthinarrow.stderr @@ -5,7 +5,7 @@ error: expected `,` | ^ error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope - --> tests/sources/wrong_conversion_longthinarrow.rs:7:19 + --> tests/sources/wrong_conversion_longthinarrow.rs:7:20 | -7 | println!("{:?}", GARASAUTO()) - | ^^^^^^^^^ not found in this scope +7 | println!("{:#?}", GARASAUTO()); + | ^^^^^^^^^ not found in this scope diff --git a/micronfig_macros/tests/sources/wrong_conversion_tildearrow.stderr b/micronfig_macros/tests/sources/wrong_conversion_tildearrow.stderr index 3ee5824..fe9f6d8 100644 --- a/micronfig_macros/tests/sources/wrong_conversion_tildearrow.stderr +++ b/micronfig_macros/tests/sources/wrong_conversion_tildearrow.stderr @@ -5,7 +5,7 @@ error: expected `,` | ^ error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope - --> tests/sources/wrong_conversion_tildearrow.rs:7:19 + --> tests/sources/wrong_conversion_tildearrow.rs:7:20 | -7 | println!("{:?}", GARASAUTO()) - | ^^^^^^^^^ not found in this scope +7 | println!("{:#?}", GARASAUTO()); + | ^^^^^^^^^ not found in this scope diff --git a/micronfig_macros/tests/sources/wrong_start.stderr b/micronfig_macros/tests/sources/wrong_start.stderr index 824193c..c386c51 100644 --- a/micronfig_macros/tests/sources/wrong_start.stderr +++ b/micronfig_macros/tests/sources/wrong_start.stderr @@ -1,11 +1,11 @@ -error: first type of a conversion chain should always be `String` or `std::string::String`, type aliases are not allowed +error: first type of a conversion chain should always be literally `String`, other aliases are not allowed --> tests/sources/wrong_start.rs:2:13 | 2 | GARASAUTO: i64, | ^^^ error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope - --> tests/sources/wrong_start.rs:7:19 + --> tests/sources/wrong_start.rs:7:20 | -7 | println!("{:?}", GARASAUTO()) - | ^^^^^^^^^ not found in this scope +7 | println!("{:#?}", GARASAUTO()); + | ^^^^^^^^^ not found in this scope diff --git a/micronfig_macros/tests/sources/wrong_syntax_colon.stderr b/micronfig_macros/tests/sources/wrong_syntax_colon.stderr index a701b6f..a462e5b 100644 --- a/micronfig_macros/tests/sources/wrong_syntax_colon.stderr +++ b/micronfig_macros/tests/sources/wrong_syntax_colon.stderr @@ -5,7 +5,7 @@ error: expected identifier | ^ error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope - --> tests/sources/wrong_syntax_colon.rs:7:19 + --> tests/sources/wrong_syntax_colon.rs:7:20 | -7 | println!("{:?}", GARASAUTO()) - | ^^^^^^^^^ not found in this scope +7 | println!("{:#?}", GARASAUTO()); + | ^^^^^^^^^ not found in this scope diff --git a/micronfig_macros/tests/sources/wrong_syntax_type.stderr b/micronfig_macros/tests/sources/wrong_syntax_type.stderr index 76a981b..4d2c0c3 100644 --- a/micronfig_macros/tests/sources/wrong_syntax_type.stderr +++ b/micronfig_macros/tests/sources/wrong_syntax_type.stderr @@ -5,7 +5,7 @@ error: expected `,` | ^^^^^^ error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope - --> tests/sources/wrong_syntax_type.rs:7:19 + --> tests/sources/wrong_syntax_type.rs:7:20 | -7 | println!("{:?}", GARASAUTO()) - | ^^^^^^^^^ not found in this scope +7 | println!("{:#?}", GARASAUTO()); + | ^^^^^^^^^ not found in this scope diff --git a/micronfig_macros/tests/tests.rs b/micronfig_macros/tests/tests.rs index 57dda18..77bfcf3 100644 --- a/micronfig_macros/tests/tests.rs +++ b/micronfig_macros/tests/tests.rs @@ -19,6 +19,8 @@ macro_rules! fail { pass!(chain_single_down); pass!(chain_single_up); pass!(empty); +pass!(example_angybot); +pass!(example_distributedarcade); pass!(example_patchedporobot); pass!(from_single_custom); pass!(parse_single_custom); @@ -30,7 +32,6 @@ pass!(string_multi_explicit); pass!(string_multi_implicit); pass!(string_multi_mixed); pass!(string_single_explicit); -pass!(string_single_expliciter); pass!(string_single_implicit); pass!(tryfrom_single_custom);