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 / assert.rs
3.3 kB 271 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2025 The Gleam contributors 3 4use crate::assert_js; 5 6#[test] 7fn assert_variable() { 8 assert_js!( 9 " 10pub fn main() { 11 let x = True 12 assert x 13} 14" 15 ); 16} 17 18#[test] 19fn assert_literal() { 20 assert_js!( 21 " 22pub fn main() { 23 assert False 24} 25" 26 ); 27} 28 29#[test] 30fn assert_binary_operation() { 31 assert_js!( 32 " 33pub fn main() { 34 let x = True 35 assert x || False 36} 37" 38 ); 39} 40 41#[test] 42fn assert_binary_operation2() { 43 assert_js!( 44 " 45pub fn eq(a, b) { 46 assert a == b 47} 48" 49 ); 50} 51 52#[test] 53fn assert_binary_operation3() { 54 assert_js!( 55 " 56pub fn assert_answer(x) { 57 assert x == 42 58} 59" 60 ); 61} 62 63#[test] 64fn assert_function_call() { 65 assert_js!( 66 " 67fn bool() { 68 True 69} 70 71pub fn main() { 72 assert bool() 73} 74" 75 ); 76} 77 78#[test] 79fn assert_function_call2() { 80 assert_js!( 81 " 82fn and(a, b) { 83 a && b 84} 85 86pub fn go(x) { 87 assert and(True, x) 88} 89" 90 ); 91} 92 93#[test] 94fn assert_nested_function_call() { 95 assert_js!( 96 " 97fn and(x, y) { 98 x && y 99} 100 101pub fn main() { 102 assert and(and(True, False), True) 103} 104" 105 ); 106} 107 108#[test] 109fn assert_binary_operator_with_side_effects() { 110 assert_js!( 111 " 112fn wibble(a, b) { 113 let result = a + b 114 result == 10 115} 116 117pub fn go(x) { 118 assert True && wibble(1, 4) 119} 120" 121 ); 122} 123 124#[test] 125fn assert_binary_operator_with_side_effects2() { 126 assert_js!( 127 " 128fn wibble(a, b) { 129 let result = a + b 130 result == 10 131} 132 133pub fn go(x) { 134 assert wibble(5, 5) && wibble(4, 6) 135} 136" 137 ); 138} 139 140#[test] 141fn assert_with_message() { 142 assert_js!( 143 r#" 144pub fn main() { 145 assert True as "This shouldn't fail" 146} 147"# 148 ); 149} 150 151#[test] 152fn assert_with_block_message() { 153 assert_js!( 154 r#" 155fn identity(a) { 156 a 157} 158 159pub fn main() { 160 assert identity(True) as { 161 let message = identity("This shouldn't fail") 162 message 163 } 164} 165"# 166 ); 167} 168 169#[test] 170fn assert_nil_always_throws() { 171 assert_js!( 172 r#" 173pub fn go(x: Nil) { 174 let assert Nil = x 175} 176"# 177 ); 178} 179 180// https://github.com/gleam-lang/gleam/issues/4643 181#[test] 182fn assert_with_pipe_on_right() { 183 assert_js!( 184 " 185fn add(a, b) { a + b } 186 187pub fn main() { 188 assert 3 == 1 |> add(2) 189} 190" 191 ); 192} 193 194#[test] 195fn prova() { 196 assert_js!( 197 " 198pub fn main() { 199 assert Ok([]) == Ok([] |> id) 200} 201 202fn id(x) { x } 203" 204 ) 205} 206 207// https://github.com/gleam-lang/gleam/issues/5251 208#[test] 209fn assert_with_logical_and_binary_rhs_1() { 210 assert_js!( 211 " 212pub fn main() { 213 assert True && 3 < 4 214} 215" 216 ); 217} 218 219// https://github.com/gleam-lang/gleam/issues/5251 220#[test] 221fn assert_with_logical_and_binary_rhs_2() { 222 assert_js!( 223 " 224pub fn main() { 225 assert True && \"wibble\" == \"wibble\" 226} 227" 228 ); 229} 230 231// https://github.com/gleam-lang/gleam/issues/5251 232#[test] 233fn assert_with_logical_and_binary_rhs_3() { 234 assert_js!( 235 " 236pub fn main() { 237 assert True && \"wobble\" != \"wobble\" 238} 239" 240 ); 241} 242 243// https://github.com/gleam-lang/gleam/issues/5251 244#[test] 245fn assert_with_case_rhs() { 246 assert_js!( 247 " 248pub fn main() { 249 assert True && case 1 > 2 { 250 True -> True 251 False -> False 252 } 253} 254" 255 ); 256} 257 258// https://github.com/gleam-lang/gleam/issues/5251 259#[test] 260fn assert_with_negated_case_rhs() { 261 assert_js!( 262 " 263pub fn main() { 264 assert True && !case 3 - 2 { 265 1 -> True 266 _ -> False 267 } 268} 269" 270 ); 271}