-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathdefmodule_test.exs
More file actions
176 lines (135 loc) · 4.16 KB
/
Copy pathdefmodule_test.exs
File metadata and controls
176 lines (135 loc) · 4.16 KB
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
170
171
172
173
174
175
176
defmodule ElixirScript.Translator.Defmodule.Test do
use ExUnit.Case
import ElixirScript.TestHelper
test "translate empty module" do
ex_ast = quote do
defmodule Elephant do
end
end
js_code = """
const __exports = {
__info__
};
"""
assert_translation(ex_ast, js_code)
end
test "translate defmodules" do
ex_ast = quote do
defmodule Elephant do
require JS
@ul "#todo-list"
def something() do
@ul
end
JS.defgenp something_else() do
end
end
end
js_code = """
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([], function() {
return ul;
}));
const __info__ = function(kind) {
return Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Symbol.for('functions')], function() {
return Object.freeze([new Bootstrap.Core.Tuple(Symbol.for('something'), 0)]);
}), Bootstrap.Core.Patterns.clause([Symbol.for('macros')], function() {
return Object.freeze([]);
}), Bootstrap.Core.Patterns.clause([Symbol.for('module')], function() {
return Symbol.for('Elixir.Elephant');
})).call(this, kind);
};
const ul = '#todo-list';
const something_else = Bootstrap.Core.Patterns.defmatchgen(Bootstrap.Core.Patterns.clause([], function*() {
return null;
}));
"""
assert_translation(ex_ast, js_code)
end
test "translate modules with inner modules" do
ex_ast = quote do
defmodule Animals do
defmodule Elephant do
defstruct [trunk: true]
end
def something() do
%Animals.Elephant{}
end
defp something_else() do
end
end
end
js_code = """
const __struct__ = function(values = {}) {
const allowed_keys = [Symbol.for('trunk')]
const value_keys = Object.keys(values)
const every_call_result = value_keys.every(function(key) {
return allowed_keys.includes(key);
})
if (every_call_result) {
return Object.assign({}, {
[Symbol.for('__struct__')]: Symbol.for('Elixir.Animals.Elephant'),
[Symbol.for('trunk')]: true
}, values);
} else {
throw 'Unallowed key found';
}
};
"""
assert_translation(ex_ast, js_code)
end
test "translate modules with inner module that has inner module" do
ex_ast = quote do
defmodule Animals do
defmodule Elephant do
defstruct trunk: true
defmodule Bear do
defstruct trunk: true
end
end
def something() do
%Animals.Elephant{}
end
defp something_else() do
end
end
end
js_code = """
const __struct__ = function(values = {}) {
const allowed_keys = [Symbol.for('trunk')]
const value_keys = Object.keys(values)
const every_call_result = value_keys.every(function(key) {
return allowed_keys.includes(key);
})
if (every_call_result) {
return Object.assign({}, {
[Symbol.for('__struct__')]: Symbol.for('Elixir.Animals.Elephant.Bear'),
[Symbol.for('trunk')]: true
}, values);
} else {
throw 'Unallowed key found';
}
};
"""
assert_translation(ex_ast, js_code)
end
test "Pull out module references and make them into imports if modules listed" do
ex_ast = quote do
defmodule Lions.Tigers.Bears do
def oh_my() do
end
end
defmodule Lions.Tigers do
def oh_my() do
end
Lions.Tigers.Bears.oh_my()
end
defmodule Animals do
Lions.Tigers.oh_my()
end
end
js_code = """
Elixir.Lions.Tigers.Bears.__load(Elixir).oh_my()
"""
assert_translation(ex_ast, js_code)
end
end