Fork of daniellemaywood.uk/gleam — Wasm codegen work
2.8 kB
109 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Gleam contributors
3
4use super::*;
5
6use wasm_bindgen_test::wasm_bindgen_test;
7
8#[wasm_bindgen_test]
9fn test_reset_filesystem() {
10 reset_filesystem(0);
11 assert_eq!(read_file_bytes(0, "hello"), None);
12 write_file_bytes(0, "hello", vec![1, 2, 3].as_slice());
13 assert_eq!(read_file_bytes(0, "hello"), Some(vec![1, 2, 3]));
14 reset_filesystem(0);
15 assert_eq!(read_file_bytes(0, "hello"), None);
16}
17
18#[wasm_bindgen_test]
19fn test_write_module() {
20 reset_filesystem(0);
21 assert_eq!(read_file_bytes(0, "/src/some/module.gleam"), None);
22 write_module(0, "some/module", "const x = 1");
23 assert_eq!(
24 read_file_bytes(0, "/src/some/module.gleam"),
25 Some(vec![99, 111, 110, 115, 116, 32, 120, 32, 61, 32, 49]),
26 );
27 reset_filesystem(0);
28 assert_eq!(read_file_bytes(0, "/src/some/module.gleam"), None);
29}
30
31#[wasm_bindgen_test]
32fn test_compile_package_bad_target() {
33 reset_filesystem(0);
34 assert!(compile_package(0, "ruby").is_err());
35}
36
37#[wasm_bindgen_test]
38fn test_compile_package_empty() {
39 reset_filesystem(0);
40 assert!(compile_package(0, "javascript").is_ok());
41}
42
43#[wasm_bindgen_test]
44fn test_compile_package_js() {
45 reset_filesystem(0);
46 write_module(0, "one/two", "pub const x = 1");
47 write_module(0, "up/down", "import one/two pub fn go() { two.x }");
48 assert!(compile_package(0, "javascript").is_ok());
49
50 assert_eq!(
51 read_compiled_javascript(0, "one/two"),
52 Some("export const x = 1;\n".into())
53 );
54
55 assert_eq!(
56 read_compiled_javascript(0, "up/down"),
57 Some(
58 r#"import * as $two from "../one/two.mjs";
59
60export function go() {
61 return $two.x;
62}
63"#
64 .into()
65 )
66 );
67
68 // And now an error!
69 write_module(0, "up/down", "import one/two/three");
70 assert!(compile_package(0, "javascript").is_err());
71
72 // Let's fix that.
73 write_module(0, "up/down", "pub const y = 1");
74 assert!(compile_package(0, "javascript").is_ok());
75 assert_eq!(
76 read_compiled_javascript(0, "up/down"),
77 Some("export const y = 1;\n".into())
78 );
79}
80
81#[wasm_bindgen_test]
82fn test_compile_package_js_unsupported_feature() {
83 reset_filesystem(0);
84 write_module(
85 0,
86 "one",
87 r#"
88fn wibble() { <<0:16-native>> }
89pub fn main() { wibble() }
90"#,
91 );
92
93 assert!(
94 compile_package(0, "javascript")
95 .unwrap_err()
96 .contains("The javascript target does not support")
97 );
98}
99
100#[wasm_bindgen_test]
101fn test_warnings() {
102 reset_filesystem(0);
103 write_module(0, "one", "const x = 1");
104 assert!(pop_warning(0).is_none());
105
106 assert!(compile_package(0, "javascript").is_ok());
107 assert!(pop_warning(0).is_some());
108 assert!(pop_warning(0).is_none());
109}