Something went wrong. Try again.
⭐️ A friendly language for building type-safe, scalable systems!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: 2023 The Gleam contributors
use crate::{assert_format, assert_format_rewrite};
#[test]fn construction() { assert_format!( "fn main() { let a = 1 let x = <<1, a, 2:bytes>> let size = <<3:2, 4:size(3), 5:bytes-size(4), 6:size(a)>> let unit = <<7:unit(1), 8:bytes-unit(2)>> x}", );}
#[test]fn pattern() { assert_format!( "fn main() { let a = 1 let <<b, c, d:bytes>> = <<1, a, 2:bytes>> b}", );}
#[test]fn long() { assert_format!( "fn main() { let some_really_long_variable_name_to_force_wrapping = 1 let bits = << some_really_long_variable_name_to_force_wrapping, some_really_long_variable_name_to_force_wrapping, >> bits}", );}
// https://github.com/gleam-lang/gleam/issues/2932#[test]fn tight_empty() { assert_format!( "fn main() { let some_really_really_really_really_really_really_really_long_variable_name_to_force_wrapping = <<>> some_really_really_really_really_really_really_really_long_variable_name_to_force_wrapping}" );}
#[test]fn comments_are_not_moved_out_of_empty_bit_array() { assert_format!( r#"pub fn main() { // This is an empty bit array! << // Nothing here... >>}"# );}
#[test]fn empty_bit_arrays_with_comment_inside_are_indented_properly() { assert_format!( r#"pub fn main() { fun( << // Nothing here... >>, wibble_wobble_wibble_wobble_wibble_wobble_wibble_wobble, << // Nothing here as well! >>, )}"# );}
#[test]fn comments_inside_non_empty_bit_arrays_are_not_moved() { assert_format!( r#"pub fn main() { fun( << // One is below me. 1, 2, // Three is below me. 3, >>, wibble_wobble_wibble_wobble_wibble_wobble_wibble_wobble, << // Three is below me. 3, >>, )}"# );}
#[test]fn concise_wrapping_of_simple_bit_arrays() { assert_format!( "pub fn main() { << 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, >>}" );}
#[test]fn concise_wrapping_of_simple_bit_arrays1() { assert_format!( "pub fn main() { << 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, 16.0, 17.0, 18.0, 19.0, 2.0, >>}" );}
#[test]fn concise_wrapping_of_simple_bit_arrays2() { assert_format!( r#"pub fn main() { << "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", >>}"# );}
#[test]fn concise_wrapping_of_simple_bit_arrays3() { assert_format!( "const values = << 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000,>>" );}
#[test]fn concise_wrapping_of_simple_bit_arrays4() { assert_format!( "const values = << 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, 16.0, 17.0, 18.0, 19.0, 2.0,>>" );}
#[test]fn concise_wrapping_of_simple_bit_arrays5() { assert_format!( r#"const values = << "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",>>"# );}
#[test]fn binop_value() { assert_format!( r#"pub fn main() { <<{ 1 + 1 }>>}"# );}
#[test]fn block_value() { assert_format!( r#"pub fn main() { << { io.println("hi") 1 }, >>}"# );}
#[test]fn operator_in_pattern_size() { assert_format!( "pub fn main() { let assert <<len, payload:size({ len + 1 } * 8 + 1)>> = <<>>}" );}
#[test]// https://github.com/gleam-lang/gleam/issues/4792#issuecomment-3096177213fn bit_array_segments_are_kept_one_per_line() { assert_format!( "pub fn main() { << 1:1, 1:1, 0:2, opcode:4, masked:1, length_section:bits, mask_key:bits, data:bits, >> |> bytes_tree.from_bit_array}" );}
#[test]fn bit_array_with_trailing_comma_is_broken() { assert_format_rewrite!( "pub fn main() { <<1, 2, a,>> }", r#"pub fn main() { << 1, 2, a, >>}"# );}
#[test]fn constant_bit_array_with_trailing_comma_is_broken() { assert_format_rewrite!( "const bit_array = <<1, 2, a,>>", r#"const bit_array = << 1, 2, a,>>"# );}
#[test]fn bit_array_with_trailing_comma_is_kept_broken() { assert_format!( r#"pub fn main() { << 1, 2, a, >>}"# );}
#[test]fn constant_bit_array_with_trailing_comma_is_kept_broken() { assert_format!( r#"const bit_array = << 1, 2, a,>>"# );}
#[test]fn bit_array_with_no_trailing_comma_is_packed_on_a_single_line() { assert_format_rewrite!( r#"pub fn main() { << 1, 2, a >>}"#, r#"pub fn main() { <<1, 2, a>>}"# );}
#[test]fn constant_bit_array_with_no_trailing_comma_is_packed_on_a_single_line() { assert_format_rewrite!( r#"const bit_array = << 1, 2, a>>"#, "const bit_array = <<1, 2, a>>\n" );}
#[test]fn bit_array_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { assert_format_rewrite!( "pub fn main() { << 1, a, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, b, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312 >>}", "pub fn main() { << 1, a, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, b, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, >>}" );}
#[test]fn constant_bit_array_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { assert_format_rewrite!( "const bit_array = << 1, a, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, b, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312>>", "const bit_array = << 1, a, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, b, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312, 12_312_312_312_312_312_312_312,>>" );}
#[test]fn simple_bit_array_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { assert_format_rewrite!( r#"pub fn main() { << "hello", "wibble wobble", "these are all simple strings", "but the bitarray won't be packed", "the formatter will keep", "one item", "per line", "since there's no trailing comma here ->" >>}"#, r#"pub fn main() { << "hello", "wibble wobble", "these are all simple strings", "but the bitarray won't be packed", "the formatter will keep", "one item", "per line", "since there's no trailing comma here ->", >>}"# );}
#[test]fn simple_bit_array_with_trailing_comma_and_multiple_items_per_line_is_packed() { assert_format_rewrite!( r#"pub fn main() { << "hello", "wibble wobble", "these are all simple strings", "and the bit array will be packed since the following strings are", "on the same", "line", "and there's a trailing comma ->", >>}"#, r#"pub fn main() { << "hello", "wibble wobble", "these are all simple strings", "and the bit array will be packed since the following strings are", "on the same", "line", "and there's a trailing comma ->", >>}"# );}
#[test]fn simple_constant_bit_array_with_trailing_comma_and_multiple_items_per_line_is_packed() { assert_format_rewrite!( r#"pub const bit_array = << "hello", "wibble wobble", "these are all simple strings", "and the bit array will be packed since the following strings are", "on the same", "line", "and there's a trailing comma ->",>>"#, r#"pub const bit_array = << "hello", "wibble wobble", "these are all simple strings", "and the bit array will be packed since the following strings are", "on the same", "line", "and there's a trailing comma ->",>>"# );}
#[test]fn simple_packed_bit_array_with_trailing_comma_is_kept_with_multiple_items_per_line() { assert_format!( r#"pub fn main() { << "hello", "wibble wobble", "these are all simple strings", "and the bit array will be kept packed since it ends with a trailing comma", "right here! ->", >>}"# );}
#[test]fn simple_single_line_bit_array_with_trailing_comma_is_split_one_item_per_line() { assert_format_rewrite!( r#"pub fn main() { <<"these are all simple strings", "but the bit array won't be packed", "since it ends with a trailing comma ->",>>}"#, r#"pub fn main() { << "these are all simple strings", "but the bit array won't be packed", "since it ends with a trailing comma ->", >>}"# );}
#[test]fn simple_single_line_bit_array_with_no_trailing_comma_is_split_one_item_per_line() { assert_format_rewrite!( r#"pub fn main() { <<"these are all simple strings", "but the bit array won't be packed", "even if it doesn't end with a trailing comma!">>}"#, r#"pub fn main() { << "these are all simple strings", "but the bit array won't be packed", "even if it doesn't end with a trailing comma!", >>}"# );}
// https://github.com/gleam-lang/gleam/issues/5606#[test]fn dont_insert_curly_braces_with_binary_operators_when_line_is_short() { assert_format!( "pub fn main() { let wubble = <<bits:bits-size(wibble * wobble)-unit(8)>>}" );}
// https://github.com/gleam-lang/gleam/issues/5606#[test]fn dont_insert_curly_braces_with_binary_operators_when_line_is_long() { assert_format_rewrite!( "pub fn main() { let some_long_variable_name = <<bits:bits-size(some_other_long_variable_name * and_some_other_long_variable_name)-unit(8)>>}", "pub fn main() { let some_long_variable_name = << bits:bits-size( some_other_long_variable_name * and_some_other_long_variable_name )-unit(8), >>}" );}
// https://github.com/gleam-lang/gleam/issues/5606#[test]fn preserve_user_inserted_curly_braces_with_binary_operators_when_line_is_short() { assert_format!( "pub fn main() { let some_variable = <<bits:bits-size({ some * other })>>}" );}
// https://github.com/gleam-lang/gleam/issues/5606#[test]fn preserve_user_inserted_curly_braces_with_binary_operators_when_line_is_long() { assert_format_rewrite!( "pub fn main() { let some_long_variable_name = <<bits:bits-size({ some_other_long_variable_name * and_some_other_long_variable_name })>>}", "pub fn main() { let some_long_variable_name = << bits:bits-size({ some_other_long_variable_name * and_some_other_long_variable_name }), >>}" );}