Skip to content

Commit e0c1bb8

Browse files
David DeSimonedavid
David DeSimone
and
david
authored
Adding support for mocking debugged traits (#25)
* Adding support for mocking debugged traits * Rather than panic on printing, we will let the user control the print message Co-authored-by: david <[email protected]>
1 parent fd0594e commit e0c1bb8

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

mock_derive/src/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ fn parse_trait(trait_block: syn::ItemTrait, raw_trait: &syn::Item) -> proc_macro
504504
}
505505
}
506506
}
507+
508+
fields.extend(quote! { print_string: ::std::sync::Mutex<Option<String>>, });
509+
ctor.extend(quote! { print_string : ::std::sync::Mutex::new(None), });
507510

508511
let mock_method_body = generate_mock_method_body(&pubtok, &mock_method_name);
509512
let static_struct_name = concat!("STATIC__", trait_name);
@@ -536,6 +539,21 @@ fn parse_trait(trait_block: syn::ItemTrait, raw_trait: &syn::Item) -> proc_macro
536539
#fields
537540
}
538541

542+
#[allow(dead_code)]
543+
#[allow(non_camel_case_types)]
544+
impl #generics ::std::fmt::Debug for #impl_name #generics #where_clause {
545+
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
546+
// panic!("Printing mocks is not supported. Do not call println on mocks.");
547+
let print_string = self.print_string.lock().unwrap();
548+
let mut message = "This is the default message for a mock object. You can set the print behavior via a call to 'set_print_string'";
549+
if print_string.as_ref().is_some() {
550+
message = print_string.as_ref().unwrap();
551+
}
552+
553+
f.pad(&message)
554+
}
555+
}
556+
539557
// Your mocks may not use all of these functions, so it's fine to allow
540558
// dead code in this impl block.
541559
#[allow(dead_code)]
@@ -546,6 +564,11 @@ fn parse_trait(trait_block: syn::ItemTrait, raw_trait: &syn::Item) -> proc_macro
546564
pub fn new() -> #impl_name #generics {
547565
#impl_name { #ctor }
548566
}
567+
568+
pub fn set_print_string(&self, arg_string: String) {
569+
let mut print_string = self.print_string.lock().unwrap();
570+
*print_string = Some(arg_string);
571+
}
549572
}
550573

551574
#mock_method_body

tests/src/advanced_traits.rs

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ SOFTWARE.
2525
use mock_derive::mock;
2626
use export;
2727

28+
use std::fmt::Debug;
29+
2830
#[mock]
2931
trait Base {
3032
fn add(&self, x: i32, y: usize) -> usize;
@@ -89,6 +91,11 @@ trait UnsafeStaticMock {
8991
unsafe fn st_method() -> usize;
9092
}
9193

94+
#[mock]
95+
trait DebuggedTrait : Debug {
96+
fn foo(&self);
97+
}
98+
9299
// @TODO support
93100
/*
94101
trait BaseG<T> {
@@ -99,6 +106,13 @@ trait DerivedG : BaseG<usize> {
99106
};
100107
*/
101108

109+
#[test]
110+
fn print_trait() {
111+
let mock_debugged = MockDebuggedTrait::new();
112+
mock_debugged.set_print_string(String::from("Hello Mocks"));
113+
println!("{:?}", mock_debugged);
114+
}
115+
102116
#[test]
103117
fn mock_derived() {
104118
let mut mock_derived = MockDerived::new();

0 commit comments

Comments
 (0)