1use crate::{assert_format, assert_format_rewrite};
2
3#[test]
4fn capture_with_single_argument() {
5 assert_format_rewrite!(
6 "pub fn main() -> Nil {
7 wibble([], wobble(_))
8}
9",
10 "pub fn main() -> Nil {
11 wibble([], wobble)
12}
13"
14 );
15}
16
17#[test]
18fn deprecated() {
19 assert_format!(
20 r#"@deprecated("use something else instead")
21pub fn main() -> Nil {
22 Nil
23}
24"#
25 );
26}
27
28#[test]
29fn deprecated_external() {
30 assert_format!(
31 r#"@deprecated("use something else instead")
32@external(erlang, "thing", "main")
33pub fn main() -> Nil
34"#
35 );
36}
37
38#[test]
39fn anonymous_function_as_final_function_argument() {
40 assert_format!(
41 r#"pub fn main() {
42 some_function(123, 456, fn(x) {
43 let y = x + 1
44 y
45 })
46}
47"#
48 );
49}
50
51#[test]
52fn anonymous_function_with_single_line_body_as_final_function_argument() {
53 assert_format!(
54 r#"pub fn main() {
55 some_function(123, 456, fn(x) { x })
56}
57"#
58 );
59}
60
61#[test]
62fn anonymous_function_with_multi_line_unbreakable_body_as_final_function_argument() {
63 assert_format!(
64 r#"pub fn main() {
65 some_function(123, 456, fn(x) {
66 call_to_other_function(a, b, c, d, e, f, g, h)
67 })
68}
69"#
70 );
71}
72
73#[test]
74fn anonymous_function_with_multi_line_breakable_body_as_final_function_argument() {
75 assert_format!(
76 r#"pub fn main() {
77 some_function(123, 456, fn(x) {
78 call_to_other_function(a, b, c, d, e, f, g, { x + x })
79 })
80}
81"#
82 );
83}
84
85#[test]
86fn anonymous_function_with_multi_line_long_breakable_body_as_final_function_argument() {
87 assert_format!(
88 r#"pub fn main() {
89 some_function(123, 456, fn(x) {
90 call_to_other_function(a, b, c, d, e, f, g, case wibble {
91 Wibble -> 1
92 Wobble -> 2
93 })
94 })
95}
96"#
97 );
98}
99
100#[test]
101fn function_call_as_final_function_argument_goes_on_its_own_line() {
102 assert_format!(
103 r#"pub fn main() {
104 some_function_with_a_long_name(
105 123,
106 456,
107 another_function_being_called(123, 456),
108 )
109}
110"#
111 );
112}
113
114#[test]
115fn tuple_as_final_function_argument() {
116 assert_format!(
117 r#"pub fn main() {
118 some_function(123, 456, #(
119 "Here is a very long string which causes the formatter to wrap it",
120 ))
121}
122"#
123 );
124}
125
126#[test]
127fn list_as_final_function_argument() {
128 assert_format!(
129 r#"pub fn main() {
130 some_function(123, 456, [
131 "Here is a very long string which causes the formatter to wrap it",
132 ])
133}
134"#
135 );
136}
137
138#[test]
139fn case_expression_as_final_function_argument() {
140 assert_format!(
141 r#"pub fn main() {
142 some_function(123, 456, case my_var {
143 True -> True
144 False -> False
145 })
146}
147"#
148 );
149}
150
151#[test]
152fn block_as_final_function_argument() {
153 assert_format!(
154 r#"pub fn main() {
155 some_function(123, 456, {
156 let days = 7
157 days * 24 * 60 * 60
158 })
159}
160"#
161 );
162}
163
164#[test]
165fn when_all_arguments_are_too_long_each_one_is_on_its_own_line() {
166 assert_format!(
167 r#"pub fn main() {
168 some_function(
169 variable_with_really_long_name,
170 whoah_this_is_getting_out_of_hand,
171 ["Here is a very long string which causes the formatter to wrap it"],
172 )
173}
174"#
175 );
176}
177
178#[test]
179fn nested_breakable_lists_in_function_calls() {
180 assert_format!(
181 r#"pub fn main() {
182 html([attribute("lang", "en")], [
183 head([attribute("wibble", "wobble")], [
184 title([], [text("Hello this is some HTML")]),
185 ]),
186 body([], [h1([], [text("Hello, world!")])]),
187 ])
188}
189"#
190 );
191}
192
193#[test]
194fn nested_breakable_tuples_in_function_calls() {
195 assert_format!(
196 r#"pub fn main() {
197 html(#(attribute("lang", "en")), #(
198 head(#(attribute("wibble", "wobble")), #(
199 title(#(), #(text("Hello this is some HTML"))),
200 body(#(), #(text("Hello this is some HTML"))),
201 )),
202 body(#(), #(h1(#(), #(text("Hello, lisp!"))))),
203 ))
204}
205"#
206 );
207}
208
209// https://github.com/gleam-lang/gleam/issues/2435
210#[test]
211fn only_last_argument_can_be_broken() {
212 assert_format!(
213 r#"pub fn main() {
214 tbd.workbook(for: "my_project")
215 |> task(
216 doc: "Run the project tests",
217 tags: list.concat([["test", "ci"], gleam, typescript]),
218 action: fn(_, _) { tbd.command(run: "gleam", with: ["test"]) },
219 )
220 |> run
221}
222"#
223 );
224
225 assert_format!(
226 r#"pub fn main() {
227 Theme(
228 flag: styler([32]),
229 heading: styler([1, 95]),
230 highlight: styler([1, 36]),
231 parameter: styler([34]),
232 tag: styler([33]),
233 given_tag: styler([3]),
234 first_tag: styler([1]),
235 tab: " ",
236 )
237}
238"#
239 );
240}
241
242#[test]
243fn function_that_is_a_little_over_the_limit() {
244 assert_format!(
245 r#"pub fn handle_request(
246 handler: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
247) -> Nil {
248 todo
249}
250"#
251 );
252}
253
254// https://github.com/gleam-lang/gleam/issues/2571
255#[test]
256fn expr_function_as_last_argument() {
257 assert_format!(
258 r#"pub fn main() {
259 Builder(
260 accumulator: "",
261 update: fn(accum, val) { accum <> val },
262 final: fn(accum) { accum },
263 )
264}
265"#
266 );
267
268 // We want to make sure that, if it goes over the limit NOT with its
269 // arguments' list the body is still the first thing that gets split.
270 assert_format!(
271 r#"pub fn main() {
272 Builder(accumulator: "", update: fn(accum, val) { accum }, final: fn(accum) {
273 accum
274 })
275}
276"#
277 );
278}
279
280#[test]
281fn comment_at_start_of_inline_function_body() {
282 assert_format!(
283 r#"pub fn main() {
284 let add = fn(x: Int, y: Int) {
285 // This is a comment
286 x + y
287 }
288}
289"#
290 );
291}
292
293#[test]
294fn comment_at_start_of_top_level_function_body() {
295 assert_format!(
296 r#"pub fn add(x: Int, y: Int) {
297 // This is a comment
298 x + y
299}
300"#
301 );
302}
303
304#[test]
305fn comment_at_end_of_inline_function_args() {
306 assert_format!(
307 r#"pub fn main() {
308 let add = fn(
309 x: Int,
310 y: Int,
311 // This is a comment
312 ) {
313 x + y
314 }
315}
316"#
317 );
318}
319
320#[test]
321fn comment_middle_of_inline_function_body() {
322 assert_format!(
323 r#"pub fn main() {
324 let add = fn(x: Int, y: Int, z: Int) {
325 let a = x + y
326 // This is a comment
327 a + z
328 }
329}
330"#
331 );
332}