mirror of
https://github.com/Steffo99/micronfig.git
synced 2024-11-22 08:04:20 +00:00
More tests
This commit is contained in:
parent
b0efe070ad
commit
3824bd2b0f
38 changed files with 379 additions and 22 deletions
|
@ -1,6 +1,6 @@
|
|||
extern crate proc_macro;
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::parse::{Parse, ParseStream};
|
||||
use syn::{Ident, parse_macro_input, Token, Type};
|
||||
use syn::punctuated::Punctuated;
|
||||
|
@ -34,7 +34,10 @@ impl Parse for ConfigItem {
|
|||
|
||||
if input.lookahead1().peek(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![];
|
||||
while let Ok(typ) = input.parse::<ConfigPair>() {
|
||||
|
@ -47,7 +50,6 @@ impl Parse for ConfigItem {
|
|||
let types = vec![];
|
||||
Ok(Self { identifier, types })
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,7 +74,7 @@ impl Parse for Conversion {
|
|||
Ok(Conversion::FromStr)
|
||||
}
|
||||
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! {
|
||||
#[allow(non_snake_case)]
|
||||
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)]
|
||||
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! {
|
||||
#[allow(non_snake_case)]
|
||||
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)]
|
||||
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 value: Option<String> = _cache().get(&key);
|
||||
|
||||
|
|
7
micronfig_macros/tests/sources/chain_single_down.rs
Normal file
7
micronfig_macros/tests/sources/chain_single_down.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String > u128 => u64 => u32 => u16 => u8,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
7
micronfig_macros/tests/sources/chain_single_up.rs
Normal file
7
micronfig_macros/tests/sources/chain_single_up.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String > u8 -> u16 -> u32 -> u64 -> u128,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
0
micronfig_macros/tests/sources/example_patchedporobot.rs
Normal file
0
micronfig_macros/tests/sources/example_patchedporobot.rs
Normal file
16
micronfig_macros/tests/sources/from_single_custom.rs
Normal file
16
micronfig_macros/tests/sources/from_single_custom.rs
Normal 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())
|
||||
}
|
18
micronfig_macros/tests/sources/parse_single_custom.rs
Normal file
18
micronfig_macros/tests/sources/parse_single_custom.rs
Normal 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())
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
micronfig::config! {
|
||||
MY_IP_ADDR: String > IpAddr,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", MY_IP_ADDR())
|
||||
}
|
18
micronfig_macros/tests/sources/tryfrom_single_custom.rs
Normal file
18
micronfig_macros/tests/sources/tryfrom_single_custom.rs
Normal 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())
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String ==> u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String --> u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String ~> u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String -> u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
|
@ -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>`
|
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String > std::convert::Infallible,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String => u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
|
@ -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>`
|
7
micronfig_macros/tests/sources/wrong_nonsense_1.rs
Normal file
7
micronfig_macros/tests/sources/wrong_nonsense_1.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
garasauto!"68"3469l
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
5
micronfig_macros/tests/sources/wrong_nonsense_1.stderr
Normal file
5
micronfig_macros/tests/sources/wrong_nonsense_1.stderr
Normal file
|
@ -0,0 +1,5 @@
|
|||
error: expected `,`
|
||||
--> tests/sources/wrong_nonsense_1.rs:2:11
|
||||
|
|
||||
2 | garasauto!"68"3469l
|
||||
| ^
|
7
micronfig_macros/tests/sources/wrong_nonsense_2.rs
Normal file
7
micronfig_macros/tests/sources/wrong_nonsense_2.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
: ->
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
5
micronfig_macros/tests/sources/wrong_nonsense_2.stderr
Normal file
5
micronfig_macros/tests/sources/wrong_nonsense_2.stderr
Normal file
|
@ -0,0 +1,5 @@
|
|||
error: expected identifier
|
||||
--> tests/sources/wrong_nonsense_2.rs:2:2
|
||||
|
|
||||
2 | : ->
|
||||
| ^
|
7
micronfig_macros/tests/sources/wrong_nonsense_3.rs
Normal file
7
micronfig_macros/tests/sources/wrong_nonsense_3.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
5
micronfig_macros/tests/sources/wrong_nonsense_3.stderr
Normal file
5
micronfig_macros/tests/sources/wrong_nonsense_3.stderr
Normal file
|
@ -0,0 +1,5 @@
|
|||
error: expected identifier
|
||||
--> tests/sources/wrong_nonsense_3.rs:2:2
|
||||
|
|
||||
2 | ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
|
||||
| ^
|
7
micronfig_macros/tests/sources/wrong_start.rs
Normal file
7
micronfig_macros/tests/sources/wrong_start.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: i64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
11
micronfig_macros/tests/sources/wrong_start.stderr
Normal file
11
micronfig_macros/tests/sources/wrong_start.stderr
Normal 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
|
7
micronfig_macros/tests/sources/wrong_syntax_colon.rs
Normal file
7
micronfig_macros/tests/sources/wrong_syntax_colon.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: ,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
11
micronfig_macros/tests/sources/wrong_syntax_colon.stderr
Normal file
11
micronfig_macros/tests/sources/wrong_syntax_colon.stderr
Normal 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
|
7
micronfig_macros/tests/sources/wrong_syntax_type.rs
Normal file
7
micronfig_macros/tests/sources/wrong_syntax_type.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
11
micronfig_macros/tests/sources/wrong_syntax_type.stderr
Normal file
11
micronfig_macros/tests/sources/wrong_syntax_type.stderr
Normal 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
|
|
@ -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
|
|
@ -0,0 +1,7 @@
|
|||
micronfig::config! {
|
||||
GARASAUTO: String > PathBuf,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{:?}", GARASAUTO())
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -16,14 +16,32 @@ macro_rules! fail {
|
|||
}
|
||||
}
|
||||
|
||||
pass!(chain_single_down);
|
||||
pass!(chain_single_up);
|
||||
pass!(empty);
|
||||
pass!(string_single_explicit);
|
||||
pass!(string_single_implicit);
|
||||
pass!(from_single_custom);
|
||||
pass!(parse_single_custom);
|
||||
pass!(parse_single_i64);
|
||||
pass!(parse_single_pathbuf);
|
||||
pass!(parse_single_u64);
|
||||
pass!(string_multi_explicit);
|
||||
pass!(string_multi_implicit);
|
||||
pass!(string_multi_mixed);
|
||||
pass!(parse_single_u64);
|
||||
pass!(parse_single_i64);
|
||||
pass!(parse_single_ipaddr);
|
||||
pass!(parse_single_pathbuf_full);
|
||||
pass!(parse_single_pathbuf_use);
|
||||
pass!(string_single_explicit);
|
||||
pass!(string_single_implicit);
|
||||
pass!(tryfrom_single_custom);
|
||||
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue