| 1 |
use std::collections::{HashMap, HashSet}; |
| 2 |
use quote::format_ident; |
| 3 |
use syn::{FnArg, Ident, Pat}; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
refident::{IntoPat, MaybeIdent, MaybePatType, MaybePatTypeMut}, |
| 7 |
resolver::pat_invert_mutability, |
| 8 |
}; |
| 9 |
|
| 10 |
#[derive(PartialEq, Debug, Clone, Copy)] |
| 11 |
#[allow(dead_code)] |
| 12 |
#[derive(Default)] |
| 13 |
pub(crate) enum FutureArg { |
| 14 |
#[default] |
| 15 |
None, |
| 16 |
Define, |
| 17 |
Await, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Clone, PartialEq, Default, Debug)] |
| 21 |
pub(crate) struct ArgumentInfo { |
| 22 |
future: FutureArg, |
| 23 |
by_ref: bool, |
| 24 |
ignore: bool, |
| 25 |
inner_pat: Option<Pat>, // Optional pat used to inject data and call test function |
| 26 |
} |
| 27 |
|
| 28 |
impl ArgumentInfo { |
| 29 |
fn future(future: FutureArg) -> Self { |
| 30 |
Self { |
| 31 |
future, |
| 32 |
..Default::default() |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
fn by_ref() -> Self { |
| 37 |
Self { |
| 38 |
by_ref: true, |
| 39 |
..Default::default() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
fn ignore() -> Self { |
| 44 |
Self { |
| 45 |
ignore: true, |
| 46 |
..Default::default() |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
fn inner_pat(pat: Pat) -> Self { |
| 51 |
Self { |
| 52 |
inner_pat: Some(pat), |
| 53 |
..Default::default() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
fn is_future(&self) -> bool { |
| 58 |
use FutureArg::*; |
| 59 |
|
| 60 |
matches!(self.future, Define | Await) |
| 61 |
} |
| 62 |
|
| 63 |
fn is_future_await(&self) -> bool { |
| 64 |
use FutureArg::*; |
| 65 |
|
| 66 |
matches!(self.future, Await) |
| 67 |
} |
| 68 |
|
| 69 |
fn is_by_ref(&self) -> bool { |
| 70 |
self.by_ref |
| 71 |
} |
| 72 |
|
| 73 |
fn is_ignore(&self) -> bool { |
| 74 |
self.ignore |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[derive(Clone, PartialEq, Default, Debug)] |
| 79 |
struct Args { |
| 80 |
args: HashMap<Pat, ArgumentInfo>, |
| 81 |
} |
| 82 |
|
| 83 |
impl Args { |
| 84 |
fn get(&self, pat: &Pat) -> Option<&ArgumentInfo> { |
| 85 |
self.args |
| 86 |
.get(pat) |
| 87 |
.or_else(|| self.args.get(&pat_invert_mutability(pat))) |
| 88 |
} |
| 89 |
|
| 90 |
fn entry(&mut self, pat: Pat) -> std::collections::hash_map::Entry<'_, Pat, ArgumentInfo> { |
| 91 |
self.args.entry(pat) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[derive(Clone, PartialEq, Debug)] |
| 96 |
pub enum TestAttr { |
| 97 |
InAttrs, |
| 98 |
Explicit(Box<syn::Attribute>), |
| 99 |
} |
| 100 |
|
| 101 |
impl From<syn::Attribute> for TestAttr { |
| 102 |
fn from(attr: syn::Attribute) -> Self { |
| 103 |
TestAttr::Explicit(attr.into()) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[derive(Clone, PartialEq, Default, Debug)] |
| 108 |
pub(crate) struct ArgumentsInfo { |
| 109 |
args: Args, |
| 110 |
is_global_await: bool, |
| 111 |
once: Option<syn::Attribute>, |
| 112 |
contexts: HashSet<Pat>, |
| 113 |
test_attr: Option<TestAttr>, |
| 114 |
} |
| 115 |
|
| 116 |
impl ArgumentsInfo { |
| 117 |
pub(crate) fn test_attr(&self) -> Option<&TestAttr> { |
| 118 |
self.test_attr.as_ref() |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl ArgumentsInfo { |
| 123 |
pub(crate) fn set_future(&mut self, pat: Pat, kind: FutureArg) { |
| 124 |
self.args |
| 125 |
.entry(pat) |
| 126 |
.and_modify(|v| v.future = kind) |
| 127 |
.or_insert_with(|| ArgumentInfo::future(kind)); |
| 128 |
} |
| 129 |
|
| 130 |
pub(crate) fn set_futures(&mut self, futures: impl Iterator<Item = (Pat, FutureArg)>) { |
| 131 |
futures.for_each(|(pat, k)| self.set_future(pat, k)); |
| 132 |
} |
| 133 |
|
| 134 |
pub(crate) fn set_global_await(&mut self, is_global_await: bool) { |
| 135 |
self.is_global_await = is_global_await; |
| 136 |
} |
| 137 |
|
| 138 |
pub(crate) fn set_test_attr(&mut self, test_attr: Option<TestAttr>) { |
| 139 |
self.test_attr = test_attr; |
| 140 |
} |
| 141 |
|
| 142 |
#[allow(dead_code)] |
| 143 |
pub(crate) fn add_future(&mut self, pat: Pat) { |
| 144 |
self.set_future(pat, FutureArg::Define); |
| 145 |
} |
| 146 |
|
| 147 |
pub(crate) fn is_future(&self, pat: &Pat) -> bool { |
| 148 |
self.args |
| 149 |
.get(pat) |
| 150 |
.map(|arg| arg.is_future()) |
| 151 |
.unwrap_or_default() |
| 152 |
} |
| 153 |
|
| 154 |
pub(crate) fn is_future_await(&self, pat: &Pat) -> bool { |
| 155 |
match self.args.get(pat) { |
| 156 |
Some(arg) => arg.is_future_await() || (arg.is_future() && self.is_global_await()), |
| 157 |
None => false, |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
pub(crate) fn is_global_await(&self) -> bool { |
| 162 |
self.is_global_await |
| 163 |
} |
| 164 |
|
| 165 |
pub(crate) fn set_once(&mut self, once: Option<syn::Attribute>) { |
| 166 |
self.once = once |
| 167 |
} |
| 168 |
|
| 169 |
pub(crate) fn get_once(&self) -> Option<&syn::Attribute> { |
| 170 |
self.once.as_ref() |
| 171 |
} |
| 172 |
|
| 173 |
pub(crate) fn is_once(&self) -> bool { |
| 174 |
self.get_once().is_some() |
| 175 |
} |
| 176 |
|
| 177 |
pub(crate) fn set_by_ref(&mut self, pat: Pat) { |
| 178 |
self.args |
| 179 |
.entry(pat) |
| 180 |
.and_modify(|v| v.by_ref = true) |
| 181 |
.or_insert_with(ArgumentInfo::by_ref); |
| 182 |
} |
| 183 |
|
| 184 |
pub(crate) fn set_ignore(&mut self, pat: Pat) { |
| 185 |
self.args |
| 186 |
.entry(pat) |
| 187 |
.and_modify(|v| v.ignore = true) |
| 188 |
.or_insert_with(ArgumentInfo::ignore); |
| 189 |
} |
| 190 |
|
| 191 |
pub(crate) fn set_by_refs(&mut self, by_refs: impl Iterator<Item = Pat>) { |
| 192 |
by_refs.for_each(|pat| self.set_by_ref(pat)); |
| 193 |
} |
| 194 |
|
| 195 |
pub(crate) fn set_ignores(&mut self, ignores: impl Iterator<Item = Pat>) { |
| 196 |
ignores.for_each(|pat| self.set_ignore(pat)); |
| 197 |
} |
| 198 |
|
| 199 |
pub(crate) fn is_by_refs(&self, id: &Pat) -> bool { |
| 200 |
self.args |
| 201 |
.get(id) |
| 202 |
.map(|arg| arg.is_by_ref()) |
| 203 |
.unwrap_or_default() |
| 204 |
} |
| 205 |
|
| 206 |
pub(crate) fn is_ignore(&self, pat: &Pat) -> bool { |
| 207 |
self.args |
| 208 |
.get(pat) |
| 209 |
.map(|arg| arg.is_ignore()) |
| 210 |
.unwrap_or_default() |
| 211 |
} |
| 212 |
|
| 213 |
pub(crate) fn set_inner_pat(&mut self, pat: Pat, inner: Pat) { |
| 214 |
self.args |
| 215 |
.entry(pat) |
| 216 |
.and_modify(|v| v.inner_pat = Some(inner.clone())) |
| 217 |
.or_insert_with(|| ArgumentInfo::inner_pat(inner)); |
| 218 |
} |
| 219 |
|
| 220 |
pub(crate) fn set_inner_ident(&mut self, pat: Pat, ident: Ident) { |
| 221 |
self.set_inner_pat(pat, ident.into_pat()); |
| 222 |
} |
| 223 |
|
| 224 |
pub(crate) fn inner_pat<'arguments: 'pat_ref, 'pat_ref>( |
| 225 |
&'arguments self, |
| 226 |
id: &'pat_ref Pat, |
| 227 |
) -> &'pat_ref Pat { |
| 228 |
self.args |
| 229 |
.get(id) |
| 230 |
.and_then(|arg| arg.inner_pat.as_ref()) |
| 231 |
.unwrap_or(id) |
| 232 |
} |
| 233 |
|
| 234 |
pub(crate) fn register_inner_destructored_idents_names(&mut self, item_fn: &syn::ItemFn) { |
| 235 |
let mut anonymous_destruct = 0_usize; |
| 236 |
// On the signature we remove all destruct arguments and replace them with `__destruct_{id}` |
| 237 |
// This is just to define the new arguments and local variable that we use in the test |
| 238 |
// and coll the original signature that should preserve the destruct arguments. |
| 239 |
for arg in item_fn.sig.inputs.iter() { |
| 240 |
if let Some(pt) = arg.maybe_pat_type() { |
| 241 |
if pt.maybe_ident().is_none() { |
| 242 |
anonymous_destruct += 1; |
| 243 |
let ident = format_ident!("__destruct_{}", anonymous_destruct); |
| 244 |
self.set_inner_ident(pt.pat.as_ref().clone(), ident); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
pub(crate) fn replace_fn_args_with_related_inner_pat<'a>( |
| 251 |
&'a self, |
| 252 |
fn_args: impl Iterator<Item = FnArg> + 'a, |
| 253 |
) -> impl Iterator<Item = FnArg> + 'a { |
| 254 |
fn_args.map(|mut fn_arg| { |
| 255 |
if let Some(p) = fn_arg.maybe_pat_type_mut() { |
| 256 |
*p.pat = self.inner_pat(&p.pat).clone(); |
| 257 |
} |
| 258 |
fn_arg |
| 259 |
}) |
| 260 |
} |
| 261 |
|
| 262 |
#[allow(dead_code)] |
| 263 |
pub(crate) fn add_context(&mut self, pat: Pat) { |
| 264 |
self.contexts.insert(pat); |
| 265 |
} |
| 266 |
|
| 267 |
pub(crate) fn set_contexts(&mut self, contexts: impl Iterator<Item = Pat>) { |
| 268 |
contexts.for_each(|c| self.add_context(c)) |
| 269 |
} |
| 270 |
|
| 271 |
pub(crate) fn contexts(&self) -> impl Iterator<Item = &Pat> + '_ { |
| 272 |
self.contexts.iter() |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
#[cfg(test)] |
| 277 |
mod should_implement_is_future_await_logic { |
| 278 |
use super::*; |
| 279 |
use crate::test::*; |
| 280 |
|
| 281 |
#[fixture] |
| 282 |
fn info() -> ArgumentsInfo { |
| 283 |
let mut a = ArgumentsInfo::default(); |
| 284 |
a.set_future(pat("simple"), FutureArg::Define); |
| 285 |
a.set_future(pat("other_simple"), FutureArg::Define); |
| 286 |
a.set_future(pat("awaited"), FutureArg::Await); |
| 287 |
a.set_future(pat("other_awaited"), FutureArg::Await); |
| 288 |
a.set_future(pat("none"), FutureArg::None); |
| 289 |
a |
| 290 |
} |
| 291 |
|
| 292 |
#[rstest] |
| 293 |
fn no_matching_ident(info: ArgumentsInfo) { |
| 294 |
assert!(!info.is_future_await(&pat("some"))); |
| 295 |
assert!(!info.is_future_await(&pat("simple"))); |
| 296 |
assert!(!info.is_future_await(&pat("none"))); |
| 297 |
} |
| 298 |
|
| 299 |
#[rstest] |
| 300 |
fn matching_ident(info: ArgumentsInfo) { |
| 301 |
assert!(info.is_future_await(&pat("awaited"))); |
| 302 |
assert!(info.is_future_await(&pat("other_awaited"))); |
| 303 |
} |
| 304 |
|
| 305 |
#[rstest] |
| 306 |
fn global_matching_future_ident(mut info: ArgumentsInfo) { |
| 307 |
info.set_global_await(true); |
| 308 |
assert!(info.is_future_await(&pat("simple"))); |
| 309 |
assert!(info.is_future_await(&pat("other_simple"))); |
| 310 |
assert!(info.is_future_await(&pat("awaited"))); |
| 311 |
|
| 312 |
assert!(!info.is_future_await(&pat("some"))); |
| 313 |
assert!(!info.is_future_await(&pat("none"))); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
#[cfg(test)] |
| 318 |
mod should_register_inner_destructored_idents_names { |
| 319 |
use super::*; |
| 320 |
use crate::test::{assert_eq, *}; |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn implement_the_correct_pat_reolver() { |
| 324 |
let item_fn = "fn test_function(A(a,b): A, (c,d,e): (u32, u32, u32), none: u32, B{s,d} : B, clean: C) {}".ast(); |
| 325 |
|
| 326 |
let mut arguments = ArgumentsInfo::default(); |
| 327 |
|
| 328 |
arguments.register_inner_destructored_idents_names(&item_fn); |
| 329 |
|
| 330 |
assert_eq!(arguments.inner_pat(&pat("A(a,b)")), &pat("__destruct_1")); |
| 331 |
assert_eq!(arguments.inner_pat(&pat("(c,d,e)")), &pat("__destruct_2")); |
| 332 |
assert_eq!(arguments.inner_pat(&pat("none")), &pat("none")); |
| 333 |
assert_eq!(arguments.inner_pat(&pat("B{s,d}")), &pat("__destruct_3")); |
| 334 |
assert_eq!(arguments.inner_pat(&pat("clean")), &pat("clean")); |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
fn and_replace_them_correctly() { |
| 339 |
let item_fn = "fn test_function(A(a,b): A, (c,d,e): (u32, u32, u32), none: u32, B{s,d} : B, clean: C) {}".ast(); |
| 340 |
|
| 341 |
let mut arguments = ArgumentsInfo::default(); |
| 342 |
|
| 343 |
arguments.register_inner_destructored_idents_names(&item_fn); |
| 344 |
|
| 345 |
let new_args = arguments |
| 346 |
.replace_fn_args_with_related_inner_pat(item_fn.sig.inputs.into_iter()) |
| 347 |
.filter_map(|f| f.maybe_ident().cloned()) |
| 348 |
.map(|id| id.to_string()) |
| 349 |
.collect::<Vec<_>>() |
| 350 |
.join(" | "); |
| 351 |
|
| 352 |
assert_eq!( |
| 353 |
new_args, |
| 354 |
"__destruct_1 | __destruct_2 | none | __destruct_3 | clean" |
| 355 |
); |
| 356 |
} |
| 357 |
} |