Fork of daniellemaywood.uk/gleam — Wasm codegen work
7.6 kB
249 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Gleam contributors
3
4use crate::engine::Compilation;
5
6use super::*;
7
8#[test]
9fn compile_please() {
10 let io = LanguageServerTestIO::new();
11 let mut engine = setup_engine(&io);
12
13 let response = engine.compile_please();
14 assert!(response.result.is_ok());
15 assert!(response.warnings.is_empty());
16 assert_eq!(response.compilation, Compilation::Yes(vec![]));
17
18 drop(engine);
19 let actions = io.into_actions();
20 assert_eq!(
21 actions,
22 vec![
23 // new
24 Action::DependencyDownloadingStarted,
25 Action::DownloadDependencies,
26 Action::DependencyDownloadingFinished,
27 Action::LockBuild,
28 Action::UnlockBuild,
29 // compile_please
30 Action::CompilationStarted,
31 Action::LockBuild,
32 Action::UnlockBuild,
33 Action::CompilationFinished,
34 ]
35 )
36}
37
38#[test]
39fn compile_error_in_src() {
40 let io = LanguageServerTestIO::new();
41 let mut engine = setup_engine(&io);
42
43 _ = io.src_module("app/error", "pub type Error {");
44
45 let response = engine.compile_please();
46 assert!(response.result.is_err());
47 assert!(response.warnings.is_empty());
48 assert_eq!(response.compilation, Compilation::Yes(vec![]));
49
50 drop(engine);
51 let actions = io.into_actions();
52 assert_eq!(
53 actions,
54 vec![
55 // new
56 Action::DependencyDownloadingStarted,
57 Action::DownloadDependencies,
58 Action::DependencyDownloadingFinished,
59 Action::LockBuild,
60 Action::UnlockBuild,
61 // compile_please
62 Action::CompilationStarted,
63 Action::LockBuild,
64 Action::UnlockBuild,
65 Action::CompilationFinished,
66 ]
67 )
68}
69
70#[test]
71fn compile_error_in_test() {
72 let io = LanguageServerTestIO::new();
73 let mut engine = setup_engine(&io);
74
75 _ = io.test_module("app/error", "pub type Error {");
76
77 let response = engine.compile_please();
78 assert!(response.result.is_err());
79 assert!(response.warnings.is_empty());
80 assert_eq!(response.compilation, Compilation::Yes(vec![]));
81
82 drop(engine);
83 let actions = io.into_actions();
84 assert_eq!(
85 actions,
86 vec![
87 // new
88 Action::DependencyDownloadingStarted,
89 Action::DownloadDependencies,
90 Action::DependencyDownloadingFinished,
91 Action::LockBuild,
92 Action::UnlockBuild,
93 // compile_please
94 Action::CompilationStarted,
95 Action::LockBuild,
96 Action::UnlockBuild,
97 Action::CompilationFinished,
98 ]
99 )
100}
101
102#[test]
103fn compile_error_in_dev() {
104 let io = LanguageServerTestIO::new();
105 let mut engine = setup_engine(&io);
106
107 _ = io.dev_module("app/error", "pub type Error {");
108
109 let response = engine.compile_please();
110 assert!(response.result.is_err());
111 assert!(response.warnings.is_empty());
112 assert_eq!(response.compilation, Compilation::Yes(vec![]));
113
114 drop(engine);
115 let actions = io.into_actions();
116 assert_eq!(
117 actions,
118 vec![
119 // new
120 Action::DependencyDownloadingStarted,
121 Action::DownloadDependencies,
122 Action::DependencyDownloadingFinished,
123 Action::LockBuild,
124 Action::UnlockBuild,
125 // compile_please
126 Action::CompilationStarted,
127 Action::LockBuild,
128 Action::UnlockBuild,
129 Action::CompilationFinished,
130 ]
131 )
132}
133
134#[test]
135fn compile_recompile() {
136 let io = LanguageServerTestIO::new();
137 let mut engine = setup_engine(&io);
138
139 let path = io.src_module("app", "pub fn main() { 0 }");
140
141 // The first time it compiles.
142 let response = engine.compile_please();
143 assert!(response.result.is_ok());
144 assert!(response.warnings.is_empty());
145 assert_eq!(response.compilation, Compilation::Yes(vec![path.clone()]));
146
147 // The source file has been updated, so the file is compiled again.
148 _ = io.src_module("app", "pub fn main() { 1 }");
149 let response = engine.compile_please();
150 assert!(response.result.is_ok());
151 assert!(response.warnings.is_empty());
152 assert_eq!(response.compilation, Compilation::Yes(vec![path]));
153
154 // This time it does not compile the module again, instead using the
155 // cache from the previous run.
156 let response = engine.compile_please();
157 assert_eq!(response.result, Ok(()));
158 assert!(response.warnings.is_empty());
159 assert_eq!(response.compilation, Compilation::Yes(vec![]));
160
161 drop(engine);
162 let actions = io.into_actions();
163 assert_eq!(
164 actions,
165 vec![
166 // new
167 Action::DependencyDownloadingStarted,
168 Action::DownloadDependencies,
169 Action::DependencyDownloadingFinished,
170 Action::LockBuild,
171 Action::UnlockBuild,
172 // compile_please
173 Action::CompilationStarted,
174 Action::LockBuild,
175 Action::UnlockBuild,
176 Action::CompilationFinished,
177 // compile_please
178 Action::CompilationStarted,
179 Action::LockBuild,
180 Action::UnlockBuild,
181 Action::CompilationFinished,
182 // compile_please
183 Action::CompilationStarted,
184 Action::LockBuild,
185 Action::UnlockBuild,
186 Action::CompilationFinished,
187 ]
188 )
189}
190
191#[test]
192fn dep_compile_recompile() {
193 let io = LanguageServerTestIO::new();
194 let mut engine = setup_engine(&io);
195 add_path_dep(&mut engine, "mydep");
196
197 let path = io.path_dep_module("mydep", "moddy", "pub fn main() { 0 }");
198
199 // The first time it compiles.
200 let response = engine.compile_please();
201 assert!(response.result.is_ok());
202 assert!(response.warnings.is_empty());
203 assert_eq!(response.compilation, Compilation::Yes(vec![path.clone()]));
204
205 assert!(!engine.compiler.project_compiler.packages.is_empty());
206
207 // The source file has been updated, so the file is compiled again.
208 _ = io.path_dep_module("mydep", "moddy", "pub fn main() { 1 }");
209 let response = engine.compile_please();
210 assert!(response.result.is_ok());
211 assert!(response.warnings.is_empty());
212 assert_eq!(response.compilation, Compilation::Yes(vec![path]));
213
214 // This time it does not compile the module again, instead using the
215 // cache from the previous run.
216 let response = engine.compile_please();
217 assert!(response.result.is_ok());
218 assert!(response.warnings.is_empty());
219 assert_eq!(response.compilation, Compilation::Yes(vec![]));
220
221 drop(engine);
222 let actions = io.into_actions();
223 assert_eq!(
224 actions,
225 vec![
226 // new
227 Action::DependencyDownloadingStarted,
228 Action::DownloadDependencies,
229 Action::DependencyDownloadingFinished,
230 Action::LockBuild,
231 Action::UnlockBuild,
232 // compile_please
233 Action::CompilationStarted,
234 Action::LockBuild,
235 Action::UnlockBuild,
236 Action::CompilationFinished,
237 // compile_please
238 Action::CompilationStarted,
239 Action::LockBuild,
240 Action::UnlockBuild,
241 Action::CompilationFinished,
242 // compile_please
243 Action::CompilationStarted,
244 Action::LockBuild,
245 Action::UnlockBuild,
246 Action::CompilationFinished,
247 ]
248 )
249}