cabalist-parser 0.1.2

CST/AST parser for .cabal files with round-trip fidelity
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
//! Round-trip corpus tests for the cabalist parser.
//!
//! Each fixture is a realistic `.cabal` file represented as a string constant.
//! The test verifies that `parse -> render` produces byte-identical output and
//! that no diagnostics are emitted.

use cabalist_parser::parse;

/// Parse the source and assert byte-identical round-trip with zero diagnostics.
fn assert_round_trip_clean(source: &str) {
    let result = parse(source);
    let rendered = result.cst.render();
    assert_eq!(
        rendered,
        source,
        "\n--- EXPECTED (len={}) ---\n{source}\n--- GOT (len={}) ---\n{rendered}\n",
        source.len(),
        rendered.len(),
    );
    assert!(
        result.diagnostics.is_empty(),
        "expected no diagnostics, got: {:?}",
        result.diagnostics
    );
}

// ============================================================================
// Fixture 1: Minimal file (just name + version + cabal-version)
// ============================================================================

#[test]
fn corpus_minimal() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: minimal-pkg
version: 0.1.0.0
",
    );
}

// ============================================================================
// Fixture 2: Library-only project
// ============================================================================

#[test]
fn corpus_library_only() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: my-lib
version: 1.0.0.0
synopsis: A simple library
license: MIT
author: Jane Doe

library
  exposed-modules:
    MyLib
    MyLib.Internal
  build-depends:
    base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 3: Application-only project
// ============================================================================

#[test]
fn corpus_application_only() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: my-app
version: 0.1.0.0
synopsis: A command-line application
license: BSD-3-Clause
build-type: Simple

executable my-app
  main-is: Main.hs
  other-modules:
    App.Config
    App.Run
  build-depends:
    base >=4.14 && <5,
    optparse-applicative ^>=0.18,
    text ^>=2.0
  hs-source-dirs: app
  default-language: Haskell2010
",
    );
}

// ============================================================================
// Fixture 4: Library + executable + test-suite (common pattern)
// ============================================================================

#[test]
fn corpus_lib_exe_test() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: my-project
version: 0.1.0.0
synopsis: A typical Haskell project
license: MIT
author: Test Author
maintainer: test@example.com
build-type: Simple

library
  exposed-modules:
    MyProject
    MyProject.Types
  other-modules:
    MyProject.Internal
  build-depends:
    base >=4.14 && <5,
    text ^>=2.0,
    containers ^>=0.6
  hs-source-dirs: src
  default-language: GHC2021

executable my-project
  main-is: Main.hs
  build-depends:
    base >=4.14 && <5,
    my-project
  hs-source-dirs: app
  default-language: GHC2021

test-suite my-project-test
  type: exitcode-stdio-1.0
  main-is: Main.hs
  other-modules:
    Test.MyProject
  build-depends:
    base >=4.14 && <5,
    my-project,
    tasty ^>=1.5,
    tasty-hunit ^>=0.10
  hs-source-dirs: test
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 5: Full project with all component types including benchmark
// ============================================================================

#[test]
fn corpus_full_project() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: full-project
version: 2.1.0.0
synopsis: A full-featured project
description: This project has every component type.
license: Apache-2.0
author: Full Author
maintainer: full@example.com
category: Development
build-type: Simple

library
  exposed-modules:
    Full.Core
    Full.Types
    Full.Utils
  build-depends:
    base >=4.14 && <5,
    bytestring ^>=0.11,
    text ^>=2.0
  hs-source-dirs: src
  default-language: GHC2021

executable full-exe
  main-is: Main.hs
  build-depends:
    base,
    full-project
  hs-source-dirs: app
  default-language: GHC2021

test-suite full-test
  type: exitcode-stdio-1.0
  main-is: Main.hs
  build-depends:
    base,
    full-project,
    tasty ^>=1.5
  hs-source-dirs: test
  default-language: GHC2021

benchmark full-bench
  type: exitcode-stdio-1.0
  main-is: Main.hs
  build-depends:
    base,
    full-project,
    criterion ^>=1.6
  hs-source-dirs: bench
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 6: Heavy use of common stanzas and imports
// ============================================================================

#[test]
fn corpus_common_stanzas() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: stanza-heavy
version: 0.1.0.0
license: MIT

common warnings
  ghc-options: -Wall -Wcompat -Widentities
               -Wincomplete-record-updates
               -Wincomplete-uni-patterns
               -Wmissing-deriving-strategies
               -Wredundant-constraints

common extensions
  default-extensions:
    OverloadedStrings
    DerivingStrategies
    DeriveGeneric
    DeriveAnyClass
    GeneralizedNewtypeDeriving
    LambdaCase
    TypeApplications
    ScopedTypeVariables
  default-language: GHC2021

common deps
  build-depends:
    base >=4.14 && <5

library
  import: warnings
  import: extensions
  import: deps
  exposed-modules: Lib
  hs-source-dirs: src

executable app
  import: warnings
  import: extensions
  import: deps
  main-is: Main.hs
  build-depends: stanza-heavy
  hs-source-dirs: app

test-suite tests
  import: warnings
  import: extensions
  import: deps
  type: exitcode-stdio-1.0
  main-is: Main.hs
  build-depends:
    stanza-heavy,
    tasty ^>=1.5
  hs-source-dirs: test
",
    );
}

// ============================================================================
// Fixture 7: Complex conditionals (nested if/else, os/arch/flag/impl checks)
// ============================================================================

#[test]
fn corpus_complex_conditionals() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: cond-project
version: 0.1.0.0
license: MIT

flag dev
  description: Development mode
  default: False
  manual: True

flag examples
  description: Build examples
  default: False
  manual: True

library
  exposed-modules: Lib
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021
  if flag(dev)
    ghc-options: -O0 -fprof-auto
  else
    ghc-options: -O2
  if os(windows)
    build-depends: Win32 ^>=2.13
    cpp-options: -DWINDOWS
  if os(linux)
    build-depends: unix ^>=2.7
    if flag(dev)
      ghc-options: -fhpc
  if impl(ghc >= 9.6)
    ghc-options: -Wno-missing-signatures
  if (flag(examples) || flag(dev)) && !os(windows)
    build-depends: directory ^>=1.3
",
    );
}

// ============================================================================
// Fixture 8: Leading-comma dependency lists
// ============================================================================

#[test]
fn corpus_leading_comma_deps() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: leading-comma
version: 0.1.0.0
license: MIT

library
  exposed-modules: Lib
  build-depends:
      base >=4.14 && <5
    , aeson ^>=2.2
    , bytestring ^>=0.11
    , containers ^>=0.6
    , http-client ^>=0.7
    , text >=2.0 && <2.2
    , unordered-containers ^>=0.2
    , vector ^>=0.13
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 9: Trailing-comma dependency lists
// ============================================================================

#[test]
fn corpus_trailing_comma_deps() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: trailing-comma
version: 0.1.0.0
license: MIT

library
  exposed-modules: Lib
  build-depends:
    base >=4.14 && <5,
    aeson ^>=2.2,
    bytestring ^>=0.11,
    containers ^>=0.6,
    text >=2.0 && <2.2
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 10: Single-line dependency lists
// ============================================================================

#[test]
fn corpus_single_line_deps() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: single-line
version: 0.1.0.0
license: MIT

library
  exposed-modules: Lib
  build-depends: base >=4.14, text >=2.0, aeson ^>=2.2, containers ^>=0.6
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 11: No-comma module lists
// ============================================================================

#[test]
fn corpus_no_comma_module_list() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: module-list
version: 0.1.0.0
license: MIT

library
  exposed-modules:
    Data.Algorithm.Search
    Data.Algorithm.Sort
    Data.Algorithm.Sort.Internal
    Data.Algorithm.Sort.Merge
    Data.Algorithm.Sort.Quick
    Data.Structures.Graph
    Data.Structures.Tree
    Data.Structures.Trie
  other-modules:
    Internal.Buffer
    Internal.Pool
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 12: Multiple flags with conditionals referencing them
// ============================================================================

#[test]
fn corpus_multiple_flags() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: flag-heavy
version: 0.1.0.0
license: MIT

flag fast
  description: Enable optimized backend
  default: True
  manual: False

flag debug
  description: Enable debug output
  default: False
  manual: True

flag profiling
  description: Enable profiling
  default: False
  manual: True

library
  exposed-modules: Lib
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021
  if flag(fast)
    cpp-options: -DFAST_BACKEND
    ghc-options: -O2
  if flag(debug)
    cpp-options: -DDEBUG
    ghc-options: -fprof-auto
  if flag(profiling)
    ghc-options: -prof -fprof-auto-calls
",
    );
}

// ============================================================================
// Fixture 13: Source-repository sections
// ============================================================================

#[test]
fn corpus_source_repository() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: with-repo
version: 0.1.0.0
license: MIT

source-repository head
  type: git
  location: https://github.com/example/with-repo

source-repository this
  type: git
  location: https://github.com/example/with-repo
  tag: v0.1.0.0

library
  exposed-modules: Lib
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 14: Extra metadata fields
// ============================================================================

#[test]
fn corpus_extra_metadata() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: meta-heavy
version: 0.1.0.0
synopsis: A well-documented package
description: This package demonstrates all the metadata fields.
license: MIT
license-file: LICENSE
author: Meta Author
maintainer: meta@example.com
copyright: 2024 Meta Author
category: Data, Testing
homepage: https://example.com/meta-heavy
bug-reports: https://github.com/example/meta-heavy/issues
build-type: Simple
tested-with: GHC == 9.6.4, GHC == 9.8.2
extra-source-files:
  CHANGELOG.md
  README.md

library
  exposed-modules: Lib
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 15: Multi-line description field
// ============================================================================

#[test]
fn corpus_multiline_description() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: long-desc
version: 0.1.0.0
synopsis: Package with a long description
description:
  This is a longer description that spans multiple lines.
  It can contain various information about the package,
  including usage examples and caveats.
  .
  This is a second paragraph of the description.
  The dot on a line by itself is a paragraph separator in Haddock.
license: MIT
build-type: Simple

library
  exposed-modules: Lib
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 16: Tab indentation
// ============================================================================

#[test]
fn corpus_tab_indentation() {
    assert_round_trip_clean(
        "cabal-version: 3.0\n\
         name: tab-indent\n\
         version: 0.1.0.0\n\
         license: MIT\n\
         \n\
         library\n\
         \texposed-modules: Lib\n\
         \tbuild-depends: base >=4.14 && <5\n\
         \ths-source-dirs: src\n\
         \tdefault-language: GHC2021\n",
    );
}

// ============================================================================
// Fixture 17: Mixed indentation styles in different sections
// ============================================================================

#[test]
fn corpus_mixed_indentation() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: mixed-indent
version: 0.1.0.0
license: MIT

library
  exposed-modules: Lib
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021

executable mixed-exe
    main-is: Main.hs
    build-depends: base, mixed-indent
    hs-source-dirs: app
    default-language: GHC2021

test-suite mixed-test
   type: exitcode-stdio-1.0
   main-is: Main.hs
   build-depends: base, mixed-indent, tasty ^>=1.5
   hs-source-dirs: test
   default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 18: Very large file (20+ sections, 50+ dependencies)
// ============================================================================

#[test]
fn corpus_very_large() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: mega-project
version: 3.2.1.0
synopsis: A very large project with many components
description:
  This is a large project that exercises the parser with many sections
  and many dependencies.
license: MIT
author: Large Author
maintainer: large@example.com
category: Development
build-type: Simple
homepage: https://example.com/mega-project
bug-reports: https://github.com/example/mega-project/issues

common warnings
  ghc-options: -Wall -Wcompat

common lang
  default-language: GHC2021
  default-extensions:
    OverloadedStrings
    DerivingStrategies

flag dev
  description: Dev mode
  default: False
  manual: True

flag postgres
  description: PostgreSQL backend
  default: True
  manual: False

flag sqlite
  description: SQLite backend
  default: False
  manual: False

library
  import: warnings
  import: lang
  exposed-modules:
    Mega.Core
    Mega.Core.Types
    Mega.Core.Internal
    Mega.API
    Mega.API.Types
    Mega.API.Handlers
    Mega.API.Middleware
    Mega.DB
    Mega.DB.Types
    Mega.DB.Pool
    Mega.DB.Migrations
    Mega.Config
    Mega.Logging
    Mega.Auth
    Mega.Auth.JWT
    Mega.Auth.OAuth
    Mega.Cache
    Mega.Queue
    Mega.Worker
  other-modules:
    Mega.Internal.Crypto
    Mega.Internal.Hash
  build-depends:
      base >=4.14 && <5
    , aeson ^>=2.2
    , async ^>=2.2
    , bytestring ^>=0.11
    , containers ^>=0.6
    , cryptonite ^>=0.30
    , directory ^>=1.3
    , exceptions ^>=0.10
    , filepath ^>=1.4
    , hashable ^>=1.4
    , http-client ^>=0.7
    , http-client-tls ^>=0.3
    , http-types ^>=0.12
    , lens ^>=5.2
    , memory ^>=0.18
    , mtl ^>=2.3
    , network ^>=3.1
    , optparse-applicative ^>=0.18
    , resource-pool ^>=0.4
    , retry ^>=0.9
    , servant ^>=0.20
    , servant-server ^>=0.20
    , stm ^>=2.5
    , text >=2.0 && <2.2
    , time ^>=1.12
    , transformers ^>=0.6
    , unliftio ^>=0.2
    , unordered-containers ^>=0.2
    , uuid ^>=1.3
    , vector ^>=0.13
    , wai ^>=3.2
    , warp ^>=3.3
    , yaml ^>=0.11
  hs-source-dirs: src
  if flag(postgres)
    build-depends: postgresql-simple ^>=0.7
    cpp-options: -DPOSTGRES
  if flag(sqlite)
    build-depends: sqlite-simple ^>=0.4
    cpp-options: -DSQLITE
  if flag(dev)
    ghc-options: -O0
  else
    ghc-options: -O2

executable mega-server
  import: warnings
  import: lang
  main-is: Main.hs
  other-modules:
    Server.Config
    Server.Init
  build-depends:
    base,
    mega-project
  hs-source-dirs: app/server

executable mega-cli
  import: warnings
  import: lang
  main-is: Main.hs
  other-modules:
    CLI.Commands
    CLI.Options
  build-depends:
    base,
    mega-project,
    optparse-applicative ^>=0.18
  hs-source-dirs: app/cli

executable mega-worker
  import: warnings
  import: lang
  main-is: Main.hs
  build-depends:
    base,
    mega-project
  hs-source-dirs: app/worker

executable mega-migrate
  import: warnings
  import: lang
  main-is: Main.hs
  build-depends:
    base,
    mega-project
  hs-source-dirs: app/migrate

test-suite mega-unit
  import: warnings
  import: lang
  type: exitcode-stdio-1.0
  main-is: Main.hs
  other-modules:
    Test.Mega.Core
    Test.Mega.API
    Test.Mega.DB
    Test.Mega.Auth
  build-depends:
    base,
    mega-project,
    tasty ^>=1.5,
    tasty-hunit ^>=0.10,
    tasty-quickcheck ^>=0.10
  hs-source-dirs: test/unit

test-suite mega-integration
  import: warnings
  import: lang
  type: exitcode-stdio-1.0
  main-is: Main.hs
  other-modules:
    Test.Integration.API
    Test.Integration.DB
  build-depends:
    base,
    mega-project,
    tasty ^>=1.5,
    tasty-hunit ^>=0.10,
    http-client ^>=0.7,
    warp ^>=3.3
  hs-source-dirs: test/integration

test-suite mega-golden
  import: warnings
  import: lang
  type: exitcode-stdio-1.0
  main-is: Main.hs
  build-depends:
    base,
    mega-project,
    tasty ^>=1.5,
    tasty-golden ^>=2.3
  hs-source-dirs: test/golden

benchmark mega-bench
  import: warnings
  import: lang
  type: exitcode-stdio-1.0
  main-is: Main.hs
  other-modules:
    Bench.Core
    Bench.API
  build-depends:
    base,
    mega-project,
    criterion ^>=1.6,
    deepseq ^>=1.4
  hs-source-dirs: bench

source-repository head
  type: git
  location: https://github.com/example/mega-project
",
    );
}

// ============================================================================
// Fixture 19: File with lots of comments
// ============================================================================

#[test]
fn corpus_lots_of_comments() {
    assert_round_trip_clean(
        "\
-- This is the top-level comment describing the package.
-- It spans multiple lines.
cabal-version: 3.0
-- Package identity
name: commented-pkg
version: 0.1.0.0
-- Brief summary
synopsis: A heavily commented package
-- License information
license: MIT
build-type: Simple

-- Common build settings shared across components.
common warnings
  -- Enable most warnings
  ghc-options: -Wall -Wcompat
  -- But not these:
  -- ghc-options: -Werror

-- The main library
library
  -- Public API
  exposed-modules:
    -- Core module
    Lib
    -- Types used across the library
    Lib.Types
  -- Internal modules not exposed to users
  other-modules:
    Lib.Internal
  build-depends:
    -- Standard library
    base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021

-- The command-line tool
executable commented-exe
  main-is: Main.hs
  build-depends: base, commented-pkg
  hs-source-dirs: app
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 20: GHC2021 features (common stanzas, import)
// ============================================================================

#[test]
fn corpus_ghc2021_features() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: modern-haskell
version: 0.1.0.0
license: MIT

common shared
  default-language: GHC2021
  default-extensions:
    DataKinds
    DefaultSignatures
    DeriveAnyClass
    DerivingStrategies
    DerivingVia
    DuplicateRecordFields
    FunctionalDependencies
    GADTs
    LambdaCase
    MultiWayIf
    NoImplicitPrelude
    OverloadedRecordDot
    OverloadedStrings
    PatternSynonyms
    QuantifiedConstraints
    RecordWildCards
    RoleAnnotations
    ScopedTypeVariables
    StandaloneDeriving
    StrictData
    TypeApplications
    TypeFamilies
    TypeOperators
    ViewPatterns
  ghc-options: -Wall -Wcompat -Wmissing-deriving-strategies
  build-depends:
    base >=4.14 && <5

library
  import: shared
  exposed-modules:
    Modern
    Modern.Types
    Modern.Effects
  hs-source-dirs: src

test-suite tests
  import: shared
  type: exitcode-stdio-1.0
  main-is: Main.hs
  build-depends:
    modern-haskell,
    tasty ^>=1.5
  hs-source-dirs: test
",
    );
}

// ============================================================================
// Fixture 21: Deprecated cabal-version with >= prefix
// ============================================================================

#[test]
fn corpus_deprecated_cabal_version() {
    // This should parse without diagnostics -- the parser doesn't enforce
    // the deprecation, that's the validator's job.
    let source = "\
cabal-version: >=1.10
name: old-style
version: 0.1.0.0
build-type: Simple

library
  exposed-modules: Lib
  build-depends: base >=4.7 && <5
  hs-source-dirs: src
  default-language: Haskell2010
";
    let result = parse(source);
    assert_eq!(
        result.cst.render(),
        source,
        "round-trip failed for deprecated cabal-version"
    );
}

// ============================================================================
// Fixture 22: File with no trailing newline
// ============================================================================

#[test]
fn corpus_no_trailing_newline() {
    assert_round_trip_clean("cabal-version: 3.0\nname: no-newline\nversion: 0.1.0.0");
}

// ============================================================================
// Fixture 23: File with CRLF line endings
// ============================================================================

#[test]
fn corpus_crlf_line_endings() {
    let source = "cabal-version: 3.0\r\nname: crlf-pkg\r\nversion: 0.1.0.0\r\n";
    let result = parse(source);
    assert_eq!(result.cst.render(), source, "round-trip failed for CRLF");
}

// ============================================================================
// Fixture 24: Multiple blank lines between sections
// ============================================================================

#[test]
fn corpus_multiple_blank_lines() {
    assert_round_trip_clean(
        "\
cabal-version: 3.0
name: blanky
version: 0.1.0.0


library
  exposed-modules: Lib
  build-depends: base >=4.14 && <5
  hs-source-dirs: src
  default-language: GHC2021


executable blanky-exe
  main-is: Main.hs
  build-depends: base, blanky
  hs-source-dirs: app
  default-language: GHC2021
",
    );
}

// ============================================================================
// Fixture 25: Wide spacing in field values
// ============================================================================

#[test]
fn corpus_wide_spacing() {
    assert_round_trip_clean(
        "\
cabal-version:   3.0
name:            wide-spacing
version:         0.1.0.0
synopsis:        Package with wide field spacing
license:         MIT
license-file:    LICENSE
author:          Spacer
maintainer:      spacer@example.com
build-type:      Simple

library
  exposed-modules:  Lib
  build-depends:    base >=4.14 && <5
  hs-source-dirs:   src
  default-language: GHC2021
",
    );
}