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

Configure Feed

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

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