1
Fork 0
mirror of https://github.com/Steffo99/micronfig.git synced 2024-11-22 08:04:20 +00:00

More tests

This commit is contained in:
Steffo 2024-01-03 04:04:57 +01:00
parent b0efe070ad
commit 3824bd2b0f
Signed by: steffo
GPG key ID: 2A24051445686895
38 changed files with 379 additions and 22 deletions

View file

@ -1,6 +1,6 @@
extern crate proc_macro; extern crate proc_macro;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::quote; use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream}; use syn::parse::{Parse, ParseStream};
use syn::{Ident, parse_macro_input, Token, Type}; use syn::{Ident, parse_macro_input, Token, Type};
use syn::punctuated::Punctuated; use syn::punctuated::Punctuated;
@ -34,7 +34,10 @@ impl Parse for ConfigItem {
if input.lookahead1().peek(Token![:]) { if input.lookahead1().peek(Token![:]) {
input.parse::<Token![:]>()?; input.parse::<Token![:]>()?;
input.parse::<Type>()?; let string_type = input.parse::<Type>()?;
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 `String`"))
}
let mut types = vec![]; let mut types = vec![];
while let Ok(typ) = input.parse::<ConfigPair>() { while let Ok(typ) = input.parse::<ConfigPair>() {
@ -47,7 +50,6 @@ impl Parse for ConfigItem {
let types = vec![]; let types = vec![];
Ok(Self { identifier, types }) Ok(Self { identifier, types })
} }
} }
} }
@ -72,7 +74,7 @@ impl Parse for Conversion {
Ok(Conversion::FromStr) Ok(Conversion::FromStr)
} }
else { else {
Err(input.error("Cannot determine conversion method to use; valid conversion tokens are `->` (From), `=>` (TryFrom) and `>` (FromStr).")) Err(input.error("cannot determine conversion method to use; valid conversion tokens are `->` (From), `=>` (TryFrom) and `>` (FromStr)."))
} }
} }
} }
@ -84,12 +86,12 @@ pub fn config(input: TokenStream) -> TokenStream {
let cache_code = quote! { let cache_code = quote! {
#[allow(non_snake_case)] #[allow(non_snake_case)]
mod _cache { mod _cache {
pub static lock: std::sync::OnceLock<micronfig::cache::Cache> = std::sync::OnceLock::new(); pub static _lock: std::sync::OnceLock<micronfig::cache::Cache> = std::sync::OnceLock::new();
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn _cache() -> &'static micronfig::cache::Cache { fn _cache() -> &'static micronfig::cache::Cache {
_cache::lock.get_or_init(micronfig::cache::Cache::new) _cache::_lock.get_or_init(micronfig::cache::Cache::new)
} }
}; };
@ -135,12 +137,12 @@ pub fn config(input: TokenStream) -> TokenStream {
quote! { quote! {
#[allow(non_snake_case)] #[allow(non_snake_case)]
mod #identifier { mod #identifier {
pub(super) static lock: std::sync::OnceLock<Option<#last_type>> = std::sync::OnceLock::new(); pub(super) static _lock: std::sync::OnceLock<Option<#last_type>> = std::sync::OnceLock::new();
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub(crate) fn #identifier() -> &'static Option<#last_type> { pub(crate) fn #identifier() -> &'static Option<#last_type> {
#identifier::lock.get_or_init(|| { #identifier::_lock.get_or_init(|| {
let key: std::ffi::OsString = #identifier_string.into(); let key: std::ffi::OsString = #identifier_string.into();
let value: Option<String> = _cache().get(&key); let value: Option<String> = _cache().get(&key);

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String > u128 => u64 => u32 => u16 => u8,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String > u8 -> u16 -> u32 -> u64 -> u128,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,16 @@
#[derive(std::fmt::Debug)]
struct MyCustomStruct(String);
impl From<String> for MyCustomStruct {
fn from(value: String) -> Self {
Self(value)
}
}
micronfig::config! {
GARASAUTO: String -> crate::MyCustomStruct,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,18 @@
#[derive(std::fmt::Debug)]
struct MyCustomStruct(String);
impl std::str::FromStr for MyCustomStruct {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_owned()))
}
}
micronfig::config! {
GARASAUTO: String > crate::MyCustomStruct,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -1,7 +0,0 @@
micronfig::config! {
MY_IP_ADDR: String > IpAddr,
}
fn main() {
println!("{:?}", MY_IP_ADDR())
}

View file

@ -0,0 +1,18 @@
#[derive(std::fmt::Debug)]
struct MyCustomStruct(String);
impl std::convert::TryFrom<String> for MyCustomStruct {
type Error = ();
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(Self(value))
}
}
micronfig::config! {
GARASAUTO: String => crate::MyCustomStruct,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String ==> u64,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,11 @@
error: expected `,`
--> tests/sources/wrong_conversion_longfatarrow.rs:2:20
|
2 | GARASAUTO: String ==> u64,
| ^
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
--> tests/sources/wrong_conversion_longfatarrow.rs:6:19
|
6 | println!("{:?}", GARASAUTO())
| ^^^^^^^^^ not found in this scope

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String --> u64,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,11 @@
error: expected `,`
--> tests/sources/wrong_conversion_longthinarrow.rs:2:20
|
2 | GARASAUTO: String --> u64,
| ^
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
--> tests/sources/wrong_conversion_longthinarrow.rs:6:19
|
6 | println!("{:?}", GARASAUTO())
| ^^^^^^^^^ not found in this scope

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String ~> u64,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,11 @@
error: expected `,`
--> tests/sources/wrong_conversion_tildearrow.rs:2:20
|
2 | GARASAUTO: String ~> u64,
| ^
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
--> tests/sources/wrong_conversion_tildearrow.rs:6:19
|
6 | println!("{:?}", GARASAUTO())
| ^^^^^^^^^ not found in this scope

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String -> u64,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,24 @@
error[E0277]: the trait bound `u64: From<String>` is not satisfied
--> tests/sources/wrong_conversion_trait_from.rs:1:1
|
1 | / micronfig::config! {
2 | | GARASAUTO: String -> u64,
3 | | }
| | ^
| | |
| |_the trait `From<String>` is not implemented for `u64`
| in this macro invocation
|
::: src/lib.rs
|
| pub fn config(input: TokenStream) -> TokenStream {
| ------------------------------------------------ in this expansion of `micronfig::config!`
|
= help: the following other types implement trait `From<T>`:
<u64 as From<bool>>
<u64 as From<char>>
<u64 as From<u8>>
<u64 as From<u16>>
<u64 as From<u32>>
<u64 as From<NonZeroU64>>
= note: required for `String` to implement `Into<u64>`

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String > std::convert::Infallible,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,28 @@
error[E0277]: the trait bound `Infallible: FromStr` is not satisfied
--> tests/sources/wrong_conversion_trait_fromstr.rs:1:1
|
1 | / micronfig::config! {
2 | | GARASAUTO: String > std::convert::Infallible,
3 | | }
| | ^
| | |
| |_the trait `FromStr` is not implemented for `Infallible`
| in this macro invocation
|
::: src/lib.rs
|
| pub fn config(input: TokenStream) -> TokenStream {
| ------------------------------------------------ in this expansion of `micronfig::config!`
|
= help: the following other types implement trait `FromStr`:
bool
char
isize
i8
i16
i32
i64
i128
and $N others
note: required by a bound in `core::str::<impl str>::parse`
--> $RUST/core/src/str/mod.rs

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String => u64,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,26 @@
error[E0277]: the trait bound `u64: From<String>` is not satisfied
--> tests/sources/wrong_conversion_trait_tryfrom.rs:1:1
|
1 | / micronfig::config! {
2 | | GARASAUTO: String => u64,
3 | | }
| | ^
| | |
| |_the trait `From<String>` is not implemented for `u64`
| in this macro invocation
|
::: src/lib.rs
|
| pub fn config(input: TokenStream) -> TokenStream {
| ------------------------------------------------ in this expansion of `micronfig::config!`
|
= help: the following other types implement trait `From<T>`:
<u64 as From<bool>>
<u64 as From<char>>
<u64 as From<u8>>
<u64 as From<u16>>
<u64 as From<u32>>
<u64 as From<NonZeroU64>>
= note: required for `String` to implement `Into<u64>`
= note: required for `u64` to implement `TryFrom<String>`
= note: required for `String` to implement `TryInto<u64>`

View file

@ -0,0 +1,7 @@
micronfig::config! {
garasauto!"68"3469l
}
fn main() {
}

View file

@ -0,0 +1,5 @@
error: expected `,`
--> tests/sources/wrong_nonsense_1.rs:2:11
|
2 | garasauto!"68"3469l
| ^

View file

@ -0,0 +1,7 @@
micronfig::config! {
: ->
}
fn main() {
}

View file

@ -0,0 +1,5 @@
error: expected identifier
--> tests/sources/wrong_nonsense_2.rs:2:2
|
2 | : ->
| ^

View file

@ -0,0 +1,7 @@
micronfig::config! {
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
}
fn main() {
}

View file

@ -0,0 +1,5 @@
error: expected identifier
--> tests/sources/wrong_nonsense_3.rs:2:2
|
2 | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
| ^

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: i64,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,11 @@
error: first type of a conversion chain should always be `String`
--> 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:6:19
|
6 | println!("{:?}", GARASAUTO())
| ^^^^^^^^^ not found in this scope

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: ,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,11 @@
error: expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, `dyn`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
--> tests/sources/wrong_syntax_colon.rs:2:13
|
2 | GARASAUTO: ,
| ^
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
--> tests/sources/wrong_syntax_colon.rs:6:19
|
6 | println!("{:?}", GARASAUTO())
| ^^^^^^^^^ not found in this scope

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO String,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,11 @@
error: expected `,`
--> tests/sources/wrong_syntax_type.rs:2:12
|
2 | GARASAUTO String,
| ^^^^^^
error[E0425]: cannot find function, tuple struct or tuple variant `GARASAUTO` in this scope
--> tests/sources/wrong_syntax_type.rs:6:19
|
6 | println!("{:?}", GARASAUTO())
| ^^^^^^^^^ not found in this scope

View file

@ -0,0 +1,9 @@
error[E0412]: cannot find type `PathBuf` in this scope
--> tests/sources/wrong_unqualified_import.rs:4:22
|
4 | GARASAUTO: String > PathBuf,
| ^^^^^^^ not found in this scope
|
= help: consider importing one of these items:
crate::PathBuf
std::path::PathBuf

View file

@ -0,0 +1,7 @@
micronfig::config! {
GARASAUTO: String > PathBuf,
}
fn main() {
println!("{:?}", GARASAUTO())
}

View file

@ -0,0 +1,19 @@
error[E0412]: cannot find type `PathBuf` in this scope
--> tests/sources/wrong_unqualified_noimport.rs:2:22
|
2 | GARASAUTO: String > PathBuf,
| ^^^^^^^ not found in this scope
|
= help: consider importing this struct:
std::path::PathBuf
error[E0412]: cannot find type `PathBuf` in this scope
--> tests/sources/wrong_unqualified_noimport.rs:2:22
|
2 | GARASAUTO: String > PathBuf,
| ^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 + use std::path::PathBuf;
|

View file

@ -16,14 +16,32 @@ macro_rules! fail {
} }
} }
pass!(chain_single_down);
pass!(chain_single_up);
pass!(empty); pass!(empty);
pass!(string_single_explicit); pass!(from_single_custom);
pass!(string_single_implicit); pass!(parse_single_custom);
pass!(parse_single_i64);
pass!(parse_single_pathbuf);
pass!(parse_single_u64);
pass!(string_multi_explicit); pass!(string_multi_explicit);
pass!(string_multi_implicit); pass!(string_multi_implicit);
pass!(string_multi_mixed); pass!(string_multi_mixed);
pass!(parse_single_u64); pass!(string_single_explicit);
pass!(parse_single_i64); pass!(string_single_implicit);
pass!(parse_single_ipaddr); pass!(tryfrom_single_custom);
pass!(parse_single_pathbuf_full);
pass!(parse_single_pathbuf_use); fail!(wrong_conversion_longfatarrow);
fail!(wrong_conversion_longthinarrow);
fail!(wrong_conversion_tildearrow);
fail!(wrong_conversion_trait_from);
fail!(wrong_conversion_trait_fromstr);
fail!(wrong_conversion_trait_tryfrom);
fail!(wrong_nonsense_1);
fail!(wrong_nonsense_2);
fail!(wrong_nonsense_3);
fail!(wrong_start);
fail!(wrong_syntax_colon);
fail!(wrong_syntax_type);
fail!(wrong_unqualified_import);
fail!(wrong_unqualified_noimport);