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
3.4 kB 258 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}