Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.2 kB
223 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Gleam contributors
3
4use crate::{assert_format, assert_format_rewrite};
5
6#[test]
7fn types_and_values() {
8 assert_format!(
9 "import one/two.{type Abc, type Bcd, Abc, Bcd, abc, bcd}
10"
11 );
12}
13
14#[test]
15fn discarded_import() {
16 assert_format!(
17 "import one/two as _three
18"
19 );
20}
21
22#[test]
23fn discarded_import_with_unqualified() {
24 assert_format!(
25 "import one/two.{type Abc, Bcd, abc} as _three
26"
27 );
28}
29
30#[test]
31fn redundant_as_name_is_removed() {
32 assert_format_rewrite!("import wibble/wobble as wobble", "import wibble/wobble\n");
33 assert_format_rewrite!("import wibble as wibble", "import wibble\n");
34}
35
36#[test]
37fn imports_are_sorted_alphabetically() {
38 assert_format_rewrite!(
39 "import c import a/a import a/c import b import a/ab import a",
40 "import a
41import a/a
42import a/ab
43import a/c
44import b
45import c
46"
47 );
48}
49
50#[test]
51fn import_groups_are_respected() {
52 assert_format_rewrite!(
53 "import group_one/a
54import group_one/b
55// another group
56import group_two/wobble
57import group_two/wibble
58// yet another group
59import group_three/b
60import group_three/c
61import group_three/a
62",
63 "import group_one/a
64import group_one/b
65
66// another group
67import group_two/wibble
68import group_two/wobble
69
70// yet another group
71import group_three/a
72import group_three/b
73import group_three/c
74"
75 );
76}
77
78#[test]
79fn empty_lines_define_different_groups() {
80 assert_format_rewrite!(
81 "import c
82@target(javascript)
83import b
84
85import a
86
87import gleam/string
88import gleam/list",
89 "@target(javascript)
90import b
91import c
92
93import a
94
95import gleam/list
96import gleam/string
97"
98 );
99}
100
101#[test]
102fn import_groups_with_empty_lines_and_comments() {
103 assert_format_rewrite!(
104 "import c
105@target(javascript)
106import b
107
108import a
109// third group
110import gleam/string
111import gleam/list
112
113import wobble
114import wibble
115",
116 "@target(javascript)
117import b
118import c
119
120import a
121
122// third group
123import gleam/list
124import gleam/string
125
126import wibble
127import wobble
128"
129 );
130}
131
132#[test]
133fn type_definition_in_between_imports() {
134 assert_format!(
135 r#"import a
136import b
137
138pub type Wibble(a) {
139 Wobble
140}
141
142import c
143import d
144
145import e
146
147pub type Wabble
148
149import f
150"#
151 );
152}
153
154#[test]
155fn function_definition_in_between_imports() {
156 assert_format!(
157 r#"import a
158import b
159
160pub fn wibble() {
161 todo
162}
163
164import c
165import d
166
167import e
168
169pub fn wobble() -> Int {
170 todo
171}
172
173import f
174"#
175 );
176}
177
178#[test]
179fn constant_definition_in_between_imports() {
180 assert_format!(
181 r#"import a
182import b
183
184pub const wibble = Wibble
185
186import c
187import d
188
189import e
190
191const wobble = 1
192
193import f
194"#
195 );
196}
197
198// https://github.com/gleam-lang/gleam/issues/2915
199#[test]
200fn white_lines_between_comments_in_import_groups_are_preserved() {
201 assert_format!(
202 r#"import a
203
204// comment
205
206import b
207"#
208 );
209}
210
211// https://github.com/gleam-lang/gleam/issues/2915
212#[test]
213fn import_sorting_doesnt_add_spurious_white_line() {
214 assert_format!(
215 r#"// comment
216
217import filepath
218import gleam/dynamic.{type Dynamic}
219import gleam/io
220import gleam/list
221"#
222 );
223}