Something went wrong. Try again.
⭐️ A friendly language for building type-safe, scalable systems!
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: 2023 The Gleam contributors
use crate::{assert_format, assert_format_rewrite};
#[test]fn capture_with_single_argument() { assert_format_rewrite!( "pub fn main() -> Nil { wibble([], wobble(_))}", "pub fn main() -> Nil { wibble([], wobble)}" );}
#[test]fn deprecated() { assert_format!( r#"@deprecated("use something else instead")pub fn main() -> Nil { Nil}"# );}
#[test]fn deprecated_external() { assert_format!( r#"@deprecated("use something else instead")@external(erlang, "thing", "main")pub fn main() -> Nil"# );}
#[test]fn anonymous_function_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, fn(x) { let y = x + 1 y })}"# );}
#[test]fn anonymous_function_with_single_line_body_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, fn(x) { x })}"# );}
#[test]fn anonymous_function_with_multi_line_unbreakable_body_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, fn(x) { call_to_other_function(a, b, c, d, e, f, g, h) })}"# );}
#[test]fn anonymous_function_with_multi_line_breakable_body_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, fn(x) { call_to_other_function(a, b, c, d, e, f, g, { x + x }) })}"# );}
#[test]fn anonymous_function_with_multi_line_long_breakable_body_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, fn(x) { call_to_other_function(a, b, c, d, e, f, g, case wibble { Wibble -> 1 Wobble -> 2 }) })}"# );}
#[test]fn function_call_as_final_function_argument_goes_on_its_own_line() { assert_format!( r#"pub fn main() { some_function_with_a_long_name( 123, 456, another_function_being_called(123, 456), )}"# );}
#[test]fn tuple_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, #( "Here is a very long string which causes the formatter to wrap it", ))}"# );}
#[test]fn list_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, [ "Here is a very long string which causes the formatter to wrap it", ])}"# );}
#[test]fn case_expression_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, case my_var { True -> True False -> False })}"# );}
#[test]fn block_as_final_function_argument() { assert_format!( r#"pub fn main() { some_function(123, 456, { let days = 7 days * 24 * 60 * 60 })}"# );}
#[test]fn when_all_arguments_are_too_long_each_one_is_on_its_own_line() { assert_format!( r#"pub fn main() { some_function( variable_with_really_long_name, whoah_this_is_getting_out_of_hand, ["Here is a very long string which causes the formatter to wrap it"], )}"# );}
#[test]fn nested_breakable_lists_in_function_calls() { assert_format!( r#"pub fn main() { html([attribute("lang", "en")], [ head([attribute("wibble", "wobble")], [ title([], [text("Hello this is some HTML")]), ]), body([], [h1([], [text("Hello, world!")])]), ])}"# );}
#[test]fn nested_breakable_tuples_in_function_calls() { assert_format!( r#"pub fn main() { html(#(attribute("lang", "en")), #( head(#(attribute("wibble", "wobble")), #( title(#(), #(text("Hello this is some HTML"))), body(#(), #(text("Hello this is some HTML"))), )), body(#(), #(h1(#(), #(text("Hello, lisp!"))))), ))}"# );}
// https://github.com/gleam-lang/gleam/issues/2435#[test]fn only_last_argument_can_be_broken() { assert_format!( r#"pub fn main() { tbd.workbook(for: "my_project") |> task( doc: "Run the project tests", tags: list.concat([["test", "ci"], gleam, typescript]), action: fn(_, _) { tbd.command(run: "gleam", with: ["test"]) }, ) |> run}"# );
assert_format!( r#"pub fn main() { Theme( flag: styler([32]), heading: styler([1, 95]), highlight: styler([1, 36]), parameter: styler([34]), tag: styler([33]), given_tag: styler([3]), first_tag: styler([1]), tab: " ", )}"# );}
#[test]fn function_that_is_a_little_over_the_limit() { assert_format!( r#"pub fn handle_request( handler: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,) -> Nil { todo}"# );}
// https://github.com/gleam-lang/gleam/issues/2571#[test]fn expr_function_as_last_argument() { assert_format!( r#"pub fn main() { Builder( accumulator: "", update: fn(accum, val) { accum <> val }, final: fn(accum) { accum }, )}"# );
// We want to make sure that, if it goes over the limit NOT with its // arguments' list the body is still the first thing that gets split. assert_format!( r#"pub fn main() { Builder(accumulator: "", update: fn(accum, val) { accum }, final: fn(accum) { accum })}"# );}
#[test]fn comment_at_start_of_inline_function_body() { assert_format!( r#"pub fn main() { let add = fn(x: Int, y: Int) { // This is a comment x + y }}"# );}
#[test]fn comment_at_start_of_top_level_function_body() { assert_format!( r#"pub fn add(x: Int, y: Int) { // This is a comment x + y}"# );}
#[test]fn comment_at_end_of_inline_function_args() { assert_format!( r#"pub fn main() { let add = fn( x: Int, y: Int, // This is a comment ) { x + y }}"# );}
#[test]fn comment_middle_of_inline_function_body() { assert_format!( r#"pub fn main() { let add = fn(x: Int, y: Int, z: Int) { let a = x + y // This is a comment a + z }}"# );}
// https://github.com/gleam-lang/gleam/issues/5004#[test]fn comment_in_tuple_return_type() { assert_format_rewrite!( r#"pub fn main() -> #( // This is a string String, // This is an awesome string) { todo}"#, r#"pub fn main() -> #( // This is a string String, // This is an awesome string) { todo}"# );}
// https://github.com/gleam-lang/gleam/issues/5225#[test]fn comments_between_function_params() { assert_format!( r#"pub fn main( // a function with argument comments a: fn( // Int, String, Int, // this is A String, // this is B String, fn( // an inner fn, fn arg Int, // trailing comment ) -> String, // multiple lines // for a really interesting // comment Bool, // trailing comment ) -> Nil, // a function with no argument comments b: fn(Int) -> Nil, c: fn() -> Nil,) { todo}"# );}
#[test]fn long_function_slightly_over_80_chars_gets_broken() { assert_format_rewrite!( "fn do_menu_items(acc: Queue(MenuItem), from tasks: List(Task)) -> List(MenuItem) { todo}", "fn do_menu_items( acc: Queue(MenuItem), from tasks: List(Task),) -> List(MenuItem) { todo}" );}