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 / bit_array.rs
9.7 kB 512 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2023 The Gleam contributors 3 4use crate::{assert_format, assert_format_rewrite}; 5 6#[test] 7fn construction() { 8 assert_format!( 9 "fn main() { 10 let a = 1 11 let x = <<1, a, 2:bytes>> 12 let size = <<3:2, 4:size(3), 5:bytes-size(4), 6:size(a)>> 13 let unit = <<7:unit(1), 8:bytes-unit(2)>> 14 x 15} 16", 17 ); 18} 19 20#[test] 21fn pattern() { 22 assert_format!( 23 "fn main() { 24 let a = 1 25 let <<b, c, d:bytes>> = <<1, a, 2:bytes>> 26 b 27} 28", 29 ); 30} 31 32#[test] 33fn long() { 34 assert_format!( 35 "fn main() { 36 let some_really_long_variable_name_to_force_wrapping = 1 37 let bits = << 38 some_really_long_variable_name_to_force_wrapping, 39 some_really_long_variable_name_to_force_wrapping, 40 >> 41 bits 42} 43", 44 ); 45} 46 47// https://github.com/gleam-lang/gleam/issues/2932 48#[test] 49fn tight_empty() { 50 assert_format!( 51 "fn main() { 52 let some_really_really_really_really_really_really_really_long_variable_name_to_force_wrapping = <<>> 53 some_really_really_really_really_really_really_really_long_variable_name_to_force_wrapping 54} 55" 56 ); 57} 58 59#[test] 60fn comments_are_not_moved_out_of_empty_bit_array() { 61 assert_format!( 62 r#"pub fn main() { 63 // This is an empty bit array! 64 << 65 // Nothing here... 66 >> 67} 68"# 69 ); 70} 71 72#[test] 73fn empty_bit_arrays_with_comment_inside_are_indented_properly() { 74 assert_format!( 75 r#"pub fn main() { 76 fun( 77 << 78 // Nothing here... 79 >>, 80 wibble_wobble_wibble_wobble_wibble_wobble_wibble_wobble, 81 << 82 // Nothing here as well! 83 >>, 84 ) 85} 86"# 87 ); 88} 89 90#[test] 91fn comments_inside_non_empty_bit_arrays_are_not_moved() { 92 assert_format!( 93 r#"pub fn main() { 94 fun( 95 << 96 // One is below me. 97 1, 2, 98 // Three is below me. 99 3, 100 >>, 101 wibble_wobble_wibble_wobble_wibble_wobble_wibble_wobble, 102 << 103 // Three is below me. 104 3, 105 >>, 106 ) 107} 108"# 109 ); 110} 111 112#[test] 113fn concise_wrapping_of_simple_bit_arrays() { 114 assert_format!( 115 "pub fn main() { 116 << 117 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 118 1500, 1600, 1700, 1800, 1900, 2000, 119 >> 120} 121" 122 ); 123} 124 125#[test] 126fn concise_wrapping_of_simple_bit_arrays1() { 127 assert_format!( 128 "pub fn main() { 129 << 130 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, 131 15.0, 16.0, 17.0, 18.0, 19.0, 2.0, 132 >> 133} 134" 135 ); 136} 137 138#[test] 139fn concise_wrapping_of_simple_bit_arrays2() { 140 assert_format!( 141 r#"pub fn main() { 142 << 143 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", 144 "ten", "eleven", "twelve", 145 >> 146} 147"# 148 ); 149} 150 151#[test] 152fn concise_wrapping_of_simple_bit_arrays3() { 153 assert_format!( 154 "const values = << 155 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 156 1500, 1600, 1700, 1800, 1900, 2000, 157>> 158" 159 ); 160} 161 162#[test] 163fn concise_wrapping_of_simple_bit_arrays4() { 164 assert_format!( 165 "const values = << 166 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, 167 16.0, 17.0, 18.0, 19.0, 2.0, 168>> 169" 170 ); 171} 172 173#[test] 174fn concise_wrapping_of_simple_bit_arrays5() { 175 assert_format!( 176 r#"const values = << 177 "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 178 "eleven", "twelve", 179>> 180"# 181 ); 182} 183 184#[test] 185fn binop_value() { 186 assert_format!( 187 r#"pub fn main() { 188 <<{ 1 + 1 }>> 189} 190"# 191 ); 192} 193 194#[test] 195fn block_value() { 196 assert_format!( 197 r#"pub fn main() { 198 << 199 { 200 io.println("hi") 201 1 202 }, 203 >> 204} 205"# 206 ); 207} 208 209#[test] 210fn operator_in_pattern_size() { 211 assert_format!( 212 "pub fn main() { 213 let assert <<len, payload:size({ len + 1 } * 8 + 1)>> = <<>> 214} 215" 216 ); 217} 218 219#[test] 220// https://github.com/gleam-lang/gleam/issues/4792#issuecomment-3096177213 221fn bit_array_segments_are_kept_one_per_line() { 222 assert_format!( 223 "pub fn main() { 224 << 225 1:1, 226 1:1, 227 0:2, 228 opcode:4, 229 masked:1, 230 length_section:bits, 231 mask_key:bits, 232 data:bits, 233 >> 234 |> bytes_tree.from_bit_array 235} 236" 237 ); 238} 239 240#[test] 241fn bit_array_with_trailing_comma_is_broken() { 242 assert_format_rewrite!( 243 "pub fn main() { <<1, 2, a,>> }", 244 r#"pub fn main() { 245 << 246 1, 247 2, 248 a, 249 >> 250} 251"# 252 ); 253} 254 255#[test] 256fn constant_bit_array_with_trailing_comma_is_broken() { 257 assert_format_rewrite!( 258 "const bit_array = <<1, 2, a,>>", 259 r#"const bit_array = << 260 1, 261 2, 262 a, 263>> 264"# 265 ); 266} 267 268#[test] 269fn bit_array_with_trailing_comma_is_kept_broken() { 270 assert_format!( 271 r#"pub fn main() { 272 << 273 1, 274 2, 275 a, 276 >> 277} 278"# 279 ); 280} 281 282#[test] 283fn constant_bit_array_with_trailing_comma_is_kept_broken() { 284 assert_format!( 285 r#"const bit_array = << 286 1, 287 2, 288 a, 289>> 290"# 291 ); 292} 293 294#[test] 295fn bit_array_with_no_trailing_comma_is_packed_on_a_single_line() { 296 assert_format_rewrite!( 297 r#"pub fn main() { 298 << 299 1, 300 2, 301 a 302 >> 303} 304"#, 305 r#"pub fn main() { 306 <<1, 2, a>> 307} 308"# 309 ); 310} 311 312#[test] 313fn constant_bit_array_with_no_trailing_comma_is_packed_on_a_single_line() { 314 assert_format_rewrite!( 315 r#"const bit_array = << 316 1, 317 2, 318 a 319>>"#, 320 "const bit_array = <<1, 2, a>>\n" 321 ); 322} 323 324#[test] 325fn bit_array_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 326 assert_format_rewrite!( 327 "pub fn main() { 328 << 329 1, 330 a, 331 12_312_312_312_312_312_312_312, 332 12_312_312_312_312_312_312_312, 333 12_312_312_312_312_312_312_312, 334 b, 335 12_312_312_312_312_312_312_312, 336 12_312_312_312_312_312_312_312, 337 12_312_312_312_312_312_312_312 338 >> 339} 340", 341 "pub fn main() { 342 << 343 1, 344 a, 345 12_312_312_312_312_312_312_312, 346 12_312_312_312_312_312_312_312, 347 12_312_312_312_312_312_312_312, 348 b, 349 12_312_312_312_312_312_312_312, 350 12_312_312_312_312_312_312_312, 351 12_312_312_312_312_312_312_312, 352 >> 353} 354" 355 ); 356} 357 358#[test] 359fn constant_bit_array_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 360 assert_format_rewrite!( 361 "const bit_array = << 362 1, 363 a, 364 12_312_312_312_312_312_312_312, 365 12_312_312_312_312_312_312_312, 366 12_312_312_312_312_312_312_312, 367 b, 368 12_312_312_312_312_312_312_312, 369 12_312_312_312_312_312_312_312, 370 12_312_312_312_312_312_312_312 371>> 372", 373 "const bit_array = << 374 1, 375 a, 376 12_312_312_312_312_312_312_312, 377 12_312_312_312_312_312_312_312, 378 12_312_312_312_312_312_312_312, 379 b, 380 12_312_312_312_312_312_312_312, 381 12_312_312_312_312_312_312_312, 382 12_312_312_312_312_312_312_312, 383>> 384" 385 ); 386} 387 388#[test] 389fn simple_bit_array_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 390 assert_format_rewrite!( 391 r#"pub fn main() { 392 << 393 "hello", 394 "wibble wobble", 395 "these are all simple strings", 396 "but the bitarray won't be packed", 397 "the formatter will keep", 398 "one item", 399 "per line", 400 "since there's no trailing comma here ->" 401 >> 402} 403"#, 404 r#"pub fn main() { 405 << 406 "hello", 407 "wibble wobble", 408 "these are all simple strings", 409 "but the bitarray won't be packed", 410 "the formatter will keep", 411 "one item", 412 "per line", 413 "since there's no trailing comma here ->", 414 >> 415} 416"# 417 ); 418} 419 420#[test] 421fn simple_bit_array_with_trailing_comma_and_multiple_items_per_line_is_packed() { 422 assert_format_rewrite!( 423 r#"pub fn main() { 424 << 425 "hello", 426 "wibble wobble", 427 "these are all simple strings", 428 "and the bit array will be packed since the following strings are", 429 "on the same", "line", "and there's a trailing comma ->", 430 >> 431} 432"#, 433 r#"pub fn main() { 434 << 435 "hello", "wibble wobble", "these are all simple strings", 436 "and the bit array will be packed since the following strings are", 437 "on the same", "line", "and there's a trailing comma ->", 438 >> 439} 440"# 441 ); 442} 443 444#[test] 445fn simple_constant_bit_array_with_trailing_comma_and_multiple_items_per_line_is_packed() { 446 assert_format_rewrite!( 447 r#"pub const bit_array = << 448 "hello", 449 "wibble wobble", 450 "these are all simple strings", 451 "and the bit array will be packed since the following strings are", 452 "on the same", "line", "and there's a trailing comma ->", 453>> 454"#, 455 r#"pub const bit_array = << 456 "hello", "wibble wobble", "these are all simple strings", 457 "and the bit array will be packed since the following strings are", 458 "on the same", "line", "and there's a trailing comma ->", 459>> 460"# 461 ); 462} 463 464#[test] 465fn simple_packed_bit_array_with_trailing_comma_is_kept_with_multiple_items_per_line() { 466 assert_format!( 467 r#"pub fn main() { 468 << 469 "hello", "wibble wobble", "these are all simple strings", 470 "and the bit array will be kept packed since it ends with a trailing comma", 471 "right here! ->", 472 >> 473} 474"# 475 ); 476} 477 478#[test] 479fn simple_single_line_bit_array_with_trailing_comma_is_split_one_item_per_line() { 480 assert_format_rewrite!( 481 r#"pub fn main() { 482 <<"these are all simple strings", "but the bit array won't be packed", "since it ends with a trailing comma ->",>> 483} 484"#, 485 r#"pub fn main() { 486 << 487 "these are all simple strings", 488 "but the bit array won't be packed", 489 "since it ends with a trailing comma ->", 490 >> 491} 492"# 493 ); 494} 495 496#[test] 497fn simple_single_line_bit_array_with_no_trailing_comma_is_split_one_item_per_line() { 498 assert_format_rewrite!( 499 r#"pub fn main() { 500 <<"these are all simple strings", "but the bit array won't be packed", "even if it doesn't end with a trailing comma!">> 501} 502"#, 503 r#"pub fn main() { 504 << 505 "these are all simple strings", 506 "but the bit array won't be packed", 507 "even if it doesn't end with a trailing comma!", 508 >> 509} 510"# 511 ); 512}