anodizer-core 0.16.1

Core configuration, context, and template engine for the anodizer release tool
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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
//! Tests for the template preprocessor.

use super::preprocess;

/// Force-touch every `LazyLock<Regex>` static in the preprocessor so an
/// invalid literal surfaces here, not on the first real preprocess() call
/// in the field. Each `LazyLock::new(|| static_regex(…))` panics on bad
/// pattern; running them under the test binary turns a deferred panic into
/// a deterministic test failure.
#[test]
fn static_regex_literals_compile() {
    let _ = preprocess("{{ Version }}");
    let _ = preprocess("{{ replace Version \"v\" \"\" }}");
    let _ = preprocess("{{ Version | replace \"v\" \"\" }}");
    let _ = preprocess("{{ in (list \"a\" \"b\") \"a\" }}");
    let _ = preprocess("{{ Now.Format \"2006\" }}");
    let _ = preprocess("{% if eq .Os \"linux\" %}x{% end %}");
    let _ = preprocess("{{ map \"k1\" \"v1\" }}");
}

#[test]
fn test_preprocess_positional_replace() {
    // Unit test for the preprocessor output
    let input = "{{ replace Version \"v\" \"\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ replace(s=Version, old=\"v\", new=\"\") }}");
}

#[test]
fn test_preprocess_positional_replace_piped() {
    let input = "{{ Version | replace \"v\" \"\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Version | replace(from=\"v\", to=\"\") }}");
}

#[test]
fn test_preprocess_positional_split() {
    let input = "{{ split Version \".\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ split(s=Version, sep=\".\") }}");
}

#[test]
fn test_preprocess_positional_contains() {
    let input = "{{ contains Version \"rc\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ contains(s=Version, substr=\"rc\") }}");
}

#[test]
fn test_preprocess_positional_piped_split() {
    let input = "{{ Version | split \".\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Version | split(sep=\".\") }}");
}

#[test]
fn test_preprocess_positional_piped_contains() {
    let input = "{{ Version | contains \"rc\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Version | contains(substr=\"rc\") }}");
}

#[test]
fn test_preprocess_named_args_unchanged() {
    // Already-named-arg syntax should pass through unmodified
    let input = "{{ replace(s=Version, old=\"v\", new=\"\") }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_named_filter_unchanged() {
    let input = "{{ Version | replace(from=\"v\", to=\"\") }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_control_block_rewritten() {
    // {% if contains Version "rc" %} should be rewritten to named-arg form
    let input = "{% if contains Version \"rc\" %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% if contains(s=Version, substr=\"rc\") %}yes{% endif %}"
    );
}

#[test]
fn test_preprocess_control_block_non_positional_unchanged() {
    // {% if Version %} should not be touched (no positional func)
    let input = "{% if Version %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_positional_replace_with_dot_var() {
    // Dot-stripping + positional rewrite combined:
    // {{ replace .Tag "v" "" }} → dot-strip → {{ replace Tag "v" "" }} → positional → {{ replace(s=Tag, old="v", new="") }}
    let input = "{{ replace .Tag \"v\" \"\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ replace(s=Tag, old=\"v\", new=\"\") }}");
}

#[test]
fn test_positional_piped_with_dot_var() {
    // {{ .Tag | replace "v" "" }} → dot-strip → {{ Tag | replace "v" "" }} → positional
    let input = "{{ .Tag | replace \"v\" \"\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Tag | replace(from=\"v\", to=\"\") }}");
}

#[test]
fn test_positional_no_spaces_compact() {
    // Compact form: {{replace .Tag "v" ""}}
    let input = "{{replace .Tag \"v\" \"\"}}";
    let result = preprocess(input);
    assert_eq!(result, "{{replace(s=Tag, old=\"v\", new=\"\")}}");
}

#[test]
fn test_optional_chaining_dot_survives_go_leading_dot_strip() {
    // `?.` is tera 2.0's native optional-chaining operator, lexed as one
    // token. The Go-leading-dot-strip pass used to treat the `.` here the
    // same as a Go `{{ .Field }}` leading dot (since `?` isn't a word
    // char) and strip it, corrupting `Some?.Missing` into the
    // parse-error `Some?Missing`. A `?` immediately before the dot must
    // count as "chained access" like a preceding identifier does.
    let input = "{{ Some?.Missing or \"fallback\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Some?.Missing or \"fallback\" }}");
}

#[test]
fn test_unrelated_expression_unchanged() {
    // A simple variable reference should not be affected
    let input = "{{ Version }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Version }}");
}

#[test]
fn test_unrelated_filter_unchanged() {
    // A normal filter chain should not be affected
    let input = "{{ Version | upper }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Version | upper }}");
}

#[test]
fn test_positional_replace_whitespace_control() {
    // Tera whitespace control: {{- and -}}
    let input = "{{- replace Version \"v\" \"\" -}}";
    let result = preprocess(input);
    assert_eq!(result, "{{- replace(s=Version, old=\"v\", new=\"\") -}}");
}

#[test]
fn test_positional_replace_whitespace_control_left_only() {
    let input = "{{- replace Version \"v\" \"\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{- replace(s=Version, old=\"v\", new=\"\") }}");
}

#[test]
fn test_chained_named_filter_then_positional_rewrite() {
    // Chained: named-arg filter followed by positional rewrite.
    // The preprocessor should rewrite ONLY the last segment's positional args.
    let input = "{{ Version | trimprefix(prefix=\"v\") | replace \".\" \"-\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ Version | trimprefix(prefix=\"v\") | replace(from=\".\", to=\"-\") }}"
    );
}

// --- `in` positional syntax preprocessing tests ---

#[test]
fn test_preprocess_in_with_list_subexpr() {
    // Go-style: {{ in (list "a" "b" "c") "b" }}
    // Pass 2: (list "a" "b" "c") → ["a", "b", "c"]
    // Pass 3: in ["a", "b", "c"] "b" → in(items=["a", "b", "c"], value="b")
    let input = "{{ in (list \"a\" \"b\" \"c\") \"b\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ in(items=[\"a\", \"b\", \"c\"], value=\"b\") }}");
}

#[test]
fn test_preprocess_in_with_variable() {
    // Positional: {{ in myList "b" }} → {{ in(items=myList, value="b") }}
    let input = "{{ in myList \"b\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ in(items=myList, value=\"b\") }}");
}

#[test]
fn test_preprocess_in_named_args_unchanged() {
    let input = "{{ in(items=[\"a\", \"b\"], value=\"a\") }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_in_with_dot_var() {
    // {{ in .MyList "val" }} → dot-strip → {{ in MyList "val" }} → positional
    let input = "{{ in .MyList \"val\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ in(items=MyList, value=\"val\") }}");
}

#[test]
fn test_preprocess_in_control_block() {
    // {% if in myList "b" %} → {% if in(items=myList, value="b") %}
    let input = "{% if in myList \"b\" %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% if in(items=myList, value=\"b\") %}yes{% endif %}"
    );
}

#[test]
fn test_preprocess_list_subexpr_rewrite() {
    // Verify the list subexpression rewrite pass in isolation:
    // (list "a" "b" "c") → ["a", "b", "c"]
    let input = "{{ in (list \"x\" \"y\") \"x\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ in(items=[\"x\", \"y\"], value=\"x\") }}");
}

#[test]
fn test_preprocess_in_control_block_with_list_subexpr() {
    // {% if in (list "a" "b") "a" %} → list rewrite → {% if in ["a", "b"] "a" %}
    // → positional → {% if in(items=["a", "b"], value="a") %}
    let input = "{% if in (list \"a\" \"b\") \"a\" %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% if in(items=[\"a\", \"b\"], value=\"a\") %}yes{% endif %}"
    );
}

// --- `reReplaceAll` positional syntax preprocessing tests ---

#[test]
fn test_preprocess_re_replace_all_positional() {
    // {{ reReplaceAll "(.*)" "hello" "$1-world" }}
    // → {{ reReplaceAll(pattern="(.*)", input="hello", replacement="$1-world") }}
    let input = "{{ reReplaceAll \"(.*)\" \"hello\" \"$1-world\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ reReplaceAll(pattern=\"(.*)\", input=\"hello\", replacement=\"$1-world\") }}"
    );
}

#[test]
fn test_preprocess_re_replace_all_with_variable() {
    // {{ reReplaceAll "(v)(.*)" Tag "prefix-$2" }}
    // → {{ reReplaceAll(pattern="(v)(.*)", input=Tag, replacement="prefix-$2") }}
    let input = "{{ reReplaceAll \"(v)(.*)\" Tag \"prefix-$2\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ reReplaceAll(pattern=\"(v)(.*)\", input=Tag, replacement=\"prefix-$2\") }}"
    );
}

#[test]
fn test_preprocess_re_replace_all_named_args_unchanged() {
    let input = "{{ reReplaceAll(pattern=\"x\", input=\"ax\", replacement=\"y\") }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_re_replace_all_piped() {
    // {{ Message | reReplaceAll "(.*)" "$1-done" }}
    // → {{ Message | reReplaceAll(pattern="(.*)", replacement="$1-done") }}
    let input = "{{ Message | reReplaceAll \"(.*)\" \"$1-done\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ Message | reReplaceAll(pattern=\"(.*)\", replacement=\"$1-done\") }}"
    );
}

#[test]
fn test_preprocess_re_replace_all_control_block() {
    // {% if reReplaceAll "v" Tag "" %} → named-arg form
    let input = "{% if reReplaceAll \"v\" Tag \"\" %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% if reReplaceAll(pattern=\"v\", input=Tag, replacement=\"\") %}yes{% endif %}"
    );
}

// --- `in` piped form preprocessing tests ---

#[test]
fn test_preprocess_in_piped() {
    // {{ myList | in "val" }} → {{ myList | in(value="val") }}
    let input = "{{ myList | in \"val\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ myList | in(value=\"val\") }}");
}

// --- list subexpr: escaped quotes and mixed quote styles ---

#[test]
fn test_preprocess_list_subexpr_escaped_double_quotes_error_loudly() {
    // Under the engine's raw string rule `"hello \"` closes at the quote
    // right after the backslash — `\"` never embedded a quote, so this
    // template was never valid. The pass must leave it unrewritten and let
    // the engine error loudly, never silently reinterpret the boundaries.
    use crate::template::{TemplateVars, render};
    let input = r#"{{ in (list "hello \"world\"" "plain") "plain" }}"#;
    assert_eq!(preprocess(input), input);
    assert!(render(input, &TemplateVars::new()).is_err());
}

#[test]
fn test_preprocess_list_subexpr_escaped_single_quotes_error_loudly() {
    // Same raw-rule reality for single quotes: `'it\'` closes after the
    // backslash, leaving a dangling `s'` — loud error, no silent rewrite.
    use crate::template::{TemplateVars, render};
    let input = "{{ in (list 'it\\'s' 'fine') \"fine\" }}";
    assert_eq!(preprocess(input), input);
    assert!(render(input, &TemplateVars::new()).is_err());
}

#[test]
fn test_preprocess_list_subexpr_mixed_quote_styles() {
    // (list "double" 'single' "another") — each item uses its own quote style
    let input = "{{ in (list \"double\" 'single' \"another\") \"double\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ in(items=[\"double\", 'single', \"another\"], value=\"double\") }}"
    );
}

// --- Finding 5: `(list ...)` with bare identifiers (variable references) ---

#[test]
fn test_preprocess_list_subexpr_with_bare_identifier() {
    // (list .Os "windows") → after dot-strip: (list Os "windows") → [Os, "windows"]
    let input = "{{ in (list .Os \"windows\") \"linux\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ in(items=[Os, \"windows\"], value=\"linux\") }}");
}

#[test]
fn test_preprocess_list_subexpr_with_dotted_path() {
    // (list .Env.FOO "fallback") → after dot-strip: (list Env.FOO "fallback") → [Env.FOO, "fallback"]
    let input = "{{ in (list .Env.FOO \"fallback\") \"val\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ in(items=[Env.FOO, \"fallback\"], value=\"val\") }}"
    );
}

#[test]
fn test_preprocess_list_subexpr_all_bare_identifiers() {
    // (list .Os .Arch) → after dot-strip: (list Os Arch) → [Os, Arch]
    let input = "{{ in (list .Os .Arch) \"linux\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ in(items=[Os, Arch], value=\"linux\") }}");
}

#[test]
fn test_preprocess_list_subexpr_mixed_vars_and_strings() {
    // (list .Os "windows" .Arch) → after dot-strip: (list Os "windows" Arch) → [Os, "windows", Arch]
    let input = "{{ in (list .Os \"windows\" .Arch) \"test\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ in(items=[Os, \"windows\", Arch], value=\"test\") }}"
    );
}

// --- Now.Format method call rewrite tests ---

#[test]
fn test_preprocess_now_format_go_style() {
    // {{ .Now.Format "2006-01-02" }} → {{ Now | now_format(format="2006-01-02") }}
    let input = "{{ .Now.Format \"2006-01-02\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Now | now_format(format=\"2006-01-02\") }}");
}

#[test]
fn test_preprocess_now_format_no_dot_prefix() {
    // {{ Now.Format "2006-01-02" }} (without leading dot) should also work
    let input = "{{ Now.Format \"2006-01-02\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Now | now_format(format=\"2006-01-02\") }}");
}

#[test]
fn test_preprocess_now_format_with_time_pattern() {
    // {{ .Now.Format "2006-01-02 15:04:05" }}
    let input = "{{ .Now.Format \"2006-01-02 15:04:05\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ Now | now_format(format=\"2006-01-02 15:04:05\") }}"
    );
}

#[test]
fn test_preprocess_now_format_single_quotes() {
    // {{ .Now.Format '2006-01-02' }} (single quotes)
    let input = "{{ .Now.Format '2006-01-02' }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Now | now_format(format='2006-01-02') }}");
}

#[test]
fn test_preprocess_now_format_whitespace_control() {
    // {{- .Now.Format "2006-01-02" -}}
    let input = "{{- .Now.Format \"2006-01-02\" -}}";
    let result = preprocess(input);
    assert_eq!(result, "{{- Now | now_format(format=\"2006-01-02\") -}}");
}

#[test]
fn test_preprocess_now_format_compact() {
    // {{.Now.Format "2006-01-02"}} (no spaces after {{ or before }})
    let input = "{{.Now.Format \"2006-01-02\"}}";
    let result = preprocess(input);
    assert_eq!(result, "{{Now | now_format(format=\"2006-01-02\")}}");
}

#[test]
fn test_preprocess_now_format_does_not_affect_other_blocks() {
    // Other blocks should not be affected
    let input = "{{ Version }} - {{ .Now.Format \"2006-01-02\" }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{{ Version }} - {{ Now | now_format(format=\"2006-01-02\") }}"
    );
}

// -----------------------------------------------------------------------
// Pass 0: Go block syntax tests
// -----------------------------------------------------------------------

#[test]
fn test_go_if_end() {
    let input = "{{ if .IsSnapshot }}pre{{ end }}";
    let result = preprocess(input);
    assert_eq!(result, "{% if IsSnapshot %}pre{% endif %}");
}

#[test]
fn test_go_if_else_end() {
    let input = "{{ if .IsSnapshot }}pre{{ else }}stable{{ end }}";
    let result = preprocess(input);
    assert_eq!(result, "{% if IsSnapshot %}pre{% else %}stable{% endif %}");
}

#[test]
fn test_go_if_else_if_end() {
    let input =
        "{{ if eq .Os \"windows\" }}win{{ else if eq .Os \"darwin\" }}mac{{ else }}linux{{ end }}";
    let result = preprocess(input);
    // `eq Os "windows"` is rewritten to `Os == "windows"` by Pass 2b
    assert_eq!(
        result,
        "{% if Os == \"windows\" %}win{% elif Os == \"darwin\" %}mac{% else %}linux{% endif %}"
    );
}

#[test]
fn test_go_range_bare() {
    let input = "{{ range .Maintainers }}# {{ . }}{{ end }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% for val in Maintainers %}# {{ val }}{% endfor %}"
    );
}

#[test]
fn test_go_range_with_variable() {
    let input = "{{ range $release := .Packages }}{{ $release.Name }}{{ end }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% for release in Packages %}{{ release.Name }}{% endfor %}"
    );
}

#[test]
fn test_go_range_kv() {
    let input = "{{ range $key, $value := .Checksums }}{{ $value }} {{ $key }}{{ end }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% for key, value in Checksums %}{{ value }} {{ key }}{% endfor %}"
    );
}

#[test]
fn test_go_with() {
    let input = "{{ with .Arm }}v{{ . }}{{ end }}";
    let result = preprocess(input);
    // `with` becomes `if`, `{{ . }}` rewrites to the with argument
    assert_eq!(result, "{% if Arm %}v{{ Arm }}{% endif %}");
}

#[test]
fn test_go_var_assignment() {
    let input = "{{ $m := map \"a\" \"1\" }}{{ index $m \"a\" }}";
    let result = preprocess(input);
    // Pass 2c rewrites `map "a" "1"` to `map(pairs=["a", "1"])`
    // Pass 3 rewrites `index m "a"` to `index(collection=m, key="a")`
    assert_eq!(
        result,
        "{% set m = map(pairs=[\"a\", \"1\"]) %}{{ index(collection=m, key=\"a\") }}"
    );
}

#[test]
fn test_go_whitespace_trim() {
    let input = "{{- if .Cond -}}yes{{- end -}}";
    let result = preprocess(input);
    assert_eq!(result, "{%- if Cond -%}yes{%- endif -%}");
}

#[test]
fn test_go_nested_if_range() {
    let input = "{{ range .Items }}{{ if .Active }}*{{ end }}{{ end }}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% for val in Items %}{% if Active %}*{% endif %}{% endfor %}"
    );
}

#[test]
fn test_go_blocks_plain_expressions_unchanged() {
    // Plain Go expressions (no block keywords) should pass through
    let input = "{{ .ProjectName }}_{{ .Version }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ ProjectName }}_{{ Version }}");
}

#[test]
fn test_go_complex_nfpm_template() {
    // Real-world template: nfpm default name_template
    let input = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ if not (eq .Amd64 \"v1\") }}{{ .Amd64 }}{{ end }}";
    let result = preprocess(input);
    // `(eq Amd64 "v1")` is rewritten to `Amd64 == "v1"` by Pass 2b
    // Parens are stripped because Tera doesn't support comparisons inside parens.
    assert_eq!(
        result,
        "{{ ProjectName }}_{{ Version }}_{{ Os }}_{{ Arch }}{% if Arm %}v{{ Arm }}{% endif %}{% if not Amd64 == \"v1\" %}{{ Amd64 }}{% endif %}"
    );
}

// -----------------------------------------------------------------------
// Pass 2b: comparison functions (eq/ne/gt/lt/ge/le), and/or, len
// -----------------------------------------------------------------------

#[test]
fn test_eq_in_if_block() {
    let input = "{% if eq Os \"windows\" %}win{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Os == \"windows\" %}win{% endif %}");
}

#[test]
fn test_eq_variadic_three_args() {
    // Go's eq is variadic: eq X Y Z means X == Y || X == Z
    let input = r#"{% if eq Os "linux" "darwin" %}unix{% endif %}"#;
    let result = preprocess(input);
    assert_eq!(
        result,
        r#"{% if Os == "linux" or Os == "darwin" %}unix{% endif %}"#
    );
}

#[test]
fn test_eq_variadic_four_args() {
    let input = r#"{% if eq Arch "amd64" "arm64" "386" %}supported{% endif %}"#;
    let result = preprocess(input);
    assert_eq!(
        result,
        r#"{% if Arch == "amd64" or Arch == "arm64" or Arch == "386" %}supported{% endif %}"#
    );
}

#[test]
fn test_ne_in_if_block() {
    let input = "{% if ne Os \"windows\" %}not-win{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Os != \"windows\" %}not-win{% endif %}");
}

#[test]
fn test_gt_in_if_block() {
    let input = "{% if gt Major 1 %}gt1{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Major > 1 %}gt1{% endif %}");
}

#[test]
fn test_lt_in_if_block() {
    let input = "{% if lt Minor 5 %}lt5{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Minor < 5 %}lt5{% endif %}");
}

#[test]
fn test_ge_in_if_block() {
    let input = "{% if ge Patch 3 %}ge3{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Patch >= 3 %}ge3{% endif %}");
}

#[test]
fn test_le_in_if_block() {
    let input = "{% if le Patch 3 %}le3{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Patch <= 3 %}le3{% endif %}");
}

#[test]
fn test_eq_with_string_literal() {
    let input = "{% if eq Arch \"amd64\" %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Arch == \"amd64\" %}yes{% endif %}");
}

#[test]
fn test_eq_with_numeric_literal() {
    let input = "{% if eq Major 1 %}v1{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Major == 1 %}v1{% endif %}");
}

#[test]
fn test_eq_parenthesized_not() {
    // not (eq .Os "windows") → not Os == "windows"
    // Tera doesn't support comparison operators inside parens, so parens are stripped.
    let input = "{% if not (eq Os \"windows\") %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if not Os == \"windows\" %}yes{% endif %}");
}

#[test]
fn test_eq_in_elif_block() {
    let input = "{% if eq Os \"linux\" %}lin{% elif eq Os \"darwin\" %}mac{% endif %}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% if Os == \"linux\" %}lin{% elif Os == \"darwin\" %}mac{% endif %}"
    );
}

#[test]
fn test_eq_in_expression_block() {
    // eq can also appear in {{ }} expression blocks
    let input = "{{ eq Os \"linux\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Os == \"linux\" }}");
}

#[test]
fn test_eq_with_already_stripped_dot_var() {
    // After dot stripping: eq Os "windows"
    let input = "{% if eq Os \"windows\" %}win{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Os == \"windows\" %}win{% endif %}");
}

#[test]
fn test_eq_with_dotted_path() {
    // eq Env.FOO "bar"
    let input = "{% if eq Env.FOO \"bar\" %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Env.FOO == \"bar\" %}yes{% endif %}");
}

// --- and/or prefix to infix ---

#[test]
fn test_and_prefix_to_infix() {
    let input = "{% if and A B %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if A and B %}yes{% endif %}");
}

#[test]
fn test_or_prefix_to_infix() {
    let input = "{% if or A B %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if A or B %}yes{% endif %}");
}

#[test]
fn test_and_with_parenthesized_or() {
    // and .A (or .B .C) → A and (B or C)
    let input = "{% if and A (or B C) %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if A and (B or C) %}yes{% endif %}");
}

#[test]
fn test_or_with_parenthesized_eq() {
    // or (eq Os "linux") (eq Os "darwin") → Os == "linux" or Os == "darwin"
    // Tera doesn't support comparisons inside parens, so all parens are stripped.
    let input = "{% if or (eq Os \"linux\") (eq Os \"darwin\") %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% if Os == \"linux\" or Os == \"darwin\" %}yes{% endif %}"
    );
}

// --- len function ---

#[test]
fn test_len_in_expression() {
    let input = "{{ len Items }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Items | length }}");
}

#[test]
fn test_len_in_if_block() {
    let input = "{% if len Items %}has items{% endif %}";
    let result = preprocess(input);
    assert_eq!(result, "{% if Items | length %}has items{% endif %}");
}

#[test]
fn test_len_with_dotted_path() {
    let input = "{{ len Env.PATH }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Env.PATH | length }}");
}

#[test]
fn test_len_does_not_match_partial_word() {
    // "length" should not be rewritten
    let input = "{{ Items | length }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Items | length }}");
}

// --- map positional syntax ---

#[test]
fn test_map_positional_two_args() {
    let input = "{{ map \"a\" \"1\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ map(pairs=[\"a\", \"1\"]) }}");
}

#[test]
fn test_map_positional_four_args() {
    let input = "{{ map \"a\" \"1\" \"b\" \"2\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ map(pairs=[\"a\", \"1\", \"b\", \"2\"]) }}");
}

#[test]
fn test_map_named_args_unchanged() {
    let input = "{{ map(pairs=[\"a\", \"1\"]) }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_map_in_set_block() {
    let input = "{% set m = map \"x\" \"y\" %}";
    let result = preprocess(input);
    assert_eq!(result, "{% set m = map(pairs=[\"x\", \"y\"]) %}");
}

// --- index positional syntax ---

#[test]
fn test_index_positional_two_args() {
    let input = "{{ index myMap \"key\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ index(collection=myMap, key=\"key\") }}");
}

#[test]
fn test_index_named_args_unchanged() {
    let input = "{{ index(collection=myMap, key=\"key\") }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_index_in_control_block() {
    let input = "{% if index myMap \"key\" %}yes{% endif %}";
    let result = preprocess(input);
    assert_eq!(
        result,
        "{% if index(collection=myMap, key=\"key\") %}yes{% endif %}"
    );
}

// --- Combined pass tests ---

#[test]
fn test_go_style_full_pipeline_eq_and_map() {
    // Full Go-style pipeline:
    // {{ $m := map "a" "1" }}{{ if eq (index $m "a") "1" }}yes{{ end }}
    let input = "{{ $m := map \"a\" \"1\" }}{{ if eq (index $m \"a\") \"1\" }}yes{{ end }}";
    let result = preprocess(input);
    // Pass 2b rewrites `eq (index m "a") "1"` to `(index m "a") == "1"`.
    // Parens around `index m "a"` are kept (no comparison operator inside).
    // Pass 2c rewrites `map "a" "1"` to `map(pairs=["a", "1"])`.
    // Note: `index m "a"` inside parens is NOT rewritten by Pass 3
    // (positional rewriter only handles top-level standalone/piped forms).
    assert_eq!(
        result,
        "{% set m = map(pairs=[\"a\", \"1\"]) %}{% if (index m \"a\") == \"1\" %}yes{% endif %}"
    );
}

#[test]
fn test_preprocess_positional_time() {
    let input = "{{ time \"2006-01-02\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ time(format=\"2006-01-02\") }}");
}

#[test]
fn test_preprocess_slice_three_args() {
    let input = "{{ slice Commit 0 7 }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Commit | slice(start=0, end=7) }}");
}

#[test]
fn test_preprocess_slice_two_args() {
    let input = "{{ slice Commit 0 }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Commit | slice(start=0) }}");
}

#[test]
fn test_preprocess_slice_string_literal() {
    let input = "{{ slice \"abcdefghij\" 0 7 }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ \"abcdefghij\" | slice(start=0, end=7) }}");
}

#[test]
fn test_preprocess_slice_named_unchanged() {
    let input = "{{ Commit | slice(start=0, end=7) }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_printf_variadic() {
    let input = "{{ printf \"%04d\" Patch }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ printf(format=\"%04d\", args=[Patch]) }}");
}

#[test]
fn test_preprocess_printf_multiple_args() {
    let input = "{{ printf \"%s-%d\" Os Patch }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ printf(format=\"%s-%d\", args=[Os, Patch]) }}");
}

#[test]
fn test_preprocess_printf_no_args() {
    let input = "{{ printf \"literal\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ printf(format=\"literal\", args=[]) }}");
}

#[test]
fn test_preprocess_print() {
    let input = "{{ print \"a\" \"b\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ print(args=[\"a\", \"b\"]) }}");
}

#[test]
fn test_preprocess_println() {
    let input = "{{ println \"x\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ println(args=[\"x\"]) }}");
}

#[test]
fn test_preprocess_printf_named_unchanged() {
    let input = "{{ printf(format=\"%d\", args=[Patch]) }}";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_preserves_emoji_in_literal_text() {
    // A non-ASCII char in plain literal text (no template syntax) must survive
    // the byte-walk intact, not get Latin-1-decoded into mojibake.
    let input = "Released with anodizer 🦀";
    let result = preprocess(input);
    assert!(result.contains('🦀'), "emoji must survive, got: {result:?}");
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_preserves_multibyte_mix_in_literal_text() {
    // Mixed multibyte literal (accents, CJK, emoji, em-dash) round-trips unchanged.
    let input = "café — 日本語 🚀 end";
    let result = preprocess(input);
    assert_eq!(result, input);
}

#[test]
fn test_preprocess_preserves_emoji_inside_block_string() {
    // A non-ASCII char inside a quoted block string must survive the
    // dots_dollars/strip_dots byte-walk that copies quoted-string content.
    let input = "{{ printf \"%s\" \"🦀\" }}";
    let result = preprocess(input);
    assert!(
        result.contains('🦀'),
        "emoji inside block string must survive, got: {result:?}"
    );
}

#[test]
fn test_optional_index_dot_survives_go_leading_dot_strip() {
    // `?[` is tera 2.0's optional-index operator, the sibling of `?.`
    // (same lexer family: exactly two `?` tokens exist). A `.` immediately
    // after the `]` that closes `Some?[0]` is chained field access
    // (`Some?[0].Field`), not a Go-style leading dot — stripping it would
    // corrupt the template into the parse error `Some?[0]Field`.
    let input = "{{ Some?[0].Field or \"fallback\" }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Some?[0].Field or \"fallback\" }}");
}

#[test]
fn test_plain_index_dot_survives_go_leading_dot_strip() {
    // Pins existing (now-correct) behavior for plain, non-optional
    // indexing: `Some[0].Field` is chained field access after an index,
    // same reasoning as the `?[` case above but without the `?`.
    let input = "{{ Some[0].Field }}";
    let result = preprocess(input);
    assert_eq!(result, "{{ Some[0].Field }}");
}

// ---- Pass 5: tera 1.x numeric-index compatibility (`list.0` → `list[0]`) ----

#[test]
fn test_numeric_index_simple() {
    assert_eq!(preprocess("{{ list.0 }}"), "{{ list[0] }}");
}

#[test]
fn test_numeric_index_multi_digit() {
    assert_eq!(preprocess("{{ list.12 }}"), "{{ list[12] }}");
}

#[test]
fn test_numeric_index_then_field() {
    assert_eq!(preprocess("{{ a.0.b }}"), "{{ a[0].b }}");
}

#[test]
fn test_numeric_index_chained_indices() {
    assert_eq!(preprocess("{{ a.0.1 }}"), "{{ a[0][1] }}");
}

#[test]
fn test_numeric_index_after_bracket_index() {
    assert_eq!(preprocess("{{ x[1].0 }}"), "{{ x[1][0] }}");
}

#[test]
fn test_numeric_index_optional_chaining() {
    // tera 2.0 lexes `?[` as its optional-index token (the `?.` sibling),
    // so the 1.x-era `a?.0` must land on `a?[0]`.
    assert_eq!(preprocess("{{ a?.0 }}"), "{{ a?[0] }}");
}

#[test]
fn test_numeric_index_go_style_leading_dot() {
    assert_eq!(
        preprocess("{{ .Artifacts.0.Name }}"),
        "{{ Artifacts[0].Name }}"
    );
}

#[test]
fn test_numeric_index_in_statement_block() {
    assert_eq!(
        preprocess("{% if list.0 %}x{% endif %}"),
        "{% if list[0] %}x{% endif %}"
    );
}

#[test]
fn test_numeric_index_float_literal_untouched() {
    // A number literal is not a path head — `1.0` stays a float.
    assert_eq!(preprocess("{{ 1.0 }}"), "{{ 1.0 }}");
    assert_eq!(preprocess("{{ 1.5 | round }}"), "{{ 1.5 | round }}");
}

#[test]
fn test_numeric_index_version_literal_untouched() {
    // Chained digits-only segments are float/version-shaped literals,
    // never paths.
    assert_eq!(preprocess("{{ 1.2 + 0.5 }}"), "{{ 1.2 + 0.5 }}");
}

#[test]
fn test_numeric_index_inside_string_literal_untouched() {
    assert_eq!(preprocess("{{ \"a.0\" }}"), "{{ \"a.0\" }}");
    assert_eq!(
        preprocess("{{ foo | replace(from=\"v1.0\", to=\"\") }}"),
        "{{ foo | replace(from=\"v1.0\", to=\"\") }}"
    );
}

#[test]
fn test_numeric_index_identifierish_segment_untouched() {
    // `.0x` is not a pure numeric segment; leave it for tera's parser
    // to diagnose.
    assert_eq!(preprocess("{{ a.0x }}"), "{{ a.0x }}");
}

#[test]
fn test_numeric_index_outside_blocks_untouched() {
    // Only expression blocks are rewritten; literal text keeps its dots.
    assert_eq!(preprocess("v1.0 of {{ name }}"), "v1.0 of {{ name }}");
}

#[test]
fn test_numeric_index_end_to_end_render() {
    // Proves `{{ list.0 }}` renders under tera 2.0 via the public API,
    // not just that the rewrite produces the expected text.
    use crate::template::{TemplateVars, render};
    let mut vars = TemplateVars::new();
    vars.set_structured("list", serde_json::json!(["first", "second"]));
    vars.set_structured("items", serde_json::json!([{"name": "n0"}]));
    assert_eq!(render("{{ list.0 }}", &vars).unwrap(), "first");
    assert_eq!(render("{{ items.0.name }}", &vars).unwrap(), "n0");
    assert_eq!(render("{{ list.1 | upper }}", &vars).unwrap(), "SECOND");
}

// --- raw string-boundary rule (shared `string_lit` helper) ---
// All passes must close string literals exactly where the engine does:
// first next occurrence of the opening delimiter (`"`, `'`, or backtick),
// no escape awareness.

#[test]
fn test_backtick_string_skipped_by_numeric_index_pass() {
    assert_eq!(preprocess("{{ `v1.0` }}"), "{{ `v1.0` }}");
}

#[test]
fn test_backtick_string_skipped_by_dollar_strip() {
    assert_eq!(preprocess("{{ `$foo` }}"), "{{ `$foo` }}");
}

#[test]
fn test_backtick_string_skipped_by_dot_strip() {
    assert_eq!(preprocess("{{ `.Field` }}"), "{{ `.Field` }}");
}

#[test]
fn test_backtick_filter_arg_content_untouched() {
    let input = "{{ 'v1.0-x' | replace(from=`v1.0`, to=\"Z\") }}";
    assert_eq!(preprocess(input), input);
}

#[test]
fn test_string_closes_at_first_quote_even_after_backslash() {
    // `'Q\'` closes at the second quote (the engine has no escape concept),
    // so `'v1.0'` is a STRING whose `.0` must survive verbatim.
    let input = r"{{ 'Q\' ~ 'v1.0' }}";
    assert_eq!(preprocess(input), input);
}

// --- multiline expression blocks receive every pass ---

#[test]
fn test_multiline_block_dot_strip_applies() {
    assert_eq!(preprocess("{{\n  .Version }}"), "{{\n  Version }}");
}

#[test]
fn test_multiline_block_dollar_strip_applies() {
    assert_eq!(preprocess("{{\n  $foo }}"), "{{\n  foo }}");
}

#[test]
fn test_multiline_block_builtin_rewrite_applies() {
    assert_eq!(
        preprocess("{% if eq Os \"linux\"\n%}yes{% endif %}"),
        "{% if Os == \"linux\"\n%}yes{% endif %}"
    );
}

#[test]
fn test_multiline_block_method_call_rewrite_applies() {
    assert_eq!(
        preprocess("{{\n  .Now.Format \"2006-01-02\" }}"),
        "{{\n  Now | now_format(format=\"2006-01-02\") }}"
    );
}

#[test]
fn test_multiline_block_numeric_index_rewrite_applies() {
    assert_eq!(preprocess("{{\n  list.0 }}"), "{{\n  list[0] }}");
}