Fork of daniellemaywood.uk/gleam — Wasm codegen work
4.0 kB
373 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2021 The Gleam contributors
3
4use crate::assert_js;
5
6#[test]
7fn block() {
8 assert_js!(
9 r#"
10pub fn go() {
11 let x = {
12 1
13 2
14 }
15 x
16}
17"#,
18 );
19}
20
21#[test]
22fn nested_simple_blocks() {
23 assert_js!(
24 r#"
25pub fn go() {
26 let x = {
27 {
28 3
29 }
30 }
31 x
32}
33"#,
34 );
35}
36
37#[test]
38fn nested_multiexpr_blocks() {
39 assert_js!(
40 r#"
41pub fn go() {
42 let x = {
43 1
44 {
45 2
46 3
47 }
48 }
49 x
50}
51"#,
52 );
53}
54
55#[test]
56fn nested_multiexpr_blocks_with_pipe() {
57 assert_js!(
58 r#"
59pub fn add1(a) {
60 a + 1
61}
62pub fn go() {
63 let x = {
64 1
65 {
66 2
67 3 |> add1
68 } |> add1
69 }
70 x
71}
72"#,
73 );
74}
75
76#[test]
77fn nested_multiexpr_non_ending_blocks() {
78 assert_js!(
79 r#"
80pub fn go() {
81 let x = {
82 1
83 {
84 2
85 3
86 }
87 4
88 }
89 x
90}
91"#,
92 );
93}
94
95#[test]
96fn nested_multiexpr_blocks_with_case() {
97 assert_js!(
98 r#"
99pub fn go() {
100 let x = {
101 1
102 {
103 2
104 case True {
105 _ -> 3
106 }
107 }
108 }
109 x
110}
111"#,
112 );
113}
114
115#[test]
116fn sequences() {
117 assert_js!(
118 r#"
119pub fn go() {
120 "one"
121 "two"
122 "three"
123}
124"#,
125 );
126}
127
128#[test]
129fn left_operator_sequence() {
130 assert_js!(
131 r#"
132pub fn go() {
133 1 == {
134 1
135 2
136 }
137}
138"#,
139 );
140}
141
142#[test]
143fn right_operator_sequence() {
144 assert_js!(
145 r#"
146pub fn go() {
147 {
148 1
149 2
150 } == 1
151}
152"#,
153 );
154}
155
156#[test]
157fn concat_blocks() {
158 assert_js!(
159 r#"
160pub fn main(f, a, b) {
161 {
162 a
163 |> f
164 } <> {
165 b
166 |> f
167 }
168}
169"#,
170 );
171}
172
173#[test]
174fn blocks_returning_functions() {
175 assert_js!(
176 r#"
177pub fn b() {
178 {
179 fn(cb) { cb(1) }
180 }
181 {
182 fn(cb) { cb(2) }
183 }
184 3
185}
186"#
187 );
188}
189
190#[test]
191fn blocks_returning_use() {
192 assert_js!(
193 r#"
194pub fn b() {
195 {
196 use a <- fn(cb) { cb(1) }
197 a
198 }
199 {
200 use b <- fn(cb) { cb(2) }
201 b
202 }
203 3
204}
205 "#
206 );
207}
208
209#[test]
210fn block_with_parenthesised_expression_returning_from_function() {
211 assert_js!(
212 r#"
213pub fn b() {
214 {
215 1 + 2
216 }
217}
218"#
219 );
220}
221
222#[test]
223fn block_in_tail_position_is_not_an_iife() {
224 assert_js!(
225 r#"
226pub fn b() {
227 let x = 1
228 {
229 Nil
230 x + 1
231 }
232}
233"#
234 );
235}
236
237#[test]
238fn block_in_tail_position_shadowing_variables() {
239 assert_js!(
240 r#"
241pub fn b() {
242 let x = 1
243 {
244 let x = 2
245 x + 1
246 }
247}
248"#
249 );
250}
251
252#[test]
253fn block_in_tail_position_with_just_an_assignment() {
254 assert_js!(
255 r#"
256pub fn b() {
257 let x = 1
258 {
259 let x = x
260 }
261}
262"#
263 );
264}
265
266#[test]
267fn shadowed_variable_in_nested_scope() {
268 assert_js!(
269 "
270pub fn main() {
271 {
272 let x = 1
273 let _ = {
274 let x = 2
275 x
276 }
277 x
278 }
279}
280"
281 )
282}
283
284// https://github.com/gleam-lang/gleam/issues/4393
285#[test]
286fn let_assert_only_statement_in_block() {
287 assert_js!(
288 "
289pub fn main() {
290 {
291 let assert Ok(1) = Error(Nil)
292 }
293}
294"
295 )
296}
297
298// https://github.com/gleam-lang/gleam/issues/4394
299#[test]
300fn assignment_last_in_block() {
301 assert_js!(
302 "
303pub fn main() {
304 let a = {
305 let b = 1
306 let c = b + 1
307 }
308 a
309}
310"
311 )
312}
313
314// https://github.com/gleam-lang/gleam/issues/4394
315#[test]
316fn pattern_assignment_last_in_block() {
317 assert_js!(
318 "
319pub fn main() {
320 let a = {
321 let b = #(1, 2)
322 let #(x, y) = b
323 }
324 a
325}
326"
327 )
328}
329
330// https://github.com/gleam-lang/gleam/issues/4395
331#[test]
332fn let_assert_message_no_lifted() {
333 assert_js!(
334 r#"
335fn side_effects(x) {
336 // Some side effects
337 x
338}
339
340pub fn main() {
341 let assert Error(Nil) = side_effects(Ok(10))
342 as {
343 let message = side_effects("some message")
344 message
345 }
346}
347"#
348 )
349}
350
351#[test]
352fn blocks_whose_values_are_unused_do_not_generate_assignments() {
353 // There's no point generating `_block` assignments here, as the values
354 // would be unused.
355 assert_js!(
356 "
357pub fn main() {
358 {
359 let x = 10
360 echo x
361 }
362
363 {
364 let a = 1
365 let b = 2
366 a + b
367 }
368
369 Nil
370}
371"
372 );
373}