aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml3
-rw-r--r--macros/Cargo.toml1
-rw-r--r--macros/src/libs/intertrait_macros/gen_caster.rs57
-rw-r--r--test_util_macros/Cargo.toml11
-rw-r--r--test_util_macros/src/lib.rs47
5 files changed, 113 insertions, 6 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 088bc01..9bf4a0a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -60,6 +60,7 @@ tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread", "time"] }
[workspace]
members = [
"macros",
- "examples/with-3rd-party/third-party-lib"
+ "test_util_macros",
+ "examples/with-3rd-party/third-party-lib",
]
diff --git a/macros/Cargo.toml b/macros/Cargo.toml
index a2ed87b..2434028 100644
--- a/macros/Cargo.toml
+++ b/macros/Cargo.toml
@@ -33,3 +33,4 @@ syrette = { version = "0.4.1", path = ".." }
mockall = "0.11.1"
pretty_assertions = "1.3.0"
syn = { version = "1.0.96", features = ["full", "extra-traits"] }
+test_util_macros = { path = "../test_util_macros" }
diff --git a/macros/src/libs/intertrait_macros/gen_caster.rs b/macros/src/libs/intertrait_macros/gen_caster.rs
index a76bb52..c2fa226 100644
--- a/macros/src/libs/intertrait_macros/gen_caster.rs
+++ b/macros/src/libs/intertrait_macros/gen_caster.rs
@@ -28,7 +28,7 @@ pub fn generate_caster(
sync: bool,
) -> TokenStream
{
- let fn_ident = create_caster_fn_ident();
+ let fn_ident = create_caster_fn_ident(Uuid::new_v4());
let new_caster = if sync {
quote! {
@@ -86,18 +86,65 @@ pub fn generate_caster(
}
}
-fn create_caster_fn_ident() -> Ident
+fn create_caster_fn_ident(uuid: impl IUuid) -> Ident
{
let buf = &mut [0u8; FN_BUF_LEN];
buf[..CASTER_FN_NAME_PREFIX.len()].copy_from_slice(CASTER_FN_NAME_PREFIX);
- Uuid::new_v4()
- .to_simple()
- .encode_lower(&mut buf[CASTER_FN_NAME_PREFIX.len()..]);
+ uuid.encode_simple_lower_into(&mut buf[CASTER_FN_NAME_PREFIX.len()..]);
let fn_name =
from_utf8(&buf[..FN_BUF_LEN]).expect("Created caster function name is not UTF-8");
format_ident!("{}", fn_name)
}
+
+/// Simple interface for `Uuid`.
+///
+/// Created for ease of testing the [`create_caster_fn_ident`] function.
+///
+/// [`Uuid`]: uuid::Uuid
+#[cfg_attr(test, mockall::automock)]
+trait IUuid
+{
+ /// Writes the Uuid as a simple lower-case string to `buf`.
+ fn encode_simple_lower_into(self, buf: &mut [u8]);
+}
+
+impl IUuid for Uuid
+{
+ fn encode_simple_lower_into(self, buf: &mut [u8])
+ {
+ self.to_simple().encode_lower(buf);
+ }
+}
+
+#[cfg(test)]
+mod tests
+{
+ use pretty_assertions::assert_eq;
+ use test_util_macros::repeat_char;
+
+ use super::*;
+
+ #[test]
+ fn can_create_caster_fn_ident()
+ {
+ let mut uuid_mock = MockIUuid::new();
+
+ uuid_mock
+ .expect_encode_simple_lower_into()
+ .return_once(|buf| {
+ for index in 0..(FN_BUF_LEN - 2) {
+ buf[index] = b'f';
+ }
+ })
+ .once();
+
+ assert_eq!(
+ create_caster_fn_ident(uuid_mock),
+ format_ident!(concat!("__", repeat_char!('f', 32)))
+ );
+ }
+}
diff --git a/test_util_macros/Cargo.toml b/test_util_macros/Cargo.toml
new file mode 100644
index 0000000..8848961
--- /dev/null
+++ b/test_util_macros/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "test_util_macros"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+proc_macro = true
+
+[dependencies]
+syn = { version = "1.0.96", features = ["full"] }
+quote = "1.0.18"
diff --git a/test_util_macros/src/lib.rs b/test_util_macros/src/lib.rs
new file mode 100644
index 0000000..aa87ecf
--- /dev/null
+++ b/test_util_macros/src/lib.rs
@@ -0,0 +1,47 @@
+#![deny(clippy::all)]
+#![deny(clippy::pedantic)]
+#![deny(missing_docs)]
+
+//! Internal macros used by tests.
+
+use std::iter::repeat;
+
+use proc_macro::TokenStream;
+use quote::quote;
+use syn::parse::Parse;
+use syn::{parse_macro_input, LitChar, LitInt, Token};
+
+/// Repeats a character N number of times.
+#[proc_macro]
+pub fn repeat_char(input: TokenStream) -> TokenStream
+{
+ let RepeatMacroArgs { character, count } =
+ parse_macro_input!(input as RepeatMacroArgs);
+
+ let repeated = repeat(character.value()).take(count).collect::<String>();
+
+ quote! {
+ #repeated
+ }
+ .into()
+}
+
+struct RepeatMacroArgs
+{
+ character: LitChar,
+ count: usize,
+}
+
+impl Parse for RepeatMacroArgs
+{
+ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self>
+ {
+ let character = input.parse::<LitChar>()?;
+
+ input.parse::<Token![,]>()?;
+
+ let count = input.parse::<LitInt>()?.base10_parse::<usize>()?;
+
+ Ok(Self { character, count })
+ }
+}