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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! **The crate stores the implementation of macros**
//!
//! Integration tests are outside in [`covers_it`](https://github.com/reanimatorzon/covers/tree/master/covers_it).
//!
//! @see [https://github.com/dtolnay/proc-macro-hack](https://github.com/dtolnay/proc-macro-hack)

use std::collections::HashMap;

use proc_macro::Delimiter::{Brace, Parenthesis};
use proc_macro::*;

use Stage::*;

#[cfg(all(feature = "__", feature = "_orig_"))]
panic!("only single prefix feature could be provided: '__' or '_orig_'. Note: '_' is default value");

#[cfg(all(not(feature = "__"), not(feature = "_orig_")))]
const ORIGINAL_FUNC_PREFIX: &str = "_";
#[cfg(feature = "__")]
const ORIGINAL_FUNC_PREFIX: &str = "__";
#[cfg(feature = "_orig_")]
const ORIGINAL_FUNC_PREFIX: &str = "_orig_";

#[derive(Clone, Copy)]
enum Stage {
    Start = 0,
    FnIdentFound = 1,
    FnNameFound = 2,
    FnArgsFound = 3,
    FnBodyFound = 4,
}

#[derive(Default)]
struct Params {
    reference: String,
    options: HashMap<String, String>,
}

/// Wraps the function below for calling another mock function
/// named according to the macro's argument when `#[cfg(debug_assertions)]`
/// enabled. Call original or mock function according to `#[cfg(test)]` flag.
///
/// Function signature should be the same as original: arguments, output.
///
/// In most cases you need to pass only the single required argument
/// fully-qualified reference to a mock function.
///
/// There only one exception when you need to hint
/// macro with `scope = impl` when you try to mock
/// static struct method (in `impl` block).
///
/// Usage
/// ======
/// ```
/// use covers::{mocked, mock};
///
/// #[mocked(mock_foo)]
/// fn foo(name: &str) -> String {
///     format!("Response: Foo = {}", name)
/// }
///
/// fn mock_foo(another_name: &str) -> String {
///     format!("Response: Mocked(Foo = {})", another_name)
/// }
///
/// #[mocked(module::mock_bar)]
/// fn bar(name: &str) -> String {
///     format!("Response: Bar = {}", name)
/// }
///
/// pub struct Struct {}
///
/// mod module {
///     use super::*;
///
///     #[mock]
///     pub fn mock_bar(name: &str) -> String {
///         let original_function_result = _bar(name);
///         format!("Response: Mocked({})", original_function_result)
///     }
///
///     pub fn yyy(this: Struct, name: &str) -> String {
///         format!("Response: Mocked({})", name)
///     }
/// }
///
/// impl Struct {
///     #[mocked(Struct::mock_baz, scope = impl)]
///     fn baz(name: &str) -> String {
///         format!("Response: Baz = {}", name)
///     }
///
///     fn mock_baz(name: &str) -> String {
///         format!("Response: Baz = {}", name)
///     }
///
///     #[mocked(module::yyy)]
///     fn xxx(self, name: &str) -> String {
///         format!("Response: Baz = {}", name)
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn mocked(args: TokenStream, input: TokenStream) -> TokenStream {
    if !(cfg!(debug_assertions) || cfg!(test)) {
        return input;
    }

    let args = parse_params(args);

    let mut stage = Start;

    let mut original = vec![];
    let mut signature = vec![];

    let mut fn_orig_name = String::new();
    let mut fn_args_string = String::new();

    // FIXME: dirty hack for 'Self::' prefix to functions inside 'impl' block.
    let mut is_impl_scope = false;

    for token in input {
        match &token {
            TokenTree::Ident(ident) if cmp(&stage, FnIdentFound) < 0 && ident.to_string() == "fn" => {
                stage = FnIdentFound;
                signature.push(token.clone());
                original.push(token);
            },
            TokenTree::Ident(ident) if cmp(&stage, FnIdentFound) == 0 => {
                stage = FnNameFound;

                signature.push(create_name_token("", ident));

                let new_token = create_name_token(ORIGINAL_FUNC_PREFIX, ident);
                fn_orig_name = new_token.to_string();
                original.push(new_token);
            },
            TokenTree::Group(group) if cmp(&stage, FnArgsFound) < 0 && group.delimiter() == Parenthesis => {
                stage = FnArgsFound;
                fn_args_string = parse_args(group);
                is_impl_scope = fn_args_string.starts_with("self,") || fn_args_string == "self";
                signature.push(token.clone());
                original.push(token);
            },
            TokenTree::Group(group) if cmp(&stage, FnBodyFound) < 0 && group.delimiter() == Brace => {
                stage = FnBodyFound;
                original.push(token);
            },
            _ => {
                if cmp(&stage, FnBodyFound) < 0 {
                    signature.push(token.clone());
                }
                original.push(token);
            },
        };
    }

    // FIXME: dirty hack for 'Self::' prefix to functions inside 'impl' block.
    is_impl_scope = is_impl_scope || args.options.get("scope").filter(|scope| *scope == "impl").is_some();

    let code = format!(
        r#"
        {fn_original}

        {signature} {{
            #[cfg(test)]
            return {fn_mock_name}{arguments};
            #[cfg(not(test))]
            return {fq}{fn_orig_name}{arguments};
        }}
        "#,
        fn_original = make_public(original.into_iter().collect())
            .into_iter()
            .collect::<TokenStream>(),
        fn_orig_name = fn_orig_name,
        fn_mock_name = args.reference,
        signature = signature.into_iter().collect::<TokenStream>(),
        arguments = format!("({})", fn_args_string),
        fq = if is_impl_scope { "Self::" } else { "" }
    );

    code.parse::<TokenStream>().unwrap().into_iter().collect()
}

/// Marks the following function to be built only for testing purposes
///
/// In other words it is prepended with `#[cfg(any(debug_assertions, test))]`.
///
/// * It is very useful to not compile mock functions for release.
/// * It makes function public - Can be disabled with `features = ["no-pub"]`
/// * It is **strictly** needed when we use reference to original logic of the
///   mocked function.
///
/// Example:
/// ```rust
/// #[mocked(mock_bar)]
/// fn bar(name: &str) -> String {
///     format!("Response: Bar = {}", name)
/// }
///
/// #[mock]
/// pub fn mock_bar(name: &str) -> String {
///     let original_function_result = _bar(name);
///     format!("Response: Mocked({})", original_function_result)
/// }
/// ```
#[proc_macro_attribute]
pub fn mock(_args: TokenStream, input: TokenStream) -> TokenStream {
    if cfg!(debug_assertions) || cfg!(test) {
        if cfg!(feature = "no-pub") {
            input
        } else {
            make_public(input)
        }
    } else {
        TokenStream::new()
    }
}

fn make_public(input: TokenStream) -> TokenStream {
    let mut result = vec![];
    let mut is_public = false;

    let mut iter = input.into_iter();
    while let Some(token) = iter.next() {
        match &token {
            TokenTree::Ident(ident) if ident.to_string() == "pub" => {
                is_public = true;
            },
            TokenTree::Ident(ident) if ident.to_string() == "fn" => {
                if !&is_public {
                    result.push(TokenTree::from(Ident::new("pub", ident.span())));
                }
                // push remaining
                result.push(token.to_owned());
                for token in iter {
                    result.push(token.to_owned());
                }
                break;
            },
            _ => (),
        }
        result.push(token.to_owned());
    }

    result.into_iter().collect()
}

fn parse_params(args: TokenStream) -> Params {
    let params = args.to_string();
    let mut params: Vec<&str> = params.split(',').map(|s| s.trim()).collect();
    assert!(
        !params.is_empty(),
        "At least fully-qualified reference to mock have to be provided!"
    );

    let mut response = Params::default();
    response.reference = params.remove(0).trim().to_string();
    for param in params {
        let entry: Vec<String> = param
            .split('=')
            .map(|s| s.trim().to_lowercase())
            .map(String::from)
            .collect();
        assert!(
            entry.len() == 2,
            "Extra parameters should be provided in `key = value` format!"
        );
        response.options.insert(entry[0].to_owned(), entry[1].to_owned());
    }
    response
}

fn create_name_token(prefix: &str, token: &Ident) -> TokenTree {
    TokenTree::from(Ident::new(&format!("{}{}", prefix, token.to_string()), token.span()))
}

fn parse_args(group: &Group) -> String {
    if group.stream().is_empty() {
        return "".to_string();
    }

    let mut vec = vec![];
    let mut args = vec![];

    for token in group.stream() {
        if let TokenTree::Punct(punct) = &token {
            if punct.to_string() == "," {
                args.push(parse_one_arg(&vec));
                vec.clear();
                continue;
            }
        }
        vec.push(token);
    }
    if !vec.is_empty() {
        args.push(parse_one_arg(&vec));
    }
    args.join(", ")
}

fn parse_one_arg(vec: &[TokenTree]) -> String {
    if vec.iter().last().unwrap().to_string() == "self" {
        "self".to_string()
    } else {
        vec[0].to_string()
    }
}

#[allow(clippy::clone_on_copy)]
fn cmp(current: &Stage, expected: Stage) -> i8 {
    (current.clone() as i8) - (expected as i8)
}