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 / strings.rs
6.9 kB 438 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2021 The Gleam contributors 3 4use crate::assert_js; 5 6#[test] 7fn unicode1() { 8 assert_js!( 9 r#" 10pub fn emoji() -> String { 11 "\u{1f600}" 12} 13"#, 14 ); 15} 16 17#[test] 18fn unicode2() { 19 assert_js!( 20 r#" 21pub fn y_with_dieresis() -> String { 22 "\u{0308}y" 23} 24"#, 25 ); 26} 27 28#[test] 29fn ascii_as_unicode_escape_sequence() { 30 assert_js!( 31 r#" 32pub fn y() -> String { 33 "\u{79}" 34} 35"#, 36 ) 37} 38 39#[test] 40fn unicode_escape_sequence_6_digits() { 41 assert_js!( 42 r#" 43pub fn unicode_escape_sequence_6_digits() -> String { 44 "\u{10abcd}" 45} 46"#, 47 ); 48} 49 50#[test] 51fn string_literals() { 52 assert_js!( 53 r#" 54pub fn go() { 55 "Hello, Gleam!" 56} 57"#, 58 ); 59} 60 61#[test] 62fn string_patterns() { 63 assert_js!( 64 r#" 65pub fn go(x) { 66 let assert "Hello" = x 67} 68"#, 69 ); 70} 71 72#[test] 73fn equality() { 74 assert_js!( 75 r#" 76pub fn go(a) { 77 a == "ok" 78 a != "ok" 79 a == a 80} 81"#, 82 ); 83} 84 85#[test] 86fn case() { 87 assert_js!( 88 r#" 89pub fn go(a) { 90 case a { 91 "" -> 0 92 "one" -> 1 93 "two" -> 2 94 _ -> 3 95 } 96} 97"#, 98 ); 99} 100 101#[test] 102fn string_concat() { 103 assert_js!( 104 r#" 105pub fn go() { 106 "Hello, " <> "Joe" 107} 108"#, 109 ); 110} 111 112#[test] 113fn string_prefix() { 114 assert_js!( 115 r#" 116pub fn go(x) { 117 case x { 118 "Hello, " <> name -> name 119 _ -> "Unknown" 120 } 121} 122"#, 123 ); 124} 125 126#[test] 127fn string_prefix_utf16() { 128 assert_js!( 129 r#" 130pub fn go(x) { 131 case "Θ wibble wobble" { 132 "Θ" <> rest -> rest 133 _ -> "" 134 } 135 case "🫥 is neutral dotted" { 136 "🫥" <> rest -> rest 137 _ -> "" 138 } 139 case "🇺🇸 is a cluster" { 140 "🇺🇸" <> rest -> rest 141 _ -> "" 142 } 143 case "\" is a an escaped quote" { 144 "\"" <> rest -> rest 145 _ -> "" 146 } 147 case "\\ is a an escaped backslash" { 148 "\\" <> rest -> rest 149 _ -> "" 150 } 151} 152"#, 153 ); 154} 155 156#[test] 157fn discard_concat_rest_pattern() { 158 // We can discard the right hand side, it parses and type checks ok 159 assert_js!( 160 r#" 161pub fn go(x) { 162 case x { 163 "Hello, " <> _ -> Nil 164 _ -> Nil 165 } 166} 167"#, 168 ); 169} 170 171#[test] 172fn string_prefix_assignment() { 173 assert_js!( 174 r#" 175pub fn go(x) { 176 case x { 177 "Hello, " as greeting <> name -> greeting 178 _ -> "Unknown" 179 } 180} 181"#, 182 ) 183} 184 185#[test] 186fn string_prefix_assignment_with_utf_escape_sequence() { 187 assert_js!( 188 r#" 189pub fn go(x) { 190 case x { 191 "\u{0032} " as greeting <> name -> greeting 192 "\u{0007ff} " as greeting <> name -> greeting 193 "\u{00ffff} " as greeting <> name -> greeting 194 "\u{10ffff} " as greeting <> name -> greeting 195 _ -> "Unknown" 196 } 197} 198"#, 199 ) 200} 201 202#[test] 203fn string_prefix_shadowing() { 204 assert_js!( 205 r#" 206pub fn go(x) { 207 case x { 208 "Hello, " as x <> name -> x 209 _ -> "Unknown" 210 } 211} 212"#, 213 ) 214} 215 216// https://github.com/gleam-lang/gleam/issues/2471 217#[test] 218fn string_prefix_assignment_with_multiple_subjects() { 219 assert_js!( 220 r#" 221pub fn go(x) { 222 case x { 223 "1" as prefix <> _ | "11" as prefix <> _ -> prefix 224 _ -> "Unknown" 225 } 226} 227"#, 228 ) 229} 230 231#[test] 232fn const_concat() { 233 assert_js!( 234 r#" 235pub const cute = "cute" 236pub const cute_bee = cute <> "bee" 237 238pub fn main() { 239 cute_bee 240} 241"# 242 ); 243} 244 245#[test] 246fn const_concat_multiple() { 247 assert_js!( 248 r#" 249pub const cute = "cute" 250pub const cute_bee = cute <> "bee" 251pub const cute_cute_bee_buzz = cute <> cute_bee <> "buzz" 252 253pub fn main() { 254 cute_cute_bee_buzz 255} 256"# 257 ); 258} 259 260// https://github.com/gleam-lang/gleam/issues/5856 261#[test] 262fn overlapping_string_prefixes_with_guard() { 263 assert_js!( 264 r#" 265pub fn classify(input: String, flag: Bool) -> String { 266 case input { 267 "aa" <> _rest if flag -> "wibble" 268 "a" <> _rest if flag -> "wobble" 269 "a" <> _rest -> "woo" 270 _ -> "other" 271 } 272} 273"#, 274 ); 275} 276 277// https://github.com/gleam-lang/gleam/issues/5856 278#[test] 279fn string_prefix_binding_rest_with_guard() { 280 assert_js!( 281 r#" 282pub fn classify(input: String, flag: Bool) -> String { 283 case input { 284 "aa" <> rest if flag -> rest 285 "a" <> rest -> rest 286 _ -> "other" 287 } 288} 289"#, 290 ); 291} 292 293// https://github.com/gleam-lang/gleam/issues/5856 294#[test] 295fn non_overlapping_sibling_string_prefix() { 296 assert_js!( 297 r#" 298pub fn classify(input: String, a: Bool, b: Bool) -> String { 299 case input { 300 "aa" <> _ if a -> "aa" 301 "a" <> _ if b -> "a-guard" 302 "ac" <> _ -> "ac" 303 _ -> "other" 304 } 305} 306"#, 307 ); 308} 309 310// https://github.com/gleam-lang/gleam/issues/5856 311#[test] 312fn exact_string_after_longer_prefix() { 313 assert_js!( 314 r#" 315pub fn classify(input: String, a: Bool, b: Bool) -> String { 316 case input { 317 "aa" <> _ if a -> "aa-prefix" 318 "a" <> _ if b -> "a-prefix" 319 "a" -> "exact-a" 320 _ -> "other" 321 } 322} 323"#, 324 ); 325} 326 327// https://github.com/gleam-lang/gleam/issues/5856 328#[test] 329fn string_prefix_name_binding_after_longer_prefix() { 330 assert_js!( 331 r#" 332pub fn classify(input: String, flag: Bool) -> String { 333 case input { 334 "aa" <> _ if flag -> "aa" 335 "a" as first <> rest -> rest <> first 336 _ -> "other" 337 } 338} 339"#, 340 ); 341} 342 343// https://github.com/gleam-lang/gleam/issues/5856 344#[test] 345fn string_prefix_guard_on_bound_variable_falls_through() { 346 assert_js!( 347 r#" 348pub fn classify(input: String) -> String { 349 case input { 350 "aa" <> rest if rest == "x" -> "aa-x" 351 "a" <> rest -> rest 352 _ -> "other" 353 } 354} 355"#, 356 ); 357} 358 359// https://github.com/gleam-lang/gleam/issues/5856 360#[test] 361fn string_prefix_nested_guard_falls_through_to_shorter() { 362 assert_js!( 363 r#" 364pub fn classify(input: String, flag: Bool) -> String { 365 case input { 366 "w" <> _ if flag -> "w" 367 "wibble" <> rest if rest == "x" -> rest 368 "wib" <> rest -> rest 369 _ -> "other" 370 } 371} 372"#, 373 ); 374} 375 376// https://github.com/gleam-lang/gleam/issues/5856 377#[test] 378fn string_prefix_name_binding_falls_through_to_shorter() { 379 assert_js!( 380 r#" 381pub fn classify(input: String) -> String { 382 case input { 383 "aa" <> rest if rest == "x" -> rest 384 "a" as first <> rest -> rest <> first 385 _ -> "other" 386 } 387} 388"#, 389 ); 390} 391 392// https://github.com/gleam-lang/gleam/issues/5856 393#[test] 394fn string_prefix_discard_falls_through_to_shorter() { 395 assert_js!( 396 r#" 397pub fn classify(input: String) -> String { 398 case input { 399 "aa" <> rest if rest == "x" -> rest 400 "a" <> _ -> "short" 401 _ -> "other" 402 } 403} 404"#, 405 ); 406} 407 408// https://github.com/gleam-lang/gleam/issues/5856 409#[test] 410fn string_prefix_multibyte_falls_through_to_shorter() { 411 assert_js!( 412 r#" 413pub fn classify(input: String) -> String { 414 case input { 415 "🫥a" <> rest if rest == "x" -> rest 416 "🫥" <> rest -> rest 417 _ -> "other" 418 } 419} 420"#, 421 ); 422} 423 424// https://github.com/gleam-lang/gleam/issues/5856 425#[test] 426fn disjoint_string_prefixes_with_guard() { 427 assert_js!( 428 r#" 429pub fn classify(input: String) -> String { 430 case input { 431 "aa" <> rest if rest == "x" -> rest 432 "b" <> _ -> "b-start" 433 _ -> "other" 434 } 435} 436"#, 437 ); 438}