Skip to content

ryo33/mry2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

mry2

A simple mocking library.

Status: Totally not implemented yet.

Usage

1. Let Cat be a struct with a method meow:

struct Cat {
    name: String,
}

2. Put #[mry2::trait(Cat)] on the impl block of Cat:

#[mry2::trait(Cat)]
impl Cat {
    fn meow(&self) -> String {
        format!("{}: meow", self.name)
    }
}

3. The expanded code:

struct CatObj(CatObjInner);

impl CatObj {
    pub fn new(cat: impl CatInterface) -> Self {
        CatObj(CatObjInner::Dyn(Box::new(cat)))
    }
}

struct CatObjInner {
    Real(Cat),
    Dyn(Box<dyn Cat>),
}

trait CatInterface {
    fn meow(&self) -> String;
}

impl Cat {
    fn meow(&self) -> String {
        format!("{}: meow", self.name)
    }
}

impl CatInterface for CatObj {
    fn meow(&self) -> String {
        match self.0 {
            CatObjInner::Real(cat) => cat.meow(),
            CatObjInner::Dyn(cat) => cat.meow(),
        }
    }
}

impl IntoObject for Cat {
    type Object = CatObj;
    fn obj(self) -> Self::Object {
        CatObj(CatObjInner::Real(self))
    }
}

#[derive(Clone, Default)]
struct MockCat { /* hidden */ };

impl CatInterface for MockCat {
    fn meow(&self) -> String {
        /* hidden */
    }
}

impl IntoObject for MockCat {
    type Object = CatObj;
    fn obj(self) -> Self::Object {
        CatObj(CatObjInner::Dyn(Box::new(self)))
    }
}

4. Use CatObj instead of Cat:

fn a_function_to_test(cat: CatObj) {
    cat.meow();
}

5. Use MockCat to mock Cat:

let mut mock_cat = MockCat::default();
/* set expectations on mock_cat */
a_function_to_test(mock_cat.obj());

About

What we need is:

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors