alef 0.81.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
use super::*;
use crate::core::config::Language;

#[test]
fn ffi_error_code_variant_names_include_the_canonical_error_type() {
    assert_eq!(
        ffi_error_code_variant_name("sample::RequestError", "Unavailable"),
        "SampleRequestErrorUnavailable"
    );
    assert_ne!(
        ffi_error_code_variant_name("sample::RequestError", "Unavailable"),
        ffi_error_code_variant_name("sample::StorageError", "Unavailable")
    );
}

#[test]
fn ffi_error_code_variant_names_do_not_repeat_error_at_the_boundary() {
    assert_eq!(
        ffi_error_code_variant_name("sample::ParserError", "ErrorLanguageNotFound"),
        "SampleParserErrorLanguageNotFound"
    );
}

#[test]
fn ffi_error_code_variant_names_collapse_repeats_inside_the_type_path() {
    let cases = [
        // A module named after its error type is the common stutter source.
        (("sample::error::Error", "NotFound"), "SampleErrorNotFound"),
        (("sample_lib::error::Error", "NotFound"), "SampleLibErrorNotFound"),
        // Case differences alone do not make two consecutive words distinct.
        (("sample::Error::Error", "NotFound"), "SampleErrorNotFound"),
        // A repeat that is not adjacent carries meaning and is preserved.
        (("sample::error::Error", "QueryError"), "SampleErrorQueryError"),
        (("sample::error::codec::Error", "Decode"), "SampleErrorCodecErrorDecode"),
        // Paths without a repeat are untouched.
        (("sample::RequestError", "Unavailable"), "SampleRequestErrorUnavailable"),
    ];
    for ((error_type, variant), expected) in cases {
        assert_eq!(
            ffi_error_code_variant_name(error_type, variant),
            expected,
            "{error_type}::{variant}"
        );
    }
}

#[test]
fn ffi_error_code_variant_names_stay_distinct_across_sibling_error_types() {
    // Path collapsing must not erase the type that distinguishes two taxonomies.
    assert_ne!(
        ffi_error_code_variant_name("sample::error::Error", "NotFound"),
        ffi_error_code_variant_name("sample::storage::Error", "NotFound")
    );
}

#[test]
fn eliding_error_at_the_boundary_can_alias_two_variants_of_one_type() {
    // Documented limitation, unchanged from the previous implementation: a type
    // carrying both `ErrorFoo` and `Foo` folds onto a single name. C rejects the
    // duplicate enumerator at compile time, so this surfaces loudly rather than
    // silently mapping a code incorrectly. Deliberately not "fixed" by dropping the elision,
    // which would reintroduce the `ErrorError` stutter this pass removes. ~keep
    assert_eq!(
        ffi_error_code_variant_name("sample::error::Error", "ErrorFoo"),
        ffi_error_code_variant_name("sample::error::Error", "Foo")
    );
}

#[test]
fn ffi_builtin_error_code_prefix_namespaces_members_per_project() {
    assert_eq!(ffi_builtin_error_code_prefix("sample"), "SampleAlef");
    assert_eq!(ffi_builtin_error_code_prefix("ts_pack"), "TsPackAlef");
    assert_ne!(
        ffi_builtin_error_code_prefix("sample"),
        ffi_builtin_error_code_prefix("other")
    );
}

#[test]
fn serde_wire_name_applies_rename_all_strategies() {
    let cases = [
        ("HttpStatus", Some("lowercase"), "httpstatus"),
        ("HttpStatus", Some("UPPERCASE"), "HTTPSTATUS"),
        ("http_status", Some("PascalCase"), "HttpStatus"),
        ("http_status", Some("camelCase"), "httpStatus"),
        ("HttpStatus", Some("snake_case"), "http_status"),
        ("HttpStatus", Some("SCREAMING_SNAKE_CASE"), "HTTP_STATUS"),
        ("HttpStatus", Some("kebab-case"), "http-status"),
        ("HttpStatus", Some("SCREAMING-KEBAB-CASE"), "HTTP-STATUS"),
        ("HttpStatus", None, "HttpStatus"),
        ("HttpStatus", Some("unknown"), "HttpStatus"),
        ("Rdfa", Some("snake_case"), "rdfa"),
        ("HTMLParser", Some("snake_case"), "html_parser"),
    ];

    for (name, rename_all, expected) in cases {
        assert_eq!(
            apply_serde_rename_all(name, rename_all),
            expected,
            "rename_all={rename_all:?} name={name}"
        );
    }
}

#[test]
fn serde_rename_wins_over_rename_all() {
    assert_eq!(
        serde_wire_name("content_type", Some("Content-Type"), Some("camelCase")),
        "Content-Type"
    );
    assert_eq!(
        wire_variant_value("HttpStatus", Some("http-status"), Some("snake_case")),
        "http-status"
    );
    assert_eq!(
        wire_variant_value("HttpStatus", None, Some("snake_case")),
        "http_status"
    );
}

// `EnumDef::rename_all_fields` cases the FIELD names of a struct-shaped variant's payload; it is
// a distinct serde namespace from `EnumDef::serde_rename_all`, which cases VARIANT names. A call
// site computing a struct-variant field's wire name must resolve it through `wire_field_name`
// with the field's own `serde_rename` and the enum's `rename_all_fields` -- never the enum's
// `serde_rename_all`, which is the exact confusion this precedence chain guards against. ~keep
#[test]
fn struct_variant_field_wire_name_prefers_field_rename_over_enum_rename_all_fields() {
    let field = crate::core::ir::FieldDef {
        name: "inner_radius".to_string(),
        serde_rename: Some("radius".to_string()),
        ..crate::core::ir::FieldDef::default()
    };
    let enum_def = crate::core::ir::EnumDef {
        rename_all_fields: Some("camelCase".to_string()),
        ..crate::core::ir::EnumDef::default()
    };

    assert_eq!(
        wire_field_name(
            &field.name,
            field.serde_rename.as_deref(),
            enum_def.rename_all_fields.as_deref()
        ),
        "radius",
        "an explicit field-level serde_rename must win over the enum's rename_all_fields"
    );
}

#[test]
fn struct_variant_field_wire_name_falls_back_to_rename_all_fields_without_field_rename() {
    let field = crate::core::ir::FieldDef {
        name: "inner_radius".to_string(),
        ..crate::core::ir::FieldDef::default()
    };
    let enum_def = crate::core::ir::EnumDef {
        rename_all_fields: Some("camelCase".to_string()),
        ..crate::core::ir::EnumDef::default()
    };

    assert_eq!(
        wire_field_name(
            &field.name,
            field.serde_rename.as_deref(),
            enum_def.rename_all_fields.as_deref()
        ),
        "innerRadius",
        "with no field-level rename, the enum's rename_all_fields must case the raw field name"
    );
}

#[test]
fn struct_variant_field_wire_name_falls_back_to_raw_name_with_no_rule_at_all() {
    let field = crate::core::ir::FieldDef {
        name: "inner_radius".to_string(),
        ..crate::core::ir::FieldDef::default()
    };
    let enum_def = crate::core::ir::EnumDef::default();

    assert_eq!(
        wire_field_name(
            &field.name,
            field.serde_rename.as_deref(),
            enum_def.rename_all_fields.as_deref()
        ),
        "inner_radius"
    );
}

/// `rename_all` (variant names) and `rename_all_fields` (struct-variant field names) are
/// independent serde container attributes: an enum may set either, both, or neither, and one
/// must never influence the other's resolved value. ~keep
#[test]
fn rename_all_and_rename_all_fields_are_independent() {
    let variant_name_only = crate::core::ir::EnumDef {
        serde_rename_all: Some("SCREAMING_SNAKE_CASE".to_string()),
        ..crate::core::ir::EnumDef::default()
    };
    assert_eq!(variant_name_only.rename_all_fields, None);

    let field_name_only = crate::core::ir::EnumDef {
        rename_all_fields: Some("kebab-case".to_string()),
        ..crate::core::ir::EnumDef::default()
    };
    assert_eq!(field_name_only.serde_rename_all, None);

    let both = crate::core::ir::EnumDef {
        serde_rename_all: Some("SCREAMING_SNAKE_CASE".to_string()),
        rename_all_fields: Some("camelCase".to_string()),
        ..crate::core::ir::EnumDef::default()
    };
    assert_eq!(
        wire_variant_value("InnerRadius", None, both.serde_rename_all.as_deref()),
        "INNER_RADIUS",
        "serde_rename_all still governs variant-name casing when both are set"
    );
    assert_eq!(
        wire_field_name("inner_radius", None, both.rename_all_fields.as_deref()),
        "innerRadius",
        "rename_all_fields still governs field-name casing when both are set"
    );
}

#[test]
fn public_identifiers_are_separate_from_wire_names() {
    assert_eq!(
        public_field_name(Language::Node, "content_type", Some("contentTypeOverride")),
        "contentTypeOverride"
    );
    assert_eq!(
        wire_field_name("content_type", Some("Content-Type"), Some("camelCase")),
        "Content-Type"
    );
}

#[test]
fn public_host_identifier_applies_language_casing_and_keywords() {
    let cases = [
        (Language::Python, PublicIdentifierKind::Field, "class", "class_"),
        (
            Language::Node,
            PublicIdentifierKind::Function,
            "request_url",
            "requestUrl",
        ),
        (
            Language::Go,
            PublicIdentifierKind::Function,
            "request_url",
            "RequestURL",
        ),
        (
            Language::Go,
            PublicIdentifierKind::Parameter,
            "request_url",
            "requestURL",
        ),
        (
            Language::Csharp,
            PublicIdentifierKind::Method,
            "graphql_route",
            "GraphQLRoute",
        ),
        (
            Language::Ruby,
            PublicIdentifierKind::EnumVariant,
            "HTTPStatus",
            "http_status",
        ),
    ];

    for (lang, kind, name, expected) in cases {
        assert_eq!(public_host_identifier(lang, kind, name), expected);
    }
}

#[test]
fn identifier_context_controls_escaping() {
    assert_eq!(
        escape_identifier_for(Language::Swift, "protocol", IdentifierContext::SwiftSource),
        "`protocol`"
    );
    assert_eq!(
        escape_identifier_for(Language::Swift, "protocol", IdentifierContext::SwiftRustShim),
        "protocol_"
    );
    assert_eq!(
        escape_identifier_for(Language::Kotlin, "object", IdentifierContext::KotlinSource),
        "`object`"
    );
    assert_eq!(
        escape_identifier_for(Language::Kotlin, "object", IdentifierContext::KotlinRustBridge),
        "object_"
    );
    assert_eq!(
        escape_identifier_for(Language::Rust, "type", IdentifierContext::InternalRust),
        "r#type"
    );
}

#[test]
fn dart_identifier_context_handles_core_types_and_tuple_fields() {
    assert_eq!(dart_type_identifier("List", Some("NodeContent")), "NodeContentList");
    assert_eq!(dart_type_identifier("List", None), "ListNode");
    assert_eq!(dart_value_identifier("required"), "required_");
    assert_eq!(dart_tuple_field_identifier("0"), "field0");
}

#[test]
fn abi_symbol_components_are_sanitized_and_joined() {
    assert_eq!(
        abi_symbol_from_components(["my-lib", "HTTPStatus", "Content-Type", "2xx"]),
        "my_lib_httpstatus_content_type_2xx"
    );
    assert_eq!(abi_symbol("ffi", "HTTPStatus"), "ffi_http_status");
}

#[test]
fn identifier_validation_is_contextual() {
    assert!(validate_identifier(Language::Swift, "`protocol`", IdentifierContext::SwiftSource).is_ok());
    assert!(validate_identifier(Language::Rust, "r#type", IdentifierContext::InternalRust).is_ok());
    assert!(validate_identifier(Language::Node, "requestUrl", IdentifierContext::PublicMember).is_ok());
    assert!(validate_identifier(Language::Node, "Content-Type", IdentifierContext::PublicMember).is_err());
    assert!(validate_identifier(Language::Node, "Content-Type", IdentifierContext::Wire).is_ok());
}

#[test]
fn name_collision_detection_groups_distinct_originals() {
    let collisions = detect_name_collisions(["foo_bar", "fooBar", "baz"], |name| {
        public_host_identifier(Language::Node, PublicIdentifierKind::Field, name)
    });

    assert_eq!(
        collisions,
        vec![NameCollision {
            generated: "fooBar".to_string(),
            originals: vec!["foo_bar".to_string(), "fooBar".to_string()],
        }]
    );
}

#[test]
fn test_to_go_name_html_initialism() {
    assert_eq!(to_go_name("html"), "HTML");
}

#[test]
fn test_to_go_name_url_initialism() {
    assert_eq!(to_go_name("url"), "URL");
}

#[test]
fn test_to_go_name_id_initialism() {
    assert_eq!(to_go_name("id"), "ID");
}

#[test]
fn test_to_go_name_plain_word() {
    assert_eq!(to_go_name("links"), "Links");
}

#[test]
fn test_to_go_name_user_id() {
    assert_eq!(to_go_name("user_id"), "UserID");
}

#[test]
fn test_to_go_name_request_url() {
    assert_eq!(to_go_name("request_url"), "RequestURL");
}

#[test]
fn test_to_go_name_http_status() {
    assert_eq!(to_go_name("http_status"), "HTTPStatus");
}

#[test]
fn test_to_go_name_json_body() {
    assert_eq!(to_go_name("json_body"), "JSONBody");
}

#[test]
fn test_go_param_name_base_url() {
    assert_eq!(go_param_name("base_url"), "baseURL");
}

#[test]
fn test_go_param_name_user_id() {
    assert_eq!(go_param_name("user_id"), "userID");
}

#[test]
fn test_go_param_name_api_key() {
    assert_eq!(go_param_name("api_key"), "apiKey");
}

#[test]
fn test_go_param_name_plain() {
    assert_eq!(go_param_name("json"), "json");
}

#[test]
fn go_package_name_from_module_takes_the_last_segment() {
    let cases = [
        ("github.com/org/my-lib", "mylib"),
        ("binding", "binding"),
        ("example.invalid/samplecrate", "samplecrate"),
        ("", "binding"),
    ];
    for (module_path, expected) in cases {
        assert_eq!(
            go_package_name_from_module(module_path),
            expected,
            "module_path={module_path:?}"
        );
    }
}

// Table-driven regression for the bug where the e2e/docs Go snippet generator referenced the
// raw Rust-side error type name while the Go backend's own error generator
// (`gen_go_error_struct`, which calls this exact function) strips the package prefix -- a
// leading case-insensitive match of the Go package name is removed from the Rust error type
// name to avoid revive's stutter lint. Any caller that re-derives this rule instead of calling
// `go_error_type_name` can drift from what the Go backend actually declares; this test pins the
// shared rule itself. ~keep
#[test]
fn go_error_type_name_strips_a_matching_package_prefix() {
    let cases = [
        // Real-world shape: crate `error_type = "SampleCrateError"`, Go package `samplecrate`.
        ("SampleCrateError", "samplecrate", "Error"),
        ("SampleLlmError", "samplellm", "Error"),
        // No overlap between the error type and the package name: left untouched.
        ("ConversionError", "converter", "ConversionError"),
        // Negative control: an unrelated error name sharing no prefix with the package.
        ("ParseError", "samplecrate", "ParseError"),
        // Exact match (type name equals package name with no suffix) is not stripped -- the
        // `len() > pkg_lower.len()` guard exists so a package literally named after its own
        // error type is left with a non-empty identifier.
        ("Error", "error", "Error"),
        // Empty package name: nothing to strip.
        ("Error", "", "Error"),
    ];
    for (error_name, pkg_name, expected) in cases {
        assert_eq!(
            go_error_type_name(error_name, pkg_name),
            expected,
            "error_name={error_name:?} pkg_name={pkg_name:?}"
        );
    }
}

#[test]
fn pascal_to_snake_normal_case() {
    assert_eq!(pascal_to_snake("MyType"), "my_type");
}

#[test]
fn pascal_to_snake_rdfa() {
    assert_eq!(pascal_to_snake("Rdfa"), "rdfa");
}

#[test]
fn pascal_to_snake_html_parser() {
    assert_eq!(pascal_to_snake("HTMLParser"), "html_parser");
}

#[test]
fn pascal_to_snake_xml_http_request() {
    assert_eq!(pascal_to_snake("XMLHttpRequest"), "xml_http_request");
}

#[test]
fn pascal_to_snake_io_error() {
    assert_eq!(pascal_to_snake("IOError"), "io_error");
}

#[test]
fn pascal_to_snake_url_path() {
    assert_eq!(pascal_to_snake("URLPath"), "url_path");
}

#[test]
fn pascal_to_snake_jsonld_all_caps() {
    assert_eq!(pascal_to_snake("JSONLD"), "jsonld");
}

#[test]
fn pascal_to_snake_camel_case() {
    assert_eq!(pascal_to_snake("myField"), "my_field");
}

#[test]
fn pascal_to_snake_already_snake() {
    assert_eq!(pascal_to_snake("already_snake"), "already_snake");
}

#[test]
fn pascal_to_snake_empty() {
    assert_eq!(pascal_to_snake(""), "");
}

#[test]
fn pascal_to_screaming_snake_rdfa() {
    assert_eq!(pascal_to_screaming_snake("Rdfa"), "RDFA");
}

#[test]
fn pascal_to_screaming_snake_html_parser() {
    assert_eq!(pascal_to_screaming_snake("HTMLParser"), "HTML_PARSER");
}

#[test]
fn pascal_to_screaming_snake_my_type() {
    assert_eq!(pascal_to_screaming_snake("MyType"), "MY_TYPE");
}

#[test]
fn test_to_csharp_name_graphql_route_config() {
    assert_eq!(to_csharp_name("graphql_route_config"), "GraphQLRouteConfig");
}

#[test]
fn test_to_csharp_name_http_status_no_acronym() {
    assert_eq!(to_csharp_name("http_status"), "HttpStatus");
}

#[test]
fn test_to_csharp_name_to_json_no_acronym() {
    assert_eq!(to_csharp_name("to_json"), "ToJson");
}

#[test]
fn test_to_csharp_name_plain() {
    assert_eq!(to_csharp_name("my_field"), "MyField");
}

#[test]
fn test_csharp_type_name_heck_corrupted() {
    assert_eq!(csharp_type_name("GraphQlRouteConfig"), "GraphQLRouteConfig");
}

#[test]
fn test_csharp_type_name_already_correct() {
    assert_eq!(csharp_type_name("GraphQLRouteConfig"), "GraphQLRouteConfig");
}

#[test]
fn test_csharp_type_name_http_status_no_acronym() {
    assert_eq!(csharp_type_name("HttpStatus"), "HttpStatus");
}

#[test]
fn test_csharp_type_name_three_letter_acronyms() {
    assert_eq!(csharp_type_name("Uri"), "Uri");
    assert_eq!(csharp_type_name("URI"), "Uri");
    assert_eq!(csharp_type_name("Xml"), "Xml");
    assert_eq!(csharp_type_name("XML"), "Xml");
    assert_eq!(csharp_type_name("Json"), "Json");
    assert_eq!(csharp_type_name("JSON"), "Json");
}

/// `node_type_name` must be the identity function: the NAPI-RS `.d.ts` emitter never applies
/// the Rust-side `Js` wrapper prefix to a TypeScript type name, on the declaration side or the
/// reference side. Table-driven so a future accidental prefix-adding edit fails immediately.
#[test]
fn node_type_name_never_adds_the_js_wrapper_prefix() {
    let cases = [
        ("Message", "Message"),
        ("ChatCompletionRequest", "ChatCompletionRequest"),
        ("StopSequence", "StopSequence"),
        ("Js", "Js"),
    ];
    for (input, expected) in cases {
        assert_eq!(node_type_name(input), expected, "node_type_name({input:?})");
    }
}

/// `qualified_type_path` is the single decision point for "does this name already carry a
/// package". Table-driven: every emitter that prefixes a JVM/.NET package onto a configured type
/// name routes through here, so a regression in any row doubles a package prefix in generated code.
#[test]
fn qualified_type_path_prefixes_bare_names_and_leaves_qualified_ones_alone() {
    let cases = [
        (
            "dev.sample.bindings",
            "SampleClient",
            "dev.sample.bindings.SampleClient",
        ),
        (
            "dev.sample.bindings",
            "dev.sample.bindings.SampleClient",
            "dev.sample.bindings.SampleClient",
        ),
        (
            "dev.sample.bindings",
            "other.pkg.SampleClient",
            "other.pkg.SampleClient",
        ),
        ("dev.sample.bindings", "*", "dev.sample.bindings.*"),
        ("", "SampleClient", "SampleClient"),
    ];
    for (package, type_name, expected) in cases {
        assert_eq!(
            qualified_type_path(package, type_name),
            expected,
            "qualified_type_path({package:?}, {type_name:?})"
        );
    }
}

/// Moved here verbatim from an inline `mod duration_wire_tests` in `naming.rs` when
/// `naming::ts_property_key` was added: `naming.rs` sat at exactly the 1,000-line cap
/// `tests/file_size_ratchet.rs` enforces, so the `mod` declaration had to be paid for. Uses
/// absolute paths rather than `use super::*` so it does not depend on a glob-of-a-glob resolving.
mod duration_wire_tests {
    use crate::codegen::naming::field_uses_duration_map_wire;
    use crate::core::ir::{FieldDef, PrimitiveType, TypeRef};

    fn duration_field(serde_with: Option<&str>) -> FieldDef {
        FieldDef {
            ty: TypeRef::Duration,
            serde_with: serde_with.map(str::to_string),
            ..FieldDef::default()
        }
    }

    #[test]
    fn duration_field_without_serde_with_uses_the_derived_map_wire() {
        assert!(field_uses_duration_map_wire(&duration_field(None)));
    }

    #[test]
    fn duration_field_with_serde_with_uses_the_scalar_wire() {
        assert!(!field_uses_duration_map_wire(&duration_field(Some("duration_ms"))));
    }

    #[test]
    fn non_duration_field_never_uses_the_duration_map_wire() {
        let mut field = duration_field(None);
        field.ty = TypeRef::Primitive(PrimitiveType::U64);
        assert!(!field_uses_duration_map_wire(&field));
    }
}

#[test]
fn underscore_camel_case_capitalizes_only_after_an_underscore() {
    let cases = [
        ("hello_world", "helloWorld"),
        ("no_underscores_here", "noUnderscoresHere"),
        ("already", "already"),
        ("", ""),
        ("a_b_c", "aBC"),
        ("trailing_", "trailing"),
        ("double__underscore", "doubleUnderscore"),
    ];
    for (input, expected) in cases {
        assert_eq!(underscore_camel_case(input), expected, "input: {input}");
    }
}

#[test]
fn underscore_camel_case_preserves_case_that_heck_would_re_split() {
    // These are the cases the helper exists for. `to_node_name` (heck) re-splits an existing
    // case run and drops a leading underscore; a caller matching a name another generator
    // already emitted must not. If these two ever agree, the separate helper is pointless.
    let divergent = [
        ("my_URL", "myURL", "myUrl"),
        ("_raw", "Raw", "raw"),
        ("parse_HTML", "parseHTML", "parseHtml"),
    ];
    for (input, preserving, heck) in divergent {
        assert_eq!(underscore_camel_case(input), preserving, "input: {input}");
        assert_eq!(to_node_name(input), heck, "input: {input}");
        assert_ne!(underscore_camel_case(input), to_node_name(input), "input: {input}");
    }
}