Skip to content

Commit 4342dcb

Browse files
GearsDatapackslpil
authored andcommitted
Add tests
1 parent 9e7ec91 commit 4342dcb

15 files changed

+566
-2
lines changed

compiler-core/src/javascript.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ impl<'a> Generator<'a> {
206206
}
207207

208208
if self.tracker.codepoint_utf16_bit_array_segment_used {
209-
self.register_prelude_usage(&mut imports, "stringToUtf16", None);
209+
self.register_prelude_usage(&mut imports, "codepointToUtf16", None);
210210
}
211211

212212
if self.tracker.codepoint_utf32_bit_array_segment_used {
213-
self.register_prelude_usage(&mut imports, "stringToUtf32", None);
213+
self.register_prelude_usage(&mut imports, "codepointToUtf32", None);
214214
}
215215

216216
if self.tracker.float_bit_array_segment_used {

compiler-core/src/javascript/tests/bit_arrays.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,3 +2097,137 @@ pub fn go(x) {
20972097
"#
20982098
);
20992099
}
2100+
2101+
#[test]
2102+
fn utf16() {
2103+
assert_js!(
2104+
r#"
2105+
pub fn main() {
2106+
<<"Hello, world!":utf16>>
2107+
}
2108+
"#
2109+
);
2110+
}
2111+
2112+
#[test]
2113+
fn utf16_codepoint() {
2114+
assert_js!(
2115+
r#"
2116+
fn codepoint() -> UtfCodepoint { todo }
2117+
2118+
pub fn main() {
2119+
let my_codepoint = codepoint()
2120+
<<my_codepoint:utf16_codepoint>>
2121+
}
2122+
"#
2123+
);
2124+
}
2125+
2126+
#[test]
2127+
fn utf32() {
2128+
assert_js!(
2129+
r#"
2130+
pub fn main() {
2131+
<<"Hello, world!":utf32>>
2132+
}
2133+
"#
2134+
);
2135+
}
2136+
2137+
#[test]
2138+
fn utf32_codepoint() {
2139+
assert_js!(
2140+
r#"
2141+
fn codepoint() -> UtfCodepoint { todo }
2142+
2143+
pub fn main() {
2144+
let my_codepoint = codepoint()
2145+
<<my_codepoint:utf32_codepoint>>
2146+
}
2147+
"#
2148+
);
2149+
}
2150+
2151+
#[test]
2152+
fn const_utf16() {
2153+
assert_js!(
2154+
r#"
2155+
pub const message = <<"Hello, world!":utf16>>
2156+
"#
2157+
);
2158+
}
2159+
2160+
#[test]
2161+
fn const_utf32() {
2162+
assert_js!(
2163+
r#"
2164+
pub const message = <<"Hello, world!":utf32>>
2165+
"#
2166+
);
2167+
}
2168+
2169+
#[test]
2170+
fn pattern_match_utf16() {
2171+
assert_js!(
2172+
r#"
2173+
pub fn go(x) {
2174+
let assert <<"Hello":utf16, _rest:bytes>> = x
2175+
}
2176+
"#
2177+
);
2178+
}
2179+
2180+
#[test]
2181+
fn pattern_match_utf32() {
2182+
assert_js!(
2183+
r#"
2184+
pub fn go(x) {
2185+
let assert <<"Hello":utf32, _rest:bytes>> = x
2186+
}
2187+
"#
2188+
);
2189+
}
2190+
2191+
#[test]
2192+
fn utf16_little_endian() {
2193+
assert_js!(
2194+
r#"
2195+
pub fn main() {
2196+
<<"Hello, world!":utf16-little>>
2197+
}
2198+
"#
2199+
);
2200+
}
2201+
2202+
#[test]
2203+
fn utf32_little_endian() {
2204+
assert_js!(
2205+
r#"
2206+
pub fn main() {
2207+
<<"Hello, world!":utf32-little>>
2208+
}
2209+
"#
2210+
);
2211+
}
2212+
2213+
#[test]
2214+
fn pattern_match_utf16_little_endian() {
2215+
assert_js!(
2216+
r#"
2217+
pub fn go(x) {
2218+
let assert <<"Hello":utf16-little, _rest:bytes>> = x
2219+
}
2220+
"#
2221+
);
2222+
}
2223+
2224+
#[test]
2225+
fn pattern_match_utf32_little_endian() {
2226+
assert_js!(
2227+
r#"
2228+
pub fn go(x) {
2229+
let assert <<"Hello":utf32-little, _rest:bytes>> = x
2230+
}
2231+
"#
2232+
);
2233+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/javascript/tests/bit_arrays.rs
3+
expression: "\npub const message = <<\"Hello, world!\":utf16>>\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub const message = <<"Hello, world!":utf16>>
8+
9+
10+
----- COMPILED JAVASCRIPT
11+
import { toBitArray, stringToUtf16 } from "../gleam.mjs";
12+
13+
export const message = /* @__PURE__ */ toBitArray([
14+
stringToUtf16("Hello, world!", true),
15+
]);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/javascript/tests/bit_arrays.rs
3+
expression: "\npub const message = <<\"Hello, world!\":utf32>>\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub const message = <<"Hello, world!":utf32>>
8+
9+
10+
----- COMPILED JAVASCRIPT
11+
import { toBitArray, stringToUtf32 } from "../gleam.mjs";
12+
13+
export const message = /* @__PURE__ */ toBitArray([
14+
stringToUtf32("Hello, world!", true),
15+
]);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
source: compiler-core/src/javascript/tests/bit_arrays.rs
3+
expression: "\npub fn go(x) {\n let assert <<\"Hello\":utf16, _rest:bytes>> = x\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn go(x) {
8+
let assert <<"Hello":utf16, _rest:bytes>> = x
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
import { makeError } from "../gleam.mjs";
14+
15+
export function go(x) {
16+
if (
17+
x.bitSize < 80 ||
18+
x.byteAt(0) !== 0 ||
19+
x.byteAt(1) !== 72 ||
20+
x.byteAt(2) !== 0 ||
21+
x.byteAt(3) !== 101 ||
22+
x.byteAt(4) !== 0 ||
23+
x.byteAt(5) !== 108 ||
24+
x.byteAt(6) !== 0 ||
25+
x.byteAt(7) !== 108 ||
26+
x.byteAt(8) !== 0 ||
27+
x.byteAt(9) !== 111 ||
28+
(x.bitSize - 80) % 8 !== 0
29+
) {
30+
throw makeError(
31+
"let_assert",
32+
"my/mod",
33+
3,
34+
"go",
35+
"Pattern match failed, no pattern matched the value.",
36+
{ value: x }
37+
)
38+
}
39+
return x;
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
source: compiler-core/src/javascript/tests/bit_arrays.rs
3+
expression: "\npub fn go(x) {\n let assert <<\"Hello\":utf16-little, _rest:bytes>> = x\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn go(x) {
8+
let assert <<"Hello":utf16-little, _rest:bytes>> = x
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
import { makeError } from "../gleam.mjs";
14+
15+
export function go(x) {
16+
if (
17+
x.bitSize < 80 ||
18+
x.byteAt(0) !== 72 ||
19+
x.byteAt(1) !== 0 ||
20+
x.byteAt(2) !== 101 ||
21+
x.byteAt(3) !== 0 ||
22+
x.byteAt(4) !== 108 ||
23+
x.byteAt(5) !== 0 ||
24+
x.byteAt(6) !== 108 ||
25+
x.byteAt(7) !== 0 ||
26+
x.byteAt(8) !== 111 ||
27+
x.byteAt(9) !== 0 ||
28+
(x.bitSize - 80) % 8 !== 0
29+
) {
30+
throw makeError(
31+
"let_assert",
32+
"my/mod",
33+
3,
34+
"go",
35+
"Pattern match failed, no pattern matched the value.",
36+
{ value: x }
37+
)
38+
}
39+
return x;
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
source: compiler-core/src/javascript/tests/bit_arrays.rs
3+
expression: "\npub fn go(x) {\n let assert <<\"Hello\":utf32, _rest:bytes>> = x\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn go(x) {
8+
let assert <<"Hello":utf32, _rest:bytes>> = x
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
import { makeError } from "../gleam.mjs";
14+
15+
export function go(x) {
16+
if (
17+
x.bitSize < 160 ||
18+
x.byteAt(0) !== 0 ||
19+
x.byteAt(1) !== 0 ||
20+
x.byteAt(2) !== 0 ||
21+
x.byteAt(3) !== 72 ||
22+
x.byteAt(4) !== 0 ||
23+
x.byteAt(5) !== 0 ||
24+
x.byteAt(6) !== 0 ||
25+
x.byteAt(7) !== 101 ||
26+
x.byteAt(8) !== 0 ||
27+
x.byteAt(9) !== 0 ||
28+
x.byteAt(10) !== 0 ||
29+
x.byteAt(11) !== 108 ||
30+
x.byteAt(12) !== 0 ||
31+
x.byteAt(13) !== 0 ||
32+
x.byteAt(14) !== 0 ||
33+
x.byteAt(15) !== 108 ||
34+
x.byteAt(16) !== 0 ||
35+
x.byteAt(17) !== 0 ||
36+
x.byteAt(18) !== 0 ||
37+
x.byteAt(19) !== 111 ||
38+
(x.bitSize - 160) % 8 !== 0
39+
) {
40+
throw makeError(
41+
"let_assert",
42+
"my/mod",
43+
3,
44+
"go",
45+
"Pattern match failed, no pattern matched the value.",
46+
{ value: x }
47+
)
48+
}
49+
return x;
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
source: compiler-core/src/javascript/tests/bit_arrays.rs
3+
expression: "\npub fn go(x) {\n let assert <<\"Hello\":utf32-little, _rest:bytes>> = x\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn go(x) {
8+
let assert <<"Hello":utf32-little, _rest:bytes>> = x
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
import { makeError } from "../gleam.mjs";
14+
15+
export function go(x) {
16+
if (
17+
x.bitSize < 160 ||
18+
x.byteAt(0) !== 72 ||
19+
x.byteAt(1) !== 0 ||
20+
x.byteAt(2) !== 0 ||
21+
x.byteAt(3) !== 0 ||
22+
x.byteAt(4) !== 101 ||
23+
x.byteAt(5) !== 0 ||
24+
x.byteAt(6) !== 0 ||
25+
x.byteAt(7) !== 0 ||
26+
x.byteAt(8) !== 108 ||
27+
x.byteAt(9) !== 0 ||
28+
x.byteAt(10) !== 0 ||
29+
x.byteAt(11) !== 0 ||
30+
x.byteAt(12) !== 108 ||
31+
x.byteAt(13) !== 0 ||
32+
x.byteAt(14) !== 0 ||
33+
x.byteAt(15) !== 0 ||
34+
x.byteAt(16) !== 111 ||
35+
x.byteAt(17) !== 0 ||
36+
x.byteAt(18) !== 0 ||
37+
x.byteAt(19) !== 0 ||
38+
(x.bitSize - 160) % 8 !== 0
39+
) {
40+
throw makeError(
41+
"let_assert",
42+
"my/mod",
43+
3,
44+
"go",
45+
"Pattern match failed, no pattern matched the value.",
46+
{ value: x }
47+
)
48+
}
49+
return x;
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: compiler-core/src/javascript/tests/bit_arrays.rs
3+
expression: "\npub fn main() {\n <<\"Hello, world!\":utf16>>\n}\n"
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
<<"Hello, world!":utf16>>
9+
}
10+
11+
12+
----- COMPILED JAVASCRIPT
13+
import { toBitArray, stringToUtf16 } from "../gleam.mjs";
14+
15+
export function main() {
16+
return toBitArray([stringToUtf16("Hello, world!", true)]);
17+
}

0 commit comments

Comments
 (0)