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

Configure Feed

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

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