alef 0.82.0

Opinionated polyglot binding generator for Rust libraries
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
use super::*;

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

#[test]
fn test_render_python_fn_sig_basic() {
    let func = make_function(
        "convert",
        vec![make_param("source", TypeRef::String, false)],
        TypeRef::String,
        false,
        None,
    );
    let sig = render_python_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "def convert(source: str) -> str");
}

#[test]
fn test_render_python_fn_sig_async() {
    let func = make_function("fetch", vec![], TypeRef::String, true, None);
    let sig = render_python_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "def fetch() -> str");
}

#[test]
fn test_render_python_fn_sig_optional_param() {
    let func = make_function(
        "search",
        vec![
            make_param("query", TypeRef::String, false),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_python_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "def search(query: str, limit: int = None) -> list[str]");
}

#[test]
fn test_render_python_fn_sig_complex_return_type() {
    let func = make_function(
        "get_mapping",
        vec![],
        TypeRef::Map(
            Box::new(TypeRef::String),
            Box::new(TypeRef::Primitive(PrimitiveType::I32)),
        ),
        false,
        None,
    );
    let sig = render_python_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "def get_mapping() -> dict[str, int]");
}

#[test]
fn test_render_rust_fn_sig_basic() {
    let func = make_function(
        "convert",
        vec![make_ref_param("source", TypeRef::String, false)],
        TypeRef::String,
        false,
        None,
    );
    let sig = render_rust_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "pub fn convert(source: &str) -> String");
}

#[test]
fn test_render_rust_fn_sig_keeps_an_owned_string_param_owned() {
    let func = make_function(
        "convert",
        vec![make_param("source", TypeRef::String, false)],
        TypeRef::String,
        false,
        None,
    );
    let sig = render_rust_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "pub fn convert(source: String) -> String");
}

#[test]
fn test_render_rust_fn_sig_async() {
    let func = make_function("fetch", vec![], TypeRef::String, true, None);
    let sig = render_rust_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "pub async fn fetch() -> String");
}

#[test]
fn test_render_rust_fn_sig_optional_param() {
    let func = make_function(
        "search",
        vec![
            make_ref_param("query", TypeRef::String, false),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_rust_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "pub fn search(query: &str, limit: Option<u32>) -> Vec<String>");
}

#[test]
fn test_render_rust_fn_sig_error_type_with_return() {
    let func = make_function(
        "parse",
        vec![make_ref_param("source", TypeRef::String, false)],
        TypeRef::Named("Ast".to_string()),
        false,
        Some("ParseError"),
    );
    let sig = render_rust_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "pub fn parse(source: &str) -> Result<Ast, ParseError>");
}

#[test]
fn test_render_rust_fn_sig_error_type_unit_return() {
    let func = make_function("save", vec![], TypeRef::Unit, false, Some("IoError"));
    let sig = render_rust_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "pub fn save() -> Result<(), IoError>");
}

#[test]
fn test_render_go_fn_sig_basic() {
    let func = make_function(
        "convert",
        vec![make_param("source", TypeRef::String, false)],
        TypeRef::String,
        false,
        None,
    );
    let sig = render_go_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "func Convert(source string) string");
}

#[test]
fn test_render_go_fn_sig_async() {
    let func = make_function("fetch", vec![], TypeRef::String, true, None);
    let sig = render_go_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "func Fetch() string");
}

#[test]
fn test_render_go_fn_sig_optional_param() {
    let func = make_function(
        "search",
        vec![make_param("limit", TypeRef::Primitive(PrimitiveType::U32), false)],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_go_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "func Search(limit uint32) []string");
}

#[test]
fn test_render_go_fn_sig_error_type_with_return() {
    let func = make_function(
        "parse",
        vec![make_param("source", TypeRef::String, false)],
        TypeRef::Named("Ast".to_string()),
        false,
        Some("ParseError"),
    );
    let sig = render_go_fn_sig(&func, TEST_PREFIX);
    // ~keep A Named return is pointer-wrapped in real Go -- see signatures.rs's
    // `go_return_type` (backends/go/type_map.rs's `go_optional_type`).
    assert_eq!(sig, "func Parse(source string) (*Ast, error)");
}

#[test]
fn test_render_go_fn_sig_error_type_unit_return() {
    let func = make_function("save", vec![], TypeRef::Unit, false, Some("IoError"));
    let sig = render_go_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "func Save() error");
}

#[test]
fn test_render_go_fn_sig_named_return_is_pointer_wrapped_even_when_infallible() {
    // Verified from source, not inference: `gen_function_wrapper`
    // (backends/go/gen_bindings/functions.rs) pointer-wraps a Named return unconditionally,
    // not only when the function is fallible. ~keep
    let func = make_function(
        "current_session",
        vec![],
        TypeRef::Named("Session".to_string()),
        false,
        None,
    );
    let sig = render_go_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "func CurrentSession() *Session");
}

/// ~keep Every generated Java free function crosses the FFI boundary through
/// `emit_method_header` (`gen_bindings/ffi_class/sync_functions.rs`), which renders
/// `ffi_method_signature.jinja` -- `"...({{ params }}) throws {{ exception_class }} {"` --
/// unconditionally, with no `error_type`-gated branch. `exception_class` is
/// `format!("{}Exception", class_name)`, i.e. the crate's FFI-facade class name, never the
/// function's own (possibly absent) domain error type. See `method_signatures.rs`'s Java
/// throws tests for the instance-method half of the same contract.
#[test]
fn test_render_java_fn_sig_basic() {
    let func = make_function(
        "convert",
        vec![make_param("source", TypeRef::String, false)],
        TypeRef::String,
        false,
        None,
    );
    let sig = render_java_fn_sig(&func, TEST_PREFIX, TEST_CRATE_NAME);
    assert_eq!(sig, "public static String convert(String source) throws HtmRsException");
}

#[test]
fn test_render_java_fn_sig_async() {
    let func = make_function("fetch", vec![], TypeRef::String, true, None);
    let sig = render_java_fn_sig(&func, TEST_PREFIX, TEST_CRATE_NAME);
    assert_eq!(sig, "public static String fetch() throws HtmRsException");
}

#[test]
fn test_render_java_fn_sig_optional_param() {
    let func = make_function(
        "search",
        vec![make_param("limit", TypeRef::Primitive(PrimitiveType::U32), false)],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_java_fn_sig(&func, TEST_PREFIX, TEST_CRATE_NAME);
    assert_eq!(
        sig,
        "public static List<String> search(int limit) throws HtmRsException"
    );
}

/// ~keep A domain `error_type` must not leak into the throws clause -- see the analogous
/// `method_signatures.rs` note. The class name always comes from `ffi_prefix`, never from
/// `func.error_type`.
#[test]
fn test_render_java_fn_sig_error_type() {
    let func = make_function(
        "parse",
        vec![make_param("source", TypeRef::String, false)],
        TypeRef::Named("Ast".to_string()),
        false,
        Some("ParseError"),
    );
    let sig = render_java_fn_sig(&func, TEST_PREFIX, TEST_CRATE_NAME);
    assert_eq!(sig, "public static Ast parse(String source) throws HtmRsException");
}

/// ~keep Positive control in the other direction: an infallible function (`error_type: None`)
/// must still declare `throws` -- the FFI crossing can fail even when the wrapped Rust
/// function cannot. A fix that only adds throws when `error_type.is_some()` would still fail
/// 59 of the 77 real signatures the task describes.
#[test]
fn test_render_java_fn_sig_declares_throws_even_when_infallible() {
    let func = make_function("current_version", vec![], TypeRef::String, false, None);
    let sig = render_java_fn_sig(&func, TEST_PREFIX, TEST_CRATE_NAME);
    assert_eq!(sig, "public static String currentVersion() throws HtmRsException");
}

#[test]
fn test_render_csharp_fn_sig_basic() {
    let func = make_function(
        "convert",
        vec![make_param("source", TypeRef::String, false)],
        TypeRef::String,
        false,
        None,
    );
    let sig = render_csharp_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "public static string Convert(string source)");
}

#[test]
fn test_render_csharp_fn_sig_async() {
    let func = make_function("fetch", vec![], TypeRef::String, true, None);
    let sig = render_csharp_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "public static async Task<string> FetchAsync()");
}

#[test]
fn test_render_csharp_fn_sig_optional_param() {
    let func = make_function(
        "search",
        vec![
            make_param("query", TypeRef::String, false),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_csharp_fn_sig(&func, TEST_PREFIX);
    assert_eq!(
        sig,
        "public static List<string> Search(string query, uint? limit = null)"
    );
}

#[test]
fn test_render_csharp_fn_sig_complex_return_type() {
    let func = make_function(
        "get_mapping",
        vec![],
        TypeRef::Map(
            Box::new(TypeRef::String),
            Box::new(TypeRef::Primitive(PrimitiveType::I32)),
        ),
        false,
        None,
    );
    let sig = render_csharp_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "public static Dictionary<string, int> GetMapping()");
}

#[test]
fn test_param_list_python_optional_uses_none_default() {
    let func = make_function(
        "run",
        vec![
            make_param("input", TypeRef::String, false),
            make_param("config", TypeRef::Named("Config".to_string()), true),
        ],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_python_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "def run(input: str, config: Config = None) -> None");
}

#[test]
fn test_param_list_node_optional_uses_question_mark() {
    let func = make_function(
        "run",
        vec![
            make_param("input", TypeRef::String, false),
            make_param("config", TypeRef::Named("Config".to_string()), true),
        ],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_typescript_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "function run(input: string, config?: Config): void");
}

#[test]
fn test_param_list_go_no_optional_syntax() {
    let func = make_function(
        "run",
        vec![make_param("input", TypeRef::String, false)],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_go_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "func Run(input string)");
}

#[test]
fn test_param_list_rust_string_params_use_refs() {
    let func = make_function(
        "process",
        vec![
            make_ref_param("name", TypeRef::String, false),
            make_ref_param("initial", TypeRef::Char, false),
            make_ref_param("data", TypeRef::Bytes, false),
        ],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_rust_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "pub fn process(name: &str, initial: &str, data: &[u8])");
}

#[test]
fn test_param_list_php_uses_dollar_prefix() {
    let func = make_function(
        "search",
        vec![
            make_param("query", TypeRef::String, false),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_php_fn_sig(&func, TEST_PREFIX);
    assert_eq!(
        sig,
        "public static function search(string $query, ?int $limit = null): array<string>"
    );
}

#[test]
fn test_render_kotlin_fn_sig_no_error_no_return() {
    let func = make_function(
        "run",
        vec![make_param("input", TypeRef::String, false)],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_kotlin_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "fun run(input: String)");
}

#[test]
fn test_render_kotlin_fn_sig_with_optional_and_return() {
    let func = make_function(
        "search",
        vec![
            make_param("query", TypeRef::String, false),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_kotlin_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "fun search(query: String, limit: Int? = null): List<String>");
}

#[test]
fn test_render_kotlin_fn_sig_with_error_emits_throws_annotation() {
    let func = make_function(
        "convert",
        vec![make_param("html", TypeRef::String, false)],
        TypeRef::String,
        false,
        Some("ConversionError"),
    );
    let sig = render_kotlin_fn_sig(&func, TEST_PREFIX);
    assert_eq!(
        sig,
        "@Throws(ConversionError::class)\nfun convert(html: String): String"
    );
}

#[test]
fn test_render_swift_fn_sig_no_error_no_return() {
    let func = make_function(
        "run",
        vec![make_param("input", TypeRef::String, false)],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_swift_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "public static func run(input: String)");
}

#[test]
fn test_render_swift_fn_sig_with_optional_param_emits_nil_default() {
    let func = make_function(
        "search",
        vec![
            make_param("query", TypeRef::String, false),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_swift_fn_sig(&func, TEST_PREFIX);
    assert_eq!(
        sig,
        "public static func search(query: String, limit: UInt32? = nil) -> [String]"
    );
}

#[test]
fn test_render_swift_fn_sig_with_error_emits_throws() {
    let func = make_function(
        "convert",
        vec![make_param("html", TypeRef::String, false)],
        TypeRef::String,
        false,
        Some("ConversionError"),
    );
    let sig = render_swift_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "public static func convert(html: String) throws -> String");
}

/// ~keep Every generated Dart free function is `Future<T>` (`Future<void>` for a `Unit`
/// return), never a bare `T` -- `emit_function`'s return-type branch
/// (`gen_bindings/functions.rs`) is `if matches!(f.return_type, TypeRef::Unit) {
/// "Future<void>" } else { format!("Future<{}>", ...) }`, with no `is_async` check: this is
/// unconditional because flutter_rust_bridge dispatches every non-`#[frb(sync)]` call across
/// the FFI boundary asynchronously, regardless of whether the wrapped Rust function is itself
/// `async fn`. `func.is_async` describes the *Rust* core signature and is the wrong oracle
/// here, same class of defect as Java's `error_type`-gated throws.
#[test]
fn test_render_dart_fn_sig_required_only() {
    let func = make_function(
        "run",
        vec![make_param("input", TypeRef::String, false)],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_dart_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "Future<void> run(String input)");
}

/// ~keep Real Dart optional parameters are grouped in curly braces (`{int? limit}`, Dart's
/// named-optional syntax), not square brackets (positional-optional) -- `emit_function`'s
/// `params_str` match arm (`gen_bindings/functions.rs`) renders
/// `format!("{}, {{{}}}", required.join(", "), optional.join(", "))` for the mixed case.
/// `[int? limit]` is syntactically different Dart (positional-optional) from `{int? limit}`
/// (named-optional) -- a caller following the bracketed doc could not call the named form.
#[test]
fn test_render_dart_fn_sig_optional_param_uses_named_braces() {
    let func = make_function(
        "search",
        vec![
            make_param("query", TypeRef::String, false),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_dart_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "Future<List<String>> search(String query, {int? limit})");
}

/// ~keep Guards the "all-optional" shape of the same real-backend match: when every parameter
/// is optional, the whole list is wrapped in one `{}` group with no leading required params
/// or comma -- `(true, false) => format!("{{{}}}", optional.join(", "))`.
#[test]
fn test_render_dart_fn_sig_all_optional_params_use_single_named_braces_group() {
    let func = make_function(
        "search",
        vec![
            make_param("query", TypeRef::String, true),
            make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true),
        ],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        None,
    );
    let sig = render_dart_fn_sig(&func, TEST_PREFIX);
    assert_eq!(sig, "Future<List<String>> search({String? query, int? limit})");
}

#[test]
fn test_render_zig_fn_sig_no_error() {
    let func = make_function(
        "search",
        vec![make_param("query", TypeRef::String, false)],
        TypeRef::Primitive(PrimitiveType::U32),
        false,
        None,
    );
    let sig = render_zig_fn_sig(&func, TEST_PREFIX, &ApiSurface::default());
    assert_eq!(sig, "pub fn search(query: []const u8) u32");
}

#[test]
fn test_render_zig_fn_sig_with_error_emits_error_union() {
    let func = make_function(
        "convert",
        vec![make_param("html", TypeRef::String, false)],
        TypeRef::String,
        false,
        Some("ConversionError"),
    );
    let sig = render_zig_fn_sig(&func, TEST_PREFIX, &ApiSurface::default());
    assert_eq!(sig, "pub fn convert(html: []const u8) ConversionError![]u8");
}

#[test]
fn test_render_zig_fn_sig_optional_param_prefixes_question_mark() {
    let func = make_function(
        "search",
        vec![make_param("limit", TypeRef::Primitive(PrimitiveType::U32), true)],
        TypeRef::Unit,
        false,
        None,
    );
    let sig = render_zig_fn_sig(&func, TEST_PREFIX, &ApiSurface::default());
    assert_eq!(sig, "pub fn search(limit: ?u32) void");
}

/// ~keep A `Named` parameter/return referring to a non-opaque DTO does not cross the Zig
/// wrapper boundary as its struct type -- `zig_param_type`/`zig_return_type`
/// (backends/zig/gen_bindings/functions.rs) serialise it to JSON-encoded `[]const u8`/`[]u8`
/// instead, because the FFI only ever hands a struct across as a scalar opaque handle
/// constructed from JSON. Before `zig_boundary_param_type`/`zig_boundary_return_type` existed,
/// `render_zig_fn_sig` asked the same generic `doc_type` every other language uses, which has
/// no notion of opacity and always spelled a `Named` type by its Rust name -- documenting an
/// API `alef build` never emits.
#[test]
fn test_render_zig_fn_sig_named_struct_param_and_return_serialize_to_bytes() {
    let func = make_function(
        "normalize",
        vec![make_param("options", TypeRef::Named("ParseOptions".to_string()), false)],
        TypeRef::Named("ParseOutput".to_string()),
        false,
        None,
    );
    let api = ApiSurface {
        types: vec![
            crate::core::ir::TypeDef {
                name: "ParseOptions".to_string(),
                is_opaque: false,
                has_serde: true,
                ..Default::default()
            },
            crate::core::ir::TypeDef {
                name: "ParseOutput".to_string(),
                is_opaque: false,
                has_serde: true,
                ..Default::default()
            },
        ],
        ..Default::default()
    };
    let sig = render_zig_fn_sig(&func, TEST_PREFIX, &api);
    assert_eq!(sig, "pub fn normalize(options: []const u8) []u8");
}

/// ~keep Positive control for the test above: an opaque handle type keeps its real type name
/// at the wrapper boundary -- only a non-opaque struct DTO serializes to bytes. Distinguishes
/// "every `Named` type becomes bytes" (wrong, would make this test fail the same way) from the
/// actual rule, which is opacity-gated.
#[test]
fn test_render_zig_fn_sig_named_opaque_return_keeps_its_type_name() {
    let func = make_function(
        "current_session",
        vec![],
        TypeRef::Named("Session".to_string()),
        false,
        None,
    );
    let api = ApiSurface {
        types: vec![crate::core::ir::TypeDef {
            name: "Session".to_string(),
            is_opaque: true,
            ..Default::default()
        }],
        ..Default::default()
    };
    let sig = render_zig_fn_sig(&func, TEST_PREFIX, &api);
    assert_eq!(sig, "pub fn current_session() Session");
}

#[test]
fn test_render_method_signature_kotlin_static_emits_jvmstatic() {
    let method = make_method(
        "default",
        vec![],
        TypeRef::Named("ParseOptions".into()),
        false,
        true,
        None,
    );
    let sig = render_method_signature(&method, "ParseOptions", Language::Kotlin, TEST_PREFIX);
    assert_eq!(sig, "@JvmStatic\nfun default(): ParseOptions");
}

#[test]
fn test_render_method_signature_swift_instance_with_throws() {
    let method = make_method(
        "apply_update",
        vec![make_param("update", TypeRef::Named("ParseOptionsUpdate".into()), false)],
        TypeRef::Unit,
        false,
        false,
        Some("ConversionError"),
    );
    let sig = render_method_signature(&method, "ParseOptions", Language::Swift, TEST_PREFIX);
    assert_eq!(sig, "public func applyUpdate(update: ParseOptionsUpdate) throws");
}

/// ~keep Same unconditional `Future<T>` wrap as `render_dart_fn_sig` -- see that test's note.
/// flutter_rust_bridge dispatches instance methods across the FFI boundary the same way it
/// dispatches free functions, so this is not gated on `method.is_async` either.
#[test]
fn test_render_method_signature_dart_instance_method() {
    let method = make_method(
        "classify_link",
        vec![make_param("href", TypeRef::String, false)],
        TypeRef::Named("LinkType".into()),
        false,
        false,
        None,
    );
    let sig = render_method_signature(&method, "LinkMetadata", Language::Dart, TEST_PREFIX);
    assert_eq!(sig, "Future<LinkType> classifyLink(String href)");
}

/// ~keep Positive control: the `Future<>` wrap is not tied to `is_static` either -- a static
/// Dart factory still crosses the same async FFI dispatch. Distinguishes this defect class
/// (uniform-wrong-in-one-direction, like Java's throws) from the receiver defect class
/// (Elixir/Go), where `is_static` is exactly the gate that must flip the rendering.
#[test]
fn test_render_method_signature_dart_static_method_still_wraps_future() {
    let method = make_method(
        "create",
        vec![make_param("name", TypeRef::String, false)],
        TypeRef::Named("Document".to_string()),
        false,
        true,
        None,
    );
    let sig = render_method_signature(&method, "Document", Language::Dart, TEST_PREFIX);
    assert_eq!(sig, "static Future<Document> create(String name)");
}

#[test]
fn test_render_method_signature_zig_instance_includes_self_receiver() {
    let method = make_method(
        "warnings",
        vec![],
        TypeRef::Vec(Box::new(TypeRef::String)),
        false,
        false,
        None,
    );
    let sig = render_method_signature(&method, "ParseOutput", Language::Zig, TEST_PREFIX);
    assert_eq!(sig, "pub fn warnings(self: *const ParseOutput) []u8");
}

/// ~keep Omitting `self` was only half of it. The Zig backend emits a static method as a
/// *top-level* function named `{method_snake}_{type_snake}`
/// (`emit_opaque_static_method` -> `opaque_static_signature.jinja`), so the bare
/// `pub fn create()` this test used to pin named a symbol that occurs nowhere in the emitted
/// module -- and, because Zig methods only exist on opaque handles at all, there is no second
/// shape it could have been describing.
#[test]
fn test_render_method_signature_zig_static_omits_self_and_suffixes_the_type() {
    let method = make_method(
        "create",
        vec![],
        TypeRef::Named("ParseOptions".into()),
        false,
        true,
        None,
    );
    let sig = render_method_signature(&method, "ParseOptions", Language::Zig, TEST_PREFIX);
    assert_eq!(sig, "pub fn create_parse_options() ParseOptions");
}

#[test]
fn test_render_method_signature_kotlin_android_shares_kotlin_renderer() {
    let method = make_method(
        "convert",
        vec![make_param("html", TypeRef::String, false)],
        TypeRef::String,
        false,
        true,
        Some("ConversionError"),
    );
    let sig = render_method_signature(&method, "Converter", Language::KotlinAndroid, TEST_PREFIX);
    assert_eq!(
        sig,
        "@Throws(ConversionError::class)\n@JvmStatic\nfun convert(html: String): String"
    );
}