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

Configure Feed

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

gleam / compiler-core / src / javascript / tests / assignments.rs
4.8 kB 372 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2021 The Gleam contributors 3 4use crate::{assert_js, assert_ts_def}; 5 6#[test] 7fn tuple_matching() { 8 assert_js!( 9 r#" 10pub fn go(x) { 11 let assert #(1, 2) = x 12} 13"#, 14 ) 15} 16 17#[test] 18fn assert() { 19 assert_js!(r#"pub fn go(x) { let assert 1 = x }"#,); 20} 21 22#[test] 23fn assert1() { 24 assert_js!(r#"pub fn go(x) { let assert #(1, 2) = x }"#,); 25} 26 27#[test] 28fn nested_binding() { 29 assert_js!( 30 r#" 31pub fn go(x) { 32 let assert #(a, #(b, c, 2) as t, _, 1) = x 33} 34"#, 35 ) 36} 37 38#[test] 39fn variable_renaming() { 40 assert_js!( 41 r#" 42 43pub fn go(x, wibble) { 44 let a = 1 45 wibble(a) 46 let a = 2 47 wibble(a) 48 let assert #(a, 3) = x 49 let b = a 50 wibble(b) 51 let c = { 52 let a = a 53 #(a, b) 54 } 55 wibble(a) 56 // make sure arguments are counted in initial state 57 let x = c 58 x 59} 60"#, 61 ) 62} 63 64#[test] 65fn constant_assignments() { 66 assert_js!( 67 r#" 68const a = True 69 70pub fn go() { 71 a 72 let a = 10 73 a + 20 74} 75 76fn second() { 77 let a = 10 78 a + 20 79} 80"#, 81 ); 82} 83 84#[test] 85fn returning_literal_subject() { 86 assert_js!(r#"pub fn go(x) { let assert 1 = x + 1 }"#,); 87} 88 89#[test] 90fn rebound_argument() { 91 assert_js!( 92 r#"pub fn main(x) { 93 let x = False 94 x 95} 96"#, 97 ); 98} 99 100#[test] 101fn rebound_function() { 102 assert_js!( 103 r#"pub fn x() { 104 Nil 105} 106 107pub fn main() { 108 let x = False 109 x 110} 111"#, 112 ); 113} 114 115#[test] 116fn rebound_function_and_arg() { 117 assert_js!( 118 r#"pub fn x() { 119 Nil 120} 121 122pub fn main(x) { 123 let x = False 124 x 125} 126"#, 127 ); 128} 129 130#[test] 131fn variable_used_in_pattern_and_assignment() { 132 assert_js!( 133 r#"pub fn main(x) { 134 let #(x) = #(x) 135 x 136} 137"#, 138 ); 139} 140 141// https://github.com/gleam-lang/gleam/issues/1253 142#[test] 143fn correct_variable_renaming_in_assigned_functions() { 144 assert_js!( 145 r#" 146pub fn debug(x) { 147 let x = x 148 fn(x) { x + 1 } 149} 150"#, 151 ); 152} 153 154#[test] 155fn module_const_var() { 156 assert_js!( 157 r#" 158pub const int = 42 159pub const int_alias = int 160pub fn use_int_alias() { int_alias } 161 162pub const compound: #(Int, Int) = #(int, int_alias) 163pub fn use_compound() { compound.0 + compound.1 } 164"# 165 ); 166} 167 168#[test] 169fn module_const_var1() { 170 assert_ts_def!( 171 r#" 172pub const int = 42 173pub const int_alias = int 174pub const compound: #(Int, Int) = #(int, int_alias) 175"# 176 ); 177} 178 179// https://github.com/gleam-lang/gleam/issues/2443 180#[test] 181fn let_assert_string_prefix() { 182 assert_js!( 183 r#" 184pub fn main() { 185 let assert "Game " <> id = "Game 1" 186} 187"# 188 ); 189} 190 191// https://github.com/gleam-lang/gleam/issues/3894 192#[test] 193fn let_assert_nested_string_prefix() { 194 assert_js!( 195 r#" 196type Wibble { 197 Wibble(wibble: String) 198} 199 200pub fn main() { 201 let assert Wibble(wibble: "w" as prefix <> rest) = Wibble("wibble") 202 prefix <> rest 203} 204"# 205 ); 206} 207 208// https://github.com/gleam-lang/gleam/issues/2931 209#[test] 210fn keyword_assignment() { 211 assert_js!( 212 r#" 213pub fn main() { 214 let class = 10 215 let debugger = 50 216} 217"# 218 ); 219} 220 221// https://github.com/gleam-lang/gleam/issues/3004 222#[test] 223fn escaped_variables_in_constants() { 224 assert_js!( 225 r#" 226pub const class = 5 227pub const something = class 228"# 229 ); 230} 231 232#[test] 233fn message() { 234 assert_js!( 235 r#" 236pub fn unwrap_or_panic(value) { 237 let assert Ok(inner) = value as "Oops, there was an error" 238 inner 239} 240"# 241 ); 242} 243 244#[test] 245fn variable_message() { 246 assert_js!( 247 r#" 248pub fn expect(value, message) { 249 let assert Ok(inner) = value as message 250 inner 251} 252"# 253 ); 254} 255 256// https://github.com/gleam-lang/gleam/issues/4471 257#[test] 258fn case_message() { 259 assert_js!( 260 r#" 261pub fn expect(value, message) { 262 let assert Ok(inner) = value as case message { 263 Ok(message) -> message 264 Error(_) -> "No message provided" 265 } 266 inner 267} 268"# 269 ); 270} 271 272#[test] 273fn assert_that_always_succeeds() { 274 assert_js!( 275 r#" 276type Wibble { 277 Wibble(Int) 278} 279 280pub fn go() { 281 let assert Wibble(n) = Wibble(1) 282 n 283} 284"#, 285 ); 286} 287 288#[test] 289fn assert_that_always_fails() { 290 assert_js!( 291 r#" 292type Wibble { 293 Wibble(Int) 294 Wobble(Int) 295} 296 297pub fn go() { 298 let assert Wobble(n) = Wibble(1) 299 n 300} 301"#, 302 ); 303} 304 305#[test] 306fn catch_all_assert() { 307 assert_js!( 308 r#" 309type Wibble { 310 Wibble(Int) 311 Wobble(Int) 312} 313 314pub fn go() { 315 let assert _ = Wibble(1) 316 1 317} 318"#, 319 ); 320} 321 322#[test] 323fn assert_with_multiple_variants() { 324 assert_js!( 325 r#" 326type Wibble { 327 Wibble(Int) 328 Wobble(Int) 329 Woo(Int) 330} 331 332pub fn go() { 333 let assert Wobble(n) = todo 334 n 335} 336"#, 337 ); 338} 339 340#[test] 341fn use_discard_assignment() { 342 assert_js!( 343 r#" 344type Wibble { 345 Wibble(Int) 346 Wobble(Int) 347 Woo(Int) 348} 349 350fn fun(f) { f(Wibble(1)) } 351 352pub fn go() { 353 use _ <- fun 354 1 355} 356"#, 357 ); 358} 359 360#[test] 361fn use_matching_assignment() { 362 assert_js!( 363 r#" 364fn fun(f) { f(#(2, 4)) } 365 366pub fn go() { 367 use #(_, n) <- fun 368 n 369} 370"#, 371 ); 372}