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 / cases.rs
5.2 kB 266 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2024 The Gleam contributors 3 4use crate::{assert_format, assert_format_rewrite}; 5 6#[test] 7fn case_with_two_long_subjects() { 8 assert_format!( 9 r#"pub fn main() { 10 case 11 wibble(one_long_argument, something_else), 12 wobble(another_argument, this_is_long) 13 { 14 _ -> todo 15 } 16} 17"# 18 ); 19} 20 21#[test] 22fn multiple_patterns_get_split_one_on_each_line() { 23 assert_format!( 24 r#"pub fn main() { 25 case wibble, wobble, wubble { 26 Wibble(one_thing, something_else, wibble), 27 Wobble( 28 this_will_go_over_the_line_limit, 29 and_the_arguments_get_broken_as_well, 30 wobble, 31 ), 32 Wubble(this_will_go_over_the_line_limit, wubble) 33 -> todo 34 } 35} 36"# 37 ); 38} 39 40#[test] 41fn multiple_patterns_with_guard_get_split_one_on_each_line() { 42 assert_format!( 43 r#"pub fn main() { 44 case wibble, wobble, wubble { 45 Wibble(one_thing, something_else, wibble), 46 Wobble(this_will_go_over_the_line_limit, wobble), 47 Wubble(this_will_go_over_the_line_limit, wubble) 48 if wibble && wobble || wubble 49 -> todo 50 } 51} 52"# 53 ); 54} 55 56#[test] 57fn multiple_patterns_with_long_guard_get_split_one_on_each_line() { 58 assert_format!( 59 r#"pub fn main() { 60 case wibble, wobble, wubble { 61 Wibble(one_thing, something_else, wibble), 62 Wobble(this_will_go_over_the_line_limit, wobble), 63 Wubble(this_will_go_over_the_line_limit, wubble) 64 if { wibble || wobble } 65 && { wibble || wobble && wibble > 10 } 66 || wobble < 10_000_000 67 -> todo 68 } 69} 70"# 71 ); 72} 73 74#[test] 75fn multiple_patterns_and_alternative_patterns_mixed_together() { 76 assert_format!( 77 r#"pub fn main() { 78 case wibble, wobble, wubble { 79 Wibble(one_thing, something_else, wibble), 80 Wobble(this_will_go_over_the_line_limit, wobble), 81 Wubble(this_will_go_over_the_line_limit, wubble) 82 | Wibble(a), Wobble(b), Wubble(c) 83 | Wibble(one_thing, something_else, wibble), 84 Wobble(this_will_go_over_the_line_limit, wobble), 85 Wubble(this_will_go_over_the_line_limit, wubble) 86 if { wibble || wobble } 87 && { wibble || wobble && wibble > 10 } 88 || wobble < 10_000_000 89 -> todo 90 } 91} 92"# 93 ); 94} 95 96#[test] 97fn case_pattern_split_on_multiple_lines_is_not_needlessly_nested() { 98 assert_format!( 99 r#"pub fn main() { 100 case thing { 101 CannotSaveNewSnapshot( 102 reason: reason, 103 title: title, 104 destination: destination, 105 ) -> todo 106 } 107} 108"# 109 ); 110} 111 112// https://github.com/gleam-lang/gleam/issues/3140 113#[test] 114fn long_comment_before_case_with_multiple_subjects_doesnt_force_a_break() { 115 assert_format!( 116 r#"fn main() { 117 case a, b { 118 // a very long comment a very long comment a very long comment a very long comment 119 _, _ -> True 120 } 121} 122"# 123 ); 124} 125 126#[test] 127fn alternatives_are_not_split_if_not_necessary() { 128 assert_format!( 129 r#"fn main() { 130 case thing { 131 Wibble | Wobble -> { 132 todo 133 todo 134 } 135 } 136} 137"# 138 ); 139} 140 141#[test] 142fn alternatives_are_not_split_if_not_necessary_2() { 143 assert_format!( 144 r#"fn main() { 145 case thing { 146 Wibble | Wobble | Wabble -> 147 loooooooong_function_call_that_barely_goes_over_the_limit() 148 } 149} 150"# 151 ); 152} 153 154#[test] 155fn subjects_are_not_split_if_not_necessary() { 156 assert_format!( 157 r#"fn main() { 158 case 159 is_all_uppercase(remark), 160 string.ends_with(remark, "?"), 161 string.trim(remark) == "" 162 { 163 _, _, _ -> todo 164 } 165} 166"# 167 ); 168} 169 170#[test] 171fn case_in_call_gets_broken_if_it_goes_over_the_limit_with_subject() { 172 assert_format!( 173 r#"fn main() { 174 do_diff_attributes( 175 dict.delete(prev, attr.name), 176 rest, 177 case attr.value == old.value { 178 True -> added 179 False -> [attr, ..added] 180 }, 181 ) 182} 183"# 184 ); 185} 186 187#[test] 188fn case_in_call_is_not_broken_if_it_goes_over_the_limit_with_branches() { 189 assert_format!( 190 r#"fn main() { 191 do_diff_attributes(rest, case attr.value == old.value { 192 True -> added 193 False -> [attr, ..added] 194 }) 195} 196"# 197 ); 198} 199 200#[test] 201fn long_alternative_patterns_1() { 202 assert_format!( 203 r#"fn test_1() { 204 case a, b, c { 205 _ignored, _ignored, _ignored 206 | _ignored, _ignored, _ignored 207 | _ignored, _ignored, _ignored 208 -> True 209 } 210} 211"# 212 ); 213} 214 215#[test] 216fn long_alternative_patterns_2() { 217 assert_format!( 218 r#"fn test_2() { 219 case a, b, c { 220 _, _, _ 221 | _, _, _ 222 | this_is_long_and_is_going_to_split_on_multiple, 223 lines, 224 and_force_the_arrow_to_break 225 -> True 226 } 227} 228"# 229 ); 230} 231 232#[test] 233fn long_alternative_patterns_3() { 234 assert_format!( 235 r#"fn test_3() { 236 case a, b, c { 237 _, _, _ 238 | this_is_long_and_is_going_to_split_on_multiple, 239 lines, 240 and_force_the_alternative_to_break 241 | _, _, _ 242 -> True 243 } 244} 245"# 246 ); 247} 248 249#[test] 250fn case_arrow_slightly_over_the_limit_gets_broken() { 251 assert_format_rewrite!( 252 r#"pub fn main() { 253 case box { 254 StackBox(width:, height:, children_width:, children_height:, child:, ..) -> todo 255 } 256} 257"#, 258 r#"pub fn main() { 259 case box { 260 StackBox(width:, height:, children_width:, children_height:, child:, ..) -> 261 todo 262 } 263} 264"# 265 ); 266}