csharp-rs 0.1.2

Generate C# type definitions from Rust structs and enums
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
771
772
773
774
775
776
// Rust guideline compliant 2026-03-14
//! Integration tests for `#[derive(CSharp)]` on structs with named fields.

#![expect(dead_code, reason = "test structs are only used via derive macro")]

use csharp_rs::{CSharp, CSharpVersion, Config, Serializer};

#[derive(CSharp)]
struct SimpleStruct {
    name: String,
    level: i32,
    active: bool,
}

#[test]
fn simple_struct_name() {
    let cfg = Config::default();
    assert_eq!(SimpleStruct::csharp_name(&cfg), "SimpleStruct");
}

#[test]
fn simple_struct_definition_contains_record() {
    let cfg = Config::default();
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("public sealed record SimpleStruct"),
        "missing record declaration:\n{def}"
    );
}

#[test]
fn simple_struct_definition_contains_properties() {
    let cfg = Config::default();
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("public string Name { get; init; }"),
        "missing Name property:\n{def}"
    );
    assert!(
        def.contains("public int Level { get; init; }"),
        "missing Level property:\n{def}"
    );
    assert!(
        def.contains("public bool Active { get; init; }"),
        "missing Active property:\n{def}"
    );
}

#[test]
fn simple_struct_definition_has_auto_generated_comment() {
    let cfg = Config::default();
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.starts_with("// <auto-generated/>"),
        "missing auto-generated comment:\n{def}"
    );
}

#[test]
fn simple_struct_definition_has_stj_attributes() {
    let cfg = Config::default();
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("using System.Text.Json.Serialization;"),
        "missing using directive:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"name\")]"),
        "missing JsonPropertyName for name:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"level\")]"),
        "missing JsonPropertyName for level:\n{def}"
    );
}

#[test]
fn simple_struct_default_namespace() {
    let cfg = Config::default();
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("namespace Generated"),
        "missing default namespace:\n{def}"
    );
}

// --- struct with rename_all ---

#[derive(CSharp)]
#[serde(rename_all = "camelCase")]
struct PlayerProfile {
    player_id: String,
    display_name: String,
    max_health: i32,
    current_score: f64,
}

#[test]
fn rename_all_camel_case_json_names() {
    let cfg = Config::default();
    let def = PlayerProfile::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"playerId\")]"),
        "missing camelCase playerId:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"displayName\")]"),
        "missing camelCase displayName:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"maxHealth\")]"),
        "missing camelCase maxHealth:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"currentScore\")]"),
        "missing camelCase currentScore:\n{def}"
    );
}

#[test]
fn rename_all_keeps_pascal_case_properties() {
    let cfg = Config::default();
    let def = PlayerProfile::csharp_definition(&cfg);
    assert!(
        def.contains("public string PlayerId { get; init; }"),
        "missing PascalCase PlayerId:\n{def}"
    );
    assert!(
        def.contains("public string DisplayName { get; init; }"),
        "missing PascalCase DisplayName:\n{def}"
    );
}

// --- struct with namespace override ---

#[derive(CSharp)]
#[csharp(namespace = "PulsarAnvil.Types")]
struct GameConfig {
    difficulty: i32,
}

#[test]
fn namespace_override() {
    let cfg = Config::default();
    let def = GameConfig::csharp_definition(&cfg);
    assert!(
        def.contains("namespace PulsarAnvil.Types"),
        "missing namespace override:\n{def}"
    );
}

// --- struct with Option fields ---

#[derive(CSharp)]
struct WithOptionals {
    required_field: String,
    optional_score: Option<f64>,
    optional_name: Option<String>,
}

#[test]
fn optional_fields_are_nullable() {
    let cfg = Config::default();
    let def = WithOptionals::csharp_definition(&cfg);
    assert!(
        def.contains("public string RequiredField { get; init; }"),
        "required field should not be nullable:\n{def}"
    );
    assert!(
        def.contains("public double? OptionalScore { get; init; }"),
        "optional field should be nullable:\n{def}"
    );
    assert!(
        def.contains("public string? OptionalName { get; init; }"),
        "optional string should be nullable:\n{def}"
    );
}

// --- struct with Vec and HashMap ---

use std::collections::HashMap;

#[derive(CSharp)]
struct WithCollections {
    tags: Vec<String>,
    scores: HashMap<String, i32>,
    items: Vec<i64>,
}

#[test]
fn vec_fields_map_to_list() {
    let cfg = Config::default();
    let def = WithCollections::csharp_definition(&cfg);
    assert!(
        def.contains("public List<string> Tags { get; init; }"),
        "Vec<String> should map to List<string>:\n{def}"
    );
    assert!(
        def.contains("public List<long> Items { get; init; }"),
        "Vec<i64> should map to List<long>:\n{def}"
    );
}

#[test]
fn hashmap_fields_map_to_dictionary() {
    let cfg = Config::default();
    let def = WithCollections::csharp_definition(&cfg);
    assert!(
        def.contains("public Dictionary<string, int> Scores { get; init; }"),
        "HashMap should map to Dictionary:\n{def}"
    );
}

// --- struct with combined serde + csharp attrs ---

#[derive(CSharp)]
#[serde(rename_all = "PascalCase")]
#[csharp(namespace = "Game.Models")]
struct FullExample {
    player_id: String,
    level: i32,
    score: Option<f64>,
    inventory: Vec<String>,
    metadata: HashMap<String, String>,
}

#[test]
fn full_example_output() {
    let cfg = Config::default();
    let def = FullExample::csharp_definition(&cfg);

    // Check structure
    assert!(def.contains("// <auto-generated/>"));
    assert!(def.contains("using System.Text.Json.Serialization;"));
    assert!(def.contains("namespace Game.Models"));
    assert!(def.contains("public sealed record FullExample"));

    // Check PascalCase rename_all for JSON names
    assert!(
        def.contains("[JsonPropertyName(\"PlayerId\")]"),
        "JSON name should be PascalCase:\n{def}"
    );
    assert!(def.contains("[JsonPropertyName(\"Level\")]"));
    assert!(def.contains("[JsonPropertyName(\"Score\")]"));

    // Check C# property names are also PascalCase
    assert!(def.contains("public string PlayerId { get; init; }"));
    assert!(def.contains("public int Level { get; init; }"));
    assert!(def.contains("public double? Score { get; init; }"));
    assert!(def.contains("public List<string> Inventory { get; init; }"));
    assert!(def.contains("public Dictionary<string, string> Metadata { get; init; }"));
}

#[test]
fn dependencies_include_field_types() {
    let cfg = Config::default();
    let deps = FullExample::dependencies(&cfg);
    assert!(deps.contains(&String::from("string")));
    assert!(deps.contains(&String::from("int")));
    assert!(deps.contains(&String::from("double")));
    assert!(deps.contains(&String::from("List<string>")));
}

// --- struct with field rename ---

#[derive(CSharp)]
struct WithFieldRename {
    #[serde(rename = "userId")]
    user_id: String,
    level: i32,
}

#[test]
fn field_rename_json_name() {
    let cfg = Config::default();
    let def = WithFieldRename::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"userId\")]"),
        "JSON name should be 'userId':\n{def}"
    );
    assert!(
        def.contains("public string UserId { get; init; }"),
        "C# property should be PascalCase UserId:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"level\")]"),
        "level should be unaffected:\n{def}"
    );
}

// --- struct with field rename + container rename_all ---

#[derive(CSharp)]
#[serde(rename_all = "camelCase")]
struct RenameOverride {
    #[serde(rename = "ID")]
    player_id: String,
    display_name: String,
}

#[test]
fn field_rename_overrides_rename_all() {
    let cfg = Config::default();
    let def = RenameOverride::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"ID\")]"),
        "field rename should override rename_all:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"displayName\")]"),
        "non-renamed field should use camelCase:\n{def}"
    );
}

// --- struct with skip ---

#[derive(CSharp)]
struct WithSkip {
    visible: String,
    #[serde(skip)]
    hidden: String,
    also_visible: i32,
}

#[test]
fn skip_excludes_field() {
    let cfg = Config::default();
    let def = WithSkip::csharp_definition(&cfg);
    assert!(
        def.contains("public string Visible { get; init; }"),
        "visible field should be present:\n{def}"
    );
    assert!(
        !def.contains("Hidden"),
        "skipped field should be absent:\n{def}"
    );
    assert!(
        def.contains("public int AlsoVisible { get; init; }"),
        "also_visible field should be present:\n{def}"
    );
}

// --- struct with skip_serializing ---

#[derive(CSharp)]
struct WithSkipSerializing {
    visible: String,
    #[serde(skip_serializing)]
    write_only: String,
}

#[test]
fn skip_serializing_excludes_field() {
    let cfg = Config::default();
    let def = WithSkipSerializing::csharp_definition(&cfg);
    assert!(
        def.contains("public string Visible { get; init; }"),
        "visible field should be present:\n{def}"
    );
    assert!(
        !def.contains("WriteOnly"),
        "skip_serializing field should be absent:\n{def}"
    );
}

// --- struct with skip_serializing_if ---

#[derive(CSharp)]
struct WithSkipSerializingIf {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    nickname: Option<String>,
    #[serde(skip_serializing_if = "String::is_empty")]
    tag: String,
}

#[test]
fn skip_serializing_if_forces_nullable() {
    let cfg = Config::default();
    let def = WithSkipSerializingIf::csharp_definition(&cfg);
    assert!(
        def.contains("public string Name { get; init; }"),
        "name should be required:\n{def}"
    );
    assert!(
        def.contains("public string? Nickname { get; init; }"),
        "nickname should be nullable (Option + skip_serializing_if):\n{def}"
    );
    assert!(
        def.contains("public string? Tag { get; init; }"),
        "tag should be forced nullable by skip_serializing_if:\n{def}"
    );
}

// --- serde(default) ---

#[derive(CSharp)]
struct WithDefault {
    name: String,
    #[serde(default)]
    level: i32,
}

#[test]
fn serde_default_makes_field_nullable() {
    let cfg = Config::default();
    let def = WithDefault::csharp_definition(&cfg);
    assert!(
        def.contains("int? Level"),
        "field with serde(default) should be nullable:\n{def}"
    );
    assert!(
        !def.contains("int? Name") && def.contains("string Name"),
        "field without default should not be affected:\n{def}"
    );
}

// --- csharp type override ---

#[derive(CSharp)]
struct WithTypeOverride {
    name: String,
    #[csharp(type = "JsonElement")]
    data: String,
}

#[test]
fn csharp_type_override_replaces_csharp_type() {
    let cfg = Config::default();
    let def = WithTypeOverride::csharp_definition(&cfg);
    assert!(
        def.contains("JsonElement Data"),
        "type override should replace C# type:\n{def}"
    );
    assert!(
        !def.contains("string Data"),
        "original type should not appear:\n{def}"
    );
}

// --- csharp type override suppresses nullable suffix ---

#[derive(CSharp)]
#[allow(clippy::option_option, reason = "testing nested Option type override")]
struct WithNestedOptionTypeOverride {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[csharp(type = "string?")]
    inherits_from: Option<Option<String>>,
}

#[test]
fn csharp_type_override_suppresses_double_nullable() {
    let cfg = Config::default();
    let def = WithNestedOptionTypeOverride::csharp_definition(&cfg);
    assert!(
        def.contains("string? InheritsFrom"),
        "type override should produce string?, not string??:\n{def}"
    );
    assert!(
        !def.contains("string??"),
        "double nullable must not appear:\n{def}"
    );
}

// --- combined field attrs ---

#[derive(CSharp)]
#[serde(rename_all = "camelCase")]
struct CombinedFieldAttrs {
    #[serde(rename = "id")]
    player_id: String,
    #[serde(skip)]
    internal: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    bio: Option<String>,
    display_name: String,
}

#[test]
fn combined_field_attrs() {
    let cfg = Config::default();
    let def = CombinedFieldAttrs::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"id\")]"),
        "player_id should be renamed to 'id':\n{def}"
    );
    assert!(
        !def.contains("Internal"),
        "internal should be skipped:\n{def}"
    );
    assert!(
        def.contains("public string? Bio { get; init; }"),
        "bio should be nullable:\n{def}"
    );
    assert!(
        def.contains("[JsonPropertyName(\"displayName\")]"),
        "display_name should use camelCase:\n{def}"
    );
}

// --- multi-config: serializer switching ---

#[test]
fn simple_struct_newtonsoft_uses_json_property() {
    let cfg = Config::default().with_serializer(Serializer::Newtonsoft);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonProperty(\"name\")]"),
        "Newtonsoft should use [JsonProperty]:\n{def}"
    );
    assert!(
        def.contains("using Newtonsoft.Json;"),
        "Newtonsoft should have Newtonsoft.Json using:\n{def}"
    );
    assert!(
        !def.contains("JsonPropertyName"),
        "Newtonsoft should NOT contain JsonPropertyName:\n{def}"
    );
}

#[test]
fn simple_struct_stj_uses_json_property_name() {
    let cfg = Config::default().with_serializer(Serializer::SystemTextJson);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("[JsonPropertyName(\"name\")]"),
        "STJ should use [JsonPropertyName]:\n{def}"
    );
    assert!(
        def.contains("using System.Text.Json.Serialization;"),
        "STJ should have System.Text.Json.Serialization using:\n{def}"
    );
}

// --- multi-config: C# version switching ---

#[test]
fn simple_struct_csharp9_block_scoped_namespace() {
    let cfg = Config::default();
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        !def.contains("namespace Generated;"),
        "C# 9 should NOT use file-scoped namespace:\n{def}"
    );
    assert!(
        def.contains("namespace Generated\n{"),
        "C# 9 should use block-scoped namespace:\n{def}"
    );
}

#[test]
fn simple_struct_csharp10_file_scoped_namespace() {
    let cfg = Config::default().with_target(CSharpVersion::CSharp10);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("namespace Generated;"),
        "C# 10 should use file-scoped namespace:\n{def}"
    );
}

#[test]
fn simple_struct_csharp11_has_required_modifier() {
    let cfg = Config::default().with_target(CSharpVersion::CSharp11);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("public required string Name"),
        "C# 11 non-optional properties should have required modifier:\n{def}"
    );
}

#[test]
fn simple_struct_csharp9_no_required_modifier() {
    let cfg = Config::default();
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        !def.contains("required"),
        "C# 9 should NOT have required modifier:\n{def}"
    );
}

// --- multi-config: namespace switching ---

#[test]
fn simple_struct_config_namespace_override() {
    let cfg = Config::default().with_namespace("Custom.Ns");
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("namespace Custom.Ns"),
        "should use config namespace:\n{def}"
    );
    assert!(
        !def.contains("Generated"),
        "should NOT contain default namespace:\n{def}"
    );
}

#[test]
fn csharp_namespace_attr_overrides_config_namespace() {
    let cfg = Config::default().with_namespace("Custom.Ns");
    let def = GameConfig::csharp_definition(&cfg);
    assert!(
        def.contains("namespace PulsarAnvil.Types"),
        "attribute namespace should take precedence over config:\n{def}"
    );
    assert!(
        !def.contains("Custom.Ns"),
        "config namespace should NOT appear when attr overrides:\n{def}"
    );
}

// --- newtype structs ---

#[derive(CSharp)]
struct UserId(String);

#[test]
fn newtype_struct_name() {
    let cfg = Config::default();
    assert_eq!(UserId::csharp_name(&cfg), "UserId");
}

#[test]
fn newtype_struct_has_value_property() {
    let cfg = Config::default();
    let def = UserId::csharp_definition(&cfg);
    assert!(
        def.contains("public sealed record UserId"),
        "newtype should generate sealed record:\n{def}"
    );
    assert!(
        def.contains("string Value"),
        "newtype should have Value property with correct type:\n{def}"
    );
}

#[test]
fn newtype_struct_has_json_property_name() {
    let cfg = Config::default();
    let def = UserId::csharp_definition(&cfg);
    assert!(
        def.contains("JsonPropertyName"),
        "non-transparent newtype should have JsonPropertyName:\n{def}"
    );
}

// --- transparent newtype ---

#[derive(CSharp)]
#[serde(transparent)]
struct PlayerId(String);

#[test]
fn transparent_newtype_has_converter() {
    let cfg = Config::default();
    let def = PlayerId::csharp_definition(&cfg);
    assert!(
        def.contains("PlayerIdConverter"),
        "transparent newtype should have converter:\n{def}"
    );
    assert!(
        def.contains("[JsonConverter(typeof(PlayerIdConverter))]"),
        "transparent newtype should have converter attribute:\n{def}"
    );
}

#[test]
fn transparent_newtype_no_json_property_on_value() {
    let cfg = Config::default();
    let def = PlayerId::csharp_definition(&cfg);
    assert!(
        !def.contains("JsonPropertyName"),
        "transparent newtype should NOT have JsonPropertyName:\n{def}"
    );
}

#[test]
fn transparent_newtype_converter_reads_inner_type() {
    let cfg = Config::default();
    let def = PlayerId::csharp_definition(&cfg);
    assert!(
        def.contains("Deserialize<string>"),
        "STJ converter should deserialize inner type:\n{def}"
    );
}

#[test]
fn transparent_newtype_newtonsoft_converter() {
    let cfg = Config::default().with_serializer(Serializer::Newtonsoft);
    let def = PlayerId::csharp_definition(&cfg);
    assert!(
        def.contains("PlayerIdConverter"),
        "Newtonsoft transparent newtype should have converter:\n{def}"
    );
    assert!(
        def.contains("ReadJson"),
        "Newtonsoft converter should have ReadJson:\n{def}"
    );
    assert!(
        def.contains("WriteJson"),
        "Newtonsoft converter should have WriteJson:\n{def}"
    );
}

#[test]
fn transparent_newtype_csharp10_file_scoped() {
    let cfg = Config::default().with_target(CSharpVersion::CSharp10);
    let def = PlayerId::csharp_definition(&cfg);
    assert!(
        def.contains("namespace Generated;"),
        "C# 10 transparent newtype should use file-scoped namespace:\n{def}"
    );
}

// --- Unity target ---

#[test]
fn unity_struct_generates_class_not_record() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("public sealed class SimpleStruct"),
        "Unity should generate sealed class:\n{def}"
    );
    assert!(
        !def.contains("sealed record"),
        "Unity should NOT generate sealed record:\n{def}"
    );
}

#[test]
fn unity_struct_uses_get_set() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        def.contains("{ get; set; }"),
        "Unity should use get; set; accessors:\n{def}"
    );
    assert!(
        !def.contains("get; init;"),
        "Unity should NOT use get; init;:\n{def}"
    );
}

#[test]
fn unity_struct_no_required_modifier() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        !def.contains("required"),
        "Unity should NOT have required modifier:\n{def}"
    );
}

#[test]
fn unity_struct_block_scoped_namespace() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = SimpleStruct::csharp_definition(&cfg);
    assert!(
        !def.contains("namespace Generated;"),
        "Unity should NOT use file-scoped namespace:\n{def}"
    );
    assert!(
        def.contains("namespace Generated\n{"),
        "Unity should use block-scoped namespace:\n{def}"
    );
}

#[test]
fn unity_transparent_newtype_uses_class() {
    let cfg = Config::default().with_target(CSharpVersion::Unity);
    let def = PlayerId::csharp_definition(&cfg);
    assert!(
        def.contains("public sealed class PlayerId"),
        "Unity transparent newtype should use class:\n{def}"
    );
}