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