Skip to content

Commit

Permalink
Merge pull request #8 from nilwurtz/master
Browse files Browse the repository at this point in the history
add `#[allow(non_snake_case)]` to function starts with underscore
  • Loading branch information
ryo33 committed Nov 4, 2022
2 parents 8d649fe + 5a4469c commit c8f252c
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 60 deletions.
2 changes: 1 addition & 1 deletion mry/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ mod nested_mock;
mod not_clone;
mod partial_mock;
mod reference_and_pattern;
mod serde_struct;
mod simple_case;
mod static_function;
mod serde_struct;
5 changes: 1 addition & 4 deletions mry/tests/integration/mut_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,5 @@ fn meow_returns_with() {
cat.mock_meow("aaa".to_string())
.returns_with(|string| format!("Called with {}", string));

assert_eq!(
cat.meow("aaa".to_string()),
"Called with aaa".to_string()
);
assert_eq!(cat.meow("aaa".to_string()), "Called with aaa".to_string());
}
3 changes: 1 addition & 2 deletions mry/tests/integration/serde_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ impl Cat {
}
}


#[test]
fn cat_can_serialize() {
let cat: Cat = mry::new!(Cat {
Expand All @@ -23,4 +22,4 @@ fn cat_can_serialize() {
let serialized = serde_json::to_string(&cat);
assert!(serialized.is_ok());
assert_eq!(serialized.unwrap(), r#"{"name":"Tama"}"#)
}
}
68 changes: 53 additions & 15 deletions mry_macros/src/item_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,64 @@ mod test {
assert_eq!(
transform(input).to_string(),
quote! {
fn meow(count: usize) -> String {
fn meow(count: usize) -> String {
#[cfg(debug_assertions)]
if let Some(out) = mry::STATIC_MOCKS.write().record_call_and_find_mock_output(std::any::Any::type_id(&meow), "meow", (count.clone())) {
return out;
}
{
if let Some(out) = mry::STATIC_MOCKS.write().record_call_and_find_mock_output(std::any::Any::type_id(&meow), "meow", (count.clone())) {
return out;
}
{
"meow".repeat(count)
}
}

#[cfg(debug_assertions)]
pub fn mock_meow<'mry>(count: impl Into<mry::Matcher<usize>>) -> mry::MockLocator<'mry, (usize), String, mry::Behavior1<(usize), String> > {
mry::MockLocator {
mocks: Box::new(mry::STATIC_MOCKS.write()),
key: std::any::Any::type_id(&meow),
name: "meow",
matcher: Some((count.into(),).into()),
_phantom: Default::default(),
}
}
}
.to_string()
);
}

#[test]
fn add_allow_non_snake_case() {
let input: ItemFn = parse2(quote! {
fn _meow(count: usize) -> String {
"meow".repeat(count)
}
})
.unwrap();

assert_eq!(
transform(input).to_string(),
quote! {
fn _meow(count: usize) -> String {
#[cfg(debug_assertions)]
if let Some(out) = mry::STATIC_MOCKS.write().record_call_and_find_mock_output(std::any::Any::type_id(&_meow), "_meow", (count.clone())) {
return out;
}
{
"meow".repeat(count)
}
}
}

#[cfg(debug_assertions)]
pub fn mock_meow<'mry>(count: impl Into<mry::Matcher<usize>>) -> mry::MockLocator<'mry, (usize), String, mry::Behavior1<(usize), String> > {
mry::MockLocator {
mocks: Box::new(mry::STATIC_MOCKS.write()),
key: std::any::Any::type_id(&meow),
name: "meow",
matcher: Some((count.into(),).into()),
_phantom: Default::default(),
}
}
#[allow(non_snake_case)]
pub fn mock__meow<'mry>(count: impl Into<mry::Matcher<usize>>) -> mry::MockLocator<'mry, (usize), String, mry::Behavior1<(usize), String> > {
mry::MockLocator {
mocks: Box::new(mry::STATIC_MOCKS.write()),
key: std::any::Any::type_id(&_meow),
name: "_meow",
matcher: Some((count.into(),).into()),
_phantom: Default::default(),
}
}
}
.to_string()
);
Expand Down
32 changes: 18 additions & 14 deletions mry_macros/src/item_struct.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
use proc_macro2::TokenTree;
use proc_macro2::TokenStream;
use proc_macro2::TokenTree;
use quote::quote;
use syn::{ItemStruct, visit::Visit};
use syn::{visit::Visit, ItemStruct};

#[derive(Default)]
struct SerdeFindVisitor(bool);

impl<'ast> Visit<'ast> for SerdeFindVisitor {
fn visit_attribute(&mut self, i: &'ast syn::Attribute) {
if i.path.is_ident("derive") && i.tokens.clone().into_iter().any(|t| match t {
TokenTree::Group(g) => {
g.stream().into_iter().any(|s| match s {
TokenTree::Ident(i) => i == "serde",
_ => false
if i.path.is_ident("derive")
&& i.tokens.clone().into_iter().any(|t| match t {
TokenTree::Group(g) => g.stream().into_iter().any(|s| match s {
TokenTree::Ident(i) => i == "serde",
_ => false,
}),
_ => false,
})
}
_ => false
}) {
self.0 =true
};
}
{
self.0 = true
};
}
}

pub(crate) fn transform(input: ItemStruct) -> TokenStream {
let vis = &input.vis;
let struct_name = &input.ident;

let mut serde_find_visitor = SerdeFindVisitor::default();
let _ = &input.attrs.clone().into_iter().for_each(|a| serde_find_visitor.visit_attribute(&a));
let _ = &input
.attrs
.clone()
.into_iter()
.for_each(|a| serde_find_visitor.visit_attribute(&a));
let serde_skip_or_blank = if serde_find_visitor.0 {
quote!(#[serde(skip)])
} else {
Expand Down
91 changes: 70 additions & 21 deletions mry_macros/src/item_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ mod test {
assert_eq!(
transform(input).to_string(),
quote! {
trait Cat {
fn meow(&self, count: usize) -> String;
}
trait Cat {
fn meow(&self, count: usize) -> String;
}

#[derive(Default, Clone, Debug)]
struct MockCat {
pub mry : mry::Mry,
}
#[derive(Default, Clone, Debug)]
struct MockCat {
pub mry : mry::Mry,
}

impl Cat for MockCat {
fn meow(&self, count: usize) -> String {
Expand Down Expand Up @@ -138,14 +138,14 @@ mod test {
assert_eq!(
transform(input).to_string(),
quote! {
pub trait Cat {
fn meow(&self, count: usize) -> String;
}
pub trait Cat {
fn meow(&self, count: usize) -> String;
}

#[derive(Default, Clone, Debug)]
pub struct MockCat {
pub mry : mry::Mry,
}
#[derive(Default, Clone, Debug)]
pub struct MockCat {
pub mry : mry::Mry,
}

impl Cat for MockCat {
fn meow(&self, count: usize) -> String {
Expand Down Expand Up @@ -188,14 +188,14 @@ mod test {
transform(input).to_string(),
quote! {
#[async_trait::async_trait]
trait Cat {
async fn meow(&self, count: usize) -> String;
}
trait Cat {
async fn meow(&self, count: usize) -> String;
}

#[derive(Default, Clone, Debug)]
struct MockCat {
pub mry : mry::Mry,
}
#[derive(Default, Clone, Debug)]
struct MockCat {
pub mry : mry::Mry,
}

#[async_trait::async_trait]
impl Cat for MockCat {
Expand Down Expand Up @@ -224,4 +224,53 @@ mod test {
.to_string()
);
}

#[test]
fn add_allow_non_snake_case() {
let input: ItemTrait = parse2(quote! {
trait Cat {
fn _meow(&self, count: usize) -> String;
}
})
.unwrap();

assert_eq!(
transform(input).to_string(),
quote! {
trait Cat {
fn _meow(&self, count: usize) -> String;
}

#[derive(Default, Clone, Debug)]
struct MockCat {
pub mry : mry::Mry,
}

impl Cat for MockCat {
fn _meow(&self, count: usize) -> String {
#[cfg(debug_assertions)]
if let Some(out) = self.mry.record_call_and_find_mock_output(std::any::Any::type_id(&MockCat::_meow), "Cat::_meow", (count.clone())) {
return out;
}
panic!("mock not found for Cat")
}
}

impl MockCat {
#[cfg(debug_assertions)]
#[allow(non_snake_case)]
pub fn mock__meow<'mry>(&'mry mut self, count: impl Into<mry::Matcher<usize>>) -> mry::MockLocator<'mry, (usize), String, mry::Behavior1<(usize), String> > {
mry::MockLocator {
mocks: self.mry.mocks_write(),
key: std::any::Any::type_id(&MockCat::_meow),
name: "Cat::_meow",
matcher: Some((count.into(),).into()),
_phantom: Default::default(),
}
}
}
}
.to_string()
);
}
}
48 changes: 45 additions & 3 deletions mry_macros/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ pub fn transform(
(mock_arg, mock_arg_into)
})
.unzip();
let allow_non_snake_case_or_blank = if ident.to_string().starts_with("_") {
quote!(#[allow(non_snake_case)])
} else {
TokenStream::default()
};
let key = quote![std::any::Any::type_id(&#method_prefix#ident)];
(
quote! {
Expand All @@ -131,6 +136,7 @@ pub fn transform(
},
quote! {
#[cfg(debug_assertions)]
#allow_non_snake_case_or_blank
pub fn #mock_ident<'mry>(#mock_receiver#(#mock_args),*) -> mry::MockLocator<'mry, #input_type_tuple, #static_output_type, #behavior_type> {
mry::MockLocator {
mocks: #mocks_write_lock,
Expand Down Expand Up @@ -256,6 +262,42 @@ mod test {
);
}

#[test]
fn adds_allow_non_snake_case() {
let input: ImplItemMethod = parse2(quote! {
fn _meow(&self, count: usize) -> String {
"meow".repeat(count)
}
})
.unwrap();

assert_eq!(
t(&input).to_string(),
quote! {
fn _meow(&self, count: usize) -> String {
#[cfg(debug_assertions)]
if let Some(out) = self.mry.record_call_and_find_mock_output(std::any::Any::type_id(&Self::_meow), "Cat::_meow", (count.clone())) {
return out;
}
"meow".repeat(count)
}

#[cfg(debug_assertions)]
#[allow(non_snake_case)]
pub fn mock__meow<'mry>(&'mry mut self, count: impl Into<mry::Matcher<usize>>) -> mry::MockLocator<'mry, (usize), String, mry::Behavior1<(usize), String> > {
mry::MockLocator {
mocks: self.mry.mocks_write(),
key: std::any::Any::type_id(&Self::_meow),
name: "Cat::_meow",
matcher: Some((count.into(),).into()),
_phantom: Default::default(),
}
}
}
.to_string()
);
}

#[test]
fn empty_args() {
let input: ImplItemMethod = parse2(quote! {
Expand Down Expand Up @@ -409,13 +451,13 @@ mod test {
assert_eq!(
t(&input).to_string(),
quote! {
fn meow(&self, arg0: A, count: usize, arg2: String) -> String {
fn meow(&self, arg0: A, count: usize, arg2: String) -> String {
#[cfg(debug_assertions)]
if let Some(out) = self.mry.record_call_and_find_mock_output(std::any::Any::type_id(&Self::meow), "Cat::meow", (arg0.clone(), count.clone(), arg2.clone())) {
return out;
}
let A { name } = arg0;
let _ = arg2;
let A { name } = arg0;
let _ = arg2;
name.repeat(count)
}

Expand Down

0 comments on commit c8f252c

Please sign in to comment.