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 / binary_operators.rs
5.3 kB 278 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2023 The Gleam contributors 3 4use crate::assert_format; 5 6// https://github.com/gleam-lang/gleam/issues/835 7#[test] 8pub fn long_binary_operation_sequence() { 9 assert_format!( 10 r#"pub fn main() { 11 int.to_string(color.red) 12 <> ", " 13 <> int.to_string(color.green) 14 <> ", " 15 <> int.to_string(color.blue) 16 <> ", " 17 <> float.to_string(color.alpha) 18} 19"# 20 ); 21} 22 23#[test] 24pub fn long_comparison_chain() { 25 assert_format!( 26 r#"pub fn main() { 27 trying_a_comparison(this, is, a, function) > with_ints 28 && trying_other_comparisons < with_ints 29 || trying_other_comparisons <= with_ints 30 && trying_other_comparisons >= with_ints 31 || and_now_an_equality_check == with_a_function(wibble, wobble) 32 && trying_other_comparisons >. with_floats 33 || trying_other_comparisons <. with_floats(wobble) 34 && trying_other_comparisons <=. with_floats 35 || trying_other_comparisons(wibble, wobble) >=. with_floats 36 && wibble <> wobble 37} 38"# 39 ); 40} 41 42#[test] 43pub fn long_chain_mixing_operators() { 44 assert_format!( 45 r#"pub fn main() { 46 variable + variable - variable * variable / variable 47 == variable * variable / variable - variable + variable 48 || wibble * wobble > 11 49} 50"# 51 ); 52 53 assert_format!( 54 r#"pub fn main() { 55 variable +. variable -. variable *. variable /. variable 56 == variable *. variable /. variable -. variable +. variable 57 || wibble *. wobble >=. 11 58} 59"# 60 ); 61} 62 63// Thanks Hayleigh for pointing this out! 64#[test] 65fn case_branch_is_not_broken_if_can_fit_on_line() { 66 assert_format!( 67 r#"pub fn main() { 68 case remainder { 69 _ if remainder >=. 0.5 && x >=. 0.0 -> 70 float_sign(x) *. truncate_float(xabs +. 1.0) /. p 71 _ -> float_sign(x) *. xabs_truncated /. p 72 } 73} 74"# 75 ); 76} 77 78// https://discord.com/channels/768594524158427167/1187508793945378847/1187508793945378847 79#[test] 80fn binary_operation_in_assignment_that_is_almost_80_chars() { 81 assert_format!( 82 r#"pub fn main() { 83 let is_vr_implicit = 84 dicom_read_context.transfer_syntax == transfer_syntax.ImplicitVrLittleEndian 85} 86"# 87 ); 88} 89 90// https://github.com/gleam-lang/gleam/issues/2480 91#[test] 92fn labelled_field_with_binary_operators_are_not_broken_if_they_can_fit() { 93 assert_format!( 94 r#"pub fn main() { 95 Ok(Lesson( 96 name: names.name, 97 text: text, 98 code: code, 99 path: chapter_path <> "/", 100 previous: None, 101 next: None, 102 )) 103} 104"# 105 ); 106 107 assert_format!( 108 r#"pub fn main() { 109 Ok(Lesson( 110 name: names.name, 111 text:, 112 code:, 113 path: chapter_path 114 <> "/" 115 <> this_one_doesnt_fit 116 <> "and ends up on multiple lines", 117 previous: None, 118 next: None, 119 )) 120} 121"# 122 ); 123 124 assert_format!( 125 r#"pub fn main() { 126 Ok(wibble( 127 name: names.name, 128 text:, 129 code:, 130 path: chapter_path <> "/", 131 previous: None, 132 next: None, 133 )) 134} 135"# 136 ); 137 138 assert_format!( 139 r#"pub fn main() { 140 Ok(wibble( 141 name: names.name, 142 text:, 143 code:, 144 path: chapter_path 145 <> "/" 146 <> this_one_doesnt_fit 147 <> "and ends up on multiple lines", 148 previous: None, 149 next: None, 150 )) 151} 152"# 153 ); 154} 155 156#[test] 157fn math_binops_kept_on_a_single_line_in_pipes() { 158 assert_format!( 159 r#"pub fn main() { 160 1 + 2 * 3 / 4 - 5 161 |> wibble 162 |> wobble 163} 164"# 165 ); 166 167 assert_format!( 168 r#"pub fn main() { 169 1 +. 2 *. 3 /. 4 -. 5 170 |> wibble 171 |> wobble 172} 173"# 174 ); 175} 176 177#[test] 178fn binop_used_as_function_arguments_gets_nested() { 179 assert_format!( 180 r#"pub fn main() { 181 wibble( 182 a_variable_with_a_long_name 183 <> another_variable_with_a_long_name 184 <> yet_another_variable_with_a_long_name, 185 wobble, 186 ) 187} 188"# 189 ); 190} 191 192#[test] 193fn binop_is_not_nested_if_the_only_argument() { 194 assert_format!( 195 r#"pub fn main() { 196 wibble( 197 a_variable_with_a_long_name 198 <> another_variable_with_a_long_name 199 <> yet_another_variable_with_a_long_name, 200 ) 201} 202"# 203 ); 204} 205 206#[test] 207fn binop_inside_list_gets_nested() { 208 assert_format!( 209 r#"pub fn main() { 210 [ 211 wibble, 212 a_variable_with_a_long_name 213 <> another_variable_with_a_long_name 214 <> yet_another_variable_with_a_long_name, 215 ] 216} 217"# 218 ); 219} 220 221#[test] 222fn binop_inside_list_is_not_nested_if_only_item() { 223 assert_format!( 224 r#"pub fn main() { 225 [ 226 a_variable_with_a_long_name 227 <> another_variable_with_a_long_name 228 <> yet_another_variable_with_a_long_name, 229 ] 230} 231"# 232 ); 233} 234 235#[test] 236fn binop_inside_tuple_gets_nested() { 237 assert_format!( 238 r#"pub fn main() { 239 #( 240 wibble, 241 a_variable_with_a_long_name 242 <> another_variable_with_a_long_name 243 <> yet_another_variable_with_a_long_name, 244 ) 245} 246"# 247 ); 248} 249 250#[test] 251fn binop_inside_tuple_is_not_nested_if_only_item() { 252 assert_format!( 253 r#"pub fn main() { 254 #( 255 a_variable_with_a_long_name 256 <> another_variable_with_a_long_name 257 <> yet_another_variable_with_a_long_name, 258 ) 259} 260"# 261 ); 262} 263 264// https://github.com/gleam-lang/gleam/issues/2624 265#[test] 266fn binop_as_argument_in_variant_with_spread_gets_nested() { 267 assert_format!( 268 r#"pub fn main() { 269 Wibble( 270 ..wibble, 271 label: string 272 <> "a long string that is making things go on multiple lines" 273 <> "another string", 274 ) 275} 276"# 277 ); 278}