Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

gleam / compiler-core / src / format / tests.rs
90 kB 6721 lines
1use pretty_assertions::assert_eq; 2 3mod asignments; 4mod binary_operators; 5mod bit_array; 6mod blocks; 7mod cases; 8mod conditional_compilation; 9mod custom_type; 10mod echo; 11mod external_fn; 12mod external_types; 13mod function; 14mod guards; 15mod imports; 16mod lists; 17mod pipeline; 18mod record_update; 19mod tuple; 20mod use_; 21 22#[macro_export] 23macro_rules! assert_format { 24 ($src:expr $(,)?) => { 25 let mut writer = String::new(); 26 $crate::format::pretty(&mut writer, &$src.into(), camino::Utf8Path::new("<stdin>")) 27 .unwrap(); 28 assert_eq!($src, writer); 29 }; 30} 31 32#[macro_export] 33macro_rules! assert_format_rewrite { 34 ($src:expr, $expected:expr $(,)?) => { 35 let mut writer = String::new(); 36 $crate::format::pretty(&mut writer, &$src.into(), camino::Utf8Path::new("<stdin>")) 37 .unwrap(); 38 assert_eq!(writer, $expected); 39 }; 40} 41 42#[test] 43fn imports() { 44 assert_format!("\n"); 45 assert_format!("import one\n"); 46 assert_format!("import one\nimport two\n"); 47 assert_format!("import one/two/three\n"); 48 assert_format!("import four/five\nimport one/two/three\n"); 49 assert_format!("import one.{fun, fun2, fun3}\n"); 50 assert_format!("import one.{One, Two, fun1, fun2}\n"); 51 assert_format!("import one.{main as entrypoint}\n"); 52 assert_format!("import one/two/three as free\n"); 53 assert_format!("import one/two/three.{thunk} as free\n"); 54 assert_format!("import one/two/three.{thunk as funky} as free\n"); 55 assert_format!( 56 "import my/cool/module.{ 57 Ane, Bwo, Chree, Dour, Eive, Fix, Geven, Hight, Iine, Jen, Kleven, Lwelve, 58 Mhirteen, Nifteen, Oixteen, 59} 60" 61 ); 62 assert_format!( 63 "import gleam/result.{ 64 Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, Abcde, 65 End, 66} 67" 68 ); 69} 70 71#[test] 72fn multiple_statements_test() { 73 assert_format!( 74 r#"import one 75import three 76import two 77 78pub type One 79 80pub type Two 81 82pub type Three 83 84pub type Four 85"# 86 ); 87} 88 89#[test] 90fn type_alias() { 91 assert_format!( 92 "type Option(a) = 93 Result(a, Nil) 94" 95 ); 96 97 assert_format!( 98 "pub type Option(a) = 99 Result(a, Nil) 100" 101 ); 102 103 assert_format!( 104 "pub type Pair(a, b) = 105 #(a, b) 106" 107 ); 108 109 assert_format!( 110 "pub type Sixteen(element) = 111 #( 112 element, 113 element, 114 element, 115 element, 116 element, 117 element, 118 element, 119 element, 120 element, 121 element, 122 element, 123 element, 124 element, 125 element, 126 element, 127 element, 128 ) 129" 130 ); 131 132 assert_format!( 133 "pub type Sixteen(element) = 134 fn( 135 element, 136 element, 137 element, 138 element, 139 element, 140 element, 141 element, 142 element, 143 element, 144 element, 145 element, 146 element, 147 element, 148 element, 149 element, 150 element, 151 ) -> 152 #( 153 element, 154 element, 155 element, 156 element, 157 element, 158 element, 159 element, 160 element, 161 element, 162 element, 163 element, 164 element, 165 element, 166 element, 167 element, 168 element, 169 ) 170" 171 ); 172 173 // assert_format!( 174 // "pub type Curried(element) = 175 // fn() -> 176 // elementttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt 177 //" 178 // ); 179 180 // assert_format!( 181 // "pub type Sixteen(element) = 182 // fn(element) -> 183 // #( 184 // element, 185 // element, 186 // element, 187 // element, 188 // element, 189 // element, 190 // element, 191 // element, 192 // element, 193 // element, 194 // element, 195 // element, 196 // element, 197 // element, 198 // element, 199 // element, 200 // ) 201 //" 202 // ); 203 204 assert_format!( 205 "pub type Curried(element) = 206 fn(element) -> fn(element) -> element 207" 208 ); 209 210 // assert_format!( 211 // "pub type Curried(element) = 212 // fn(element) 213 // -> fn(element) 214 // -> fn(element) 215 // -> fn(element) 216 // -> fn(element) 217 // -> element 218 //" 219 // ); 220 221 assert_format!( 222 "type WowThisTypeHasJustTheLongestName = 223 WowThisTypeHasAnEvenLongerNameHowIsThatPossible 224" 225 ); 226 227 assert_format!( 228 "type WowThisTypeHasJustTheLongestName = 229 Container( 230 Int, 231 String, 232 List(a), 233 SomethingElse, 234 WowThisTypeHasJustTheLongestName, 235 ) 236" 237 ); 238 239 assert_format!( 240 "type WowThisTypeHasJustTheLongestName( 241 some_long_type_variable, 242 and_another, 243 and_another_again, 244) = 245 Container( 246 Int, 247 String, 248 List(a), 249 SomethingElse, 250 WowThisTypeHasJustTheLongestName, 251 ) 252" 253 ); 254 255 assert_format!( 256 "/// 257type Many(a) = 258 List(a) 259" 260 ); 261} 262 263#[test] 264fn expr_fn() { 265 assert_format!( 266 r#"fn main() { 267 fn(x) { x } 268} 269"# 270 ); 271 272 assert_format!( 273 r#"fn main() { 274 fn(_) { x } 275} 276"# 277 ); 278 279 assert_format!( 280 r#"fn main() { 281 fn(_discarded) { x } 282} 283"# 284 ); 285 286 assert_format!( 287 r#"fn main() { 288 fn() { 289 1 290 2 291 } 292} 293"# 294 ); 295 296 assert_format!( 297 r#"fn main() { 298 fn() { 299 let y = x 300 y 301 } 302} 303"# 304 ); 305 306 assert_format!( 307 r#"fn main() { 308 fn() { 309 let x: Int = 1 310 x 311 } 312} 313"# 314 ); 315 316 assert_format!( 317 r#"fn main() { 318 fn() { 319 let x: Box(_) = call() 320 x 321 } 322} 323"# 324 ); 325 326 assert_format!( 327 r#"fn main() { 328 fn() { 329 let x: Box(_whatever) = call() 330 x 331 } 332} 333"# 334 ); 335 336 assert_format!( 337 r#"fn main() { 338 fn(_) { 339 1 340 2 341 } 342} 343"# 344 ); 345 346 assert_format!( 347 r#"fn main() { 348 fn(_) -> Int { 349 1 350 2 351 } 352} 353"# 354 ); 355 356 assert_format!( 357 r#"fn main() { 358 fn(_: Int) -> Int { 2 } 359} 360"# 361 ); 362 363 assert_format!( 364 r#"fn main() { 365 fn(x) { 366 case x { 367 Ok(i) -> i + 1 368 Error(_) -> 0 369 } 370 } 371} 372"# 373 ); 374} 375 376#[test] 377fn expr_call() { 378 assert_format!( 379 r#"fn main() { 380 run() 381} 382"# 383 ); 384 385 assert_format!( 386 r#"fn main() { 387 run(1) 388} 389"# 390 ); 391 392 assert_format!( 393 r#"fn main() { 394 run(with: 1) 395} 396"# 397 ); 398 399 assert_format!( 400 r#"fn main() { 401 run( 402 with: 1, 403 loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: 1, 404 ) 405} 406"# 407 ); 408 409 assert_format!( 410 r#"fn main() { 411 run( 412 with: something(1, 2, 3), 413 loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: 1, 414 ) 415} 416"# 417 ); 418 419 assert_format!( 420 r#"fn main() { 421 run( 422 with: something( 423 loooooooooooooooooooooooooooooooooooooooong: 1, 424 looooooooooooooooooooooooooooooooooooooooong: 2, 425 ), 426 loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: 1, 427 ) 428} 429"# 430 ); 431 432 assert_format!( 433 "fn main() { 434 succ(1) 435} 436" 437 ); 438 439 assert_format!( 440 "fn main() { 441 add(1)(2)(3) 442} 443" 444 ); 445 446 assert_format!( 447 "fn main() { 448 Ok(1) 449} 450" 451 ); 452 453 assert_format!( 454 "fn main() { 455 Ok(1, { 456 1 457 2 458 }) 459} 460" 461 ); 462 463 assert_format!( 464 r#"fn main() { 465 Person("Al", is_cool: VeryTrue) 466} 467"# 468 ); 469 470 assert_format!( 471 r#"fn main() { 472 Person(name: "Al", is_cool: VeryTrue) 473} 474"# 475 ); 476} 477 478#[test] 479fn compact_single_argument_call() { 480 assert_format!( 481 r#"fn main() { 482 thingy(fn(x) { 483 1 484 2 485 }) 486} 487"# 488 ); 489 490 assert_format!( 491 r#"fn main() { 492 thingy([ 493 // ok! 494 one(), 495 two(), 496 ]) 497} 498"# 499 ); 500 501 assert_format!( 502 r#"fn main() { 503 thingy(<< 504 // ok! 505 one(), 506 two(), 507 >>) 508} 509"# 510 ); 511 512 assert_format!( 513 r#"fn main() { 514 thingy(#( 515 // ok! 516 one(), 517 two(), 518 )) 519} 520"# 521 ); 522 523 assert_format!( 524 r#"fn main() { 525 thingy( 526 wiggle(my_function( 527 // ok! 528 one(), 529 two(), 530 )), 531 ) 532} 533"# 534 ); 535 536 assert_format!( 537 r#"fn main() { 538 thingy(case x { 539 1 -> 1 540 _ -> 0 541 }) 542} 543"# 544 ); 545 546 assert_format!( 547 r#"fn main() { 548 thingy({ 549 1 550 2 551 3 552 }) 553} 554"# 555 ); 556 557 assert_format!( 558 r#"fn main() { 559 thingy({ 560 let x = 1 561 x 562 }) 563} 564"# 565 ); 566} 567 568#[test] 569fn expr_tuple() { 570 assert_format!( 571 r#"fn main(one, two, three) { 572 #(1, { 573 1 574 2 575 }) 576} 577"# 578 ); 579 580 assert_format!( 581 r#"fn main() { 582 #( 583 atom.create_from_string("module"), 584 atom.create_from_string("gleam@otp@actor"), 585 ) 586} 587"# 588 ); 589 590 assert_format!( 591 r#"fn main() { 592 #() 593} 594"# 595 ); 596 597 assert_format!( 598 r#"fn main() { 599 #(1) 600} 601"# 602 ); 603 604 assert_format!( 605 r#"fn main() { 606 #(1, 2) 607} 608"# 609 ); 610 611 assert_format!( 612 r#"fn main() { 613 #(1, 2, 3) 614} 615"# 616 ); 617 618 assert_format!( 619 r#"fn main() { 620 #( 621 really_long_variable_name, 622 really_long_variable_name, 623 really_long_variable_name, 624 really_long_variable_name, 625 really_long_variable_name, 626 ) 627} 628"# 629 ); 630} 631 632#[test] 633fn statement_fn() { 634 assert_format!( 635 r#"fn main(one, two, three) { 636 Nil 637} 638"# 639 ); 640} 641 642#[test] 643fn statement_fn1() { 644 assert_format!( 645 r#"fn main(label_one one, label_two two, label_three three) { 646 Nil 647} 648"# 649 ); 650} 651 652#[test] 653fn statement_fn2() { 654 assert_format!( 655 r#"fn main(label_one one: One, label_two two: Two) { 656 Nil 657} 658"# 659 ); 660} 661 662#[test] 663fn statement_fn3() { 664 assert_format!( 665 r#"fn main( 666 label_one one: One, 667 label_two two: Two, 668 label_three three: Three, 669 label_four four: Four, 670) { 671 Nil 672} 673"# 674 ); 675 676 assert_format!( 677 r#"fn main(_discarded) { 678 Nil 679} 680"# 681 ); 682} 683 684#[test] 685fn statement_fn4() { 686 assert_format!( 687 r#"fn main(label _discarded) { 688 Nil 689} 690"# 691 ); 692} 693 694#[test] 695fn statement_fn5() { 696 // https://github.com/gleam-lang/gleam/issues/613 697 assert_format!( 698 r#"fn main() { 699 Nil 700 // Done 701} 702"# 703 ); 704} 705 706#[test] 707fn statement_fn6() { 708 // 709 // Module function return annotations 710 // 711 712 assert_format!( 713 r#"fn main() -> Nil { 714 Nil 715} 716"# 717 ); 718} 719 720#[test] 721fn statement_fn7() { 722 assert_format!( 723 r#"fn main() -> Loooooooooooooooooooong( 724 Looooooooooooooong, 725 Looooooooooooooooooong, 726 Loooooooooooooooooooooong, 727 Looooooooooooooooooooooooong, 728) { 729 Nil 730} 731"# 732 ); 733} 734 735#[test] 736fn statement_fn8() { 737 assert_format!( 738 r#"fn main() -> Loooooooooooooooooooong( 739 Loooooooooooooooooooooooooooooooooooooooooong, 740) { 741 Nil 742} 743"# 744 ); 745} 746 747#[test] 748fn statement_fn9() { 749 assert_format!( 750 r#"fn main() -> program.Exit { 751 Nil 752} 753"# 754 ); 755} 756 757#[test] 758fn statement_fn10() { 759 assert_format!( 760 "fn order( 761 first: Set(member), 762 second: Set(member), 763) -> #(Set(member), Set(member), a) { 764 Nil 765} 766" 767 ); 768} 769 770#[test] 771fn statement_fn11() { 772 assert_format!( 773 "/// 774pub fn try_map( 775 over list: List(a), 776 with fun: fn(a) -> Result(b, e), 777) -> Result(List(b), e) { 778 Nil 779} 780" 781 ); 782} 783 784#[test] 785fn binary_operators() { 786 assert_format!( 787 r#"fn main() { 788 True && False 789} 790"# 791 ); 792 793 assert_format!( 794 r#"fn main() { 795 True || False 796} 797"# 798 ); 799 800 assert_format!( 801 r#"fn main() { 802 1 < 1 803} 804"# 805 ); 806 807 assert_format!( 808 r#"fn main() { 809 1 <= 1 810} 811"# 812 ); 813 814 assert_format!( 815 r#"fn main() { 816 1.0 <. 1.0 817} 818"# 819 ); 820 821 assert_format!( 822 r#"fn main() { 823 1.0 <=. 1.0 824} 825"# 826 ); 827 828 assert_format!( 829 r#"fn main() { 830 1 == 1 831} 832"# 833 ); 834 835 assert_format!( 836 r#"fn main() { 837 1 != 1 838} 839"# 840 ); 841 842 assert_format!( 843 r#"fn main() { 844 1 >= 1 845} 846"# 847 ); 848 849 assert_format!( 850 r#"fn main() { 851 1 > 1 852} 853"# 854 ); 855 856 assert_format!( 857 r#"fn main() { 858 1.0 >=. 1.0 859} 860"# 861 ); 862 863 assert_format!( 864 r#"fn main() { 865 1.0 >. 1.0 866} 867"# 868 ); 869 870 assert_format!( 871 r#"fn main() { 872 1 + 1 873} 874"# 875 ); 876 877 assert_format!( 878 r#"fn main() { 879 1.0 +. 1.0 880} 881"# 882 ); 883 884 assert_format!( 885 r#"fn main() { 886 1 - 1 887} 888"# 889 ); 890 891 assert_format!( 892 r#"fn main() { 893 1.0 -. 1.0 894} 895"# 896 ); 897 898 assert_format!( 899 r#"fn main() { 900 1 * 1 901} 902"# 903 ); 904 905 assert_format!( 906 r#"fn main() { 907 1.0 *. 1.0 908} 909"# 910 ); 911 912 assert_format!( 913 r#"fn main() { 914 1 / 1 915} 916"# 917 ); 918 919 assert_format!( 920 r#"fn main() { 921 1.0 /. 1.0 922} 923"# 924 ); 925 926 assert_format!( 927 r#"fn main() { 928 1 % 1 929} 930"# 931 ); 932} 933 934#[test] 935fn expr_int() { 936 assert_format!( 937 r#"fn i() { 938 1 939} 940"# 941 ); 942 943 assert_format!( 944 r#"fn i() { 945 121_234_345_989_000 946} 947"# 948 ); 949 950 assert_format!( 951 r#"fn i() { 952 -12_928_347_925 953} 954"# 955 ); 956 957 assert_format!( 958 r#"fn i() { 959 1_234_567_890 960} 961"# 962 ); 963 964 assert_format!( 965 r#"fn i() { 966 123_456_789 967} 968"# 969 ); 970 971 assert_format!( 972 r#"fn i() { 973 12_345_678 974} 975"# 976 ); 977 978 assert_format!( 979 r#"fn i() { 980 1_234_567 981} 982"# 983 ); 984 985 assert_format!( 986 r#"fn i() { 987 123_456 988} 989"# 990 ); 991 992 assert_format!( 993 r#"fn i() { 994 12_345 995} 996"# 997 ); 998 999 assert_format!( 1000 r#"fn i() { 1001 1234 1002} 1003"# 1004 ); 1005 1006 assert_format!( 1007 r#"fn i() { 1008 123 1009} 1010"# 1011 ); 1012 1013 assert_format!( 1014 r#"fn i() { 1015 12 1016} 1017"# 1018 ); 1019 1020 assert_format!( 1021 r#"fn i() { 1022 1 1023} 1024"# 1025 ); 1026 1027 assert_format!( 1028 r#"fn i() { 1029 -1_234_567_890 1030} 1031"# 1032 ); 1033 1034 assert_format!( 1035 r#"fn i() { 1036 -123_456_789 1037} 1038"# 1039 ); 1040 1041 assert_format!( 1042 r#"fn i() { 1043 -12_345_678 1044} 1045"# 1046 ); 1047 1048 assert_format!( 1049 r#"fn i() { 1050 -1_234_567 1051} 1052"# 1053 ); 1054 1055 assert_format!( 1056 r#"fn i() { 1057 -123_456 1058} 1059"# 1060 ); 1061 1062 assert_format!( 1063 r#"fn i() { 1064 -12_345 1065} 1066"# 1067 ); 1068 1069 assert_format!( 1070 r#"fn i() { 1071 -1234 1072} 1073"# 1074 ); 1075 1076 assert_format!( 1077 r#"fn i() { 1078 -123 1079} 1080"# 1081 ); 1082 1083 assert_format!( 1084 r#"fn i() { 1085 -12 1086} 1087"# 1088 ); 1089 1090 assert_format!( 1091 r#"fn i() { 1092 -1 1093} 1094"# 1095 ); 1096 1097 assert_format_rewrite!( 1098 r#"fn i() { 1099 1_234 1100} 1101"#, 1102 r#"fn i() { 1103 1234 1104} 1105"# 1106 ); 1107 1108 assert_format_rewrite!( 1109 r#"fn i() { 1110 12_34 1111} 1112"#, 1113 r#"fn i() { 1114 1234 1115} 1116"# 1117 ); 1118 1119 assert_format_rewrite!( 1120 r#"fn i() { 1121 123_4 1122} 1123"#, 1124 r#"fn i() { 1125 1234 1126} 1127"# 1128 ); 1129 1130 assert_format_rewrite!( 1131 r#"fn i() { 1132 1234_5 1133} 1134"#, 1135 r#"fn i() { 1136 12_345 1137} 1138"# 1139 ); 1140 1141 assert_format_rewrite!( 1142 r#"fn i() { 1143 12345_6 1144} 1145"#, 1146 r#"fn i() { 1147 123_456 1148} 1149"# 1150 ); 1151 1152 assert_format_rewrite!( 1153 r#"fn i() { 1154 123456_7 1155} 1156"#, 1157 r#"fn i() { 1158 1_234_567 1159} 1160"# 1161 ); 1162 1163 assert_format_rewrite!( 1164 r#"fn i() { 1165 1234567_8 1166} 1167"#, 1168 r#"fn i() { 1169 12_345_678 1170} 1171"# 1172 ); 1173 1174 assert_format_rewrite!( 1175 r#"fn i() { 1176 -1_234 1177} 1178"#, 1179 r#"fn i() { 1180 -1234 1181} 1182"# 1183 ); 1184 1185 assert_format_rewrite!( 1186 r#"fn i() { 1187 -12_34 1188} 1189"#, 1190 r#"fn i() { 1191 -1234 1192} 1193"# 1194 ); 1195 1196 assert_format_rewrite!( 1197 r#"fn i() { 1198 -123_4 1199} 1200"#, 1201 r#"fn i() { 1202 -1234 1203} 1204"# 1205 ); 1206 1207 assert_format_rewrite!( 1208 r#"fn i() { 1209 -1234_5 1210} 1211"#, 1212 r#"fn i() { 1213 -12_345 1214} 1215"# 1216 ); 1217 1218 assert_format_rewrite!( 1219 r#"fn i() { 1220 -12345_6 1221} 1222"#, 1223 r#"fn i() { 1224 -123_456 1225} 1226"# 1227 ); 1228 1229 assert_format_rewrite!( 1230 r#"fn i() { 1231 -123456_7 1232} 1233"#, 1234 r#"fn i() { 1235 -1_234_567 1236} 1237"# 1238 ); 1239 1240 assert_format_rewrite!( 1241 r#"fn i() { 1242 -1234567_8 1243} 1244"#, 1245 r#"fn i() { 1246 -12_345_678 1247} 1248"# 1249 ); 1250 1251 assert_format_rewrite!( 1252 r#"fn i() { 1253 let #(1_234, _) = #(1_234, Nil) 1254} 1255"#, 1256 r#"fn i() { 1257 let #(1234, _) = #(1234, Nil) 1258} 1259"# 1260 ); 1261 assert_format_rewrite!( 1262 r#"fn i() { 1263 let #(12_34, _) = #(12_34, Nil) 1264} 1265"#, 1266 r#"fn i() { 1267 let #(1234, _) = #(1234, Nil) 1268} 1269"# 1270 ); 1271 assert_format_rewrite!( 1272 r#"fn i() { 1273 let #(1234567_8, _) = #(1234567_8, Nil) 1274} 1275"#, 1276 r#"fn i() { 1277 let #(12_345_678, _) = #(12_345_678, Nil) 1278} 1279"# 1280 ); 1281 assert_format_rewrite!( 1282 r#"fn i() { 1283 let #(-1_234, _) = #(-1_234, Nil) 1284} 1285"#, 1286 r#"fn i() { 1287 let #(-1234, _) = #(-1234, Nil) 1288} 1289"# 1290 ); 1291 assert_format_rewrite!( 1292 r#"fn i() { 1293 let #(-12_34, _) = #(-12_34, Nil) 1294} 1295"#, 1296 r#"fn i() { 1297 let #(-1234, _) = #(-1234, Nil) 1298} 1299"# 1300 ); 1301 assert_format_rewrite!( 1302 r#"fn i() { 1303 let #(-1234567_8, _) = #(-1234567_8, Nil) 1304} 1305"#, 1306 r#"fn i() { 1307 let #(-12_345_678, _) = #(-12_345_678, Nil) 1308} 1309"# 1310 ); 1311 1312 assert_format_rewrite!( 1313 r#"const an_int = 1_234 1314"#, 1315 r#"const an_int = 1234 1316"# 1317 ); 1318 assert_format_rewrite!( 1319 r#"const an_int = 12_34 1320"#, 1321 r#"const an_int = 1234 1322"# 1323 ); 1324 assert_format_rewrite!( 1325 r#"const an_int = 1234567_8 1326"#, 1327 r#"const an_int = 12_345_678 1328"# 1329 ); 1330 assert_format_rewrite!( 1331 r#"const an_int = -1_234 1332"#, 1333 r#"const an_int = -1234 1334"# 1335 ); 1336 assert_format_rewrite!( 1337 r#"const an_int = -12_34 1338"#, 1339 r#"const an_int = -1234 1340"# 1341 ); 1342 assert_format_rewrite!( 1343 r#"const an_int = -1234567_8 1344"#, 1345 r#"const an_int = -12_345_678 1346"# 1347 ); 1348 1349 assert_format!("fn n() {\n 1_234_567\n}\n"); 1350 assert_format!("fn h() {\n 0xCAB005E\n}\n"); 1351 assert_format!("fn h() {\n 0xC_AB_00_5E\n}\n"); 1352 assert_format!("fn h() {\n 0xCA_B0_05_E\n}\n"); 1353 assert_format!("fn b() {\n 0b10100001\n}\n"); 1354 assert_format!("fn b() {\n 0b_1010_0001\n}\n"); 1355 assert_format!("fn o() {\n 0o1234567\n}\n"); 1356 assert_format!("fn o() {\n 0o1_234_567\n}\n"); 1357 assert_format!("fn o() {\n 0o_123_456_7\n}\n"); 1358} 1359 1360#[test] 1361fn expr_float() { 1362 assert_format_rewrite!( 1363 r#"fn f() { 1364 1. 1365} 1366"#, 1367 r#"fn f() { 1368 1.0 1369} 1370"# 1371 ); 1372 1373 assert_format_rewrite!( 1374 r#"fn f() { 1375 1.00 1376} 1377"#, 1378 r#"fn f() { 1379 1.0 1380} 1381"# 1382 ); 1383 1384 assert_format_rewrite!( 1385 r#"fn f() { 1386 1.00100 1387} 1388"#, 1389 r#"fn f() { 1390 1.001 1391} 1392"# 1393 ); 1394 1395 assert_format_rewrite!( 1396 r#"fn f() { 1397 1.001001 1398} 1399"#, 1400 r#"fn f() { 1401 1.001001 1402} 1403"# 1404 ); 1405 1406 assert_format_rewrite!( 1407 r#"fn f() { 1408 1.00e100_100 1409} 1410"#, 1411 r#"fn f() { 1412 1.0e100_100 1413} 1414"# 1415 ); 1416 1417 assert_format_rewrite!( 1418 r#"fn f() { 1419 1.00100e100_100 1420} 1421"#, 1422 r#"fn f() { 1423 1.001e100_100 1424} 1425"# 1426 ); 1427 1428 assert_format_rewrite!( 1429 r#"fn f() { 1430 1.001001e100_100 1431} 1432"#, 1433 r#"fn f() { 1434 1.001001e100_100 1435} 1436"# 1437 ); 1438 1439 assert_format!( 1440 r#"fn f() { 1441 1.0 1442} 1443"# 1444 ); 1445 1446 assert_format!( 1447 r#"fn f() { 1448 -1.0 1449} 1450"# 1451 ); 1452 1453 assert_format!( 1454 r#"fn f() { 1455 9999.6666 1456} 1457"# 1458 ); 1459 1460 assert_format!( 1461 r#"fn f() { 1462 -1_234_567_890.0 1463} 1464"# 1465 ); 1466 assert_format!( 1467 r#"fn f() { 1468 -1_234_567_890.0 1469} 1470"# 1471 ); 1472 assert_format!( 1473 r#"fn f() { 1474 -123_456_789.0 1475} 1476"# 1477 ); 1478 assert_format!( 1479 r#"fn f() { 1480 -12_345_678.0 1481} 1482"# 1483 ); 1484 assert_format!( 1485 r#"fn f() { 1486 -1_234_567.0 1487} 1488"# 1489 ); 1490 assert_format!( 1491 r#"fn f() { 1492 -123_456.0 1493} 1494"# 1495 ); 1496 assert_format!( 1497 r#"fn f() { 1498 -12_345.0 1499} 1500"# 1501 ); 1502 assert_format!( 1503 r#"fn f() { 1504 -1234.0 1505} 1506"# 1507 ); 1508 assert_format!( 1509 r#"fn f() { 1510 -123.0 1511} 1512"# 1513 ); 1514 assert_format!( 1515 r#"fn f() { 1516 -12.0 1517} 1518"# 1519 ); 1520 assert_format!( 1521 r#"fn f() { 1522 -1.0 1523} 1524"# 1525 ); 1526 assert_format!( 1527 r#"fn f() { 1528 -0.0 1529} 1530"# 1531 ); 1532 assert_format!( 1533 r#"fn f() { 1534 -1_234_567_890.1 1535} 1536"# 1537 ); 1538 assert_format!( 1539 r#"fn f() { 1540 -123_456_789.1 1541} 1542"# 1543 ); 1544 assert_format!( 1545 r#"fn f() { 1546 -12_345_678.1 1547} 1548"# 1549 ); 1550 assert_format!( 1551 r#"fn f() { 1552 -1_234_567.1 1553} 1554"# 1555 ); 1556 assert_format!( 1557 r#"fn f() { 1558 -123_456.1 1559} 1560"# 1561 ); 1562 assert_format!( 1563 r#"fn f() { 1564 -12_345.1 1565} 1566"# 1567 ); 1568 assert_format!( 1569 r#"fn f() { 1570 -1234.1 1571} 1572"# 1573 ); 1574 assert_format!( 1575 r#"fn f() { 1576 -123.1 1577} 1578"# 1579 ); 1580 assert_format!( 1581 r#"fn f() { 1582 -12.1 1583} 1584"# 1585 ); 1586 assert_format!( 1587 r#"fn f() { 1588 -1.1 1589} 1590"# 1591 ); 1592 assert_format!( 1593 r#"fn f() { 1594 -0.1 1595} 1596"# 1597 ); 1598 assert_format!( 1599 r#"fn f() { 1600 -1_234_567_890.123456 1601} 1602"# 1603 ); 1604 assert_format!( 1605 r#"fn f() { 1606 -123_456_789.123456 1607} 1608"# 1609 ); 1610 assert_format!( 1611 r#"fn f() { 1612 -12_345_678.123456 1613} 1614"# 1615 ); 1616 assert_format!( 1617 r#"fn f() { 1618 -1_234_567.123456 1619} 1620"# 1621 ); 1622 assert_format!( 1623 r#"fn f() { 1624 -123_456.123456 1625} 1626"# 1627 ); 1628 assert_format!( 1629 r#"fn f() { 1630 -12_345.123456 1631} 1632"# 1633 ); 1634 assert_format!( 1635 r#"fn f() { 1636 -1234.123456 1637} 1638"# 1639 ); 1640 assert_format!( 1641 r#"fn f() { 1642 -123.123456 1643} 1644"# 1645 ); 1646 assert_format!( 1647 r#"fn f() { 1648 -12.123456 1649} 1650"# 1651 ); 1652 assert_format!( 1653 r#"fn f() { 1654 -1.123456 1655} 1656"# 1657 ); 1658 assert_format!( 1659 r#"fn f() { 1660 -0.123456 1661} 1662"# 1663 ); 1664 1665 assert_format_rewrite!( 1666 r#"fn f() { 1667 1_234.0 1668} 1669"#, 1670 r#"fn f() { 1671 1234.0 1672} 1673"# 1674 ); 1675 assert_format_rewrite!( 1676 r#"fn f() { 1677 12_34.0 1678} 1679"#, 1680 r#"fn f() { 1681 1234.0 1682} 1683"# 1684 ); 1685 assert_format_rewrite!( 1686 r#"fn f() { 1687 123_4.0 1688} 1689"#, 1690 r#"fn f() { 1691 1234.0 1692} 1693"# 1694 ); 1695 assert_format_rewrite!( 1696 r#"fn f() { 1697 1234_5.0 1698} 1699"#, 1700 r#"fn f() { 1701 12_345.0 1702} 1703"# 1704 ); 1705 assert_format_rewrite!( 1706 r#"fn f() { 1707 12345_6.0 1708} 1709"#, 1710 r#"fn f() { 1711 123_456.0 1712} 1713"# 1714 ); 1715 assert_format_rewrite!( 1716 r#"fn f() { 1717 123456_7.0 1718} 1719"#, 1720 r#"fn f() { 1721 1_234_567.0 1722} 1723"# 1724 ); 1725 assert_format_rewrite!( 1726 r#"fn f() { 1727 1234567_8.0 1728} 1729"#, 1730 r#"fn f() { 1731 12_345_678.0 1732} 1733"# 1734 ); 1735 assert_format_rewrite!( 1736 r#"fn f() { 1737 -1_234.0 1738} 1739"#, 1740 r#"fn f() { 1741 -1234.0 1742} 1743"# 1744 ); 1745 assert_format_rewrite!( 1746 r#"fn f() { 1747 -12_34.0 1748} 1749"#, 1750 r#"fn f() { 1751 -1234.0 1752} 1753"# 1754 ); 1755 assert_format_rewrite!( 1756 r#"fn f() { 1757 -123_4.0 1758} 1759"#, 1760 r#"fn f() { 1761 -1234.0 1762} 1763"# 1764 ); 1765 assert_format_rewrite!( 1766 r#"fn f() { 1767 -1234_5.0 1768} 1769"#, 1770 r#"fn f() { 1771 -12_345.0 1772} 1773"# 1774 ); 1775 assert_format_rewrite!( 1776 r#"fn f() { 1777 -12345_6.0 1778} 1779"#, 1780 r#"fn f() { 1781 -123_456.0 1782} 1783"# 1784 ); 1785 assert_format_rewrite!( 1786 r#"fn f() { 1787 -123456_7.0 1788} 1789"#, 1790 r#"fn f() { 1791 -1_234_567.0 1792} 1793"# 1794 ); 1795 assert_format_rewrite!( 1796 r#"fn f() { 1797 -1234567_8.0 1798} 1799"#, 1800 r#"fn f() { 1801 -12_345_678.0 1802} 1803"# 1804 ); 1805 1806 assert_format_rewrite!( 1807 r#"fn f() { 1808 let #(1_234.0, _) = #(1_234.0, Nil) 1809} 1810"#, 1811 r#"fn f() { 1812 let #(1234.0, _) = #(1234.0, Nil) 1813} 1814"# 1815 ); 1816 assert_format_rewrite!( 1817 r#"fn f() { 1818 let #(12_34.0, _) = #(12_34.0, Nil) 1819} 1820"#, 1821 r#"fn f() { 1822 let #(1234.0, _) = #(1234.0, Nil) 1823} 1824"# 1825 ); 1826 assert_format_rewrite!( 1827 r#"fn f() { 1828 let #(1234567_8.0, _) = #(1234567_8.0, Nil) 1829} 1830"#, 1831 r#"fn f() { 1832 let #(12_345_678.0, _) = #(12_345_678.0, Nil) 1833} 1834"# 1835 ); 1836 assert_format_rewrite!( 1837 r#"fn f() { 1838 let #(-1_234.0, _) = #(-1_234.0, Nil) 1839} 1840"#, 1841 r#"fn f() { 1842 let #(-1234.0, _) = #(-1234.0, Nil) 1843} 1844"# 1845 ); 1846 assert_format_rewrite!( 1847 r#"fn f() { 1848 let #(-12_34.0, _) = #(-12_34.0, Nil) 1849} 1850"#, 1851 r#"fn f() { 1852 let #(-1234.0, _) = #(-1234.0, Nil) 1853} 1854"# 1855 ); 1856 assert_format_rewrite!( 1857 r#"fn f() { 1858 let #(-1234567_8.0, _) = #(-1234567_8.0, Nil) 1859} 1860"#, 1861 r#"fn f() { 1862 let #(-12_345_678.0, _) = #(-12_345_678.0, Nil) 1863} 1864"# 1865 ); 1866 assert_format_rewrite!( 1867 r#"const a_float = 1_234.0 1868"#, 1869 r#"const a_float = 1234.0 1870"# 1871 ); 1872 assert_format_rewrite!( 1873 r#"const a_float = 12_34.0 1874"#, 1875 r#"const a_float = 1234.0 1876"# 1877 ); 1878 assert_format_rewrite!( 1879 r#"const a_float = 1234567_8.0 1880"#, 1881 r#"const a_float = 12_345_678.0 1882"# 1883 ); 1884 assert_format_rewrite!( 1885 r#"const a_float = -1_234.0 1886"#, 1887 r#"const a_float = -1234.0 1888"# 1889 ); 1890 assert_format_rewrite!( 1891 r#"const a_float = -12_34.0 1892"#, 1893 r#"const a_float = -1234.0 1894"# 1895 ); 1896 assert_format_rewrite!( 1897 r#"const a_float = -1234567_8.0 1898"#, 1899 r#"const a_float = -12_345_678.0 1900"# 1901 ); 1902 1903 assert_format_rewrite!( 1904 r#"const a_float = 1234.00 1905"#, 1906 r#"const a_float = 1234.0 1907"# 1908 ); 1909 assert_format_rewrite!( 1910 r#"const a_float = 1234.00100 1911"#, 1912 r#"const a_float = 1234.001 1913"# 1914 ); 1915 assert_format_rewrite!( 1916 r#"const a_float = 1234.001001 1917"#, 1918 r#"const a_float = 1234.001001 1919"# 1920 ); 1921 1922 assert_format!( 1923 r#"fn f() { 1924 1.0e1 1925} 1926"# 1927 ); 1928 assert_format!( 1929 r#"fn f() { 1930 1.0e-1 1931} 1932"# 1933 ); 1934 assert_format!( 1935 r#"fn f() { 1936 -1.0e1 1937} 1938"# 1939 ); 1940 assert_format!( 1941 r#"fn f() { 1942 -1.0e-1 1943} 1944"# 1945 ); 1946 assert_format!( 1947 r#"fn f() { 1948 1.0e10 1949} 1950"# 1951 ); 1952 assert_format!( 1953 r#"fn f() { 1954 1.0e-10 1955} 1956"# 1957 ); 1958 assert_format!( 1959 r#"fn f() { 1960 -1.0e10 1961} 1962"# 1963 ); 1964 assert_format!( 1965 r#"fn f() { 1966 -11.0e-10 1967} 1968"# 1969 ); 1970 assert_format!( 1971 r#"fn f() { 1972 1.0e100 1973} 1974"# 1975 ); 1976 assert_format!( 1977 r#"fn f() { 1978 1.0e-100 1979} 1980"# 1981 ); 1982 assert_format!( 1983 r#"fn f() { 1984 -1.0e100 1985} 1986"# 1987 ); 1988 assert_format!( 1989 r#"fn f() { 1990 -11.0e-100 1991} 1992"# 1993 ); 1994 assert_format!( 1995 r#"fn f() { 1996 1.0e100 1997} 1998"# 1999 ); 2000 assert_format!( 2001 r#"fn f() { 2002 1.0e100_100 2003} 2004"# 2005 ); 2006 assert_format!( 2007 r#"fn f() { 2008 1.0e100_100 2009} 2010"# 2011 ); 2012 assert_format!( 2013 r#"fn f() { 2014 1.001e100_100 2015} 2016"# 2017 ); 2018} 2019 2020#[test] 2021fn expr_string() { 2022 assert_format!( 2023 r#"fn main() { 2024 "Hello" 2025} 2026"# 2027 ); 2028 2029 assert_format!( 2030 r#"fn main() { 2031 "Hello 2032 2033World" 2034} 2035"# 2036 ); 2037 2038 assert_format!( 2039 r#"fn main() { 2040 "\\n\\t" 2041} 2042"# 2043 ); 2044} 2045#[test] 2046fn expr_seq() { 2047 assert_format!( 2048 r#"fn main() { 2049 1 2050 2 2051 3 2052} 2053"# 2054 ); 2055 2056 assert_format!( 2057 r#"fn main() { 2058 first(1) 2059 1 2060} 2061"# 2062 ); 2063} 2064#[test] 2065fn expr_lists() { 2066 assert_format!( 2067 "fn main() { 2068 [] 2069} 2070" 2071 ); 2072 2073 assert_format!( 2074 "fn main() { 2075 [1] 2076} 2077" 2078 ); 2079 2080 assert_format!( 2081 "fn main() { 2082 [ 2083 { 2084 1 2085 2 2086 }, 2087 ] 2088} 2089" 2090 ); 2091 2092 assert_format!( 2093 "fn main() { 2094 [1, 2, 3] 2095} 2096" 2097 ); 2098 2099 assert_format!( 2100 "fn main() { 2101 [ 2102 1, 2103 { 2104 2 2105 3 2106 }, 2107 3, 2108 ] 2109} 2110" 2111 ); 2112 2113 assert_format!( 2114 "fn main() { 2115 [ 2116 really_long_variable_name, 2117 really_long_variable_name, 2118 really_long_variable_name, 2119 [1, 2, 3], 2120 really_long_variable_name, 2121 ] 2122} 2123" 2124 ); 2125 2126 assert_format!( 2127 "fn main() { 2128 [ 2129 really_long_variable_name, 2130 really_long_variable_name, 2131 really_long_variable_name, 2132 [ 2133 really_long_variable_name, 2134 really_long_variable_name, 2135 really_long_variable_name, 2136 2, 2137 3, 2138 [1, 2, 3, 4], 2139 ], 2140 really_long_variable_name, 2141 ] 2142} 2143" 2144 ); 2145 2146 assert_format!( 2147 "fn main() { 2148 [1, 2, 3, ..x] 2149} 2150" 2151 ); 2152 2153 assert_format!( 2154 "fn main() { 2155 [ 2156 really_long_variable_name, 2157 really_long_variable_name, 2158 really_long_variable_name, 2159 ..tail 2160 ] 2161} 2162" 2163 ); 2164 2165 assert_format!( 2166 "fn main() { 2167 [ 2168 really_long_variable_name, 2169 really_long_variable_name, 2170 really_long_variable_name, 2171 [ 2172 really_long_variable_name, 2173 really_long_variable_name, 2174 really_long_variable_name, 2175 2, 2176 3, 2177 [1, 2, 3, 4], 2178 ..tail 2179 ], 2180 really_long_variable_name, 2181 ] 2182} 2183" 2184 ); 2185} 2186 2187#[test] 2188fn expr_pipe() { 2189 assert_format!( 2190 r#"fn main() { 2191 1 2192 |> really_long_variable_name 2193 |> really_long_variable_name 2194 |> really_long_variable_name 2195 |> really_long_variable_name 2196 |> really_long_variable_name 2197 |> really_long_variable_name 2198} 2199"# 2200 ); 2201 2202 assert_format!( 2203 r#"fn main() { 2204 #( 2205 1 2206 |> succ 2207 |> succ, 2208 2, 2209 3, 2210 ) 2211} 2212"# 2213 ); 2214 2215 assert_format!( 2216 r#"fn main() { 2217 some_call( 2218 1 2219 |> succ 2220 |> succ, 2221 2, 2222 3, 2223 ) 2224} 2225"# 2226 ); 2227 2228 assert_format!( 2229 r#"fn main() { 2230 [ 2231 1 2232 |> succ 2233 |> succ, 2234 2, 2235 3, 2236 ] 2237} 2238"# 2239 ); 2240 2241 assert_format!( 2242 r#"fn main() { 2243 let x = 2244 1 2245 |> succ 2246 |> succ 2247 x 2248} 2249"# 2250 ); 2251 2252 assert_format!( 2253 r#"fn main() { 2254 #(1, 2) 2255 |> pair.first 2256 |> should.equal(1) 2257} 2258"# 2259 ); 2260 2261 assert_format!( 2262 r#"fn main() { 2263 #(1, 2) 2264 |> pair.first(1, 2, 4) 2265 |> should.equal(1) 2266} 2267"# 2268 ); 2269 2270 assert_format!( 2271 r#"fn main() { 2272 1 2273 // 1 2274 |> func1 2275 // 2 2276 |> func2 2277} 2278"# 2279 ); 2280 2281 // https://github.com/gleam-lang/gleam/issues/618 2282 2283 assert_format!( 2284 r#"fn main() { 2285 { 2286 1 2287 2 2288 } 2289 |> func 2290} 2291"# 2292 ); 2293 2294 assert_format!( 2295 r#"fn main() { 2296 1 2297 |> { 2298 1 2299 2 2300 } 2301} 2302"# 2303 ); 2304 2305 // https://github.com/gleam-lang/gleam/issues/658 2306 assert_format!( 2307 r#"fn main() { 2308 { os.system_time(os.Millisecond) < june_12_2020 * 1_000_000 } 2309 |> should.equal(True) 2310} 2311"# 2312 ); 2313 2314 assert_format!( 2315 r#"fn main() { 2316 { os.system_time(os.Millisecond) < june_12_2020 * 1_000_000 } 2317 |> transform 2318 |> should.equal(True) 2319} 2320"# 2321 ); 2322} 2323 2324#[test] 2325fn expr_let() { 2326 assert_format!( 2327 r#"fn main() { 2328 let x = 1 2329 Nil 2330} 2331"# 2332 ); 2333} 2334 2335#[test] 2336fn expr_let1() { 2337 assert_format!( 2338 r#"fn main() { 2339 let assert x = 1 2340 Nil 2341} 2342"# 2343 ); 2344} 2345 2346#[test] 2347fn expr_let2() { 2348 assert_format!( 2349 r#"fn main() { 2350 let x = { 2351 let y = 1 2352 y 2353 } 2354 Nil 2355} 2356"# 2357 ); 2358} 2359 2360#[test] 2361fn expr_let3() { 2362 assert_format!( 2363 r#"fn main() { 2364 let x = { 2365 1 2366 2 2367 } 2368 Nil 2369} 2370"# 2371 ); 2372} 2373 2374#[test] 2375fn expr_let4() { 2376 assert_format!( 2377 r#"fn main() { 2378 let y = case x { 2379 1 -> 1 2380 _ -> 0 2381 } 2382 y 2383} 2384"# 2385 ); 2386} 2387 2388#[test] 2389fn expr_let5() { 2390 assert_format!( 2391 r#"fn main() { 2392 let y = case x { 2393 1 -> 1 2394 2395 _ -> 0 2396 } 2397 y 2398} 2399"# 2400 ); 2401} 2402 2403#[test] 2404fn expr_let6() { 2405 assert_format!( 2406 r#"fn main() { 2407 let x = fn(x) { x } 2408 x 2409} 2410"# 2411 ); 2412} 2413 2414#[test] 2415fn expr_let7() { 2416 assert_format!( 2417 r#"fn main() { 2418 let x = fn() { 2419 1 2420 2 2421 } 2422 x 2423} 2424"# 2425 ); 2426} 2427 2428#[test] 2429fn expr_let8() { 2430 assert_format!( 2431 r#"fn main() { 2432 let x = fn( 2433 state: state, 2434 acc: visitor_acc, 2435 visitor: fn(visitor_acc, Pid(a)) -> new_visitor_acc, 2436 ) { 2437 1 2438 2 2439 } 2440 x 2441} 2442"# 2443 ); 2444} 2445 2446#[test] 2447fn expr_let9() { 2448 assert_format!( 2449 r#"fn main() { 2450 let x = fn( 2451 state: state, 2452 acc: visitor_acc, 2453 visitor: fn(visitor_acc, Pid(a)) -> new_visitor_acc, 2454 ) { 2455 2 2456 } 2457 x 2458} 2459"# 2460 ); 2461} 2462 2463#[test] 2464fn expr_let10() { 2465 assert_format!( 2466 r#"fn main() { 2467 let dict = map.from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3)]) 2468 1 2469} 2470"# 2471 ); 2472} 2473 2474#[test] 2475fn pattern_simple() { 2476 // Pattern::Float 2477 assert_format!( 2478 r#"fn main() { 2479 let 1 = 1 2480 Nil 2481} 2482"# 2483 ); 2484 2485 // Pattern::String 2486 assert_format!( 2487 r#"fn main() { 2488 let 1.0 = 1 2489 Nil 2490} 2491"# 2492 ); 2493 2494 // Pattern::Var 2495 assert_format!( 2496 r#"fn main() { 2497 let x = 1 2498 let y = 1 2499 Nil 2500} 2501"# 2502 ); 2503} 2504 2505#[test] 2506fn breakable_pattern() { 2507 assert_format!( 2508 r#"fn main() { 2509 let Ok(Thingybob( 2510 one: _one, 2511 two: _two, 2512 three: _three, 2513 four: _four, 2514 five: _five, 2515 six: _six, 2516 )) = 1 2517 Nil 2518} 2519"# 2520 ); 2521} 2522 2523#[test] 2524fn pattern_let() { 2525 assert_format!( 2526 r#"fn main() { 2527 let x as y = 1 2528 Nil 2529} 2530"# 2531 ); 2532 2533 assert_format!( 2534 r#"fn main() { 2535 let #(x, y, 123 as z) = 1 2536 Nil 2537} 2538"# 2539 ); 2540} 2541 2542#[test] 2543fn pattern_discard() { 2544 assert_format!( 2545 r#"fn main() { 2546 let _ = 1 2547 Nil 2548} 2549"# 2550 ); 2551 2552 assert_format!( 2553 r#"fn main() { 2554 let _wibble = 1 2555 Nil 2556} 2557"# 2558 ); 2559} 2560 2561#[test] 2562fn pattern_lists() { 2563 assert_format!( 2564 r#"fn main() { 2565 let [] = 1 2566 Nil 2567} 2568"# 2569 ); 2570 2571 assert_format!( 2572 r#"fn main() { 2573 let [1] = 1 2574 Nil 2575} 2576"# 2577 ); 2578 2579 assert_format!( 2580 r#"fn main() { 2581 let [1, 2, 3, 4] = 1 2582 Nil 2583} 2584"# 2585 ); 2586 2587 assert_format!( 2588 r#"fn main() { 2589 let [1, 2, 3, 4, ..x] = 1 2590 Nil 2591} 2592"# 2593 ); 2594 2595 assert_format!( 2596 r#"fn main() { 2597 let [ 2598 really_long_variable_name, 2599 really_long_variable_name, 2600 really_long_variable_name, 2601 [1, 2, 3, 4, xyz], 2602 ..thingy 2603 ] = 1 2604 Nil 2605} 2606"# 2607 ); 2608} 2609 2610#[test] 2611fn pattern_constructor() { 2612 assert_format!( 2613 r#"fn main() { 2614 let True = 1 2615 Nil 2616} 2617"# 2618 ); 2619 2620 assert_format!( 2621 r#"fn main() { 2622 let False = 1 2623 Nil 2624} 2625"# 2626 ); 2627 2628 assert_format!( 2629 r#"fn main() { 2630 let Ok(1) = 1 2631 Nil 2632} 2633"# 2634 ); 2635 2636 assert_format!( 2637 r#"fn main() { 2638 let Person(name, age: the_age) = 1 2639 Nil 2640} 2641"# 2642 ); 2643 2644 assert_format!( 2645 r#"fn main() { 2646 let Person(name: the_name, age: the_age) = 1 2647 Nil 2648} 2649"# 2650 ); 2651 2652 assert_format!( 2653 r#"fn main() { 2654 let Person(age: age, name: name) = 1 2655 Nil 2656} 2657"# 2658 ); 2659 2660 assert_format!( 2661 r#"fn main() { 2662 let Person(age: really_long_variable_name, name: really_long_variable_name) = 2663 1 2664 Nil 2665} 2666"# 2667 ); 2668} 2669 2670#[test] 2671fn pattern_tuple() { 2672 assert_format!( 2673 r#"fn main() { 2674 let #() = 1 2675 Nil 2676} 2677"# 2678 ); 2679 2680 assert_format!( 2681 r#"fn main() { 2682 let #(x) = 1 2683 Nil 2684} 2685"# 2686 ); 2687 2688 assert_format!( 2689 r#"fn main() { 2690 let #(x, y) = 1 2691 Nil 2692} 2693"# 2694 ); 2695 2696 assert_format!( 2697 r#"fn main() { 2698 let #(x, y, z) = 1 2699 Nil 2700} 2701"# 2702 ); 2703} 2704 2705#[test] 2706fn expr_case() { 2707 assert_format!( 2708 r#"fn main() { 2709 case 1 { 2710 1 -> { 2711 1 2712 2 2713 } 2714 1 -> 1 2715 } 2716} 2717"# 2718 ); 2719 2720 assert_format!( 2721 r#"fn main() { 2722 case 1 { 2723 1 -> { 2724 let x = 1 2725 x 2726 } 2727 1 -> 1 2728 } 2729} 2730"# 2731 ); 2732 2733 assert_format!( 2734 r#"fn do() { 2735 case list { 2736 [x, ..xs] -> { 2737 let x = 1 2738 x 2739 } 2740 } 2741} 2742"# 2743 ); 2744 2745 assert_format!( 2746 r#"fn do() { 2747 case list { 2748 [x, ..xs] -> { 2749 let x = 1 2750 x 2751 1 2752 2 2753 3 2754 4 2755 } 2756 } 2757} 2758"# 2759 ); 2760 2761 assert_format!( 2762 "fn main() { 2763 case x { 2764 1 -> 2 2765 2766 2 -> 3 2767 2768 _ -> 0 2769 } 2770} 2771" 2772 ); 2773 2774 assert_format!( 2775 r#"fn main() { 2776 case bool { 2777 True -> { 2778 "Wibble" 2779 |> io.println 2780 2781 "Wobble" 2782 |> io.println 2783 2784 Nil 2785 } 2786 False -> Nil 2787 } 2788} 2789"# 2790 ); 2791} 2792 2793#[test] 2794fn expr_case_nested() { 2795 assert_format!( 2796 r#"fn main() { 2797 case 1 { 2798 1 -> 2799 case x { 2800 1 -> 1 2801 _ -> 0 2802 } 2803 1 -> 1 2804 } 2805} 2806"# 2807 ); 2808 2809 assert_format!( 2810 r#"fn main() { 2811 case list { 2812 [x] -> 2813 case x { 2814 _ -> 1 2815 } 2816 } 2817} 2818"# 2819 ); 2820} 2821 2822#[test] 2823fn expr_case_then_fn() { 2824 assert_format!( 2825 r#"fn main() { 2826 case 1 { 2827 1 -> fn(x) { x } 2828 1 -> 1 2829 } 2830} 2831"# 2832 ); 2833 2834 assert_format!( 2835 r#"fn main() { 2836 case 1 { 2837 1 -> fn() { 2838 1 2839 2 2840 } 2841 1 -> 1 2842 } 2843} 2844"# 2845 ); 2846} 2847 2848#[test] 2849fn expr_case_multiple_subjects() { 2850 assert_format!( 2851 r#"fn main() { 2852 case 1 { 2853 1 -> 1 2854 1 -> 1 2855 } 2856} 2857"# 2858 ); 2859 2860 assert_format!( 2861 r#"fn main() { 2862 case 1, 2, 3, 4 { 2863 1, 2, 3, 4 -> 1 2864 1, 2, 3, 4 -> 1 2865 } 2866} 2867"# 2868 ); 2869} 2870 2871#[test] 2872fn expr_case_alternative_patterns() { 2873 assert_format!( 2874 r#"fn main() { 2875 case 1 { 2876 1 | 2 | 3 -> Nil 2877 } 2878} 2879"# 2880 ); 2881 2882 assert_format!( 2883 r#"fn main() { 2884 case 1, 2 { 2885 1, 1 | 2, 2 | 3, 3 -> Nil 2886 1, 1 | 2, 2 | 3, 3 -> Nil 2887 1, 1 | 2, 2 | 3, 3 -> Nil 2888 1, 1 | 2, 2 | 3, 3 -> Nil 2889 } 2890} 2891"# 2892 ); 2893 2894 assert_format!( 2895 r#"fn main() { 2896 case pat { 2897 pat.Typeof("Boolean", pat) 2898 | pat.Typeof("Number", pat) 2899 | pat.Typeof("String", pat) -> Nil 2900 } 2901} 2902"# 2903 ); 2904} 2905 2906#[test] 2907fn expr_case_clause_guards() { 2908 assert_format!( 2909 r#"fn main() { 2910 case 1 { 2911 _ if x == y -> Nil 2912 } 2913} 2914"# 2915 ); 2916 2917 assert_format!( 2918 r#"fn main() { 2919 case "x" { 2920 _ if x == "x" -> Nil 2921 } 2922} 2923"# 2924 ); 2925 2926 assert_format!( 2927 r#"fn main() { 2928 case #(1, 2, 3) { 2929 _ if x == #(1, 2, 3) -> Nil 2930 } 2931} 2932"# 2933 ); 2934 2935 assert_format!( 2936 r#"type Test { 2937 Test(x: Int, y: Float) 2938} 2939 2940pub fn main() { 2941 let x = Test(1, 3.0) 2942 case x { 2943 _ if x == Test(1, 1.0) -> 1 2944 _ if x == Test(y: 2.0, x: 2) -> 2 2945 _ if x != Test(2, 3.0) -> 2 2946 _ -> 0 2947 } 2948} 2949"# 2950 ); 2951 2952 assert_format!( 2953 r#"fn main() { 2954 case 1 { 2955 _ if x != y -> Nil 2956 } 2957} 2958"# 2959 ); 2960 2961 assert_format!( 2962 r#"fn main() { 2963 case 1 { 2964 _ if x || y -> Nil 2965 } 2966} 2967"# 2968 ); 2969 2970 assert_format!( 2971 r#"fn main() { 2972 case 1 { 2973 _ if x && y -> Nil 2974 } 2975} 2976"# 2977 ); 2978 2979 assert_format!( 2980 r#"fn main() { 2981 case 1 { 2982 _ if x != y && x == z -> Nil 2983 } 2984} 2985"# 2986 ); 2987} 2988 2989#[test] 2990fn expr_case_clause_comments() { 2991 assert_format!( 2992 r#"fn main() { 2993 case 1 { 2994 // Hello Louis! 2995 1 | 2 | 3 -> Nil 2996 } 2997} 2998"# 2999 ); 3000 3001 assert_format!( 3002 r#"fn main() { 3003 case 1 { 3004 // Hello José! 3005 1 | 2 -> Nil 3006 // Hello Louis! 3007 n -> Nil 3008 } 3009} 3010"# 3011 ); 3012 3013 assert_format!( 3014 r#"fn main() { 3015 case 1 { 3016 // Hello Joe! 3017 1 | 2 -> Nil 3018 3019 // Hello Louis! 3020 n -> Nil 3021 } 3022} 3023"# 3024 ); 3025 3026 assert_format!( 3027 r#"fn main() { 3028 case pat { 3029 // Hello Ada 3030 pat.Typeof("Boolean", pat) | pat.Typeof("Number", pat) -> True 3031 3032 // Hello Alan 3033 pat.Typeof("Boolean", pat) 3034 | pat.Typeof("Number", pat) 3035 | pat.Typeof("String", pat) -> False 3036 } 3037} 3038"# 3039 ); 3040} 3041 3042#[test] 3043fn field_access() { 3044 assert_format!( 3045 r#"fn main() { 3046 one.two 3047} 3048"# 3049 ); 3050 3051 assert_format!( 3052 r#"fn main() { 3053 one.two.three.four 3054} 3055"# 3056 ); 3057} 3058 3059#[test] 3060fn tuple_access() { 3061 assert_format!( 3062 r#"fn main() { 3063 tup.0 3064} 3065"# 3066 ); 3067} 3068 3069#[test] 3070fn tuple_access1() { 3071 assert_format!( 3072 r#"fn main() { 3073 tup.1 3074} 3075"# 3076 ); 3077} 3078 3079#[test] 3080fn tuple_access2() { 3081 assert_format!( 3082 r#"fn main() { 3083 tup.777 3084} 3085"# 3086 ); 3087} 3088 3089#[test] 3090fn tuple_access3() { 3091 assert_format!( 3092 r#"fn main() { 3093 tup.1.2 3094} 3095"# 3096 ); 3097} 3098 3099#[test] 3100fn expr_panic() { 3101 assert_format!( 3102 "fn main() { 3103 panic 3104} 3105" 3106 ); 3107} 3108 3109#[test] 3110fn expr_panic_as() { 3111 assert_format!( 3112 r#"fn main() { 3113 panic as "panicking" 3114} 3115"# 3116 ); 3117} 3118 3119#[test] 3120fn expr_panic_as_value() { 3121 assert_format!( 3122 r#"fn main() { 3123 let x = "panicking" <> "with a value" 3124 panic as x 3125} 3126"# 3127 ); 3128} 3129 3130#[test] 3131fn expr_todo_as_value() { 3132 assert_format!( 3133 r#"fn main() { 3134 let x = "Need to" <> "do this" 3135 todo as x 3136} 3137"# 3138 ); 3139} 3140 3141#[test] 3142fn expr_todo() { 3143 assert_format!( 3144 "fn main() { 3145 todo 3146} 3147" 3148 ); 3149} 3150 3151#[test] 3152fn expr_todo_with_label() { 3153 assert_format!( 3154 r#"fn main() { 3155 todo as "todo with a label" 3156} 3157"# 3158 ); 3159} 3160 3161#[test] 3162fn expr_todo1() { 3163 assert_format_rewrite!( 3164 r#"fn main() { 3165 fn() {} 3166} 3167"#, 3168 r#"fn main() { 3169 fn() { todo } 3170} 3171"# 3172 ); 3173} 3174 3175#[test] 3176fn doc_comments_test() { 3177 assert_format!( 3178 "/// one 3179fn main() { 3180 Nil 3181} 3182" 3183 ); 3184} 3185 3186#[test] 3187fn doc_comments_1_test() { 3188 assert_format!( 3189 "/// one 3190///two 3191fn main() { 3192 Nil 3193} 3194" 3195 ); 3196} 3197 3198#[test] 3199fn doc_comments_2_test() { 3200 assert_format!( 3201 r#"/// one 3202///two 3203@external(javascript, "", "") 3204fn whatever() -> Nil 3205"# 3206 ); 3207} 3208 3209#[test] 3210fn doc_comments_3_test() { 3211 assert_format!( 3212 r#"/// one 3213///two 3214type Thingy 3215"# 3216 ); 3217} 3218 3219#[test] 3220fn doc_comments_4_test() { 3221 assert_format!( 3222 r#"/// one 3223///two 3224type Thingy 3225"# 3226 ); 3227} 3228 3229#[test] 3230fn doc_comments_5_test() { 3231 assert_format!( 3232 r#"/// one 3233///two 3234type Whatever { 3235 Whatever 3236} 3237"# 3238 ); 3239} 3240 3241#[test] 3242fn doc_comments_6_test() { 3243 assert_format!( 3244 r#"/// one 3245///two 3246type Whatever = 3247 Int 3248"# 3249 ); 3250} 3251 3252#[test] 3253fn comments3() { 3254 assert_format!( 3255 "// one 3256fn main() { 3257 Nil 3258} 3259" 3260 ); 3261} 3262 3263#[test] 3264fn comments4() { 3265 assert_format!( 3266 "// one 3267//two 3268fn main() { 3269 Nil 3270} 3271" 3272 ); 3273} 3274 3275#[test] 3276fn comments5() { 3277 assert_format!( 3278 r#"// one 3279//two 3280@external(javascript, "", "") 3281fn whatever() -> Nil 3282"# 3283 ); 3284} 3285 3286#[test] 3287fn comments9() { 3288 assert_format!( 3289 r#"// one 3290//two 3291type Whatever = 3292 Int 3293"# 3294 ); 3295} 3296 3297#[test] 3298fn comment23() { 3299 assert_format!( 3300 "fn main() { 3301 // Hello 3302 // world 3303 1 3304} 3305" 3306 ); 3307} 3308 3309#[test] 3310fn comment24() { 3311 assert_format!( 3312 "fn main() { 3313 // Hello 3314 // world 3315 1.0 3316} 3317" 3318 ); 3319} 3320 3321#[test] 3322fn comment25() { 3323 assert_format!( 3324 "fn main() { 3325 // Hello 3326 // world 3327 Nil 3328} 3329" 3330 ); 3331} 3332 3333#[test] 3334fn comment14() { 3335 assert_format!( 3336 "fn main() { 3337 // Hello 3338 // world 3339 [] 3340} 3341" 3342 ); 3343} 3344 3345#[test] 3346fn comment15() { 3347 assert_format!( 3348 "fn main() { 3349 // Hello 3350 // world 3351 [ 3352 // One 3353 1, 3354 // Two 3355 2, 3356 ] 3357} 3358" 3359 ); 3360} 3361 3362#[test] 3363fn comment16() { 3364 assert_format!( 3365 "fn main() { 3366 // Hello 3367 // world 3368 [ 3369 // One 3370 1, 3371 // Two 3372 2, 3373 [ 3374 // Five 3375 3, 3376 [ 3377 // Four 3378 4, 3379 ], 3380 ], 3381 ] 3382} 3383" 3384 ); 3385} 3386 3387#[test] 3388fn comment17() { 3389 assert_format!( 3390 "fn main() { 3391 // Hello 3392 // world 3393 one( 3394 // One 3395 1, 3396 // Two 3397 2, 3398 two( 3399 // Five 3400 3, 3401 three( 3402 // Four 3403 4, 3404 ), 3405 ), 3406 ) 3407} 3408" 3409 ); 3410} 3411 3412#[test] 3413fn comment18() { 3414 assert_format!( 3415 "fn main() { 3416 // Hello 3417 1 3418 // world 3419 2 3420} 3421" 3422 ); 3423} 3424 3425#[test] 3426fn comment19() { 3427 assert_format!( 3428 "fn main() { 3429 let // hello 3430 x = 1 3431 x 3432} 3433" 3434 ); 3435} 3436 3437#[test] 3438fn comment20() { 3439 assert_format!( 3440 "fn main() { 3441 let [ 3442 // 1 3443 1, 3444 // 2 3445 2, 3446 ] = xs 3447 x 3448} 3449" 3450 ); 3451} 3452 3453#[test] 3454fn comment21() { 3455 assert_format!( 3456 "pub type Spec { 3457 Spec( 3458 // Hello 3459 hello: Int, 3460 // World 3461 world: Int, 3462 ) 3463} 3464" 3465 ); 3466} 3467 3468#[test] 3469fn comment22() { 3470 assert_format!( 3471 "/// ß↑e̊ 3472/// 3473pub fn one() { 3474 1 3475} 3476 3477pub fn two() { 3478 2 3479} 3480", 3481 ); 3482} 3483 3484#[test] 3485fn trailing_comments() { 3486 assert_format!( 3487 "fn main() { 3488 x 3489} 3490// Hello world 3491// ok! 3492" 3493 ); 3494 3495 assert_format!( 3496 "fn main() { 3497 x 3498} 3499/// Hello world 3500/// ok! 3501" 3502 ); 3503 assert_format!( 3504 "fn main() { 3505 x 3506} 3507/// Hello world 3508/// ok! 3509// Hello world 3510// ok! 3511" 3512 ); 3513} 3514 3515#[test] 3516fn commented_fn_arguments() { 3517 assert_format!( 3518 "fn main( 3519 // comment 3520 label argument: Type, 3521) { 3522 x 3523} 3524" 3525 ); 3526} 3527 3528#[test] 3529fn commented_fn_arguments1() { 3530 assert_format!( 3531 "fn main( 3532 // comment1 3533 label argument1: Type, 3534 // comment2 3535 label argument2: Type, 3536) { 3537 x 3538} 3539" 3540 ); 3541} 3542 3543#[test] 3544fn commented_fn_arguments2() { 3545 assert_format!( 3546 "@external(erlang, \"\", \"\") 3547pub fn main( 3548 // comment1 3549 argument1: Type, 3550 // comment2 3551 argument2: Type, 3552) -> Int 3553" 3554 ); 3555} 3556 3557#[test] 3558fn commented_binop() { 3559 assert_format!( 3560 "fn main() { 3561 1 3562 // hello 3563 + 2 3564} 3565" 3566 ); 3567 3568 assert_format!( 3569 "fn main() { 3570 // one 3571 1 3572 // two 3573 + 2 3574 // three 3575 + 3 3576} 3577" 3578 ); 3579} 3580 3581#[test] 3582fn commented_constructors() { 3583 assert_format!( 3584 "pub type Number { 3585 // 1 3586 One 3587 // 2 3588 Two 3589 // 3 3590 Three 3591 // ??? 3592 More 3593} 3594" 3595 ); 3596 3597 assert_format!( 3598 "pub type Number { 3599 /// 1 3600 One 3601 /// 2 3602 Two 3603 /// 3 3604 Three 3605 /// ??? 3606 More 3607} 3608" 3609 ); 3610 3611 assert_format!( 3612 "pub type Number { 3613 // a 3614 /// 1 3615 One 3616 // b 3617 /// 2 3618 Two 3619 // c 3620 /// 3 3621 Three 3622 // defg 3623 /// ??? 3624 More 3625} 3626" 3627 ); 3628 3629 assert_format!( 3630 "pub type Number { 3631 /// 1 3632 One(value: Int) 3633 /// > 1 3634 Many(value: Int) 3635} 3636" 3637 ); 3638} 3639 3640#[test] 3641fn function_captures_test() { 3642 assert_format_rewrite!( 3643 "pub fn main() { 3644 run(_) 3645} 3646", 3647 "pub fn main() { 3648 run 3649} 3650" 3651 ); 3652 3653 assert_format!( 3654 "pub fn main() { 3655 run(1, 2, _, 4, 5) 3656} 3657" 3658 ); 3659 3660 assert_format_rewrite!( 3661 "pub fn main() { 3662 run(1, 2, _, 4, 5)(_) 3663} 3664", 3665 "pub fn main() { 3666 run(1, 2, _, 4, 5) 3667} 3668" 3669 ); 3670} 3671 3672#[test] 3673fn pattern_record_spread() { 3674 assert_format!( 3675 "type Triple { 3676 Triple(a: Int, b: Int, c: Int) 3677} 3678 3679fn main() { 3680 let triple = Triple(1, 2, 3) 3681 let Triple(the_a, c: the_c, ..) = triple 3682 the_c 3683} 3684" 3685 ); 3686 3687 // Formats the operator spread syntax with long names 3688 assert_format!( 3689 "type Triple { 3690 Triple(a: Int, b: Int, c: Int) 3691} 3692 3693fn main() { 3694 let triple = Triple(1, 2, 3) 3695 let Triple( 3696 really_really_long_variable_name_a, 3697 c: really_really_long_variable_name_c, 3698 .., 3699 ) = triple 3700 really_really_long_variable_name_c 3701} 3702" 3703 ); 3704 3705 // https://github.com/gleam-lang/gleam/issues/776 3706 assert_format!( 3707 "fn main() { 3708 let Triple(..) = triple() 3709 1 3710} 3711" 3712 ); 3713} 3714 3715#[test] 3716fn empty_lines() { 3717 assert_format!( 3718 "pub fn main() { 3719 1 3720 3721 2 3722} 3723" 3724 ); 3725 3726 assert_format!( 3727 "pub fn main() { 3728 // one 3729 1 3730 3731 // two 3732 2 3733} 3734" 3735 ); 3736 3737 assert_format!( 3738 "pub fn main() { 3739 // one 3740 1 3741 3742 // two 3743 2 3744 3745 // three 3746 3 3747} 3748" 3749 ); 3750 3751 assert_format!( 3752 "pub type Number { 3753 One 3754 3755 Two 3756 3757 Three 3758} 3759" 3760 ); 3761 3762 assert_format!( 3763 "pub fn main() { 3764 let x = 1 3765 3766 x 3767} 3768" 3769 ); 3770 3771 // Lines with only spaces are treated as empty 3772 assert_format_rewrite!( 3773 "pub fn main() { 3774 let x = 1\n \n x 3775} 3776", 3777 "pub fn main() { 3778 let x = 1 3779 3780 x 3781} 3782" 3783 ); 3784 3785 assert_format!( 3786 "pub fn main() { 3787 let inc = fn(a) { a + 1 } 3788 3789 pair.map_first(#(1, 2), inc) 3790 |> should.equal(#(2, 2)) 3791 3792 pair.map_first(#(1, 2), inc) 3793 |> should.equal(#(2, 2)) 3794} 3795" 3796 ); 3797} 3798 3799#[test] 3800fn modules_docs() { 3801 assert_format!( 3802 "//// One 3803//// Two 3804//// Three 3805 3806pub fn main() { 3807 let x = 1 3808 3809 x 3810} 3811" 3812 ); 3813 3814 assert_format!( 3815 "//// 3816//// 3817//// 3818//// 3819//// 3820 3821type X { 3822 X 3823} 3824// Hello 3825" 3826 ); 3827} 3828 3829#[test] 3830fn binary_operator_precedence() { 3831 assert_format!( 3832 "fn main() { 3833 { 1 + 2 } * 3 3834} 3835" 3836 ); 3837 3838 assert_format!( 3839 "fn main() { 3840 3 * { 1 + 2 } 3841} 3842" 3843 ); 3844 3845 assert_format!( 3846 "fn main() { 3847 3 3848 * { 3849 1 3850 |> inc 3851 } 3852} 3853" 3854 ); 3855 3856 assert_format!( 3857 "fn main() { 3858 { 3859 1 3860 |> inc 3861 } 3862 * 3 3863} 3864" 3865 ); 3866 3867 assert_format!( 3868 "fn main() { 3869 1 3870 |> { a || b } 3871} 3872" 3873 ); 3874 3875 assert_format!( 3876 "fn main() { 3877 { a || b } 3878 |> go 3879} 3880" 3881 ); 3882} 3883 3884// https://github.com/gleam-lang/gleam/issues/868 3885#[test] 3886fn precedence_rhs() { 3887 assert_format!( 3888 "fn main() { 3889 True != { a == b } 3890} 3891" 3892 ); 3893 3894 assert_format!( 3895 "fn main() { 3896 True != { a == { b != c } } 3897} 3898" 3899 ); 3900} 3901 3902#[test] 3903fn module_constants() { 3904 assert_format!( 3905 "pub const str = \"a string\" 3906 3907const my_constant: String = \"Hello\" 3908 3909pub const int = 4 3910 3911pub const float = 3.14 3912" 3913 ); 3914} 3915 3916#[test] 3917fn concise_wrapping_of_simple_lists() { 3918 assert_format!( 3919 "pub fn main() { 3920 [ 3921 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 3922 1500, 1600, 1700, 1800, 1900, 2000, 3923 ] 3924} 3925" 3926 ); 3927 3928 assert_format!( 3929 "pub fn main() { 3930 [ 3931 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 1.0, 11.0, 12.0, 13.0, 14.0, 3932 15.0, 16.0, 17.0, 18.0, 19.0, 2.0, 3933 ] 3934} 3935" 3936 ); 3937 3938 assert_format!( 3939 r#"pub fn main() { 3940 [ 3941 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", 3942 "ten", "eleven", "twelve", 3943 ] 3944} 3945"# 3946 ); 3947 3948 assert_format!( 3949 "const values = [ 3950 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 3951 1500, 1600, 1700, 1800, 1900, 2000, 3952] 3953" 3954 ); 3955 3956 assert_format!( 3957 "const values = [ 3958 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 1.0, 11.0, 12.0, 13.0, 14.0, 15.0, 3959 16.0, 17.0, 18.0, 19.0, 2.0, 3960] 3961" 3962 ); 3963 3964 assert_format!( 3965 r#"const values = [ 3966 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 3967 "eleven", "twelve", 3968] 3969"# 3970 ); 3971} 3972 3973#[test] 3974fn commented_labelled_arguments() { 3975 assert_format!( 3976 "fn main() { 3977 Emulator( 3978 // one 3979 one: 1, 3980 // two 3981 two: 1, 3982 ) 3983} 3984" 3985 ); 3986 3987 assert_format!( 3988 "fn main() { 3989 my_func( 3990 // one 3991 one: 1, 3992 // two 3993 two: 1, 3994 ) 3995} 3996" 3997 ); 3998} 3999 4000#[test] 4001fn module_rewrites_test() { 4002 // Module comments are moved to the top 4003 assert_format_rewrite!( 4004 "//// One 4005 4006//// Two 4007 4008fn main() { 4009 1 4010} 4011//// Three 4012 4013//// Four 4014", 4015 "//// One 4016//// Two 4017//// Three 4018//// Four 4019 4020fn main() { 4021 1 4022} 4023", 4024 ); 4025 4026 // Superfluous function captures are removed from pipe expressions 4027 assert_format_rewrite!( 4028 "fn main() { 4029 1 4030 |> run(_, 1) 4031} 4032", 4033 "fn main() { 4034 1 4035 |> run(1) 4036} 4037", 4038 ); 4039 4040 assert_format_rewrite!( 4041 "fn main() { 4042 1 4043 |> run(_) 4044} 4045", 4046 "fn main() { 4047 1 4048 |> run 4049} 4050", 4051 ); 4052 4053 assert_format_rewrite!( 4054 "fn main() { 4055 let some_really_long_variable_name_to_force_wrapping = 1 4056 let bits = << 4057 some_really_long_variable_name_to_force_wrapping, 4058 some_really_long_variable_name_to_force_wrapping, 4059 >> 4060 bits 4061} 4062", 4063 "fn main() { 4064 let some_really_long_variable_name_to_force_wrapping = 1 4065 let bits = << 4066 some_really_long_variable_name_to_force_wrapping, 4067 some_really_long_variable_name_to_force_wrapping, 4068 >> 4069 bits 4070} 4071", 4072 ); 4073} 4074 4075// TODO: improve. This is too wide 4076#[test] 4077// https://github.com/gleam-lang/gleam/issues/748 4078fn assignments_break_value_first_test() { 4079 assert_format!( 4080 r#"fn main() { 4081 let assert Ok(1) = [ 4082 10_000_000_000_000_000_000_000_000_001, 4083 20_000_000_000_000_000_000_000_000_001, 4084 30_000_000_000_000_000_000_000_000_001, 4085 40_000_000_000_000_000_000_000_000_001, 4086 ] 4087 Nil 4088} 4089"# 4090 ); 4091 4092 assert_format!( 4093 r#"fn main() { 4094 let assert Ok(1) = [ 4095 1_000_000_000_000_000_000_000_000_000, 2_000_000_000_000_000_000_000_000_000, 4096 3_000_000_000_000_000_000_000_000_000, 4_000_000_000_000_000_000_000_000_000, 4097 ] 4098 Nil 4099} 4100"# 4101 ); 4102 4103 assert_format!( 4104 r#"fn main() { 4105 let assert <<11, 2, 4, 5, 6>> = [ 4106 10_000_000_000_000_000_000_000_000_001, 4107 20_000_000_000_000_000_000_000_000_001, 4108 30_000_000_000_000_000_000_000_000_001, 4109 40_000_000_000_000_000_000_000_000_001, 4110 ] 4111 Nil 4112} 4113"# 4114 ); 4115 4116 assert_format!( 4117 r#"fn main() { 4118 let assert <<11, 2, 4, 5, 6>> = [ 4119 1_000_000_000_000_000_000_000_000_000, 2_000_000_000_000_000_000_000_000_000, 4120 3_000_000_000_000_000_000_000_000_000, 4_000_000_000_000_000_000_000_000_000, 4121 ] 4122 Nil 4123} 4124"# 4125 ); 4126 4127 assert_format!( 4128 r#"fn main() { 4129 let assert [11, 2, 4, 5, 6] = [ 4130 10_000_000_000_000_000_000_000_000_001, 4131 20_000_000_000_000_000_000_000_000_001, 4132 30_000_000_000_000_000_000_000_000_001, 4133 40_000_000_000_000_000_000_000_000_001, 4134 ] 4135 Nil 4136} 4137"# 4138 ); 4139 4140 assert_format!( 4141 r#"fn main() { 4142 let assert [11, 2, 4, 5, 6] = [ 4143 1_000_000_000_000_000_000_000_000_000, 2_000_000_000_000_000_000_000_000_000, 4144 3_000_000_000_000_000_000_000_000_000, 4_000_000_000_000_000_000_000_000_000, 4145 ] 4146 Nil 4147} 4148"# 4149 ); 4150} 4151 4152#[test] 4153fn function_type_type() { 4154 assert_format!( 4155 "type F = 4156 fn(some, really, long, set, of, arguments) -> 4157 #(some, really, long, set, of, arguments) 4158" 4159 ); 4160} 4161 4162#[test] 4163fn tuple_constant() { 4164 assert_format!( 4165 "const x: #(Int, Int) = #(1, 2) 4166" 4167 ); 4168} 4169 4170#[test] 4171fn var_constant() { 4172 assert_format!( 4173 r#"const x = 1 4174 4175const x_alias = x 4176 4177fn f(i: Int) -> Int { 4178 i 4179} 4180 4181const f_alias: fn(Int) -> Int = f 4182"# 4183 ); 4184} 4185 4186#[test] 4187fn let_as_expression() { 4188 assert_format!( 4189 "pub fn main() { 4190 let x = 1 4191} 4192" 4193 ); 4194 4195 assert_format!( 4196 "pub fn main() { 4197 let x = { 4198 let y = 1 4199 } 4200} 4201" 4202 ); 4203} 4204 4205#[test] 4206fn assert_as_expression() { 4207 assert_format!( 4208 "pub fn main() { 4209 let assert x = 1 4210} 4211" 4212 ); 4213 4214 assert_format!( 4215 "pub fn main() { 4216 let assert x = { 4217 let assert y = 1 4218 } 4219} 4220" 4221 ); 4222} 4223 4224#[test] 4225fn case_in_call() { 4226 assert_format!( 4227 "fn clause_guard_tests(_fns) -> List(Test) { 4228 example(fn() { 4229 assert_equal(0, case Nil { 4230 _ if yes -> 0 4231 _ -> 1 4232 }) 4233 }) 4234} 4235" 4236 ); 4237} 4238 4239// https://github.com/gleam-lang/gleam/issues/1390 4240#[test] 4241fn list_spread_pattern() { 4242 assert_format!( 4243 "pub fn main(x) { 4244 case x { 4245 [y, ..] -> y 4246 _ -> 0 4247 } 4248} 4249" 4250 ); 4251} 4252 4253// https://github.com/gleam-lang/gleam/issues/1431 4254#[test] 4255fn first_argument_capture_special_case_list() { 4256 assert_format!( 4257 r#"pub fn main(x) { 4258 wibble(_, [ 4259 "one argument that is both breakable and long enough to cause it to wrap", 4260 ]) 4261} 4262"# 4263 ); 4264} 4265 4266// https://github.com/gleam-lang/gleam/issues/1431 4267#[test] 4268fn first_argument_capture_special_case_fn() { 4269 assert_format!( 4270 r#"pub fn main(x) { 4271 wibble(_, fn() { 4272 "one argument that is both breakable and long enough to cause it to wrap" 4273 }) 4274} 4275"# 4276 ); 4277} 4278 4279#[test] 4280fn negation() { 4281 assert_format!( 4282 "pub fn negate(x) { 4283 !x 4284} 4285" 4286 ); 4287} 4288 4289#[test] 4290fn negation_block() { 4291 assert_format!( 4292 "pub fn negate(x) { 4293 !{ 4294 123 4295 x 4296 } 4297} 4298" 4299 ); 4300} 4301 4302#[test] 4303fn empty_lines_work_with_trailing_space() { 4304 let src = "pub fn main() { 4305 let inc = fn(a) { a + 1 } 4306 4307 4308 pair.map_first(#(1, 2), inc) 4309 |> should.equal(#(2, 2)) 4310 4311 // Comment 4312 4313 1 4314 4315 4316 // Comment 4317 4318 4319 2 4320} 4321"; 4322 let expected = "pub fn main() { 4323 let inc = fn(a) { a + 1 } 4324 4325 pair.map_first(#(1, 2), inc) 4326 |> should.equal(#(2, 2)) 4327 4328 // Comment 4329 4330 1 4331 4332 // Comment 4333 4334 2 4335} 4336"; 4337 // We first make extra sure we've not messed up the expected output and 4338 // check it's well formatted. 4339 assert_format!(expected); 4340 4341 assert_format_rewrite!(src, expected); 4342} 4343 4344#[test] 4345fn empty_lines_work_with_eol_normalisation() { 4346 let src = "pub fn main() { 4347 let inc = fn(a) { a + 1 } 4348 4349 4350 pair.map_first(#(1, 2), inc) 4351 |> should.equal(#(2, 2)) 4352 4353 // Comment 4354 4355 1 4356 4357 4358 // Comment 4359 4360 4361 2 4362} 4363"; 4364 let expected = "pub fn main() { 4365 let inc = fn(a) { a + 1 } 4366 4367 pair.map_first(#(1, 2), inc) 4368 |> should.equal(#(2, 2)) 4369 4370 // Comment 4371 4372 1 4373 4374 // Comment 4375 4376 2 4377} 4378"; 4379 4380 // We first make extra sure we've not messed up the expected output and 4381 // check it's well formatted. 4382 assert_format!(expected); 4383 4384 assert_format_rewrite!(&src.replace('\n', "\r\n"), expected); 4385 assert_format_rewrite!(&src.replace('\n', "\r"), expected); 4386} 4387 4388#[test] 4389fn empty_lines_work_with_trailing_space_and_eol_normalisation() { 4390 let src = "pub fn main() { 4391 let inc = fn(a) { a + 1 } 4392 4393 4394 pair.map_first(#(1, 2), inc) 4395 |> should.equal(#(2, 2)) 4396 4397 // Comment 4398 4399 1 4400 4401 4402 // Comment 4403 4404 4405 2 4406} 4407"; 4408 let expected = "pub fn main() { 4409 let inc = fn(a) { a + 1 } 4410 4411 pair.map_first(#(1, 2), inc) 4412 |> should.equal(#(2, 2)) 4413 4414 // Comment 4415 4416 1 4417 4418 // Comment 4419 4420 2 4421} 4422"; 4423 4424 // We first make extra sure we've not messed up the expected output and 4425 // check it's well formatted. 4426 assert_format!(expected); 4427 4428 assert_format_rewrite!(src.replace('\n', "\r\n"), expected); 4429 assert_format_rewrite!(&src.replace('\n', "\r"), expected); 4430} 4431#[test] 4432fn single_empty_line_between_comments() { 4433 // empty line isn't added if it's not already present 4434 assert_format!( 4435 "pub fn wibble() { 4436 // wibble 4437 // wobble 4438 123 4439} 4440" 4441 ); 4442} 4443 4444#[test] 4445fn single_empty_line_between_comments1() { 4446 // single empty line between comments/statement preserved 4447 assert_format!( 4448 "pub fn wibble() { 4449 // wibble 4450 4451 // wobble 4452 4453 123 4454} 4455" 4456 ); 4457} 4458 4459#[test] 4460fn single_empty_line_between_comments2() { 4461 // multiple consecutive empty lines condensed into one 4462 assert_format_rewrite!( 4463 "pub fn wibble() { 4464 // wibble 4465 4466 4467 // wobble 4468 4469 4470 123 4471} 4472", 4473 "pub fn wibble() { 4474 // wibble 4475 4476 // wobble 4477 4478 123 4479} 4480" 4481 ); 4482} 4483 4484#[test] 4485fn single_empty_line_between_comments3() { 4486 // freestanding comments keep empty lines 4487 assert_format!( 4488 "// wibble 4489 4490// wobble 4491" 4492 ); 4493} 4494 4495#[test] 4496fn single_empty_line_between_comments4() { 4497 // freestanding comments condense consecutive empty lines 4498 assert_format_rewrite!( 4499 "// wibble 4500 4501 4502// wobble 4503", 4504 "// wibble 4505 4506// wobble 4507", 4508 ); 4509} 4510 4511// https://github.com/gleam-lang/gleam/issues/1640 4512#[test] 4513fn no_newline_before_comments() { 4514 assert_format!( 4515 "// wibble 4516// wobble 4517" 4518 ); 4519} 4520 4521// https://github.com/gleam-lang/gleam/issues/1647 4522#[test] 4523fn list_at_end_of_long_expr_line() { 4524 assert_format!( 4525 "pub fn example() { 4526 Ok( 4527 RecordConstructorWithALongName( 4528 a_field: RecordConstructorWithALongName(a_field: Record(a_field: [])), 4529 ), 4530 ) 4531} 4532" 4533 ); 4534} 4535 4536// https://github.com/gleam-lang/gleam/issues/1647 4537#[test] 4538fn list_at_end_of_long_pattern_line() { 4539 assert_format!( 4540 "pub fn example() { 4541 let assert LongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLongLong([]) = 4542 1 4543} 4544" 4545 ); 4546} 4547 4548// https://github.com/gleam-lang/gleam/issues/1647 4549#[test] 4550fn list_at_end_of_long_constant_line() { 4551 assert_format!( 4552 "const longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong = [] 4553" 4554 ); 4555} 4556 4557// https://github.com/gleam-lang/gleam/issues/1649 4558#[test] 4559fn dont_remove_braces_when_accessing_tuple() { 4560 assert_format!( 4561 r#"fn main() { 4562 { typed.0 }.type_ 4563} 4564"# 4565 ); 4566} 4567 4568// https://github.com/gleam-lang/gleam/issues/1681 4569#[test] 4570fn wrap_case_subjects() { 4571 assert_format!( 4572 r#"fn main() { 4573 case 4574 "This is a really really long string to force wrapping", 4575 "This is a really really long string to force wrapping", 4576 "This is a really really long string to force wrapping", 4577 "This is a really really long string to force wrapping" 4578 { 4579 _, _, _, _ -> Nil 4580 } 4581} 4582"# 4583 ); 4584} 4585 4586// A bug reported on Discord. This would cause a compiler crash. 4587#[test] 4588fn multiple_empty_line_collapse_bug() { 4589 assert_format_rewrite!( 4590 r#"// Comment 4591 4592 4593 4594const x = 1 4595"#, 4596 r#"// Comment 4597 4598const x = 1 4599"# 4600 ); 4601} 4602 4603#[test] 4604fn do_not_remove_required_braces_case_guard() { 4605 assert_format!( 4606 "fn main() { 4607 let is_enabled = False 4608 let is_confirmed = False 4609 let is_admin = True 4610 case is_enabled, is_confirmed, is_admin { 4611 is_enabled, is_confirmed, is_admin 4612 if is_enabled && { is_confirmed || is_admin } 4613 -> Nil 4614 _, _, _ -> Nil 4615 } 4616} 4617" 4618 ); 4619 4620 assert_format!( 4621 "fn main() { 4622 let wibble = True 4623 case wibble { 4624 wibble if True != { 1 == 2 } -> Nil 4625 _ -> Nil 4626 } 4627} 4628" 4629 ); 4630 4631 assert_format!( 4632 "fn main() { 4633 let wibble = True 4634 let wobble = False 4635 case wibble { 4636 wibble if True != { 1 == { wobble == wibble } } -> Nil 4637 _ -> Nil 4638 } 4639} 4640" 4641 ); 4642 4643 assert_format!( 4644 "fn main() { 4645 let wibble = #(10, [0]) 4646 case wibble { 4647 wibble if True && { wibble.0 == 10 || wibble.0 == 1 } -> Nil 4648 _ -> Nil 4649 } 4650} 4651" 4652 ); 4653} 4654 4655#[test] 4656fn do_not_remove_braces_from_case_guard() { 4657 assert_format!( 4658 "fn main() { 4659 let is_enabled = False 4660 let is_confirmed = False 4661 let is_admin = True 4662 case is_enabled, is_confirmed, is_admin { 4663 is_enabled, is_confirmed, is_admin 4664 if { is_enabled && is_confirmed } || is_admin 4665 -> Nil 4666 _, _, _ -> Nil 4667 } 4668} 4669" 4670 ); 4671} 4672 4673#[test] 4674fn do_not_remove_braces_from_case_guard_2() { 4675 assert_format!( 4676 "fn main() { 4677 let wibble = #(10, [0]) 4678 case wibble { 4679 wibble if True && { wibble.0 == 10 } -> Nil 4680 _ -> Nil 4681 } 4682} 4683" 4684 ); 4685} 4686 4687#[test] 4688fn const_multi_line_string_breaks() { 4689 assert_format!( 4690 r#"const string = [ 4691 "hello 4692world", 4693] 4694"# 4695 ); 4696} 4697 4698#[test] 4699fn expr_multi_line_string_breaks() { 4700 assert_format!( 4701 r#"pub fn main() { 4702 let string = [ 4703 "hello 4704world", 4705 ] 4706} 4707"# 4708 ); 4709} 4710 4711// https://github.com/gleam-lang/gleam/issues/1724 4712#[test] 4713fn case_subject_block() { 4714 assert_format!( 4715 r#"pub fn main() { 4716 case 4717 { 4718 let assert Ok(x) = thing() 4719 let assert Ok(y) = thing() 4720 x + y 4721 } 4722 { 4723 _ -> Nil 4724 } 4725} 4726"# 4727 ); 4728} 4729 4730#[test] 4731fn qualified_const_fn() { 4732 assert_format!( 4733 r#"import other 4734 4735const x = other.function 4736"# 4737 ); 4738} 4739 4740#[test] 4741fn qualified_const_fn_fn_after() { 4742 assert_format!( 4743 r#"import other 4744 4745const x = other.function 4746 4747pub fn main() { 4748 io.println("Hello, Joe!") 4749} 4750"# 4751 ); 4752} 4753 4754// https://github.com/gleam-lang/gleam/issues/1872 4755#[test] 4756fn multiple_line_spread_list_comments() { 4757 assert_format!( 4758 r#"fn main() { 4759 [ 4760 // First! 4761 // First? 4762 1, 4763 // Spread! 4764 // Spread? 4765 ..[2, 3] 4766 ] 4767} 4768"# 4769 ); 4770} 4771 4772// https://github.com/gleam-lang/gleam/issues/1872 4773#[test] 4774fn list_spread_comment_pattern() { 4775 assert_format!( 4776 r#"fn main() { 4777 let assert [ 4778 1, 4779 // Spread! 4780 // Spread? 4781 ..rest 4782 ] = x 4783} 4784"# 4785 ); 4786} 4787 4788// https://github.com/gleam-lang/gleam/issues/1872 4789#[test] 4790fn list_spread_discard_comment_pattern() { 4791 assert_format!( 4792 r#"fn main() { 4793 let assert [ 4794 1, 4795 // Spread! 4796 // Spread? 4797 .. 4798 ] = x 4799} 4800"# 4801 ); 4802} 4803 4804// https://github.com/gleam-lang/gleam/issues/1786 4805#[test] 4806fn multiple_line_documentation_comment_statement_grouping() { 4807 assert_format!( 4808 r#"/// This is the first line of the documentation comment. 4809/// This is the second line of the documentation comment. 4810/// This is the third line of the documentation comment. 4811pub type Map(key, value) 4812"# 4813 ); 4814} 4815 4816#[test] 4817fn not_and() { 4818 assert_format!( 4819 r#"pub fn main() { 4820 !{ True && False } 4821} 4822"# 4823 ); 4824} 4825 4826#[test] 4827fn not_or() { 4828 assert_format!( 4829 r#"pub fn main() { 4830 !{ True || False } 4831} 4832"# 4833 ); 4834} 4835 4836#[test] 4837fn not_add() { 4838 assert_format!( 4839 r#"pub fn main() { 4840 !{ 1 + 3 } 4841} 4842"# 4843 ); 4844} 4845 4846#[test] 4847fn deprecated_assert() { 4848 assert_format_rewrite!( 4849 r#"fn main(x) { 4850 let assert True = x 4851} 4852"#, 4853 r#"fn main(x) { 4854 let assert True = x 4855} 4856"# 4857 ); 4858} 4859 4860#[test] 4861fn negate() { 4862 assert_format_rewrite!( 4863 r#"pub fn main() { 4864 let a = 3 4865 let b = - a 4866} 4867"#, 4868 r#"pub fn main() { 4869 let a = 3 4870 let b = -a 4871} 4872"# 4873 ); 4874} 4875 4876#[test] 4877fn double_negate() { 4878 assert_format_rewrite!( 4879 r#"pub fn main() { 4880 let a = 3 4881 let b = --a 4882} 4883"#, 4884 r#"pub fn main() { 4885 let a = 3 4886 let b = a 4887} 4888"# 4889 ); 4890} 4891 4892#[test] 4893fn triple_negate() { 4894 assert_format_rewrite!( 4895 r#"pub fn main() { 4896 let a = 3 4897 let b = - - - a 4898} 4899"#, 4900 r#"pub fn main() { 4901 let a = 3 4902 let b = -a 4903} 4904"# 4905 ); 4906} 4907 4908#[test] 4909fn binary_negate() { 4910 assert_format_rewrite!( 4911 r#"pub fn main() { 4912 let a = 3 4913 let b = -{a+3} 4914} 4915"#, 4916 r#"pub fn main() { 4917 let a = 3 4918 let b = -{ a + 3 } 4919} 4920"# 4921 ); 4922} 4923 4924#[test] 4925fn binary_double_negate() { 4926 assert_format_rewrite!( 4927 r#"pub fn main() { 4928 let a = 3 4929 let b = --{a + 3} 4930} 4931"#, 4932 r#"pub fn main() { 4933 let a = 3 4934 let b = { a + 3 } 4935} 4936"# 4937 ); 4938} 4939 4940#[test] 4941fn even_repeated_negate_after_subtract() { 4942 assert_format_rewrite!( 4943 r#"pub fn main() { 4944 let a = 3 4945 let b = 4 4946 let c = a-------b 4947} 4948"#, 4949 r#"pub fn main() { 4950 let a = 3 4951 let b = 4 4952 let c = a - b 4953} 4954"# 4955 ); 4956} 4957 4958#[test] 4959fn odd_repeated_negate_after_subtract() { 4960 assert_format_rewrite!( 4961 r#"pub fn main() { 4962 let a = 3 4963 let b = 4 4964 let c = a--------b 4965} 4966"#, 4967 r#"pub fn main() { 4968 let a = 3 4969 let b = 4 4970 let c = a - -b 4971} 4972"# 4973 ); 4974} 4975 4976#[test] 4977fn double_negation_on_bools_is_removed() { 4978 assert_format_rewrite!( 4979 r#"pub fn main() { 4980 !!True 4981} 4982"#, 4983 "pub fn main() { 4984 True 4985} 4986" 4987 ); 4988} 4989 4990#[test] 4991fn wrap_long_line_with_int_negation() { 4992 assert_format_rewrite!( 4993 r#"pub fn main() { 4994 let a = 3 4995 let b = a * a * a * a * a * a * a * a * a * a * a * a * a * { a * a * a * a * a * a * a * a * a * a } 4996 let c = c * c * c * c * c * c * c * c * c * c * c * c * c * - { c * c * c * c * c * c * c * c * c * c } 4997} 4998"#, 4999 r#"pub fn main() { 5000 let a = 3 5001 let b = 5002 a 5003 * a 5004 * a 5005 * a 5006 * a 5007 * a 5008 * a 5009 * a 5010 * a 5011 * a 5012 * a 5013 * a 5014 * a 5015 * { a * a * a * a * a * a * a * a * a * a } 5016 let c = 5017 c 5018 * c 5019 * c 5020 * c 5021 * c 5022 * c 5023 * c 5024 * c 5025 * c 5026 * c 5027 * c 5028 * c 5029 * c 5030 * -{ c * c * c * c * c * c * c * c * c * c } 5031} 5032"# 5033 ); 5034} 5035 5036#[test] 5037fn wrap_long_line_with_bool_negation() { 5038 assert_format_rewrite!( 5039 r#"pub fn main() { 5040 let a = True 5041 let b = a || a || a || a || a || a || a || a || a || a || a || a || a || { a || a || a || a || a || a || a || a || a || a } 5042 let c = c || c || c || c || c || c || c || c || c || c || c || c || c || ! { c || c || c || c || c || c || c || c || c || c } 5043} 5044"#, 5045 r#"pub fn main() { 5046 let a = True 5047 let b = 5048 a 5049 || a 5050 || a 5051 || a 5052 || a 5053 || a 5054 || a 5055 || a 5056 || a 5057 || a 5058 || a 5059 || a 5060 || a 5061 || { a || a || a || a || a || a || a || a || a || a } 5062 let c = 5063 c 5064 || c 5065 || c 5066 || c 5067 || c 5068 || c 5069 || c 5070 || c 5071 || c 5072 || c 5073 || c 5074 || c 5075 || c 5076 || !{ c || c || c || c || c || c || c || c || c || c } 5077} 5078"# 5079 ); 5080} 5081 5082// https://github.com/gleam-lang/gleam/issues/1977 5083#[test] 5084fn preserve_single_expression_blocks() { 5085 assert_format!( 5086 r#"pub fn main(x) { 5087 case x { 5088 1 -> { 5089 1 5090 } 5091 _ -> 2 5092 } 5093} 5094"# 5095 ); 5096} 5097 5098#[test] 5099fn calling_pipeline0() { 5100 assert_format!( 5101 r#"pub fn main() { 5102 { 5103 one 5104 |> two 5105 }() 5106} 5107"# 5108 ); 5109} 5110 5111#[test] 5112fn calling_pipeline1() { 5113 assert_format!( 5114 r#"pub fn main() { 5115 { 5116 one 5117 |> two 5118 }(1) 5119} 5120"# 5121 ); 5122} 5123 5124#[test] 5125fn calling_pipeline2() { 5126 assert_format!( 5127 r#"pub fn main() { 5128 { 5129 one 5130 |> two 5131 }(1, 2) 5132} 5133"# 5134 ); 5135} 5136 5137#[test] 5138fn calling_pipeline_1_list() { 5139 assert_format!( 5140 r#"pub fn main() { 5141 { 5142 one 5143 |> two 5144 }([1, 2, 3]) 5145} 5146"# 5147 ); 5148} 5149 5150// https://github.com/gleam-lang/gleam/issues/2119 5151#[test] 5152fn empty_line_after_fn_with_return_annotation() { 5153 assert_format!( 5154 r#"fn main() { 5155 fn() -> String { "" } 5156 5157 0 5158} 5159"# 5160 ); 5161} 5162 5163// https://github.com/gleam-lang/gleam/issues/2174 5164#[test] 5165fn empty_line_after_crash() { 5166 assert_format_rewrite!( 5167 r#"pub type One { 5168 One // Comment 5169 5170} 5171 5172"#, 5173 r#"pub type One { 5174 One 5175 // Comment 5176} 5177"# 5178 ); 5179} 5180 5181// https://github.com/gleam-lang/gleam/issues/2196 5182#[test] 5183fn comment_at_end_of_type() { 5184 assert_format!( 5185 r#"pub type X { 5186 X 5187 // Afterwards 5188} 5189"# 5190 ); 5191} 5192 5193#[test] 5194fn deprecated_type_alias() { 5195 assert_format!( 5196 r#"@deprecated("Deprecated type") 5197pub type Tiger = 5198 Nil 5199"# 5200 ); 5201} 5202 5203// https://github.com/gleam-lang/gleam/issues/2423 5204#[test] 5205fn prefix_as() { 5206 assert_format!( 5207 r#"pub fn main(x) { 5208 case x { 5209 "0" as digit <> rest | "1" as digit <> rest -> rest 5210 } 5211} 5212"# 5213 ); 5214} 5215 5216#[test] 5217fn case_splits_function_on_newline() { 5218 assert_format!( 5219 r#"pub fn main() { 5220 case x { 5221 1 -> 5222 some_module.some_long_name_function([ 5223 some_module.some_long_name_function(), 5224 ]) 5225 _ -> todo 5226 } 5227} 5228"# 5229 ); 5230} 5231 5232// https://github.com/gleam-lang/gleam/issues/2442 5233#[test] 5234fn single_argument_list() { 5235 assert_format!( 5236 r#"pub fn main() { 5237 Ok([ 5238 some_long_variable_name_to_force_wrapping, 5239 some_long_variable_name_to_force_wrapping, 5240 some_long_variable_name_to_force_wrapping, 5241 ]) 5242} 5243"# 5244 ); 5245} 5246 5247// https://github.com/gleam-lang/gleam/issues/2442 5248#[test] 5249fn single_argument_function() { 5250 assert_format!( 5251 r#"pub fn main() { 5252 Ok(fn() { 5253 some_long_variable_name_to_force_wrapping() 5254 some_long_variable_name_to_force_wrapping() 5255 some_long_variable_name_to_force_wrapping() 5256 }) 5257} 5258"# 5259 ); 5260} 5261 5262// https://github.com/gleam-lang/gleam/issues/2442 5263#[test] 5264fn single_argument_tuple() { 5265 assert_format!( 5266 r#"pub fn main() { 5267 Ok(#( 5268 some_long_variable_name_to_force_wrapping, 5269 some_long_variable_name_to_force_wrapping, 5270 some_long_variable_name_to_force_wrapping, 5271 )) 5272} 5273"# 5274 ); 5275} 5276 5277// https://github.com/gleam-lang/gleam/issues/2442 5278#[test] 5279fn single_argument_call() { 5280 assert_format!( 5281 r#"pub fn main() { 5282 Ok(do_something( 5283 some_long_variable_name_to_force_wrapping, 5284 some_long_variable_name_to_force_wrapping, 5285 some_long_variable_name_to_force_wrapping, 5286 )) 5287} 5288"# 5289 ); 5290} 5291 5292// https://github.com/gleam-lang/gleam/issues/2442 5293#[test] 5294fn single_argument_call_nested() { 5295 assert_format!( 5296 r#"pub fn main() { 5297 Ok( 5298 do_something(do_something_else( 5299 some_long_variable_name_to_force_wrapping, 5300 some_long_variable_name_to_force_wrapping, 5301 some_long_variable_name_to_force_wrapping, 5302 )), 5303 ) 5304} 5305"# 5306 ); 5307} 5308 5309// https://github.com/gleam-lang/gleam/issues/2442 5310#[test] 5311fn single_argument_call_nested_nested() { 5312 assert_format!( 5313 r#"pub fn main() { 5314 Ok( 5315 do_something( 5316 do_something_else(do_a_last_thing( 5317 some_long_variable_name_to_force_wrapping, 5318 some_long_variable_name_to_force_wrapping, 5319 some_long_variable_name_to_force_wrapping, 5320 )), 5321 ), 5322 ) 5323} 5324"# 5325 ); 5326} 5327 5328// https://github.com/gleam-lang/gleam/issues/2512 5329#[test] 5330fn list_with_pipe_format() { 5331 assert_format!( 5332 r#"pub fn main() { 5333 [ 5334 "Success!" 5335 |> ansi(apply: [1, 31]), 5336 "", 5337 "Wrote `" <> bin <> "`, `" <> pwsh_bin <> "`", 5338 ] 5339} 5340"# 5341 ); 5342} 5343 5344#[test] 5345fn function_call_close_to_line_limit() { 5346 assert_format!( 5347 r#"pub fn main() { 5348 function_call( 5349 that, 5350 is, 5351 super, 5352 close, 5353 to, 5354 the, 5355 max, 5356 line, 5357 limit, 5358 of, 5359 80, 5360 chars, 5361 ) 5362} 5363"# 5364 ); 5365} 5366 5367#[test] 5368fn multiline_string_are_not_broken_with_string_concatenation_if_they_fit() { 5369 assert_format!( 5370 r#"pub fn main() { 5371 "pub fn wibble(" <> arg <> ") ->" <> type_ <> "{ 5372 body 5373}" 5374} 5375"# 5376 ); 5377} 5378 5379#[test] 5380fn nesting_goes_back_to_normal_after_multiline_string() { 5381 assert_format!( 5382 r#"pub fn main() { 5383 let x = { 5384 " 53851 53862 5387" <> long_name_function_call( 5388 1_111_111_111_111_111, 5389 222_222_222_222, 5390 3_333_333_333_333_333, 5391 ) 5392 } 5393} 5394"# 5395 ); 5396} 5397 5398#[test] 5399fn multiline_string_get_broken_on_newlines_as_function_arguments() { 5400 assert_format!( 5401 r#"pub fn main() { 5402 wibble( 5403 wobble, 5404 "wobble 5405 wibble 5406 wobble", 5407 wibble, 5408 wobble, 5409 ) 5410} 5411"# 5412 ); 5413} 5414 5415#[test] 5416fn pipeline_used_as_function_arguments_gets_nested() { 5417 assert_format!( 5418 r#"pub fn main() { 5419 wibble( 5420 a_variable_with_a_long_name 5421 |> another_variable_with_a_long_name 5422 |> yet_another_variable_with_a_long_name, 5423 wobble, 5424 ) 5425} 5426"# 5427 ); 5428} 5429 5430#[test] 5431fn pipeline_used_as_function_arguments_is_not_nested_if_it_is_the_only_argument() { 5432 assert_format!( 5433 r#"pub fn main() { 5434 wibble( 5435 a_variable_with_a_long_name 5436 |> another_variable_with_a_long_name 5437 |> yet_another_variable_with_a_long_name, 5438 ) 5439} 5440"# 5441 ); 5442} 5443 5444#[test] 5445fn pipeline_inside_list_gets_nested() { 5446 assert_format!( 5447 r#"pub fn main() { 5448 [ 5449 wibble, 5450 a_variable_with_a_long_name 5451 |> another_variable_with_a_long_name 5452 |> yet_another_variable_with_a_long_name, 5453 ] 5454} 5455"# 5456 ); 5457} 5458 5459#[test] 5460fn pipeline_inside_list_is_not_nested_if_only_item() { 5461 assert_format!( 5462 r#"pub fn main() { 5463 [ 5464 a_variable_with_a_long_name 5465 |> another_variable_with_a_long_name 5466 |> yet_another_variable_with_a_long_name, 5467 ] 5468} 5469"# 5470 ); 5471} 5472 5473#[test] 5474fn pipeline_inside_tuple_gets_nested() { 5475 assert_format!( 5476 r#"pub fn main() { 5477 #( 5478 wibble, 5479 a_variable_with_a_long_name 5480 |> another_variable_with_a_long_name 5481 |> yet_another_variable_with_a_long_name, 5482 ) 5483} 5484"# 5485 ); 5486} 5487 5488#[test] 5489fn pipeline_inside_tuple_is_not_nested_if_only_item() { 5490 assert_format!( 5491 r#"pub fn main() { 5492 #( 5493 a_variable_with_a_long_name 5494 |> another_variable_with_a_long_name 5495 |> yet_another_variable_with_a_long_name, 5496 ) 5497} 5498"# 5499 ); 5500} 5501 5502// github.com/gleam-lang/gleam/issues/2608 5503#[test] 5504fn comments_are_not_moved_out_of_list_of_literals() { 5505 assert_format!( 5506 r#"fn main() { 5507 [ 5508 1, 2, 5509 // list 5510 ] 5511} 5512"# 5513 ); 5514} 5515 5516// github.com/gleam-lang/gleam/issues/2608 5517#[test] 5518fn comments_are_not_moved_out_of_list() { 5519 assert_format!( 5520 r#"fn main() { 5521 [ 5522 wibble, 5523 wobble, 5524 // list 5525 ] 5526} 5527"# 5528 ); 5529} 5530 5531// github.com/gleam-lang/gleam/issues/2608 5532#[test] 5533fn comments_are_not_moved_out_of_case_expressions() { 5534 assert_format!( 5535 r#"fn main() { 5536 case True { 5537 _ -> Nil 5538 // case 5539 } 5540} 5541"# 5542 ); 5543} 5544 5545// github.com/gleam-lang/gleam/issues/2608 5546#[test] 5547fn comments_are_not_moved_out_of_tuples() { 5548 assert_format!( 5549 r#"fn main() { 5550 #( 5551 1, 5552 2, 5553 // tuple 5554 ) 5555} 5556"# 5557 ); 5558} 5559 5560// github.com/gleam-lang/gleam/issues/2608 5561#[test] 5562fn comments_are_not_moved_out_of_function_calls() { 5563 assert_format!( 5564 r#"fn main() { 5565 call( 5566 1, 5567 2, 5568 // function call 5569 ) 5570} 5571"# 5572 ); 5573} 5574 5575// https://github.com/gleam-lang/gleam/issues/2607 5576#[test] 5577fn function_arguments_after_comment_are_not_indented() { 5578 assert_format!( 5579 r#"pub fn main() { 5580 wibble( 5581 // Wobble 5582 1 + 1, 5583 "wibble", 5584 ) 5585} 5586"# 5587 ); 5588} 5589 5590// https://github.com/gleam-lang/gleam/issues/2607 5591#[test] 5592fn tuple_items_after_comment_are_not_indented() { 5593 assert_format!( 5594 r#"pub fn main() { 5595 #( 5596 // Wobble 5597 1 + 1, 5598 "wibble", 5599 ) 5600} 5601"# 5602 ); 5603} 5604 5605// https://github.com/gleam-lang/gleam/issues/2607 5606#[test] 5607fn list_items_after_comment_are_not_indented() { 5608 assert_format!( 5609 r#"pub fn main() { 5610 [ 5611 // Wobble 5612 1 + 1, 5613 "wibble", 5614 ] 5615} 5616"# 5617 ); 5618} 5619 5620// https://github.com/gleam-lang/gleam/issues/2990 5621#[test] 5622fn comments_are_not_moved_out_of_empty_list() { 5623 assert_format!( 5624 r#"pub fn main() { 5625 // This is an empty list! 5626 [ 5627 // Nothing here... 5628 ] 5629} 5630"# 5631 ); 5632} 5633 5634#[test] 5635fn empty_lists_with_comment_inside_are_indented_properly() { 5636 assert_format!( 5637 r#"pub fn main() { 5638 fun( 5639 [ 5640 // Nothing here... 5641 ], 5642 wibble_wobble_wibble_wobble_wibble_wobble_wibble_wobble, 5643 [ 5644 // Nothing here as well! 5645 ], 5646 ) 5647} 5648"# 5649 ); 5650} 5651 5652// https://github.com/gleam-lang/gleam/issues/2890 5653#[test] 5654fn piped_blocks_are_not_needlessly_indented() { 5655 assert_format!( 5656 r#"pub fn main() { 5657 #( 5658 1, 5659 { 5660 "long enough to need to wrap. blah blah blah blah blah blah blah blah blah" 5661 } 5662 |> wibble, 5663 3, 5664 ) 5665} 5666"# 5667 ); 5668} 5669 5670// https://github.com/gleam-lang/gleam/issues/2924 5671#[test] 5672fn record_update_fields_are_not_needlessly_broken() { 5673 assert_format!( 5674 r#"pub fn main() { 5675 Model( 5676 ..model, 5677 wibble: wibble_wobble_wibble_wobble + 1, 5678 wobble: Some(wibble_wobble_wibble_wobble), 5679 ) 5680} 5681"# 5682 ); 5683} 5684 5685// https://github.com/gleam-lang/gleam/issues/2890 5686#[test] 5687fn piped_lists_are_not_needlessly_indented() { 5688 assert_format!( 5689 r#"pub fn main() { 5690 fun( 5691 [ 5692 ["wibble wobble", "wibble", "wobble"], 5693 ["long enough to go over", "line limit"], 5694 ] 5695 |> list.concat, 5696 todo, 5697 ) 5698} 5699"# 5700 ); 5701} 5702 5703#[test] 5704fn comments_inside_nested_pipe_chain() { 5705 assert_format!( 5706 r#"pub fn main() { 5707 fun( 5708 thing 5709 // A comment 5710 |> wibble 5711 // Another comment 5712 |> wobble, 5713 thing, 5714 ) 5715} 5716"# 5717 ); 5718} 5719 5720#[test] 5721fn comments_inside_nested_binop_chain() { 5722 assert_format!( 5723 r#"pub fn main() { 5724 fun( 5725 thing 5726 // A comment 5727 <> wibble 5728 // Another comment 5729 <> wobble, 5730 thing, 5731 ) 5732} 5733"# 5734 ); 5735} 5736 5737#[test] 5738fn comments_inside_binop_chain() { 5739 assert_format!( 5740 r#"pub fn main() { 5741 thing 5742 // A comment 5743 <> wibble 5744 // Another comment 5745 <> wobble 5746} 5747"# 5748 ); 5749} 5750 5751#[test] 5752fn internal_attribute_on_function() { 5753 assert_format!( 5754 r#"@internal 5755pub fn main() { 5756 todo 5757} 5758"# 5759 ); 5760} 5761 5762#[test] 5763fn internal_attribute_on_type() { 5764 assert_format!( 5765 r#"@internal 5766pub type Type 5767"# 5768 ); 5769} 5770 5771#[test] 5772fn internal_attribute_on_const() { 5773 assert_format!( 5774 r#"@internal 5775pub const wibble = 1 5776"# 5777 ); 5778} 5779 5780#[test] 5781fn comments_inside_contant_list() { 5782 assert_format!( 5783 r#"const wibble = [ 5784 // A comment 5785 1, 2, 5786 // Another comment 5787 3, 5788 // One last comment 5789] 5790"# 5791 ); 5792} 5793 5794#[test] 5795fn comments_inside_contant_empty_list() { 5796 assert_format!( 5797 r#"const wibble = [ 5798 // A comment 5799] 5800"# 5801 ); 5802} 5803 5804#[test] 5805fn comments_inside_contant_tuple() { 5806 assert_format!( 5807 r#"const wibble = #( 5808 // A comment 5809 1, 5810 2, 5811 // Another comment 5812 3, 5813 // One last comment 5814) 5815"# 5816 ); 5817} 5818 5819#[test] 5820fn comments_inside_contant_empty_tuple() { 5821 assert_format!( 5822 r#"const wibble = #( 5823 // A comment 5824) 5825"# 5826 ); 5827} 5828 5829#[test] 5830fn comments_inside_empty_tuple() { 5831 assert_format!( 5832 r#"pub fn main() { 5833 #( 5834 // A comment! 5835 ) 5836} 5837"# 5838 ); 5839} 5840 5841#[test] 5842fn comments_at_the_end_of_anonymous_function() { 5843 assert_format!( 5844 r#"pub fn main() { 5845 fn() { 5846 1 5847 // a final comment 5848 5849 // another final comment 5850 // at the end of the block 5851 } 5852} 5853"# 5854 ); 5855} 5856 5857#[test] 5858fn comments_in_anonymous_function_args() { 5859 assert_format!( 5860 r#"pub fn main() { 5861 fn( 5862 // A comment 1 5863 // A comment 2 5864 ) { 5865 1 5866 } 5867} 5868"# 5869 ); 5870 assert_format!( 5871 r#"pub fn main() { 5872 fn( 5873 // A comment 1 5874 a, 5875 // A comment 2 5876 ) { 5877 1 5878 } 5879} 5880"# 5881 ); 5882} 5883 5884#[test] 5885fn comments_after_last_argument_of_record_constructor() { 5886 assert_format!( 5887 r#"type Record { 5888 Record( 5889 field: String, 5890 // comment_line_1: String, 5891 // comment_line_2: String, 5892 ) 5893} 5894"# 5895 ); 5896} 5897 5898#[test] 5899fn only_comments_in_record_constructor() { 5900 assert_format!( 5901 r#"type Record { 5902 Record( 5903 // comment_line_1: String, 5904 // comment_line_2: String, 5905 ) 5906} 5907"# 5908 ); 5909} 5910#[test] 5911fn comment_after_spread_operator() { 5912 assert_format!( 5913 "type Triple { 5914 Triple(a: Int, b: Int, c: Int) 5915} 5916 5917fn main() { 5918 let triple = Triple(1, 2, 3) 5919 let Triple( 5920 really_really_long_variable_name_a, 5921 c: really_really_long_variable_name_c, 5922 .., 5923 // comment 5924 ) = triple 5925 really_really_long_variable_name_c 5926} 5927" 5928 ); 5929} 5930 5931#[test] 5932fn multiline_comment_in_case_block() { 5933 assert_format!( 5934 r#"pub fn do_len(list, acc) { 5935 case list { 5936 [] -> acc 5937 [_, ..rest] -> rest |> do_len(acc + 1) 5938 // Even the opposite wouldn't be optimised: 5939 // { acc + 1 } |> do_len(rest, _) 5940 } 5941} 5942"# 5943 ); 5944} 5945 5946// https://github.com/gleam-lang/gleam/issues/3190 5947#[test] 5948fn trailing_comments_inside_non_empty_bit_arrays_are_not_moved() { 5949 assert_format!( 5950 r#"pub fn main() { 5951 << 5952 1, 2, 5953 // One and two are above me. 5954 >> 5955} 5956"# 5957 ); 5958} 5959 5960// https://github.com/gleam-lang/gleam/issues/3210 5961#[test] 5962fn newlines_are_not_stripped_if_two_consecutive_anonymous_function_are_passed_as_arguments() { 5963 assert_format!( 5964 r#"pub fn main() { 5965 fun( 5966 fn() { 5967 wibble 5968 5969 wobble 5970 }, 5971 fn() { wibble }, 5972 ) 5973} 5974"# 5975 ); 5976} 5977 5978#[test] 5979fn const_long_concat_string() { 5980 assert_format_rewrite!( 5981 r#"const long_string = "some" <> " very" <> " long" <> " string" <> " indeed" <> " please" <> " break" 5982"#, 5983 r#"const long_string = "some" 5984 <> " very" 5985 <> " long" 5986 <> " string" 5987 <> " indeed" 5988 <> " please" 5989 <> " break" 5990"# 5991 ); 5992} 5993 5994#[test] 5995fn const_concat_short_unbroken() { 5996 assert_format!( 5997 r#"const x = "some" <> "short" <> "string" 5998"# 5999 ); 6000} 6001 6002#[test] 6003fn const_concat_long_including_list() { 6004 assert_format_rewrite!( 6005 r#"const x = "some long string 1" <> "some long string 2" <> ["here is a list", "with several elements", "in order to make it be too long to fit on one line", "so we can see how it breaks", "onto multiple lines"] <> "and a last string" 6006"#, 6007 r#"const x = "some long string 1" 6008 <> "some long string 2" 6009 <> [ 6010 "here is a list", 6011 "with several elements", 6012 "in order to make it be too long to fit on one line", 6013 "so we can see how it breaks", 6014 "onto multiple lines", 6015 ] 6016 <> "and a last string" 6017"#, 6018 ); 6019} 6020 6021// https://github.com/gleam-lang/gleam/issues/3397 6022#[test] 6023fn comment_after_case_branch() { 6024 assert_format!( 6025 r#"pub fn main() { 6026 case x { 6027 _ -> 6028 // comment 6029 [123] 6030 } 6031} 6032"# 6033 ); 6034} 6035 6036// https://github.com/gleam-lang/gleam/issues/3397 6037#[test] 6038fn comment_after_case_branch_case() { 6039 assert_format!( 6040 r#"pub fn main() { 6041 case x { 6042 _ -> 6043 // comment 6044 case y { 6045 _ -> todo 6046 } 6047 } 6048} 6049"# 6050 ); 6051} 6052 6053#[test] 6054fn label_shorthand_call_arg_is_split_like_regular_labelled_args() { 6055 assert_format!( 6056 r#"pub fn main() { 6057 wibble( 6058 a_punned_arg_that_is_super_long:, 6059 another_punned_arg:, 6060 yet_another_pun:, 6061 ok_thats_enough: wibble, 6062 ) 6063} 6064"# 6065 ); 6066} 6067 6068#[test] 6069fn commented_label_shorthand_call_arg_is_split_like_regular_labelled_args() { 6070 assert_format!( 6071 r#"pub fn main() { 6072 wibble( 6073 // A comment here 6074 a_punned_arg_that_is_super_long:, 6075 another_punned_arg:, 6076 // And a comment there 6077 yet_another_pun:, 6078 ok_thats_enough: wibble, 6079 ) 6080} 6081"# 6082 ); 6083} 6084 6085#[test] 6086fn label_shorthand_pattern_arg_is_split_like_regular_labelled_patterns() { 6087 assert_format!( 6088 r#"pub fn main() { 6089 let Wibble( 6090 a_punned_arg_that_is_super_long:, 6091 another_punned_arg:, 6092 yet_another_pun:, 6093 ok_thats_enough: wibble, 6094 ) = todo 6095} 6096"# 6097 ); 6098} 6099 6100#[test] 6101fn record_pattern_with_no_label_shorthand() { 6102 assert_format!( 6103 r#"pub fn main() { 6104 let Wibble(x: x) = todo 6105} 6106"# 6107 ); 6108} 6109 6110#[test] 6111fn record_with_no_label_shorthand() { 6112 assert_format!( 6113 r#"pub fn main() { 6114 Wibble(x: x) 6115} 6116"# 6117 ); 6118} 6119 6120#[test] 6121fn function_without_label_shorthand() { 6122 assert_format!( 6123 r#"pub fn main() { 6124 wibble(x: x) 6125} 6126"# 6127 ); 6128} 6129 6130// https://github.com/gleam-lang/gleam/issues/2015 6131#[test] 6132fn doc_comments_are_split_by_regular_comments() { 6133 assert_format!( 6134 r#"/// Doc comment 6135// Commented function 6136// fn wibble() {} 6137 6138/// Other doc comment 6139pub fn main() { 6140 todo 6141} 6142"# 6143 ); 6144} 6145 6146// https://github.com/gleam-lang/gleam/issues/2015 6147#[test] 6148fn it_is_easy_to_tell_two_different_doc_comments_apart_when_a_regular_comment_is_separating_those() 6149{ 6150 assert_format_rewrite!( 6151 r#"/// Doc comment 6152// regular comment 6153/// Other doc comment 6154pub fn main() { 6155 todo 6156} 6157"#, 6158 r#"/// Doc comment 6159// regular comment 6160 6161/// Other doc comment 6162pub fn main() { 6163 todo 6164} 6165"# 6166 ); 6167} 6168 6169// https://github.com/gleam-lang/gleam/issues/2015 6170#[test] 6171fn multiple_commented_definitions_in_a_row_2() { 6172 assert_format!( 6173 r#"/// Stray comment 6174// regular comment 6175 6176/// Stray comment 6177// regular comment 6178 6179/// Doc comment 6180pub fn wibble() { 6181 todo 6182} 6183"# 6184 ); 6185} 6186 6187// https://github.com/gleam-lang/gleam/issues/2015 6188#[test] 6189fn only_stray_comments_and_definition_with_no_doc_comments() { 6190 assert_format!( 6191 r#"/// Stray comment 6192// regular comment 6193 6194/// Stray comment 6195// regular comment 6196 6197pub fn wibble() { 6198 todo 6199} 6200"# 6201 ); 6202} 6203 6204// https://github.com/gleam-lang/gleam/issues/2015 6205#[test] 6206fn only_stray_comments_and_definition_with_no_doc_comments_2() { 6207 assert_format_rewrite!( 6208 r#"/// Stray comment 6209// regular comment 6210pub fn wibble () { 6211 todo 6212} 6213"#, 6214 r#"/// Stray comment 6215// regular comment 6216 6217pub fn wibble() { 6218 todo 6219} 6220"# 6221 ); 6222} 6223 6224#[test] 6225fn discard_in_pipe_is_not_turned_into_shorthand_label() { 6226 assert_format!( 6227 r#"pub fn main() { 6228 wibble |> wobble(one: 1, label: _, two: 2) 6229} 6230"# 6231 ); 6232} 6233 6234// Bug found by Louis 6235#[test] 6236fn internal_attribute_does_not_change_formatting_of_a_function() { 6237 assert_format!( 6238 r#"@internal 6239pub fn init( 6240 start: #(SupervisorFlags, List(ChildSpecification)), 6241) -> Result(#(Dynamic, Dynamic), never) { 6242 todo 6243} 6244"# 6245 ); 6246} 6247 6248// https://github.com/gleam-lang/gleam/issues/3627 6249#[test] 6250fn big_grapheme_cluster() { 6251 assert_format!( 6252 r#"pub fn main() { 6253 sw("👩‍👩‍👧‍👦👩‍👩‍👧‍👦👩‍👩‍👧‍👦", []) 6254} 6255"# 6256 ); 6257} 6258 6259// https://github.com/gleam-lang/gleam/issues/3720 6260#[test] 6261fn record_inside_const_list() { 6262 assert_format_rewrite!( 6263 r#"const commands = [ 6264 Command( 6265 "dev", 6266 "Start a file watcher that automatically re-compiles your app on all file changes.", 6267 ), Command("help", "Show this help text."), 6268] 6269"#, 6270 r#"const commands = [ 6271 Command( 6272 "dev", 6273 "Start a file watcher that automatically re-compiles your app on all file changes.", 6274 ), 6275 Command("help", "Show this help text."), 6276] 6277"# 6278 ); 6279} 6280 6281#[test] 6282fn formatter_adds_todo_inside_empty_block() { 6283 assert_format_rewrite!( 6284 "pub fn main() {{}}", 6285 r#"pub fn main() { 6286 { todo } 6287} 6288"# 6289 ); 6290} 6291 6292#[test] 6293fn let_assert_as() { 6294 assert_format!( 6295 r#"pub fn main() { 6296 let assert 10 = 10 as "10 == 10" 6297} 6298"# 6299 ); 6300} 6301 6302#[test] 6303fn let_assert_as_long_message() { 6304 assert_format!( 6305 r#"pub fn main() { 6306 let assert Ok(10) = Ok(10) 6307 as "It's pretty obvious that this will never fail, but just in case, here is why." 6308} 6309"# 6310 ); 6311} 6312 6313#[test] 6314fn let_assert_as_long_message_and_value() { 6315 assert_format!( 6316 r#"pub fn main() { 6317 let assert Ok(something) = 6318 some_very_long_variable_which_always_represents_a_successful_result 6319 as "As you can see by the incredibly descriptive variable name, this operation never fails." 6320} 6321"# 6322 ); 6323} 6324 6325#[test] 6326fn let_assert_as_concatenated_message() { 6327 assert_format!( 6328 r#"pub fn main() { 6329 let assert 1 = 2 as { "This will" <> " " <> "crash" } 6330} 6331"# 6332 ); 6333} 6334 6335#[test] 6336fn let_assert_as_variable_message() { 6337 assert_format!( 6338 r#"pub fn main() { 6339 let message = "Hi :)" 6340 let assert 1 = 2 as message 6341} 6342"# 6343 ); 6344} 6345 6346// https://github.com/gleam-lang/gleam/issues/4121 6347#[test] 6348fn function_capture_formatted_like_regular_calls() { 6349 assert_format!( 6350 r#"pub fn main() { 6351 capture(a, _, [ 6352 really_long_thing_that_can_be_broken, 6353 something_else_for_good_measure, 6354 ]) 6355} 6356"# 6357 ); 6358} 6359 6360#[test] 6361fn function_capture_formatted_like_regular_calls_2() { 6362 assert_format!( 6363 r#"pub fn main() { 6364 capture( 6365 a, 6366 _, 6367 really_long_thing_that_cannot_be_broken, 6368 something_else_for_good_measure, 6369 ) 6370} 6371"# 6372 ); 6373} 6374 6375#[test] 6376fn function_capture_formatted_like_regular_calls_3() { 6377 assert_format!( 6378 r#"pub fn main() { 6379 list.fold(my_list, _, fn(a) { 6380 io.print("Meh") 6381 io.print("Meh") 6382 }) 6383} 6384"# 6385 ); 6386} 6387 6388#[test] 6389fn function_capture_formatted_like_regular_calls_inside_a_long_list() { 6390 assert_format!( 6391 r#"pub fn main() { 6392 [ 6393 capture(a, _, [ 6394 really_long_thing_that_can_be_broken, 6395 something_else_for_good_measure, 6396 ]), 6397 regular_call(a, [ 6398 really_long_thing_that_can_be_broken, 6399 something_else_for_good_measure, 6400 ]), 6401 ] 6402} 6403"# 6404 ); 6405} 6406 6407#[test] 6408fn function_capture_formatted_like_regular_calls_in_a_pipe() { 6409 assert_format!( 6410 r#"pub fn main() { 6411 [1, 2, 3] 6412 |> list.fold(from: 1, over: _, with: fn(a, b) { 6413 // a comment! 6414 a + b 6415 }) 6416} 6417"# 6418 ); 6419} 6420 6421#[test] 6422fn assert() { 6423 assert_format!( 6424 "pub fn main() { 6425 assert True 6426} 6427" 6428 ); 6429} 6430 6431#[test] 6432fn assert_with_long_expression() { 6433 assert_format!( 6434 "pub fn main() { 6435 assert some_function_with_a_very_long_name_that_exceeds_the_eighty_character_limit() 6436} 6437" 6438 ); 6439} 6440 6441#[test] 6442fn assert_with_message() { 6443 assert_format!( 6444 r#"pub fn main() { 6445 assert True as "This is always true" 6446} 6447"# 6448 ); 6449} 6450 6451#[test] 6452fn assert_with_long_message() { 6453 assert_format!( 6454 r#"pub fn main() { 6455 assert True 6456 as "This should never panic, because it is a literal True value, and so will always be true." 6457} 6458"# 6459 ); 6460} 6461 6462#[test] 6463fn assert_with_long_expression_and_long_message() { 6464 assert_format!( 6465 r#"pub fn main() { 6466 assert some_long_function_name_which_if_everything_is_right_should_always_be_true 6467 as "This should never panic, because the function only ever returns true." 6468} 6469"# 6470 ); 6471} 6472 6473#[test] 6474fn echo_with_long_binary_expression() { 6475 assert_format!( 6476 r#"pub fn main() { 6477 echo wibble_wobble_wibble_wobble_wibble_wobble_wibble 6478 >= wibble_wobble_wibble_wobble_wibble_wobble_wibble 6479 6480 echo wibble_wobble_wibble_wobble_wibble_wobble_wibble 6481 + wibble_wobble_wibble_wobble_wibble_wobble_wibble 6482 6483 echo wibble_wobble_wibble_wobble_wibble_wobble_wibble 6484 == wibble_wobble_wibble_wobble_wibble_wobble_wibble 6485} 6486"# 6487 ); 6488} 6489 6490#[test] 6491fn assert_with_long_binary_expression() { 6492 assert_format!( 6493 r#"pub fn main() { 6494 assert wibble_wobble_wibble_wobble_wibble_wobble_wibble 6495 >= wibble_wobble_wibble_wobble_wibble_wobble_wibble 6496 6497 assert wibble_wobble_wibble_wobble_wibble_wobble_wibble 6498 + wibble_wobble_wibble_wobble_wibble_wobble_wibble 6499 6500 assert wibble_wobble_wibble_wobble_wibble_wobble_wibble 6501 == wibble_wobble_wibble_wobble_wibble_wobble_wibble 6502} 6503"# 6504 ); 6505} 6506 6507#[test] 6508fn comment_is_not_moved_after_assert() { 6509 assert_format!( 6510 "pub fn main() { 6511 // Wibble! 6512 assert True 6513} 6514" 6515 ); 6516} 6517 6518#[test] 6519fn todo_as_with_comment() { 6520 assert_format!( 6521 r#"pub fn main() { 6522 todo as 6523 // A little comment explaining something 6524 "wibble" 6525} 6526"# 6527 ); 6528} 6529 6530#[test] 6531fn todo_as_with_comment_on_the_same_line() { 6532 assert_format_rewrite!( 6533 r#"pub fn main() { 6534 todo as // A little comment explaining something 6535 "wibble" 6536} 6537"#, 6538 r#"pub fn main() { 6539 todo as 6540 // A little comment explaining something 6541 "wibble" 6542} 6543"# 6544 ); 6545} 6546 6547#[test] 6548fn todo_as_with_comment_before_the_as() { 6549 assert_format_rewrite!( 6550 r#"pub fn main() { 6551 todo // A little comment explaining something 6552 as "wibble" 6553} 6554"#, 6555 r#"pub fn main() { 6556 todo as 6557 // A little comment explaining something 6558 "wibble" 6559} 6560"# 6561 ); 6562} 6563 6564#[test] 6565fn panic_as_with_comment() { 6566 assert_format!( 6567 r#"pub fn main() { 6568 panic as 6569 // A little comment explaining something 6570 "wibble" 6571} 6572"# 6573 ); 6574} 6575 6576#[test] 6577fn panic_as_with_comment_on_the_same_line() { 6578 assert_format_rewrite!( 6579 r#"pub fn main() { 6580 panic as // A little comment explaining something 6581 "wibble" 6582} 6583"#, 6584 r#"pub fn main() { 6585 panic as 6586 // A little comment explaining something 6587 "wibble" 6588} 6589"# 6590 ); 6591} 6592 6593#[test] 6594fn panic_as_with_comment_before_the_as() { 6595 assert_format_rewrite!( 6596 r#"pub fn main() { 6597 panic // A little comment explaining something 6598 as "wibble" 6599} 6600"#, 6601 r#"pub fn main() { 6602 panic as 6603 // A little comment explaining something 6604 "wibble" 6605} 6606"# 6607 ); 6608} 6609 6610#[test] 6611fn echo_as_with_comment() { 6612 assert_format!( 6613 r#"pub fn main() { 6614 echo 1 as 6615 // A little comment explaining something 6616 "wibble" 6617} 6618"# 6619 ); 6620} 6621 6622#[test] 6623fn echo_as_with_comment_on_the_same_line() { 6624 assert_format_rewrite!( 6625 r#"pub fn main() { 6626 echo 1 as // A little comment explaining something 6627 "wibble" 6628} 6629"#, 6630 r#"pub fn main() { 6631 echo 1 as 6632 // A little comment explaining something 6633 "wibble" 6634} 6635"# 6636 ); 6637} 6638 6639#[test] 6640fn echo_as_with_comment_before_the_as() { 6641 assert_format_rewrite!( 6642 r#"pub fn main() { 6643 echo 1 // A little comment explaining something 6644 as "wibble" 6645} 6646"#, 6647 r#"pub fn main() { 6648 echo 1 as 6649 // A little comment explaining something 6650 "wibble" 6651} 6652"# 6653 ); 6654} 6655 6656#[test] 6657fn assert_as_with_comment() { 6658 assert_format!( 6659 r#"pub fn main() { 6660 assert True as 6661 // A little comment explaining something 6662 "wibble" 6663} 6664"# 6665 ); 6666} 6667 6668#[test] 6669fn assert_as_with_comment_on_the_same_line() { 6670 assert_format_rewrite!( 6671 r#"pub fn main() { 6672 assert True as // A little comment explaining something 6673 "wibble" 6674} 6675"#, 6676 r#"pub fn main() { 6677 assert True as 6678 // A little comment explaining something 6679 "wibble" 6680} 6681"# 6682 ); 6683} 6684 6685#[test] 6686fn assert_as_with_comment_before_the_as() { 6687 assert_format_rewrite!( 6688 r#"pub fn main() { 6689 assert True // A little comment explaining something 6690 as "wibble" 6691} 6692"#, 6693 r#"pub fn main() { 6694 assert True as 6695 // A little comment explaining something 6696 "wibble" 6697} 6698"# 6699 ); 6700} 6701 6702// https://github.com/gleam-lang/gleam/issues/4664 6703#[test] 6704fn pattern_unused_discard() { 6705 assert_format_rewrite!( 6706 r#"pub fn main() { 6707 let a = 10 6708 let _ = case a { 6709 _ as b -> b 6710 } 6711} 6712"#, 6713 r#"pub fn main() { 6714 let a = 10 6715 let _ = case a { 6716 b -> b 6717 } 6718} 6719"# 6720 ); 6721}