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

Configure Feed

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

gleam / format / src / tests / lists.rs
6.5 kB 383 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2025 The Gleam contributors 3 4use crate::{assert_format, assert_format_rewrite}; 5 6#[test] 7fn list_with_trailing_comma_is_broken() { 8 assert_format_rewrite!( 9 "pub fn main() { [ 1, 2, a, ] }", 10 r#"pub fn main() { 11 [ 12 1, 13 2, 14 a, 15 ] 16} 17"# 18 ); 19} 20 21#[test] 22fn constant_list_with_trailing_comma_is_broken() { 23 assert_format_rewrite!( 24 "const list = [ 1, 2, a, ]", 25 r#"const list = [ 26 1, 27 2, 28 a, 29] 30"# 31 ); 32} 33 34#[test] 35fn list_with_trailing_comma_is_kept_broken() { 36 assert_format!( 37 r#"pub fn main() { 38 [ 39 1, 40 2, 41 a, 42 ] 43} 44"# 45 ); 46} 47 48#[test] 49fn constant_list_with_trailing_comma_is_kept_broken() { 50 assert_format!( 51 r#"const list = [ 52 1, 53 2, 54 a, 55] 56"# 57 ); 58} 59 60#[test] 61fn list_with_no_trailing_comma_is_packed_on_a_single_line() { 62 assert_format_rewrite!( 63 r#"pub fn main() { 64 [ 65 1, 66 2, 67 a 68 ] 69} 70"#, 71 r#"pub fn main() { 72 [1, 2, a] 73} 74"# 75 ); 76} 77 78#[test] 79fn constant_list_with_no_trailing_comma_is_packed_on_a_single_line() { 80 assert_format_rewrite!( 81 r#"const list = [ 82 1, 83 2, 84 a 85]"#, 86 "const list = [1, 2, a]\n" 87 ); 88} 89 90#[test] 91fn list_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 92 assert_format_rewrite!( 93 "pub fn main() { 94 [ 95 1, 96 a, 97 12_312_312_312_312_312_312_312, 98 12_312_312_312_312_312_312_312, 99 12_312_312_312_312_312_312_312, 100 b, 101 12_312_312_312_312_312_312_312, 102 12_312_312_312_312_312_312_312, 103 12_312_312_312_312_312_312_312 104 ] 105} 106", 107 "pub fn main() { 108 [ 109 1, 110 a, 111 12_312_312_312_312_312_312_312, 112 12_312_312_312_312_312_312_312, 113 12_312_312_312_312_312_312_312, 114 b, 115 12_312_312_312_312_312_312_312, 116 12_312_312_312_312_312_312_312, 117 12_312_312_312_312_312_312_312, 118 ] 119} 120" 121 ); 122} 123 124#[test] 125fn constant_list_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 126 assert_format_rewrite!( 127 "const list = [ 128 1, 129 a, 130 12_312_312_312_312_312_312_312, 131 12_312_312_312_312_312_312_312, 132 12_312_312_312_312_312_312_312, 133 b, 134 12_312_312_312_312_312_312_312, 135 12_312_312_312_312_312_312_312, 136 12_312_312_312_312_312_312_312 137] 138", 139 "const list = [ 140 1, 141 a, 142 12_312_312_312_312_312_312_312, 143 12_312_312_312_312_312_312_312, 144 12_312_312_312_312_312_312_312, 145 b, 146 12_312_312_312_312_312_312_312, 147 12_312_312_312_312_312_312_312, 148 12_312_312_312_312_312_312_312, 149] 150" 151 ); 152} 153 154#[test] 155fn simple_list_with_no_comma_is_packed_on_a_single_line_or_split_one_item_per_line() { 156 assert_format_rewrite!( 157 r#"pub fn main() { 158 [ 159 "hello", 160 "wibble wobble", 161 "these are all simple strings", 162 "but the list won't be packed", 163 "the formatter will keep", 164 "one item", 165 "per line", 166 "since there's no trailing comma here ->" 167 ] 168} 169"#, 170 r#"pub fn main() { 171 [ 172 "hello", 173 "wibble wobble", 174 "these are all simple strings", 175 "but the list won't be packed", 176 "the formatter will keep", 177 "one item", 178 "per line", 179 "since there's no trailing comma here ->", 180 ] 181} 182"# 183 ); 184} 185 186#[test] 187fn simple_list_with_trailing_comma_and_multiple_items_per_line_is_packed() { 188 assert_format_rewrite!( 189 r#"pub fn main() { 190 [ 191 "hello", 192 "wibble wobble", 193 "these are all simple strings", 194 "and the list will be packed since the following strings are", 195 "on the same", "line", "and there's a trailing comma ->", 196 ] 197} 198"#, 199 r#"pub fn main() { 200 [ 201 "hello", "wibble wobble", "these are all simple strings", 202 "and the list will be packed since the following strings are", "on the same", 203 "line", "and there's a trailing comma ->", 204 ] 205} 206"# 207 ); 208} 209 210#[test] 211fn simple_constant_list_with_trailing_comma_and_multiple_items_per_line_is_packed() { 212 assert_format_rewrite!( 213 r#"pub const list = [ 214 "hello", 215 "wibble wobble", 216 "these are all simple strings", 217 "and the list will be packed since the following strings are", 218 "on the same", "line", "and there's a trailing comma ->", 219] 220"#, 221 r#"pub const list = [ 222 "hello", "wibble wobble", "these are all simple strings", 223 "and the list will be packed since the following strings are", "on the same", 224 "line", "and there's a trailing comma ->", 225] 226"# 227 ); 228} 229 230#[test] 231fn simple_packed_list_with_trailing_comma_is_kept_with_multiple_items_per_line() { 232 assert_format!( 233 r#"pub fn main() { 234 [ 235 "hello", "wibble wobble", "these are all simple strings", 236 "and the list will be kept packed since it ends with a trailing comma", 237 "right here! ->", 238 ] 239} 240"# 241 ); 242} 243 244#[test] 245fn simple_single_line_list_with_trailing_comma_is_split_one_item_per_line() { 246 assert_format_rewrite!( 247 r#"pub fn main() { 248 ["these are all simple strings", "but the list won't be packed", "since it ends with a trailing comma ->",] 249} 250"#, 251 r#"pub fn main() { 252 [ 253 "these are all simple strings", 254 "but the list won't be packed", 255 "since it ends with a trailing comma ->", 256 ] 257} 258"# 259 ); 260} 261 262#[test] 263fn simple_single_line_list_with_no_trailing_comma_is_split_one_item_per_line() { 264 assert_format_rewrite!( 265 r#"pub fn main() { 266 ["these are all simple strings", "but the list won't be packed", "even if it doesn't end with a trailing comma!"] 267} 268"#, 269 r#"pub fn main() { 270 [ 271 "these are all simple strings", 272 "but the list won't be packed", 273 "even if it doesn't end with a trailing comma!", 274 ] 275} 276"# 277 ); 278} 279 280#[test] 281fn empty_lines_in_list_are_not_ignored() { 282 assert_format_rewrite!( 283 "pub fn main() { 284 [1, 2, 285 286 3 287 ] 288} 289", 290 "pub fn main() { 291 [ 292 1, 293 2, 294 295 3, 296 ] 297} 298" 299 ); 300} 301 302#[test] 303fn empty_lines_in_const_list_are_not_ignored() { 304 assert_format_rewrite!( 305 "const list = 306 [1, 2, 307 308 3 309 ] 310", 311 "const list = [ 312 1, 313 2, 314 315 3, 316] 317" 318 ); 319} 320 321#[test] 322fn lists_with_empty_lines_are_always_broken() { 323 assert_format_rewrite!( 324 "pub fn main() { 325 [ 326 1, 327 2, 328 329 3, 4, 5 330 ] 331} 332", 333 "pub fn main() { 334 [ 335 1, 336 2, 337 338 3, 339 4, 340 5, 341 ] 342} 343" 344 ); 345} 346 347#[test] 348fn const_lists_with_empty_lines_are_always_broken() { 349 assert_format_rewrite!( 350 "const list = 351 [ 352 1, 353 2, 354 355 3, 4, 5 356 ] 357", 358 "const list = [ 359 1, 360 2, 361 362 3, 363 4, 364 5, 365] 366" 367 ); 368} 369 370#[test] 371fn const_list_trailing_comment_indent() { 372 assert_format!( 373 "const list = [ 374 1, 375 2, 376 3, 377 // 4, 378 // 5, 379 // 6, 380] 381" 382 ); 383}