Fork of daniellemaywood.uk/gleam — Wasm codegen work
2.2 kB
166 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2021 The Gleam contributors
3
4use crate::{assert_js, assert_ts_def};
5
6#[test]
7fn list_literals() {
8 assert_js!(
9 r#"
10pub fn go(x) {
11 []
12 [1]
13 [1, 2]
14 [1, 2, ..x]
15}
16"#,
17 );
18}
19
20#[test]
21fn long_list_literals() {
22 assert_js!(
23 r#"
24pub fn go() {
25 [111111111111111111111111111111111111111111111111111111111111111111111111]
26 [11111111111111111111111111111111111111111111, 1111111111111111111111111111111111111111111]
27}
28"#,
29 );
30}
31
32#[test]
33fn multi_line_list_literals() {
34 assert_js!(
35 r#"
36pub fn go(x) {
37 [{True 1}]
38}
39"#,
40 );
41}
42
43#[test]
44fn list_constants() {
45 assert_js!(
46 r#"
47pub const a = []
48pub const b = [1, 2, 3]
49"#,
50 );
51}
52
53#[test]
54fn list_constants_typescript() {
55 assert_ts_def!(
56 r#"
57pub const a = []
58pub const b = [1, 2, 3]
59"#,
60 );
61}
62
63#[test]
64fn list_destructuring() {
65 assert_js!(
66 r#"
67pub fn go(x, y) {
68 let assert [] = x
69 let assert [a] = x
70 let assert [1, 2] = x
71 let assert [_, #(3, b)] = y
72 let assert [head, ..tail] = y
73}
74"#,
75 );
76}
77
78#[test]
79fn equality() {
80 assert_js!(
81 r#"
82pub fn go() {
83 [] == [1]
84 [] != [1]
85}
86"#,
87 );
88}
89
90#[test]
91fn case() {
92 assert_js!(
93 r#"
94pub fn go(xs) {
95 case xs {
96 [] -> 0
97 [_] -> 1
98 [_, _] -> 2
99 _ -> 9999
100 }
101}
102"#,
103 );
104}
105
106// https://github.com/gleam-lang/gleam/issues/2904
107#[test]
108fn tight_empty_list() {
109 assert_js!(
110 r#"
111pub fn go(func) {
112 let huuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuge_variable = []
113}
114"#,
115 );
116}
117
118#[test]
119fn comparison_with_empty_list() {
120 assert_js!(
121 "
122pub fn go(x) {
123 x == [] && [] == x
124}
125"
126 );
127}
128
129#[test]
130fn negated_comparison_with_empty_list() {
131 assert_js!(
132 "
133pub fn go(x) {
134 x != [] && [] != x
135}
136"
137 );
138}
139
140#[test]
141fn comparison_with_empty_list_ing_guard() {
142 assert_js!(
143 "
144pub fn go(x) {
145 case x {
146 _ if x == [] && [] == x -> 1
147 _ -> 2
148 }
149}
150"
151 );
152}
153
154#[test]
155fn negated_comparison_with_empty_list_ing_guard() {
156 assert_js!(
157 "
158pub fn go(x) {
159 case x {
160 _ if x != [] && [] != x -> 1
161 _ -> 2
162 }
163}
164"
165 );
166}