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
/* Copyright 2017 Christopher Bacher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#![feature(proc_macro)]
#![recursion_limit = "128"]

#[macro_use] mod acquire;
mod new_mock;
mod given;
mod expect;
mod generate;
mod data;

extern crate proc_macro;
#[macro_use] extern crate lazy_static;

extern crate syn;
#[macro_use] extern crate synom;
#[macro_use] extern crate quote;

#[cfg(test)]#[macro_use]
extern crate galvanic_assert;

use proc_macro::TokenStream;

use new_mock::handle_new_mock;
use given::handle_given;
use expect::handle_expect_interactions;
use generate::handle_generate_mocks;
use data::*;

use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;


enum MockedTraitLocation {
    TraitDef(syn::Path),
    Referred(syn::Path)
}

named!(parse_trait_path -> MockedTraitLocation,
    delimited!(
        punct!("("),
        do_parse!(
            external: option!(alt!(keyword!("intern") | keyword!("extern"))) >> path: call!(syn::parse::path) >>
            (match external {
                Some(..) => MockedTraitLocation::Referred(path),
                None => MockedTraitLocation::TraitDef(path)
            })
        ),
        punct!(")")
    )
);

#[proc_macro_attribute]
pub fn mockable(args: TokenStream, input: TokenStream) -> TokenStream {
    let s = input.to_string();
    let trait_item = syn::parse_item(&s).expect("Expecting a trait definition.");

    let args_str = &args.to_string();

    match trait_item.node {
        syn::ItemKind::Trait(safety, generics, bounds, items) => {
            let mut mockable_traits = acquire!(MOCKABLE_TRAITS);

            if args_str.is_empty() {
                mockable_traits.insert(trait_item.ident.clone().into(), TraitInfo::new(safety, generics, bounds, items));
                return input;
            }

            let trait_location = parse_trait_path(args_str)
                                 .expect(concat!("#[mockable(..)] requires the absolute path of the trait's module.",
                                                 "It must be preceded with `extern`/`intern` if the trait is defined in another crate/module"));
            match trait_location {
                MockedTraitLocation::TraitDef(mut trait_path) => {
                    trait_path.segments.push(trait_item.ident.clone().into());
                    mockable_traits.insert(trait_path, TraitInfo::new(safety, generics, bounds, items));
                    input
                },
                MockedTraitLocation::Referred(mut trait_path) => {
                    trait_path.segments.push(trait_item.ident.clone().into());
                    mockable_traits.insert(trait_path, TraitInfo::new(safety, generics, bounds, items));
                    "".parse().unwrap()
                }
            }
        },
        _ => panic!("Expecting a trait definition.")
    }
}


#[proc_macro_attribute]
pub fn use_mocks(_: TokenStream, input: TokenStream) -> TokenStream {
    use MacroInvocationPos::*;

    // to parse the macros related to mock ussage the function is converted to string form
    let mut reassembled = String::new();
    let parsed = syn::parse_item(&input.to_string()).unwrap();
    let mut remainder = quote!(#parsed).to_string();

    // parse one macro a time then search for the next macro in the remaining string
    let mut absolute_pos = 0;
    while !remainder.is_empty() {

        match find_next_mock_macro_invocation(&remainder) {
            None => {
                reassembled.push_str(&remainder);
                remainder = String::new();
            },
            Some(invocation) => {
                let (left, new_absolute_pos, right) = match invocation {
                    NewMock(pos) => handle_macro(&remainder, pos, absolute_pos, handle_new_mock),
                    Given(pos) => handle_macro(&remainder, pos, absolute_pos, handle_given),
                    ExpectInteractions(pos) => handle_macro(&remainder, pos, absolute_pos, handle_expect_interactions),
                };

                absolute_pos = new_absolute_pos;
                reassembled.push_str(&left);
                remainder = right;
            }
        }
    }

    // once all macro invocations have been removed from the string (and replaced with the actual mock code) it can be parsed back into a function item
    let fn_ = syn::parse_item(&reassembled).expect("Reassembled function whi");
    let fn_ident = &fn_.ident;
    let fn_vis = &fn_.vis;
    let mod_fn = syn::Ident::from(format!("mod_{}", fn_ident));

    let mocks = handle_generate_mocks();

    let generated_mock = (quote! {
        #[allow(unused_imports)]
        #fn_vis use self::#mod_fn::#fn_ident;
        mod #mod_fn {
            #![allow(dead_code)]
            #![allow(unused_imports)]
            #![allow(unused_variables)]
            use super::*;
            use std;

            #fn_

            #(#mocks)*
        }
    }).to_string();

    if let Some((_, path)) = env::vars().find(|&(ref key, _)| key == "GA_WRITE_MOCK") {
        if path.is_empty() {
            println!("{}", generated_mock);
        } else {
            let success = File::create(Path::new(&path).join(&(fn_ident.to_string())))
                               .and_then(|mut f| f.write_all(generated_mock.as_bytes()));
            if let Err(err) = success {
                eprintln!("Unable to write generated mock: {}", err);
            }
        }
    }
    generated_mock.parse().unwrap()
}


fn has_balanced_quotes(source: &str)  -> bool {
    let mut count = 0;
    let mut skip = false;
    for c in source.chars() {
        if skip {
            skip = false;
            continue;
        }

        if c == '\\' {
            skip = true;
        } else if c == '\"' {
            count += 1;
        }
        //TODO handle raw strings
    }
    count % 2 == 0
}

/// Stores position of a macro invocation with the variant naming the macro.
enum MacroInvocationPos {
    NewMock(usize),
    Given(usize),
    ExpectInteractions(usize),
}

/// Find the next galvanic-mock macro invocation in the source string.
///
/// Looks for `new_mock!``, `given!`, `expect_interactions!`, and `then_verify_interactions!`.
/// The `source` string must have been reassembled from a `TokenTree`.
/// The `source` string is expected to start in a code context, i.e., not inside
/// a string.
fn find_next_mock_macro_invocation(source: &str) -> Option<MacroInvocationPos> {
    use MacroInvocationPos::*;
    // there must be a space between the macro name and the ! as the ! is a separate token in the tree
    let macro_names = ["new_mock !", "given !", "expect_interactions !"];
    // not efficient but does the job
    macro_names.into_iter()
               .filter_map(|&mac| {
                            source.find(mac).and_then(|pos| {
                                if has_balanced_quotes(&source[.. pos]) {
                                    Some((pos, mac))
                                } else { None }
                            })
               })
               .min_by_key(|&(pos, _)| pos)
               .and_then(|(pos, mac)| Some(match mac {
                   "new_mock !" => NewMock(pos),
                   "given !" => Given(pos),
                   "expect_interactions !" => ExpectInteractions(pos),
                   _ => panic!("Unreachable. No variant for macro name: {}", mac)
                }))
}

fn handle_macro<F>(source: &str, mac_pos_relative_to_source: usize, absolute_pos_of_source: usize, handler: F) -> (String, usize, String)
where F: Fn(&str, usize) -> (String, String) {
    let absolute_pos_of_mac = absolute_pos_of_source + mac_pos_relative_to_source;

    let (left_of_mac, right_with_mac) = source.split_at(mac_pos_relative_to_source);
    let (mut generated_source, unhandled_source) = handler(right_with_mac, absolute_pos_of_mac);
    generated_source.push_str(&unhandled_source);

    (left_of_mac.to_string(), absolute_pos_of_mac, generated_source)
}


#[cfg(test)]
mod test_has_balanced_quotes {
    use super::*;

    #[test]
    fn should_have_balanced_quotes_if_none_exist() {
        let x = "df df df";
        assert!(has_balanced_quotes(x));
    }

    #[test]
    fn should_have_balanced_quotes_if_single_pair() {
        let x = "df \"df\" df";
        assert!(has_balanced_quotes(x));
    }

    #[test]
    fn should_have_balanced_quotes_if_single_pair_with_escapes() {
        let x = "df \"d\\\"f\" df";
        assert!(has_balanced_quotes(x));
    }

    #[test]
    fn should_have_balanced_quotes_if_multiple_pairs() {
        let x = "df \"df\" \"df\" df";
        assert!(has_balanced_quotes(x));
    }

    #[test]
    fn should_not_have_balanced_quotes_if_single() {
        let x = "df \"df df";
        assert!(!has_balanced_quotes(x));
    }

    #[test]
    fn should_not_have_balanced_quotes_if_escaped_pair() {
        let x = "df \"d\\\" df";
        assert!(!has_balanced_quotes(x));
    }
}