├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-MIT ├── README.md ├── LICENSE-APACHE └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | Cargo.lock 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - stable 4 | - beta 5 | - nightly 6 | matrix: 7 | allow_failures: 8 | - rust: nightly 9 | branches: 10 | only: 11 | - master 12 | script: 13 | - cargo test --verbose 14 | notifications: 15 | email: 16 | on_success: never 17 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "spy" 3 | version = "0.2.0" 4 | authors = ["Alex Pikalov "] 5 | description = "Rust spy functions for testing purposes" 6 | documentation = "https://docs.rs/spy" 7 | homepage = "https://github.com/AlexPikalov/spy" 8 | repository = "https://github.com/AlexPikalov/spy" 9 | readme = "./README.md" 10 | keywords = ["testing", "spy", "TDD"] 11 | license = "MIT/Apache-2.0" 12 | 13 | [dependencies] 14 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Spy Project Developers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | of the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included 13 | in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR 17 | A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 19 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spy [![Build Status](https://travis-ci.org/AlexPikalov/spy.svg?branch=master)](https://travis-ci.org/AlexPikalov/spy) 2 | --- 3 | [Documentation](https://docs.rs/spy) 4 | 5 | Spy crate is inspired by such famous Javascript testing tools 6 | as Jasmine and Sinon.js. It provides easy configurable spies 7 | with predefined behaviour. 8 | 9 | ```rust 10 | #[test] 11 | fn iterator_all_test() { 12 | let integers = vec![0i32, 1i32, 2i32]; 13 | 14 | // create spy function that returns true if provided 15 | // argument is an even number 16 | let (spy_fn, spy) = spy!(|n| n % 2 == 0); 17 | 18 | // test call 19 | let res = integers.iter().all(spy_fn); 20 | 21 | // check Iterator::all result 22 | assert!(!res, "should be false"); 23 | 24 | // take a snapshot of made calls 25 | let snapshot = spy.snapshot(); 26 | 27 | // make assertions 28 | assert!(snapshot.called(), "should be called"); 29 | assert!( 30 | snapshot.called_with(&(&1i32)), 31 | "should be called with 1i32 at least once" 32 | ); 33 | assert!( 34 | !snapshot.each_called_with(&(&1i32)), 35 | "should be called with different arguments" 36 | ); 37 | assert_eq!(snapshot.all_calls(), &vec![(&0i32), (&1i32)]); 38 | assert_eq!(snapshot.first_call().expect("should be Some"), &(&0i32)); 39 | assert_eq!(snapshot.last_call().expect("should be Some"), &(&1i32)); 40 | assert_eq!(snapshot.nth_call(1).expect("should be Some"), &(&1i32)); 41 | } 42 | ``` 43 | 44 | ## LICENSE 45 | 46 | Spy is licensed under [MIT](./LICENSE_MIT) and [Apache-2.0](./LICENSE_APACHE) license 47 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 Spy Project Developers 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Spy crate is inspired by such famous Javascript testing tools 2 | //! as Jasmine and Sinon.js. It provides easy configurable spies 3 | //! with predefined behaviour. 4 | //! 5 | //! # Spy without arguments 6 | //! 7 | //! `spy!()` creates a pair of a spy function that receives no arguments 8 | //! and does nothing and corresponded `Spy` object. 9 | //! 10 | //! ```rust 11 | //! #[macro_use] 12 | //! extern crate spy; 13 | //! 14 | //! use spy::*; 15 | //! 16 | //! fn main() { 17 | //! let (spy_fn, spy) = spy!(); 18 | //! 19 | //! assert_eq!(spy_fn(), ()); 20 | //! assert_eq!(spy_fn(), ()); 21 | //! 22 | //! // take a snapshot 23 | //! let snapshot = spy.snapshot(); 24 | //! 25 | //! // make assertions 26 | //! assert!( 27 | //! snapshot.called_with(()), 28 | //! "should be called with () at least once" 29 | //! ); 30 | //! assert!( 31 | //! snapshot.each_called_with(()), 32 | //! "should be called with () each time" 33 | //! ); 34 | //! assert_eq!(snapshot.num_of_calls(), 2, "should be called 2 times"); 35 | //! assert_eq!(snapshot.all_calls(), &vec![(), ()]); 36 | //! assert_eq!(snapshot.first_call().expect("should be Some"), &()); 37 | //! assert_eq!(snapshot.last_call().expect("should be Some"), &()); 38 | //! assert_eq!(snapshot.nth_call(1).expect("should be Some"), &()); 39 | //! } 40 | //! 41 | //! ``` 42 | //! 43 | //! # Spy with arguments 44 | //! 45 | //! If a spy function should receive arguments it can be created in one 46 | //! of following ways: 47 | //! 48 | //! * `spy!(|n, m|)` creates a spy function that can receive two arguments. 49 | //! Any number of arguments could be passed inside `||`. 50 | //! Types of these arguments will be inferred from a spy function usage. 51 | //! This function does nothing and returns `()`. 52 | //! 53 | //! * `spy!(|n, m| a + b)` creates a spy function that can receive two 54 | //! arguments followed by a function body but any number of arguments could be requested. 55 | //! This syntax is pretty much the same as Rust closures. Types of arguments 56 | //! and an output will be inferred from a spy function usage. 57 | //! 58 | //! * `spy!(|n: u8, m: u8| -> u8 { return n + m; })` creates spy function 59 | //! with type annotations both for arguments and for an output. 60 | //! 61 | 62 | use std::sync::mpsc::Receiver; 63 | 64 | /// Spy object that tracks calls of associated spy function. 65 | pub struct Spy { 66 | pub calls_recv: Receiver, 67 | } 68 | 69 | impl Spy { 70 | /// It takes a snapshot of a spy object. Taken snapshot 71 | /// contains all the calls starting from previous snapshot. 72 | pub fn snapshot(&self) -> SpySnapshot { 73 | let mut calls: Vec = vec![]; 74 | loop { 75 | match self 76 | .calls_recv 77 | .recv_timeout(::std::time::Duration::from_millis(0)) 78 | { 79 | Ok(args) => calls.push(args), 80 | Err(_) => { 81 | break; 82 | } 83 | } 84 | } 85 | SpySnapshot { calls } 86 | } 87 | } 88 | 89 | /// The structure represents a snapshot of a spy object. Taken snapshot 90 | /// contains all the calls starting from the moment when `Spy` object was created 91 | /// or previous snapshot if there was taken one. 92 | /// 93 | /// Generic type `Args` is in fact a tuple that describes argument values for each 94 | /// call. 95 | pub struct SpySnapshot { 96 | calls: Vec, 97 | } 98 | 99 | impl SpySnapshot { 100 | /// It returns `true` if spy function was called at least once and `false` otherwise. 101 | pub fn called(&self) -> bool { 102 | !self.calls.is_empty() 103 | } 104 | 105 | /// It returns `true` if spy function was called at least once 106 | /// with provided arguments and `false` otherwise. 107 | pub fn called_with(&self, args: Args) -> bool { 108 | for call in &self.calls { 109 | if &args == call { 110 | return true; 111 | } 112 | } 113 | false 114 | } 115 | 116 | /// It returns `true` if spy function was called with provided arguments each time 117 | /// it was called. 118 | pub fn each_called_with(&self, args: Args) -> bool { 119 | for call in &self.calls { 120 | if &args != call { 121 | return false; 122 | } 123 | } 124 | true 125 | } 126 | 127 | /// It returns how many times spy function was called. 128 | pub fn num_of_calls(&self) -> usize { 129 | self.calls.len() 130 | } 131 | 132 | /// It returns a vector that contains all calls arguments. 133 | pub fn all_calls(&self) -> &Vec { 134 | &self.calls 135 | } 136 | 137 | /// It returns first call arguments. `None` will be returned if spy function was not 138 | /// called till the moment when a snapshot was taken. 139 | pub fn first_call(&self) -> Option<&Args> { 140 | self.calls.first() 141 | } 142 | 143 | /// It returns last call arguments. `None` will be returned if spy function was not 144 | /// called till the moment when a snapshot was taken. 145 | pub fn last_call(&self) -> Option<&Args> { 146 | self.calls.last() 147 | } 148 | 149 | /// It returns n-th call arguments. `None` will be returned if spy function was not 150 | /// called enough times till the moment when a snapshot was taken. 151 | pub fn nth_call(&self, n: usize) -> Option<&Args> { 152 | self.calls.get(n) 153 | } 154 | } 155 | 156 | /// The macro that creates a pair of a `Spy` object and a spy function tracked by returned 157 | /// spy. 158 | /// 159 | /// ```rust 160 | /// use spy::{spy, Spy}; 161 | /// 162 | /// /// spy function that takes no argument and returns no value 163 | /// let (spy_fn, spy) = spy!(); 164 | /// 165 | /// spy_fn(); 166 | /// 167 | /// /// spy function that takes one argument argument and returns no value. 168 | /// let (spy_fn, spy) = spy!(|n|); 169 | /// 170 | /// spy_fn(42); 171 | /// 172 | /// /// spy function that takes one argument argument and returns some value 173 | /// /// evaluated basing on taken argument. 174 | /// let (spy_fn, spy) = spy!(|n| n % 2 == 0); 175 | /// 176 | /// spy_fn(42); 177 | /// ``` 178 | #[macro_export] 179 | macro_rules! spy { 180 | () => { 181 | { 182 | let (s, recv) = ::std::sync::mpsc::channel(); 183 | let spy_fn = move || { 184 | s.send(()) 185 | .expect("Problem with call agregation: cannot send call arguments to channel"); 186 | }; 187 | (spy_fn, Spy{ calls_recv: recv }) 188 | } 189 | }; 190 | (|$arg:ident|) => { 191 | { 192 | let (s, recv) = ::std::sync::mpsc::channel(); 193 | let spy_fn = move |$arg| { 194 | s.send($arg) 195 | .expect("Problem with call agregation: cannot send call arguments to channel"); 196 | }; 197 | (spy_fn, Spy{ calls_recv: recv }) 198 | } 199 | }; 200 | (|$($arg:ident),*|) => { 201 | { 202 | let (s, recv) = ::std::sync::mpsc::channel(); 203 | let spy_fn = move |$($arg),*| { 204 | let args_tuple = ($($arg,) *); 205 | s.send(args_tuple) 206 | .expect("Problem with call agregation: cannot send call arguments to channel"); 207 | }; 208 | (spy_fn, Spy{ calls_recv: recv }) 209 | } 210 | }; 211 | (|$arg:ident| $expression:expr) => { 212 | { 213 | let (s, recv) = ::std::sync::mpsc::channel(); 214 | 215 | let spy_fn = move |$arg| { 216 | s.send($arg) 217 | .expect("Problem with call agregation: cannot send call arguments to channel"); 218 | $expression 219 | }; 220 | (spy_fn, Spy{ calls_recv: recv }) 221 | } 222 | }; 223 | (|$($arg:ident),*| $expression:expr) => { 224 | { 225 | let (s, recv) = ::std::sync::mpsc::channel(); 226 | 227 | let spy_fn = move |$($arg),*| { 228 | let args_tuple = ($($arg,) *); 229 | s.send(args_tuple) 230 | .expect("Problem with call agregation: cannot send call arguments to channel"); 231 | $expression 232 | }; 233 | (spy_fn, Spy{ calls_recv: recv }) 234 | } 235 | }; 236 | (|$($arg:ident : $arg_type:ty),*| $(-> $return_type:ty)* $expression:block) => { 237 | { 238 | let (s, recv) = ::std::sync::mpsc::channel(); 239 | 240 | let spy_fn = move |$($arg : $arg_type),*| $(-> $return_type)* { 241 | let args_tuple = ($($arg,) *); 242 | s.send(args_tuple) 243 | .expect("Problem with call agregation: cannot send call arguments to channel"); 244 | $expression 245 | }; 246 | (spy_fn, Spy{ calls_recv: recv }) 247 | } 248 | }; 249 | } 250 | 251 | #[cfg(test)] 252 | mod tests { 253 | use super::*; 254 | 255 | fn no_args_cb(cb: F) 256 | where 257 | F: Fn() + Sized, 258 | { 259 | // 3 times call 260 | cb(); 261 | cb(); 262 | cb(); 263 | } 264 | 265 | #[test] 266 | fn create_spy_test() { 267 | let (spy_fn, spy) = spy!(); 268 | 269 | no_args_cb(spy_fn); 270 | let snapshot = spy.snapshot(); 271 | 272 | assert!(snapshot.called(), "should be called"); 273 | assert!( 274 | snapshot.called_with(()), 275 | "should be called with () at least once" 276 | ); 277 | assert!( 278 | snapshot.each_called_with(()), 279 | "should be called with () each time" 280 | ); 281 | assert_eq!(snapshot.num_of_calls(), 3, "should be called 3 times"); 282 | assert_eq!(snapshot.all_calls(), &vec![(), (), ()]); 283 | assert_eq!(snapshot.first_call().expect("should be Some"), &()); 284 | assert_eq!(snapshot.last_call().expect("should be Some"), &()); 285 | assert_eq!(snapshot.nth_call(1).expect("should be Some"), &()); 286 | } 287 | 288 | #[test] 289 | fn create_spy_with_args_test() { 290 | let (spy_fn, spy) = spy!(|n|); 291 | 292 | spy_fn(1u8); 293 | spy_fn(2u8); 294 | 295 | let snapshot = spy.snapshot(); 296 | 297 | assert!(snapshot.called(), "should be called"); 298 | 299 | assert!( 300 | snapshot.called_with(1u8), 301 | "should be called with &1u8 at least once" 302 | ); 303 | assert!( 304 | !snapshot.each_called_with(1u8), 305 | "should be called with different arguments" 306 | ); 307 | assert_eq!(snapshot.num_of_calls(), 2, "should be called 3 times"); 308 | assert_eq!(snapshot.all_calls(), &vec![1u8, 2u8]); 309 | assert_eq!(snapshot.first_call().expect("should be Some"), &1u8); 310 | assert_eq!(snapshot.last_call().expect("should be Some"), &2u8); 311 | assert_eq!(snapshot.nth_call(1).expect("should be Some"), &2u8); 312 | } 313 | 314 | #[test] 315 | fn create_spy_with_many_args_test() { 316 | let (spy_fn, spy) = spy!(|n, s|); 317 | 318 | spy_fn(1u8, "first"); 319 | spy_fn(2u8, "second"); 320 | 321 | let snapshot = spy.snapshot(); 322 | 323 | assert!(snapshot.called(), "should be called"); 324 | 325 | assert!( 326 | snapshot.called_with((1u8, "first")), 327 | "should be called with &1u8 at least once" 328 | ); 329 | assert!( 330 | !snapshot.each_called_with((1u8, "first")), 331 | "should be called with different arguments" 332 | ); 333 | assert_eq!(snapshot.num_of_calls(), 2, "should be called 3 times"); 334 | assert_eq!(snapshot.all_calls(), &vec![(1u8, "first"), (2u8, "second")]); 335 | assert_eq!( 336 | snapshot.first_call().expect("should be Some"), 337 | &(1u8, "first") 338 | ); 339 | assert_eq!( 340 | snapshot.last_call().expect("should be Some"), 341 | &(2u8, "second") 342 | ); 343 | assert_eq!( 344 | snapshot.nth_call(1).expect("should be Some"), 345 | &(2u8, "second") 346 | ); 347 | } 348 | 349 | #[test] 350 | fn create_spy_with_args_and_return_test() { 351 | let (spy_fn, spy) = spy!(|n| n % 2 == 0); 352 | 353 | assert!(spy_fn(2), "should return true for an even number"); 354 | assert!(!spy_fn(3), "should return false for an odd number"); 355 | 356 | let snapshot = spy.snapshot(); 357 | 358 | assert!(snapshot.called(), "should be called"); 359 | assert!( 360 | snapshot.called_with(2), 361 | "should be called with 2 at least once" 362 | ); 363 | assert!( 364 | !snapshot.each_called_with(2), 365 | "should be called with different arguments" 366 | ); 367 | assert_eq!(snapshot.all_calls(), &vec![2, 3]); 368 | assert_eq!(snapshot.first_call().expect("should be Some"), &2); 369 | assert_eq!(snapshot.last_call().expect("should be Some"), &3); 370 | assert_eq!(snapshot.nth_call(1).expect("should be Some"), &3); 371 | } 372 | 373 | #[test] 374 | fn create_spy_with_many_args_and_return_test() { 375 | let (spy_fn, spy) = spy!(|n, m| n + m); 376 | 377 | assert_eq!(spy_fn(2u8, 1u8), 3u8, "should return 3"); 378 | assert_eq!(spy_fn(3u8, 1u8), 4u8, "should return 4"); 379 | 380 | let snapshot = spy.snapshot(); 381 | 382 | assert!(snapshot.called(), "should be called"); 383 | assert!( 384 | snapshot.called_with((2u8, 1u8)), 385 | "should be called with 2 at least once" 386 | ); 387 | assert!( 388 | !snapshot.each_called_with((2u8, 1u8)), 389 | "should be called with different arguments" 390 | ); 391 | assert_eq!(snapshot.all_calls(), &vec![(2, 1), (3, 1)]); 392 | assert_eq!(snapshot.first_call().expect("should be Some"), &(2, 1)); 393 | assert_eq!(snapshot.last_call().expect("should be Some"), &(3, 1)); 394 | assert_eq!(snapshot.nth_call(1).expect("should be Some"), &(3, 1)); 395 | } 396 | 397 | #[test] 398 | fn create_spy_type_annotations() { 399 | let (spy_fn, spy) = spy!(|n: u8, m: u8| -> u8 { n + m }); 400 | 401 | assert_eq!(spy_fn(2, 1), 3, "should return 3"); 402 | assert_eq!(spy_fn(3, 1), 4, "should return 4"); 403 | 404 | let snapshot = spy.snapshot(); 405 | 406 | assert!(snapshot.called(), "should be called"); 407 | assert!( 408 | snapshot.called_with((2, 1)), 409 | "should be called with 2 at least once" 410 | ); 411 | assert!( 412 | !snapshot.each_called_with((2, 1)), 413 | "should be called with different arguments" 414 | ); 415 | assert_eq!(snapshot.all_calls(), &vec![(2, 1), (3, 1)]); 416 | assert_eq!(snapshot.first_call().expect("should be Some"), &(2, 1)); 417 | assert_eq!(snapshot.last_call().expect("should be Some"), &(3, 1)); 418 | assert_eq!(snapshot.nth_call(1).expect("should be Some"), &(3, 1)); 419 | } 420 | } 421 | --------------------------------------------------------------------------------