Skip to content

Commit

Permalink
feat: add mock name + function name in panic message
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielsimard committed Jun 28, 2022
1 parent a1dee26 commit 140b3b4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mock-it"
version = "0.6.0"
version = "0.7.0"
authors = ["nathanielsimard <nathaniel.simard@outlook.com>"]
description = "Make mocking reliable"
repository = "https://github.com/nathanielsimard/mock-it"
Expand All @@ -11,7 +11,7 @@ categories = ["development-tools::testing"]
edition = "2021"

[dependencies]
mock-it_codegen = { version = "0.6.0", path = "./mock-it_codegen" }
mock-it_codegen = { version = "0.7.0", path = "./mock-it_codegen" }

[dev-dependencies]
async-trait = "0.1.5"
Expand Down
2 changes: 1 addition & 1 deletion mock-it_codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mock-it_codegen"
version = "0.6.0"
version = "0.7.0"
authors = ["Mcat12 <mark.drobnak@gmail.com>", "nathanielsimard <nathaniel.simard@outlook.com>"]
description = "Codegen for mock-it"
repository = "https://github.com/nathanielsimard/mock-it"
Expand Down
6 changes: 3 additions & 3 deletions mock-it_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn mock_it(

// Generate the mock
let fields = create_fields(&mock_fns);
let field_init = create_field_init(&mock_fns);
let field_init = create_field_init(&mock_ident, &mock_fns);
let trait_impls = create_trait_impls(&mock_fns);
let clone_impl = create_clone_impl(&mock_fns);
let async_attribute = async_attribute(&mock_fns);
Expand Down Expand Up @@ -117,14 +117,14 @@ fn create_fields(mock_fns: &Vec<MockFn>) -> Vec<TokenStream> {
}

/// Create the field initializers for the `new` method
fn create_field_init(mock_fns: &Vec<MockFn>) -> Vec<TokenStream> {
fn create_field_init(mock_ident: &Ident, mock_fns: &Vec<MockFn>) -> Vec<TokenStream> {
mock_fns
.iter()
.map(|mock_fn| {
let name = mock_fn.name();

quote! {
#name: mock_it::Mock::new()
#name: mock_it::Mock::new(format!("{}.{}", stringify!(#mock_ident), stringify!(#name)))
}
})
.collect()
Expand Down
10 changes: 9 additions & 1 deletion src/matcher.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#[derive(Debug)]
pub enum Matcher<I> {
Val(I),
Any,
}

impl<I: std::fmt::Debug> std::fmt::Debug for Matcher<I> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Val(val) => write!(f, "{:?}", val),
Self::Any => write!(f, "Any"),
}
}
}

impl<I: PartialEq> PartialEq for Matcher<I> {
fn eq(&self, other: &Matcher<I>) -> bool {
use crate::matcher::Matcher::*;
Expand Down
12 changes: 9 additions & 3 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ use std::sync::Arc;
use std::sync::Mutex;

pub struct Mock<I, O> {
name: String,
calls: Arc<Mutex<Vec<I>>>,
rules: Arc<Mutex<Vec<Rule<I, O>>>>,
}

impl<I, O> Clone for Mock<I, O> {
fn clone(&self) -> Mock<I, O> {
Mock {
name: self.name.clone(),
calls: self.calls.clone(),
rules: self.rules.clone(),
}
}
}

impl<I: PartialEq + std::fmt::Debug, O: Clone> Mock<I, O> {
pub fn new() -> Mock<I, O> {
pub fn new(name: String) -> Mock<I, O> {
Mock {
name,
calls: Arc::new(Mutex::new(Vec::new())),
rules: Arc::new(Mutex::new(Vec::new())),
}
Expand All @@ -43,7 +46,10 @@ impl<I: PartialEq + std::fmt::Debug, O: Clone> Mock<I, O> {
// Return the when value, or fail if there is no when value
match when_value {
Some(value) => value.output.clone(),
None => panic!("Mock called with unexpected input: {:?}", input_str),
None => panic!(
"Mock \"{}\" called with unexpected input: {:?}",
self.name, input_str
),
}
}

Expand Down Expand Up @@ -71,7 +77,7 @@ mod test {
impl MyMock {
fn new() -> MyMock {
MyMock {
int_to_string: Mock::new(),
int_to_string: Mock::new("AMockName".to_string()),
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/codegen_panic_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use mock_it::mock_it;

#[mock_it]
trait ATrait {
fn a_fn(&self, arg1: usize);
}

#[test]
#[should_panic(expected = "Mock \"ATraitMock.a_fn\" called with unexpected input: \"23\"")]
fn mock_no_when_should_panic() {
let mock = ATraitMock::new();
let _output = mock.a_fn(23);
}

0 comments on commit 140b3b4

Please # to comment.