forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelixir_script_test.exs
More file actions
122 lines (98 loc) · 3.43 KB
/
Copy pathelixir_script_test.exs
File metadata and controls
122 lines (98 loc) · 3.43 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
defmodule ElixirScript.Test do
use ShouldI
import ElixirScript.TestHelper
should "chain methods" do
js_code = ElixirScript.compile("""
JQuery.("<div/>").text(html)
""")
assert hd(js_code) == "JQuery('<div/>').text(html)"
end
should "turn javascript ast into javascript code strings" do
js_code = ElixirScript.compile(":atom")
assert hd(js_code) == "Kernel.SpecialForms.atom('atom')"
end
should "parse one module correctly" do
js_code = ElixirScript.compile("""
defmodule Elephant do
@ul JQuery.("#todo-list")
def something() do
@ul
end
defp something_else() do
end
end
""")
assert_js_matches """
import { Patterns, Kernel, Atom, Enum, Integer, JS, List, Range, Tuple, Agent, Keyword, BitString, Base, String, Bitwise } from 'elixir';
const __MODULE__ = Kernel.SpecialForms.atom('Elephant');
let something_else = Patterns.defmatch(Patterns.make_case([],function() {
return null;
}));
let something = Patterns.defmatch(Patterns.make_case([],function() {
return ul;
}));
const ul = JQuery('#todo-list');
export {
something
};
""", hd(js_code)
end
should "parse multiple modules correctly" do
js_code = ElixirScript.compile("""
defmodule Animals do
defmodule Elephant do
defstruct trunk: true
end
def something() do
%Elephant{}
end
end
""", env: make_custom_env)
assert_js_matches """
import { Patterns, Kernel, Atom, Enum, Integer, JS, List, Range, Tuple, Agent, Keyword, BitString, Base, String, Bitwise } from 'elixir';
import * as Elephant from 'animals/elephant';
const __MODULE__ = Kernel.SpecialForms.atom('Animals');
let something = Patterns.defmatch(Patterns.make_case([],function() {
return Elephant.defstruct();
}));
export {
something
};
""", hd(js_code)
assert_js_matches """
import { Patterns, Kernel, Atom, Enum, Integer, JS, List, Range, Tuple, Agent, Keyword, BitString, Base, String, Bitwise } from 'elixir';
const __MODULE__ = Kernel.SpecialForms.atom('Elephant');
function defstruct(trunk = true) {
return Kernel.SpecialForms.map({
[Kernel.SpecialForms.atom('__struct__')]: __MODULE__, [Kernel.SpecialForms.atom('trunk')]: trunk
});
}
export {
defstruct
};
""", List.last(js_code)
end
should "parse macros" do
js_code = ElixirScript.compile("""
defmodule Animals do
use ElixirScript.Using
defp something_else() do
ElixirScript.Math.squared(1)
end
end
""", env: make_custom_env)
assert_js_matches """
import { Patterns, Kernel, Atom, Enum, Integer, JS, List, Range, Tuple, Agent, Keyword, BitString, Base, String, Bitwise } from 'elixir';
const __MODULE__ = Kernel.SpecialForms.atom('Animals');
let something_else = Patterns.defmatch(Patterns.make_case([],function() {
return 1 * 1;
}));
let sandwich = Patterns.defmatch(Patterns.make_case([],function() {
return null;
}));
export {
sandwich
};
""", hd(js_code)
end
end