ferridriver-test-macros 0.1.0

Proc macros for the ferridriver E2E test framework
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Proc macros for the ferridriver test framework.
//!
//! Provides `#[ferritest]` to register async browser test functions
//! with automatic fixture injection based on parameter types.
//!
//! ```ignore
//! use ferridriver_test::prelude::*;
//!
//! #[ferritest]
//! async fn basic_navigation(page: Page) {
//!     page.goto("https://example.com", None).await.unwrap();
//!     expect(&page).to_have_title("Example").await.unwrap();
//! }
//!
//! #[ferritest(retries = 2, timeout = "30s", tag = "smoke")]
//! async fn flaky_test(page: Page, context: BrowserContext) {
//!     // ...
//! }
//! ```

use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{Expr, FnArg, ItemFn, Lit, Meta, Pat, Token, Type, parse_macro_input};

/// Attribute arguments: `#[ferritest(retries = 2, timeout = "30s", tag = "smoke")]`
struct FerritestArgs {
  retries: Option<u32>,
  timeout_ms: Option<u64>,
  tags: Vec<String>,
  /// None = not set, Some(None) = unconditional, Some(Some("firefox")) = conditional
  skip: Option<Option<String>>,
  /// None = not set, Some(None) = unconditional, Some(Some("ci")) = conditional
  slow: Option<Option<String>>,
  /// None = not set, Some(None) = unconditional, Some(Some("linux")) = conditional
  fixme: Option<Option<String>>,
  /// None = not set, Some(None) = unconditional, Some(Some("webkit")) = conditional
  fail: Option<Option<String>>,
  only: bool,
  /// Structured metadata annotations: `info = "type:description"`.
  infos: Vec<(String, String)>,
  /// Raw JSON string for fixture/context overrides (viewport, locale, etc.)
  use_options: Option<String>,
}

impl Parse for FerritestArgs {
  fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
    let mut args = Self {
      retries: None,
      timeout_ms: None,
      tags: Vec::new(),
      skip: None,
      slow: None,
      fixme: None,
      fail: None,
      only: false,
      infos: Vec::new(),
      use_options: None,
    };

    let metas = Punctuated::<Meta, Token![,]>::parse_terminated(input)?;
    for meta in metas {
      match &meta {
        Meta::NameValue(nv) => {
          let ident = nv.path.get_ident().map(ToString::to_string).unwrap_or_default();
          match ident.as_str() {
            "retries" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Int(i) = &lit.lit {
                  args.retries = Some(i.base10_parse()?);
                }
              }
            },
            "timeout" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  args.timeout_ms = Some(parse_duration_str(&s.value())?);
                }
              }
            },
            "tag" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  args.tags.push(s.value());
                }
              }
            },
            "skip" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  args.skip = Some(Some(s.value()));
                }
              }
            },
            "slow" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  args.slow = Some(Some(s.value()));
                }
              }
            },
            "fixme" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  args.fixme = Some(Some(s.value()));
                }
              }
            },
            "fail" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  args.fail = Some(Some(s.value()));
                }
              }
            },
            "use_options" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  args.use_options = Some(s.value());
                }
              }
            },
            "info" => {
              if let syn::Expr::Lit(lit) = &nv.value {
                if let Lit::Str(s) = &lit.lit {
                  let val = s.value();
                  if let Some((type_name, desc)) = val.split_once(':') {
                    args.infos.push((type_name.trim().to_string(), desc.trim().to_string()));
                  } else {
                    args.infos.push((val, String::new()));
                  }
                }
              }
            },
            _ => {
              return Err(syn::Error::new_spanned(
                &nv.path,
                format!("unknown ferritest attribute: {ident}"),
              ));
            },
          }
        },
        Meta::Path(p) => {
          let ident = p.get_ident().map(ToString::to_string).unwrap_or_default();
          match ident.as_str() {
            "skip" => args.skip = Some(None),
            "slow" => args.slow = Some(None),
            "fixme" => args.fixme = Some(None),
            "fail" => args.fail = Some(None),
            "only" => args.only = true,
            _ => return Err(syn::Error::new_spanned(p, format!("unknown ferritest flag: {ident}"))),
          }
        },
        Meta::List(_) => {
          return Err(syn::Error::new_spanned(&meta, "unexpected nested attribute"));
        },
      }
    }
    Ok(args)
  }
}

fn parse_duration_str(s: &str) -> syn::Result<u64> {
  let s = s.trim();
  if let Some(secs) = s.strip_suffix('s') {
    secs
      .trim()
      .parse::<u64>()
      .map(|v| v * 1000)
      .map_err(|e| syn::Error::new(proc_macro2::Span::call_site(), format!("invalid timeout: {e}")))
  } else if let Some(ms) = s.strip_suffix("ms") {
    ms.trim()
      .parse::<u64>()
      .map_err(|e| syn::Error::new(proc_macro2::Span::call_site(), format!("invalid timeout: {e}")))
  } else {
    s.parse::<u64>().map_err(|e| {
      syn::Error::new(
        proc_macro2::Span::call_site(),
        format!("invalid timeout (use '30s' or '5000ms'): {e}"),
      )
    })
  }
}

/// `#[ferritest]` attribute macro.
///
/// Transforms an async function into a registered test case with automatic
/// fixture injection based on parameter types.
#[proc_macro_attribute]
pub fn ferritest(attr: TokenStream, item: TokenStream) -> TokenStream {
  let args = parse_macro_input!(attr as FerritestArgs);
  let input = parse_macro_input!(item as ItemFn);

  let fn_name = &input.sig.ident;
  let fn_name_str = fn_name.to_string();
  let vis = &input.vis;
  let block = &input.block;
  let attrs = &input.attrs;

  // The function receives a TestContext. Extract the parameter name the user chose
  // (e.g., `ctx`, `context`, `t`, etc.)
  let ctx_param_name = if let Some(FnArg::Typed(pt)) = input.sig.inputs.first() {
    if let Pat::Ident(pi) = pt.pat.as_ref() {
      pi.ident.clone()
    } else {
      format_ident!("ctx")
    }
  } else {
    format_ident!("ctx")
  };

  // Rust tests resolve built-in fixtures lazily via TestContext getters.
  let fixture_names: Vec<String> = Vec::new();
  let fixture_array = fixture_names.iter().map(|f| quote! { #f });

  // Build annotations.
  // Helper: parse "condition" or "condition | reason" into (condition, reason) tokens.
  fn annotation_tokens(variant: &str, arg: &Option<Option<String>>, annotations: &mut Vec<proc_macro2::TokenStream>) {
    let variant_ident = quote::format_ident!("{}", variant);
    match arg {
      Some(None) => {
        annotations
          .push(quote! { ferridriver_test::model::TestAnnotation::#variant_ident { reason: None, condition: None } });
      },
      Some(Some(val)) => {
        // Support "condition | reason" format.
        if let Some((cond, reason)) = val.split_once('|') {
          let cond = cond.trim();
          let reason = reason.trim();
          annotations.push(quote! { ferridriver_test::model::TestAnnotation::#variant_ident {
            reason: Some(#reason.to_string()),
            condition: Some(#cond.to_string()),
          } });
        } else {
          annotations.push(quote! { ferridriver_test::model::TestAnnotation::#variant_ident {
            reason: None,
            condition: Some(#val.to_string()),
          } });
        }
      },
      None => {},
    }
  }

  let mut annotations = Vec::new();
  annotation_tokens("Skip", &args.skip, &mut annotations);
  annotation_tokens("Slow", &args.slow, &mut annotations);
  annotation_tokens("Fixme", &args.fixme, &mut annotations);
  annotation_tokens("Fail", &args.fail, &mut annotations);
  if args.only {
    annotations.push(quote! { ferridriver_test::model::TestAnnotation::Only });
  }
  for tag in &args.tags {
    annotations.push(quote! { ferridriver_test::model::TestAnnotation::Tag(#tag.to_string()) });
  }
  for (type_name, desc) in &args.infos {
    annotations.push(
      quote! { ferridriver_test::model::TestAnnotation::Info { type_name: #type_name.to_string(), description: #desc.to_string() } },
    );
  }

  let retries_expr = match args.retries {
    Some(r) => quote! { Some(#r) },
    None => quote! { None },
  };
  let timeout_ms_expr = match args.timeout_ms {
    Some(ms) => quote! { Some(#ms) },
    None => quote! { None },
  };
  let use_options_expr = match &args.use_options {
    Some(json) => quote! { Some(#json) },
    None => quote! { None },
  };

  let expanded = quote! {
    #(#attrs)*
    #vis async fn #fn_name(__pool: ferridriver_test::fixture::FixturePool) -> Result<(), ferridriver_test::model::TestFailure> {
      let #ctx_param_name = ferridriver_test::TestContext::new(__pool);
      #block
      Ok(())
    }

    inventory::submit! {
      ferridriver_test::discovery::TestRegistration {
        file: file!(),
        module_path: module_path!(),
        name: #fn_name_str,
        fixture_requests: &[#(#fixture_array),*],
        annotations: &[#(#annotations),*],
        timeout_ms: #timeout_ms_expr,
        retries: #retries_expr,
        use_options: #use_options_expr,
        test_fn: |pool| Box::pin(#fn_name(pool)),
      }
    }
  };

  expanded.into()
}

/// Arguments for `#[ferritest_each]`: `data = [(...), (...)]`.
struct FerritestEachArgs {
  data: Vec<Vec<Expr>>,
}

impl Parse for FerritestEachArgs {
  fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
    // Parse: data = [(...), (...)]
    let ident: syn::Ident = input.parse()?;
    if ident != "data" {
      return Err(syn::Error::new_spanned(&ident, "expected `data = [...]`"));
    }
    let _: Token![=] = input.parse()?;

    let content;
    syn::bracketed!(content in input);

    let mut data = Vec::new();
    while !content.is_empty() {
      let inner;
      syn::parenthesized!(inner in content);
      let exprs: Punctuated<Expr, Token![,]> = Punctuated::parse_terminated(&inner)?;
      data.push(exprs.into_iter().collect());

      if content.peek(Token![,]) {
        let _: Token![,] = content.parse()?;
      }
    }

    Ok(Self { data })
  }
}

/// `#[ferritest_each(data = [("a", 1), ("b", 2)])]` — parameterized test macro.
///
/// Expands a single async test function into N registered tests, one per data row.
/// First parameter is `FixturePool`, remaining parameters receive the data values.
///
/// ```ignore
/// #[ferritest_each(data = [("admin", "admin@example.com"), ("guest", "guest@example.com")])]
/// async fn login(pool: FixturePool, role: &str, email: &str) {
///     let page = pool.page().await.unwrap();
///     page.goto(&format!("/login?role={role}"), None).await.unwrap();
/// }
/// ```
/// Registers: `login (admin, admin@example.com)` and `login (guest, guest@example.com)`.
#[proc_macro_attribute]
pub fn ferritest_each(attr: TokenStream, item: TokenStream) -> TokenStream {
  let args = parse_macro_input!(attr as FerritestEachArgs);
  let input = parse_macro_input!(item as ItemFn);

  let fn_name = &input.sig.ident;
  let fn_name_str = fn_name.to_string();
  let block = &input.block;
  let attrs = &input.attrs;

  // First param is TestContext, rest are data params.
  let all_params: Vec<_> = input.sig.inputs.iter().collect();
  let ctx_param_name = if let Some(FnArg::Typed(pt)) = all_params.first() {
    if let Pat::Ident(pi) = pt.pat.as_ref() {
      pi.ident.clone()
    } else {
      format_ident!("ctx")
    }
  } else {
    format_ident!("ctx")
  };

  let data_params: Vec<(&syn::Ident, &Type)> = all_params
    .iter()
    .skip(1) // skip FixturePool
    .filter_map(|arg| {
      if let FnArg::Typed(pat_type) = arg {
        if let Pat::Ident(pat_ident) = pat_type.pat.as_ref() {
          return Some((&pat_ident.ident, &*pat_type.ty));
        }
      }
      None
    })
    .collect();

  let fixture_names: Vec<String> = Vec::new();

  // Generate one inventory::submit! per data row.
  let mut submissions = Vec::new();
  for (row_idx, row) in args.data.iter().enumerate() {
    if row.len() != data_params.len() {
      return syn::Error::new_spanned(
        &input.sig.ident,
        format!(
          "data row {} has {} values but function expects {} data parameters",
          row_idx,
          row.len(),
          data_params.len()
        ),
      )
      .to_compile_error()
      .into();
    }

    // Build name suffix: "(val1, val2)"
    let row_values_str: Vec<String> = row.iter().map(|e| quote!(#e).to_string().replace('"', "")).collect();
    let suffix = row_values_str.join(", ");
    let test_name = format!("{fn_name_str} ({suffix})");

    // Build let bindings for data params.
    let data_bindings: Vec<_> = data_params
      .iter()
      .zip(row.iter())
      .map(|((param_name, param_type), value)| {
        quote! { let #param_name: #param_type = #value; }
      })
      .collect();

    let inner_fn_name = format_ident!("__ferritest_each_{}_{}", fn_name, row_idx);
    let fixture_array = fixture_names.iter().map(|f| quote! { #f });
    let ctx_param = ctx_param_name.clone();

    submissions.push(quote! {
      async fn #inner_fn_name(__pool: ferridriver_test::fixture::FixturePool) -> Result<(), ferridriver_test::model::TestFailure> {
        let #ctx_param = ferridriver_test::TestContext::new(__pool);
        #(#data_bindings)*
        #block
        Ok(())
      }

      inventory::submit! {
        ferridriver_test::discovery::TestRegistration {
          file: file!(),
          module_path: module_path!(),
          name: #test_name,
          fixture_requests: &[#(#fixture_array),*],
          annotations: &[],
          timeout_ms: None,
          retries: None,
          test_fn: |pool| Box::pin(#inner_fn_name(pool)),
        }
      }
    });
  }

  let expanded = quote! {
    #(#attrs)*
    #(#submissions)*
  };

  expanded.into()
}

// ── Hook macros ──

/// Shared implementation for all four hook macros.
fn hook_impl(kind_tag: &str, is_suite_hook: bool, item: TokenStream) -> TokenStream {
  let input = parse_macro_input!(item as ItemFn);
  let fn_name = &input.sig.ident;
  let vis = &input.vis;
  let block = &input.block;
  let attrs = &input.attrs;

  let kind_ident = format_ident!("{}", kind_tag);

  // Extract parameter name for TestContext.
  let ctx_param_name = if let Some(FnArg::Typed(pt)) = input.sig.inputs.first() {
    if let Pat::Ident(pi) = pt.pat.as_ref() {
      pi.ident.clone()
    } else {
      format_ident!("ctx")
    }
  } else {
    format_ident!("ctx")
  };

  if is_suite_hook {
    // before_all / after_all: fn(FixturePool) -> Result
    let expanded = quote! {
      #(#attrs)*
      #vis fn #fn_name(__pool: ferridriver_test::fixture::FixturePool)
        -> ::std::pin::Pin<Box<dyn ::std::future::Future<Output = Result<(), ferridriver_test::model::TestFailure>> + Send>>
      {
        Box::pin(async move {
          let #ctx_param_name = ferridriver_test::TestContext::new(__pool);
          #block
          Ok(())
        })
      }

      inventory::submit! {
        ferridriver_test::discovery::HookRegistration {
          module_path: module_path!(),
          suite_hook_fn: Some(#fn_name),
          each_hook_fn: None,
          kind: ferridriver_test::discovery::HookKindTag::#kind_ident,
        }
      }
    };
    expanded.into()
  } else {
    // before_each / after_each: fn(FixturePool, Arc<TestInfo>) -> Result
    let expanded = quote! {
      #(#attrs)*
      #vis fn #fn_name(
        __pool: ferridriver_test::fixture::FixturePool,
        __info: ::std::sync::Arc<ferridriver_test::model::TestInfo>,
      ) -> ::std::pin::Pin<Box<dyn ::std::future::Future<Output = Result<(), ferridriver_test::model::TestFailure>> + Send>>
      {
        Box::pin(async move {
          let #ctx_param_name = ferridriver_test::TestContext::new(__pool);
          #block
          Ok(())
        })
      }

      inventory::submit! {
        ferridriver_test::discovery::HookRegistration {
          module_path: module_path!(),
          suite_hook_fn: None,
          each_hook_fn: Some(#fn_name),
          kind: ferridriver_test::discovery::HookKindTag::#kind_ident,
        }
      }
    };
    expanded.into()
  }
}

/// Runs once before all tests in the containing module (suite).
///
/// ```ignore
/// mod my_suite {
///     use ferridriver_test::prelude::*;
///
///     #[before_all]
///     async fn setup(ctx: TestContext) {
///         // seed database, etc.
///     }
///
///     #[ferritest]
///     async fn test_one(ctx: TestContext) { ... }
/// }
/// ```
#[proc_macro_attribute]
pub fn before_all(_attr: TokenStream, item: TokenStream) -> TokenStream {
  hook_impl("BeforeAll", true, item)
}

/// Runs once after all tests in the containing module (suite).
#[proc_macro_attribute]
pub fn after_all(_attr: TokenStream, item: TokenStream) -> TokenStream {
  hook_impl("AfterAll", true, item)
}

/// Runs before each test in the containing module (suite).
///
/// ```ignore
/// mod my_suite {
///     use ferridriver_test::prelude::*;
///
///     #[before_each]
///     async fn login(ctx: TestContext) {
///         let page = ctx.page().await?;
///         page.goto("/login", None).await?;
///     }
///
///     #[ferritest]
///     async fn dashboard_test(ctx: TestContext) { ... }
/// }
/// ```
#[proc_macro_attribute]
pub fn before_each(_attr: TokenStream, item: TokenStream) -> TokenStream {
  hook_impl("BeforeEach", false, item)
}

/// Runs after each test in the containing module (suite), even on failure.
#[proc_macro_attribute]
pub fn after_each(_attr: TokenStream, item: TokenStream) -> TokenStream {
  hook_impl("AfterEach", false, item)
}