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