1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
//! A strict, yet friendly mocking library for Rust 2018 //! //! # Examples //! //! ``` //! use mockiato::mockable; //! //! # const IGNORED: &str = " //! #[cfg_attr(test, mockable)] //! # "; //! # #[mockable] //! trait Greeter { //! fn greet(&self, name: &str) -> String; //! } //! //! let mut greeter = GreeterMock::new(); //! //! greeter //! .expect_greet(|arg| arg.partial_eq("Jane")) //! .times(1..) //! .returns(String::from("Hello Jane")); //! //! assert_eq!("Hello Jane", greeter.greet("Jane")); //! ``` //! //! # Configuring Expected Calls //! //! Each method on the trait receives two companion methods on the mock struct: //! //! ## `expect_<method_name>` //! //! Registers an expected call to the mocked method. Exactly one call is expected by default. //! The order in which these methods are called is irrelevant, unless configured otherwise. //! //! It has the same amount of arguments as the mocked method. //! Each argument accepts a closure that is invoked with a reference to [`Argument`], which lets //! you create different argument matchers. //! //! This method returns a [`MethodCallBuilder`] which allows for further customization of an expected call's behavior. //! //! ``` //! use mockiato::mockable; //! //! # const IGNORED: &str = " //! #[cfg_attr(test, mockable)] //! # "; //! # #[mockable] //! trait MessageSender { //! fn send_message(&self, recipient: &str, message: &str); //! } //! //! let mut message_sender = MessageSenderMock::new(); //! message_sender //! .expect_send_message(|arg| arg.partial_eq("Paul"), |arg| arg.any()) //! .times(..) //! .returns(()); //! ``` //! //! ## `expect_<method_name>_calls_in_order` //! //! Configures the mocked method so that the expected calls are processed sequentially. //! When this is enabled, the calls to the mocked method must be in the same order as the `expect_` methods were called. //! //! ``` //! # use mockiato::mockable; //! # //! # const IGNORED: &str = " //! #[cfg_attr(test, mockable)] //! # "; //! # #[mockable] //! # trait MessageSender { //! # fn send_message(&self, recipient: &str, message: &str); //! # } //! # //! # let mut message_sender = MessageSenderMock::new(); //! message_sender.expect_send_message_calls_in_order(); //! ``` //! //! # Call Verification //! Mockiato automatically verifies that all expected calls were made when the mock goes out of scope. //! The mock panics when a method is called that was not configured, or if the parameters did not match. //! ```no_run //! use mockiato::mockable; //! //! # const IGNORED: &str = " //! #[cfg_attr(test, mockable)] //! # "; //! # #[mockable] //! trait Greeter { //! fn greet(&self, name: &str) -> String; //! } //! //! { //! let mut greeter = GreeterMock::new(); //! //! greeter //! .expect_greet(|arg| arg.partial_eq("Doe")) //! .times(1..) //! .returns(String::from("Hello Doe")); //! //! assert_eq!("Hello Jane", greeter.greet("Jane")); //! // ^^^^^^^^^^^^^ //! // This call was not configured, which results in a panic //! //! // The mock verifies that all expected calls have been made //! // <-- and panics otherwise //! } //! ``` #![cfg_attr(rustc_is_nightly, feature(doc_cfg, external_doc, specialization))] #![warn(missing_docs, clippy::dbg_macro, clippy::unimplemented)] #![deny( rust_2018_idioms, future_incompatible, missing_debug_implementations, clippy::doc_markdown, clippy::default_trait_access, clippy::enum_glob_use, clippy::needless_borrow, clippy::large_digit_groups, clippy::explicit_into_iter_loop )] #[cfg(any(not(rustc_is_nightly), not(rustdoc)))] pub use mockiato_codegen::mockable; #[cfg(all(rustc_is_nightly, rustdoc))] #[macro_export] /// Generates a mock struct from a trait. /// /// # Parameters /// /// ## `static_references` /// Forces values stored in argument matchers to be `'static`. This is used when the mock needs to satisfy /// `'static` e.g. when downcasting the mocked trait to a concrete implementation using the `Any` trait. /// There is an [example] available on how to do this. /// /// [example]: https://github.com/myelin-ai/mockiato/blob/master/examples/downcasting.rs /// /// ``` /// use mockiato::mockable; /// use std::any::Any; /// /// #[cfg_attr(test, mockable(static_references))] /// pub trait Animal: Any { /// fn make_sound(&self); /// } /// ``` /// /// ## `name` /// Sets a custom name for the mock struct instead of the default. /// ``` /// use mockiato::mockable; /// /// #[cfg_attr(test, mockable(name = "CuteAnimalMock"))] /// trait Animal { /// fn make_sound(&self); /// } /// ``` macro_rules! mockable { () => {}; } #[cfg_attr(rustc_is_nightly, doc(include = "../readme.md"))] mod test_readme {} pub use crate::internal::argument::Argument; pub use crate::internal::expected_calls::ExpectedCalls; pub use crate::internal::MethodCallBuilder; #[doc(hidden)] pub mod internal;