harn-vm 0.10.29

Async bytecode virtual machine for the Harn programming language
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
    try std.testing.expect(std.mem.indexOf(u8, output, "# inline") == null);
}

// ─── Comprehensive round-trip and writer behavior tests ──────────────────────

test "round-trip: parse then write preserves content" {
    const allocator = std.testing.allocator;
    const input =
        \\[server]
        \\host = 0.0.0.0
        \\port = 8080
        \\
        \\[logging]
        \\level = info
        \\file = app.log
        \\
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Parse the output again and verify same content
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();

    try std.testing.expectEqualStrings("0.0.0.0", doc2.get("server", "host").?);
    try std.testing.expectEqualStrings("8080", doc2.get("server", "port").?);
    try std.testing.expectEqualStrings("info", doc2.get("logging", "level").?);
    try std.testing.expectEqualStrings("app.log", doc2.get("logging", "file").?);
}

test "round-trip: sections preserve order" {
    const allocator = std.testing.allocator;
    const input =
        \\[alpha]
        \\key = a
        \\
        \\[beta]
        \\key = b
        \\
        \\[gamma]
        \\key = c
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Verify section order in output
    const alpha_pos = std.mem.indexOf(u8, output, "[alpha]").?;
    const beta_pos = std.mem.indexOf(u8, output, "[beta]").?;
    const gamma_pos = std.mem.indexOf(u8, output, "[gamma]").?;
    try std.testing.expect(alpha_pos < beta_pos);
    try std.testing.expect(beta_pos < gamma_pos);

    // Re-parse and verify order
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqual(@as(usize, 3), doc2.sectionCount());
    try std.testing.expectEqualStrings("alpha", doc2.sections.items[0].name);
    try std.testing.expectEqualStrings("beta", doc2.sections.items[1].name);
    try std.testing.expectEqualStrings("gamma", doc2.sections.items[2].name);
}

test "round-trip: comments are retained in output" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\# This is a comment
        \\key = value # inline comment
        \\
        \\# Another comment
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "# This is a comment") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# inline comment") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# Another comment") != null);

    // Re-parse and verify comments are still there
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqualStrings("value", doc2.get("section", "key").?);
}

test "round-trip: quoted values round-trip correctly" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\quoted = "hello world"
        \\single_quoted = 'single'
        \\with_escape = "has\\backslash"
        \\with_newline = "line1\\nline2"
        \\unquoted = plain
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Re-parse and verify values
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqualStrings("hello world", doc2.get("section", "quoted").?);
    try std.testing.expectEqualStrings("single", doc2.get("section", "single_quoted").?);
    try std.testing.expectEqualStrings("has\\backslash", doc2.get("section", "with_escape").?);
    try std.testing.expectEqualStrings("line1\nline2", doc2.get("section", "with_newline").?);
    try std.testing.expectEqualStrings("plain", doc2.get("section", "unquoted").?);
}

test "round-trip: multiline values round-trip" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\multi = first\\
        \\second\\
        \\third
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Re-parse and verify the multiline value was preserved
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    const val = doc2.get("section", "multi").?;
    try std.testing.expectEqualStrings("first\nsecond\nthird", val);
}

test "round-trip: empty sections are preserved" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\
        \\[empty]
        \\
        \\[another]
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Verify all sections exist in output
    try std.testing.expect(std.mem.indexOf(u8, output, "[section]") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "[empty]") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "[another]") != null);

    // Re-parse and verify empty section count
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqual(@as(usize, 3), doc2.sectionCount());
    try std.testing.expectEqual(@as(usize, 0), doc2.get("empty", "key"). == null);
}

test "alignment option produces padded output" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("db", "host", "localhost");
    try document.set("db", "port", "5432");
    try document.set("db", "connection_string", "tcp://localhost:5432");

    const output = try writeWithOptions(allocator, &document, .{
        .align_values = true,
        .section_spacing = false,
    });
    defer allocator.free(output);

    // Find the line with "host"
    const host_line_start = std.mem.indexOf(u8, output, "host").?;
    const host_line_end = std.mem.indexOfScalar(u8, output[host_line_start..], '\n').?;
    const host_line = output[host_line_start .. host_line_start + host_line_end];

    // Find the line with "connection_string"
    const conn_line_start = std.mem.indexOf(u8, output, "connection_string").?;
    const conn_line_end = std.mem.indexOfScalar(u8, output[conn_line_start..], '\n').?;
    const conn_line = output[conn_line_start .. conn_line_start + conn_line_end];

    // Both lines should have the '=' at the same column
    const host_eq_pos = std.mem.indexOfScalar(u8, host_line, '=').?;
    const conn_eq_pos = std.mem.indexOfScalar(u8, conn_line, '=').?;
    try std.testing.expectEqual(host_eq_pos, conn_eq_pos);
}

test "round-trip: global entries are preserved" {
    const allocator = std.testing.allocator;
    const input =
        \\global_key = global_value
        \\
        \\[section]
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Re-parse and verify global entries
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqualStrings("global_value", doc2.get("section", "key").?);
}

test "round-trip: semicolon comments are retained" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\; This is a semicolon comment
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "; This is a semicolon comment") != null);
}

test "round-trip: section header comments are retained" {
    const allocator = std.testing.allocator;
    const input =
        \\# Header comment for section
        \\[section]
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "# Header comment for section") != null);
}

test "write: empty document produces empty string" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expectEqualStrings("", output);
}

test "write: multiple sections with spacing between them" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("first", "a", "1");
    try document.set("second", "b", "2");
    try document.set("third", "c", "3");

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "\n\n[second]") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "\n\n[third]") != null);
}

test "write: section_spacing=false removes blank lines between sections" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("a", "x", "1");
    try document.set("b", "y", "2");

    const output = try writeWithOptions(allocator, &document, .{
        .section_spacing = false,
    });
    defer allocator.free(output);

    // No blank line between sections
    try std.testing.expect(std.mem.indexOf(u8, output, "\n\n[b]") == null);
}

test "write: custom delimiter" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "key", "value");

    const output = try writeWithOptions(allocator, &document, .{
        .delimiter = ':',
        .space_around_delimiter = true,
    });
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "key : value") != null);
}

test "write: auto_quote quotes values with special chars" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "path", "/usr/local/bin");
    try document.set("s", "commented", "has # hash");
    try document.set("s", "normal", "ok");

    const output = try writeWithOptions(allocator, &document, .{
        .auto_quote = true,
        .write_comments = false,
    });
    defer allocator.free(output);

    // Values with special chars should be quoted
    try std.testing.expect(std.mem.indexOf(u8, output, '"has # hash"') != null);
    // Normal values should not be quoted
    try std.testing.expect(std.mem.indexOf(u8, output, '"ok"') == null);
    try std.testing.expect(std.mem.indexOf(u8, output, "normal = ok") != null);
}

test "write: was_quoted preserves quoting on round-trip" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\name = "quoted value"
        \\plain = unquoted
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // The originally-quoted value should still be quoted in output
    try std.testing.expect(std.mem.indexOf(u8, output, '"quoted value"') != null);
    // The unquoted value should remain unquoted
    try std.testing.expect(std.mem.indexOf(u8, output, "plain = unquoted") != null);
}

test "write: indented output" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "key1", "val1");
    try document.set("s", "key2", "val2");

    const output = try writeWithOptions(allocator, &document, .{
        .indent = "    ",
    });
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "    key1 = val1") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "    key2 = val2") != null);
}

test "write: align_values with indent" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("section", "a", "1");
    try document.set("section", "very_long_key", "2");

    const output = try writeWithOptions(allocator, &document, .{
        .align_values = true,
        .indent = "  ",
    });
    defer allocator.free(output);

    // Both lines should start with indent
    try std.testing.expect(std.mem.indexOf(u8, output, "  a") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "  very_long_key") != null);

    // Both '=' should be at the same column
    const a_eq = std.mem.indexOf(u8, output, "  a").?;
    const long_eq = std.mem.indexOf(u8, output, "  very_long_key").?;
    // Find '=' position relative to line start
    const a_line = output[a_eq..];
    const long_line = output[long_eq..];
    const a_eq_rel = std.mem.indexOfScalar(u8, a_line, '=').?;
    const long_eq_rel = std.mem.indexOfScalar(u8, long_line, '=').?;
    try std.testing.expectEqual(a_eq_rel, long_eq_rel);
}

test "round-trip: complex document with all features" {
    const allocator = std.testing.allocator;
    const input =
        \\# Global comment
        \\global = value
        \\
        \\# Server section
        \\[server]
        \\host = 0.0.0.0
        \\port = "8080"
        \\mode = debug # enable debug mode
        \\
        \\# Database section
        \\[database]
        \\connection = "tcp://localhost:5432"
        \\timeout = 30
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Re-parse and verify all values
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();

    try std.testing.expectEqualStrings("value", doc2.get("server", "host").?);
    try std.testing.expectEqualStrings("8080", doc2.get("server", "port").?);
    try std.testing.expectEqualStrings("debug", doc2.get("server", "mode").?);
    try std.testing.expectEqualStrings("tcp://localhost:5432", doc2.get("database", "connection").?);
    try std.testing.expectEqualStrings("30", doc2.get("database", "timeout").?);

    // Verify comments are retained
    try std.testing.expect(std.mem.indexOf(u8, output, "# Global comment") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# Server section") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# Database section") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# enable debug mode") != null);

    // Verify quoted values are preserved
    try std.testing.expect(std.mem.indexOf(u8, output, '"8080"') != null);
    try std.testing.expect(std.mem.indexOf(u8, output, '"tcp://localhost:5432"') != null);

    // Verify section order
    const server_pos = std.mem.indexOf(u8, output, "[server]").?;
    const database_pos = std.mem.indexOf(u8, output, "[database]").?;
    try std.testing.expect(server_pos < database_pos);
}

test "write: empty value is written" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "empty", "");

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "empty = ") != null);
}

test "write: entries within a section preserve order" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "z_last", "1");
    try document.set("s", "a_first", "2");
    try document.set("s", "m_middle", "3");

    const output = try write(allocator, &document);
    defer allocator.free(output);

    const z_pos = std.mem.indexOf(u8, output, "z_last").?;
    const a_pos = std.mem.indexOf(u8, output, "a_first").?;
    const m_pos = std.mem.indexOf(u8, output, "m_middle").?;
    // Order should match insertion order, not alphabetical
    try std.testing.expect(z_pos < a_pos);
    try std.testing.expect(a_pos < m_pos);
}

test "write: inline comments preserved" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\key = value # this is a comment
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "# this is a comment") != null);
}

test "write: delimiter=colon with spaces" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "key", "value");

    const output = try writeWithOptions(allocator, &document, .{
        .delimiter = ':',
        .space_around_delimiter = true,
    });
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "key : value") != null);
}

test "write: delimiter=colon without spaces" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "key", "value");

    const output = try writeWithOptions(allocator, &document, .{
        .delimiter = ':',
        .space_around_delimiter = false,
    });
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "key:value") != null);
}

test "needsQuoting: detects hash and semicolon" {
    try std.testing.expect(needsQuoting("has # hash"));
    try std.testing.expect(needsQuoting("has ; semicolon"));
}

test "needsQuoting: detects leading/trailing whitespace" {
    try std.testing.expect(needsQuoting(" leading"));
    try std.testing.expect(needsQuoting("trailing "));
    try std.testing.expect(needsQuoting(" both "));
}

test "needsQuoting: no quoting needed for normal values" {
    try std.testing.expect(!needsQuoting("normal"));
    try std.testing.expect(!needsQuoting("with-dash"));
    try std.testing.expect(!needsQuoting("with_underscore"));
    try std.testing.expect(!needsQuoting("with.dots"));
    try std.testing.expect(!needsQuoting("with123numbers"));
}

test "needsQuoting: no quoting for empty string" {
    try std.testing.expect(!needsQuoting(""));
}

test "needsQuoting: detects special characters" {
    try std.testing.expect(needsQuoting("has\"quote"));
    try std.testing.expect(needsQuoting("has'apostrophe"));
    try std.testing.expect(needsQuoting("has\ntab"));
    try std.testing.expect(needsQuoting("has\\backslash"));
}

test "round-trip: parse then write with alignment option" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\a = 1
        \\long_key = 2
        \\x = 3
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try writeWithOptions(allocator, &document, .{
        .align_values = true,
        .section_spacing = false,
    });
    defer allocator.free(output);

    // Verify alignment in output
    const a_line_start = std.mem.indexOf(u8, output, "a =").?;
    const long_line_start = std.mem.indexOf(u8, output, "long_key =").?;
    const x_line_start = std.mem.indexOf(u8, output, "x =").?;

    const a_line = output[a_line_start..];
    const long_line = output[long_line_start..];
    const x_line = output[x_line_start..];

    const a_eq = std.mem.indexOfScalar(u8, a_line, '=').?;
    const long_eq = std.mem.indexOfScalar(u8, long_line, '=').?;
    const x_eq = std.mem.indexOfScalar(u8, x_line, '=').?;

    try std.testing.expectEqual(a_eq, long_eq);
    try std.testing.expectEqual(long_eq, x_eq);
}

test "round-trip: comment-only section" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\# Just a comment
        \\# And another
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "[section]") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# Just a comment") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# And another") != null);

    // Re-parse
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqual(@as(usize, 1), doc2.sectionCount());
}

test "write: suppresses blank lines when section_spacing false" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("a", "x", "1");
    try document.set("b", "y", "2");

    const output = try writeWithOptions(allocator, &document, .{
        .section_spacing = false,
    });
    defer allocator.free(output);

    // Should not have double newlines between sections
    try std.testing.expect(std.mem.indexOf(u8, output, "\n\n[b]") == null);
}

test "round-trip: preserves section with only blank lines" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\
        \\
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Re-parse and verify
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqualStrings("value", doc2.get("section", "key").?);
}

test "write: quoted multiline escape sequences round-trip" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\value = "tab\\there\\nnewline"
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Re-parse and verify
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    const val = doc2.get("section", "value").?;
    try std.testing.expectEqualStrings("tab\there\nnewline", val);
}

test "round-trip: global entries round-trip" {
    const allocator = std.testing.allocator;
    const input =
        \\key = value
        \\
        \\[section]
        \\key2 = value2
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    // Re-parse and verify
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqualStrings("value2", doc2.get("section", "key2").?);
}

test "round-trip: section header comments round-trip" {
    const allocator = std.testing.allocator;
    const input =
        \\# comment before section
        \\[section]
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "# comment before section") != null);
}

test "write: write_comments=false suppresses all comments" {
    const allocator = std.testing.allocator;
    const input =
        \\# global comment
        \\[section]
        \\# section comment
        \\key = value # inline
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try writeWithOptions(allocator, &document, .{
        .write_comments = false,
    });
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "# global comment") == null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# section comment") == null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# inline") == null);
    // But key-value should still be there
    try std.testing.expect(std.mem.indexOf(u8, output, "key = value") != null);
}

test "write: section header comment with semicolon prefix" {
    const allocator = std.testing.allocator;
    const input =
        \\; semicolon comment
        \\[section]
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "; semicolon comment") != null);
}

test "write: multiple global entries" {
    const allocator = std.testing.allocator;
    const input =
        \\key1 = val1
        \\key2 = val2
        \\
        \\[section]
        \\key3 = val3
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "key1 = val1") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "key2 = val2") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "key3 = val3") != null);
}

test "write: document with only global entries" {
    const allocator = std.testing.allocator;
    const input =
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "key = value") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "[") == null);
}

test "write: document with only sections, no globals" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\key = value
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "[section]") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "key = value") != null);
}

test "round-trip: section with only comments" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\# comment 1
        \\# comment 2
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "[section]") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# comment 1") != null);
    try std.testing.expect(std.mem.indexOf(u8, output, "# comment 2") != null);
}

test "write: alignment with mixed entry types" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "short", "1");
    try document.set("s", "longer_key", "2");

    const output = try writeWithOptions(allocator, &document, .{
        .align_values = true,
    });
    defer allocator.free(output);

    // Find both lines
    const short_line_start = std.mem.indexOf(u8, output, "short").?;
    const long_line_start = std.mem.indexOf(u8, output, "longer_key").?;
    const short_line = output[short_line_start..];
    const long_line = output[long_line_start..];
    const short_eq = std.mem.indexOfScalar(u8, short_line, '=').?;
    const long_eq = std.mem.indexOfScalar(u8, long_line, '=').?;
    try std.testing.expectEqual(short_eq, long_eq);
}

test "round-trip: complex alignment preserves values" {
    const allocator = std.testing.allocator;
    const input =
        \\[section]
        \\a = 1
        \\bb = 2
        \\ccc = 3
    ;

    var document = try parser.parse(allocator, input);
    defer document.deinit();

    const output = try writeWithOptions(allocator, &document, .{
        .align_values = true,
        .section_spacing = false,
    });
    defer allocator.free(output);

    // Re-parse and verify values
    var doc2 = try parser.parse(allocator, output);
    defer doc2.deinit();
    try std.testing.expectEqualStrings("1", doc2.get("section", "a").?);
    try std.testing.expectEqualStrings("2", doc2.get("section", "bb").?);
    try std.testing.expectEqualStrings("3", doc2.get("section", "ccc").?);
}

test "write: comment entry type is written" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "key", "value");

    const section = document.findSectionMut("s").?;
    try section.entries.append(allocator, .{ .comment = .{
        .text = try allocator.dupe(u8, "a comment"),
        .prefix = '#',
    } });

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "# a comment") != null);
}

test "write: blank entry type produces blank line" {
    const allocator = std.testing.allocator;
    var document = doc.IniDocument.init(allocator);
    defer document.deinit();

    try document.set("s", "key", "value");

    const section = document.findSectionMut("s").?;
    try section.entries.append(allocator, .{ .blank = {} });

    const output = try write(allocator, &document);
    defer allocator.free(output);

    try std.testing.expect(std.mem.indexOf(u8, output, "key = value\n\n") != null);
}