Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.6 kB
286 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2025 The Gleam contributors
3
4use crate::assert_source_map;
5
6#[test]
7fn sourcemap_function_definition() {
8 assert_source_map!(
9 "
10/// my add function
11pub fn add_2(x) {
12 x + 2
13}"
14 )
15}
16
17#[test]
18fn sourcemap_function_definitions_with_unused() {
19 assert_source_map!(
20 "
21/// my add function
22pub fn add_2(x) {
23 x + 2
24}
25
26fn unused(x) {
27 x + 1
28}
29
30pub fn add_3(x) {
31 x + 3
32}
33"
34 )
35}
36
37#[test]
38fn sourcemap_function_definition_with_variable_assignment() {
39 assert_source_map!(
40 "
41/// my function
42pub fn wibble() {
43 let wibble = 1
44 wibble + 2
45}"
46 )
47}
48
49#[test]
50fn sourcemap_function_definition_with_string_with_newline_escaped() {
51 assert_source_map!(
52 "
53/// my function
54pub fn wibble() {
55 let wibble = \"hello\\nworld\"
56 wibble <> \"!\"
57}"
58 )
59}
60
61#[test]
62fn sourcemap_function_definition_with_string_with_newline() {
63 assert_source_map!(
64 "
65/// my function
66pub fn wibble() {
67 let wibble = \"hello
68world\"
69 wibble <> \"!\"
70}"
71 )
72}
73
74#[test]
75fn sourcemap_custom_type_definition() {
76 assert_source_map!(
77 "
78/// my custom type
79pub type Wibble {
80 /// Wibble
81 Wibble
82 /// Wobble
83 Wobble(field: Int)
84 /// Wabble
85 Wabble(Wibble)
86}"
87 )
88}
89
90#[test]
91fn sourcemap_custom_type_definition_with_unused() {
92 assert_source_map!(
93 "
94/// my custom type
95pub type Wibble {
96 /// Wibble
97 Wibble
98}
99
100type Unused {
101 Unused
102}
103
104pub type Wobble {
105 Wobble(Wibble)
106}
107"
108 )
109}
110
111#[test]
112fn sourcemap_module_constant() {
113 assert_source_map!(
114 "
115/// my constant
116pub const wibble = 1
117/// wobble
118const wobble = 2
119/// wabble
120pub const wabble = 3"
121 )
122}
123
124#[test]
125fn sourcemap_assert() {
126 assert_source_map!(
127 "
128pub fn main() {
129 let x = True
130 assert x
131}
132"
133 )
134}
135
136#[test]
137fn sourcemap_let_assert() {
138 assert_source_map!(
139 "
140pub fn unwrap_or_panic(result) {
141 let assert Ok(value) = result
142 value
143}
144"
145 )
146}
147
148#[test]
149fn sourcemap_let_assert_that_always_succeeds() {
150 assert_source_map!(
151 "
152pub fn go(x) {
153 let assert #(wibble, wobble) = x
154}
155"
156 )
157}
158
159#[test]
160fn sourcemap_case_destructure_assignment_statement() {
161 assert_source_map!(
162 "
163pub type Wibble {
164 Wibble(Int, Int)
165}
166
167pub fn go(x) {
168 case Wibble(1, 2) {
169 Wibble(wibble, wobble) -> wibble + wobble
170 }
171}
172"
173 )
174}
175
176#[test]
177fn sourcemap_with_complex_case_expression() {
178 assert_source_map!(
179 "
180pub fn go(one, other) {
181 case one, other {
182 Ok(1), Error(_) -> 1
183 Ok(_), _ -> 2
184 Error(_), Ok(2) -> 3
185 _, _ -> 4
186 }
187}
188"
189 )
190}
191
192#[test]
193fn sourcemap_pipe() {
194 assert_source_map!(
195 "
196fn add_2(x) {
197 x + 2
198}
199
200pub fn go(x) {
201 x |> add_2
202}
203"
204 )
205}
206
207#[test]
208fn sourcemap_use() {
209 assert_source_map!(
210 "
211fn add_3_to_result(i, f) {
212 f(i) + 3
213}
214
215pub fn go(x) {
216 use a <- add_3_to_result(1)
217 a + 2
218}
219"
220 )
221}
222
223#[test]
224fn sourcemap_with_list() {
225 assert_source_map!(
226 "
227pub fn go(x) {
228 [1, 2, 3]
229}
230"
231 )
232}
233
234#[test]
235fn sourcemap_with_tuple() {
236 assert_source_map!(
237 "
238pub fn go(x) {
239 #(1, 2, 3)
240}
241"
242 )
243}
244
245#[test]
246fn sourcemap_with_bitarray() {
247 assert_source_map!(
248 "
249pub fn go(x) {
250 <<256:int>>
251}
252"
253 )
254}
255
256#[test]
257fn sourcemap_with_tail_recursive_functions() {
258 assert_source_map!(
259 "
260pub fn wibble(lon, acc) {
261 case lon {
262 [] -> 0
263 [n, ..rest] -> wibble(rest, acc + n)
264 }
265}
266"
267 )
268}
269
270#[test]
271fn string_with_newlines() {
272 assert_source_map!(
273 r#"
274pub fn wibble(lon, acc) {
275 let one = "one"
276 let two = "one
277
278two
279
280three
281"
282 let three = "three"
283}
284"#
285 )
286}