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