| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#![cfg_attr(feature = "nightly", feature(proc_macro_diagnostic))] |
| 11 |
#![cfg_attr(test, deny(warnings))] |
| 12 |
extern crate proc_macro; |
| 13 |
|
| 14 |
use cfg_if::cfg_if; |
| 15 |
use proc_macro2::{Span, TokenStream}; |
| 16 |
use quote::{format_ident, quote}; |
| 17 |
use syn::{ |
| 18 |
*, |
| 19 |
spanned::Spanned |
| 20 |
}; |
| 21 |
|
| 22 |
cfg_if! { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
if #[cfg(all(feature = "nightly", not(test)))] { |
| 27 |
fn compile_error(span: Span, msg: &'static str) { |
| 28 |
span.unstable() |
| 29 |
.error(msg) |
| 30 |
.emit(); |
| 31 |
} |
| 32 |
} else { |
| 33 |
fn compile_error(_span: Span, msg: &str) { |
| 34 |
panic!("{msg}. More information may be available when mockall_double is built with the \"nightly\" feature."); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
fn do_double(_attrs: TokenStream, input: TokenStream) -> TokenStream { |
| 40 |
let mut item: Item = match parse2(input.clone()) { |
| 41 |
Ok(u) => u, |
| 42 |
Err(e) => return e.to_compile_error() |
| 43 |
}; |
| 44 |
match &mut item { |
| 45 |
Item::Use(use_stmt) => mock_itemuse(use_stmt), |
| 46 |
Item::Type(item_type) => mock_itemtype(item_type), |
| 47 |
_ => { |
| 48 |
compile_error(item.span(), |
| 49 |
"Only use statements and type aliases may be doubled"); |
| 50 |
} |
| 51 |
}; |
| 52 |
quote!( |
| 53 |
#[cfg(not(test))] |
| 54 |
#input |
| 55 |
#[cfg(test)] |
| 56 |
#item |
| 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 |
#[proc_macro_attribute] |
| 157 |
pub fn double(attrs: proc_macro::TokenStream, input: proc_macro::TokenStream) |
| 158 |
-> proc_macro::TokenStream |
| 159 |
{ |
| 160 |
do_double(attrs.into(), input.into()).into() |
| 161 |
} |
| 162 |
|
| 163 |
fn mock_itemtype(orig: &mut ItemType) { |
| 164 |
match &mut *orig.ty { |
| 165 |
Type::Path(tp) => { |
| 166 |
let ident = &tp.path.segments.last_mut().unwrap().ident; |
| 167 |
tp.path.segments.last_mut().unwrap().ident = mock_ident(ident); |
| 168 |
} |
| 169 |
x => compile_error(x.span(), "Only path types may be doubled") |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
fn mock_itemuse(orig: &mut ItemUse) { |
| 174 |
if let UseTree::Name(un) = &orig.tree { |
| 175 |
compile_error(un.span(), |
| 176 |
"Cannot double types in the current module. Use a submodule (use foo::Foo) or a rename (use Foo as Bar)"); |
| 177 |
} else { |
| 178 |
mock_usetree(&mut orig.tree) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
fn mock_ident(i: &Ident) -> Ident { |
| 183 |
let is_type = format!("{i}") |
| 184 |
.chars() |
| 185 |
.next() |
| 186 |
.expect("zero-length ident?") |
| 187 |
.is_uppercase(); |
| 188 |
if is_type { |
| 189 |
|
| 190 |
format_ident!("Mock{}", i) |
| 191 |
} else { |
| 192 |
|
| 193 |
format_ident!("mock_{}", i) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
fn mock_usetree(mut orig: &mut UseTree) { |
| 198 |
match &mut orig { |
| 199 |
UseTree::Glob(star) => { |
| 200 |
compile_error(star.span(), |
| 201 |
"Cannot double glob imports. Import by fully qualified name instead."); |
| 202 |
}, |
| 203 |
UseTree::Group(ug) => { |
| 204 |
for ut in ug.items.iter_mut() { |
| 205 |
mock_usetree(ut); |
| 206 |
} |
| 207 |
}, |
| 208 |
UseTree::Name(un) => { |
| 209 |
*orig = UseTree::Rename(UseRename { |
| 210 |
ident: mock_ident(&un.ident), |
| 211 |
as_token: <Token![as]>::default(), |
| 212 |
rename: un.ident.clone() |
| 213 |
}); |
| 214 |
}, |
| 215 |
UseTree::Path(up) => { |
| 216 |
mock_usetree(up.tree.as_mut()); |
| 217 |
}, |
| 218 |
UseTree::Rename(ur) => { |
| 219 |
ur.ident = mock_ident(&ur.ident) |
| 220 |
}, |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[cfg(test)] |
| 225 |
mod t { |
| 226 |
use super::*; |
| 227 |
|
| 228 |
mod double { |
| 229 |
use super::*; |
| 230 |
use std::str::FromStr; |
| 231 |
|
| 232 |
fn cmp(attrs: &str, code: &str, expected: &str) { |
| 233 |
let attrs_ts = TokenStream::from_str(attrs).unwrap(); |
| 234 |
let code_ts = TokenStream::from_str(code).unwrap(); |
| 235 |
let output = do_double(attrs_ts, code_ts); |
| 236 |
let output = output.to_string(); |
| 237 |
|
| 238 |
|
| 239 |
let expected = TokenStream::from_str(expected) |
| 240 |
.unwrap() |
| 241 |
.to_string(); |
| 242 |
assert_eq!(output, expected); |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
#[should_panic(expected = "Cannot double glob")] |
| 247 |
fn glob() { |
| 248 |
let code = "use foo::*;"; |
| 249 |
cmp("", code, ""); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn group() { |
| 254 |
let code = " |
| 255 |
use foo::bar::{ |
| 256 |
Baz, |
| 257 |
Bean |
| 258 |
}; |
| 259 |
"; |
| 260 |
let expected = " |
| 261 |
#[cfg(not(test))] |
| 262 |
use foo::bar::{ |
| 263 |
Baz, |
| 264 |
Bean |
| 265 |
}; |
| 266 |
#[cfg(test)] |
| 267 |
use foo::bar::{ |
| 268 |
MockBaz as Baz, |
| 269 |
MockBean as Bean |
| 270 |
}; |
| 271 |
"; |
| 272 |
cmp("", code, expected); |
| 273 |
} |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn module() { |
| 277 |
let code = "use foo::bar;"; |
| 278 |
let expected = " |
| 279 |
#[cfg(not(test))] |
| 280 |
use foo::bar; |
| 281 |
#[cfg(test)] |
| 282 |
use foo::mock_bar as bar; |
| 283 |
"; |
| 284 |
cmp("", code, expected); |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
#[should_panic(expected = "Cannot double types in the current module")] |
| 289 |
fn name() { |
| 290 |
let code = "use Foo;"; |
| 291 |
cmp("", code, ""); |
| 292 |
} |
| 293 |
|
| 294 |
#[test] |
| 295 |
fn path() { |
| 296 |
let code = "use foo::bar::Baz;"; |
| 297 |
let expected = " |
| 298 |
#[cfg(not(test))] |
| 299 |
use foo::bar::Baz; |
| 300 |
#[cfg(test)] |
| 301 |
use foo::bar::MockBaz as Baz; |
| 302 |
"; |
| 303 |
cmp("", code, expected); |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn pub_use() { |
| 308 |
let code = "pub use foo::bar;"; |
| 309 |
let expected = " |
| 310 |
#[cfg(not(test))] |
| 311 |
pub use foo::bar; |
| 312 |
#[cfg(test)] |
| 313 |
pub use foo::mock_bar as bar; |
| 314 |
"; |
| 315 |
cmp("", code, expected); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn rename() { |
| 320 |
let code = "use Foo as Bar;"; |
| 321 |
let expected = " |
| 322 |
#[cfg(not(test))] |
| 323 |
use Foo as Bar; |
| 324 |
#[cfg(test)] |
| 325 |
use MockFoo as Bar; |
| 326 |
"; |
| 327 |
cmp("", code, expected); |
| 328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn type_() { |
| 332 |
let code = "type Foo = bar::Baz;"; |
| 333 |
let expected = " |
| 334 |
#[cfg(not(test))] |
| 335 |
type Foo = bar::Baz; |
| 336 |
#[cfg(test)] |
| 337 |
type Foo = bar::MockBaz; |
| 338 |
"; |
| 339 |
cmp("", code, expected); |
| 340 |
} |
| 341 |
|
| 342 |
#[test] |
| 343 |
#[should_panic(expected = "Only use statements and type aliases")] |
| 344 |
fn undoubleable() { |
| 345 |
let code = "struct Foo{}"; |
| 346 |
cmp("", code, ""); |
| 347 |
} |
| 348 |
} |
| 349 |
} |