-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathmodules.re
169 lines (163 loc) · 3.42 KB
/
modules.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
open Grain_tests.TestFramework;
open Grain_tests.Runner;
describe("modules", ({test, testSkip}) => {
let test_or_skip =
Sys.backend_type == Other("js_of_ocaml") ? testSkip : test;
let assertSnapshot = makeSnapshotRunner(test);
let assertCompileError = makeCompileErrorRunner(test);
let assertRun = makeRunner(test_or_skip);
let assertFileRun = makeFileRunner(test_or_skip);
assertSnapshot(
"smallest_submodule",
{|
module Foo {
void
}
|},
);
assertRun(
"module_side_effect",
{|
module Foo {
print("foo")
}
|},
"foo\n",
);
assertRun(
"module_provide_value",
{|
module Foo {
provide let foo = "foo"
}
print(Foo.foo)
|},
"foo\n",
);
assertRun(
"module_provide_function",
{|
module Foo {
provide let foo = () => "foo"
}
print(Foo.foo())
|},
"foo\n",
);
assertRun(
"module_provide_type",
{|
module Foo {
provide enum Foo { Foo }
}
print(Foo.Foo)
|},
"Foo\n",
);
assertRun(
"module_provide_module",
{|
module Foo {
provide module Foo {
provide let foo = () => "foo"
}
}
print(Foo.Foo.foo())
|},
"foo\n",
);
assertRun(
"module_shadow",
{|
module Foo {
provide let foo = "foo"
}
module Foo {
provide let foo = "bar"
}
print(Foo.foo)
|},
"bar\n",
);
assertRun(
"local_module_use",
{|
module Foo {
provide let foo = "foo"
provide enum Foo { Foo }
provide module Foo {
provide let foo = "foo2"
}
}
use Foo.{ foo, type Foo, module Foo }
print(foo)
print(Foo.foo)
print(Foo)
|},
"foo\nfoo2\nFoo\n",
);
assertCompileError(
"local_module_include",
{|
module Foo {
from "list" include List
}
|},
"`include` statements may only appear at the file level",
);
assertRun(
"local_module_scoping",
{|
let foo = "foo"
module Foo {
provide let foo = foo
}
print(Foo.foo)
|},
"foo\n",
);
assertFileRun(
"nested_and_reprovided_modules",
"nestedModules",
"hello from foo\nhello from bar\n[2, 3, 4]\n9\n[> 2, 3, 4]\nfalse\nfoo\n",
);
test("reprovided_module", ({expect}) => {
let name = "reprovided_module";
let outfile = wasmfile(name);
ignore @@
compile(
~hook=Grain.Compile.stop_after_assembled,
name,
{|
module ReprovidedSimple
from "simpleModule" include Simple
provide { module Simple }
|},
);
let ic = open_in_bin(outfile);
let sections = Grain_utils.Wasm_utils.get_wasm_sections(ic);
close_in(ic);
let export_sections =
List.find_map(
(sec: Grain_utils.Wasm_utils.wasm_bin_section) =>
switch (sec) {
| {sec_type: Export(exports)} => Some(exports)
| _ => None
},
sections,
);
expect.option(export_sections).toBeSome();
expect.list(Option.get(export_sections)).toContainEqual((
WasmFunction,
"Simple.Simple.func",
));
expect.list(Option.get(export_sections)).toContainEqual((
WasmGlobal,
"GRAIN$EXPORT$Simple.Simple.func",
));
expect.list(Option.get(export_sections)).toContainEqual((
WasmGlobal,
"GRAIN$EXPORT$Simple.Simple.foo",
));
});
});