Code Monkey home page Code Monkey logo

pseudo's Introduction

Pseudo

A small mocking library for Rust.

Crates.io Version Docs Build Status

Pseudo lets you mock Trait implementations so that you can track function call arguments and set return values or overrides functions at test time.

Here's a quick example:

extern crate pseudo;

use pseudo::Mock;

trait Foo: Clone {
    fn expensive_fn(&self, x: i64, y: i64) -> i64;
}

#[derive(Clone)]
struct MockFoo {
    pub expensive_fn: Mock<(i64, i64), i64>,
}

impl Foo for MockFoo {
    fn expensive_fn(&self, x: i64, y: i64) -> i64 {
        self.expensive_fn.call((x + 10, y))
    }
}

fn double_expensive_fn<T: Foo>(foo: &T, x: i64, y: i64) -> i64 {
    foo.expensive_fn(x, y) * 2
}

#[test]
fn doubles_return_value() {
    let mock = MockFoo { expensive_fn: Mock::default() };

    mock.expensive_fn.return_value(1000);

    assert_eq!(double_expensive_fn(&mock, 1, 2), 2000);
}

#[test]
fn uses_correct_args() {
    let mock = MockFoo { expensive_fn: Mock::default() };

    assert!(!mock.expensive_fn.called());

    double_expensive_fn(&mock, 1, 2);

    assert_eq!(mock.expensive_fn.num_calls(), 1);
    assert!(mock.expensive_fn.called_with((11, 2)));
}

More examples are available in the examples directory.

pseudo's People

Contributors

iredelmeier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pseudo's Issues

Can't mock a Send Trait

Pseudo cannot mock a trait that is Send. Example:

    fn send() {
        pub trait A {
            fn foo(&self);
        }

        struct MockA {
            foo: Mock<(), ()>
        }
        impl A for MockA {
            fn foo(&self) {
                self.foo.call(())
            }
        }

        let mock = MockA{foo: Mock::default()};
        let _ = Box::new(mock) as Box<A + Send>;
    }

gives the following error:

error[E0277]: `(dyn std::ops::Fn(()) + 'static)` cannot be shared between threads safely
   --> src/t_pseudo.rs:431:17
    |
431 |         let _ = Box::new(mock) as Box<A + Send>;
    |                 ^^^^^^^^^^^^^^ `(dyn std::ops::Fn(()) + 'static)` cannot be shared between threads safely                                                   
    |
    = help: the trait `std::marker::Sync` is not implemented for `(dyn std::ops::Fn(()) + 'static)`
    = note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<(dyn std::ops::Fn(()) + 'static)>`
    = note: required because it appears within the type `std::boxed::Box<(dyn std::ops::Fn(()) + 'static)>`
    = note: required because it appears within the type `std::option::Option<std::boxed::Box<(dyn std::ops::Fn(()) + 'static)>>`
    = note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::RwLock<std::option::Option<std::boxed::Box<(dyn std::ops::Fn(()) + 'static)>>>`
    = note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<std::sync::RwLock<std::option::Option<std::boxed::Box<(dyn std::ops::Fn(()) + 'static)>>>>`
    = note: required because it appears within the type `pseudo::Mock<(), ()>`
    = note: required because it appears within the type `<t_pseudo::t::Pseudo as TestSuite>::send::MockA`
    = note: required for the cast to the object type `dyn <t_pseudo::t::Pseudo as TestSuite>::send::A + std::marker::Send`

Feature Request: Sequential Returns

struct MyMock {
    pub mock_do_stuff: Mock<(), i32>,
}

impl MyMock {
    pub fn do_stuff(&self) -> i32 {
        self.mock_do_stuff.call(())
    }
}

impl Default for MyMock {
    fn default() -> MyMock {
        MyMock {
            mock_do_stuff: Mock::new(10),
        }
    }
}

#[test]
pub fn playground() {
    let mock = MyMock::default();

    assert_eq!(mock.do_stuff(), 10);

    mock.mock_do_stuff.return_value(42);
    mock.mock_do_stuff.return_value(84);

    let result1 = mock.do_stuff();
    let result2 = mock.do_stuff();

    assert_eq!(result1, 42); // returns 84; desire is it would return 42 on the first call and 84 on any remaining calls.
    assert_eq!(result2, 84);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.