-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_helper.exs
More file actions
92 lines (71 loc) · 1.86 KB
/
Copy pathtest_helper.exs
File metadata and controls
92 lines (71 loc) · 1.86 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
exclude = if Node.alive?, do: [], else: [skip: true]
ExUnit.start(exclude: exclude)
defmodule ElixirScript.Math do
defmacro squared(x) do
quote do
unquote(x) * unquote(x)
end
end
end
defmodule ElixirScript.Using do
defmacro __using__(_) do
quote do
def sandwich() do
end
end
end
end
defmodule ElixirScript.TestHelper do
use ExUnit.Case
alias ESTree.Tools.Generator
def make_custom_env do
use ElixirScript
require ElixirScript.Math
require ElixirScript.Using
__ENV__
end
def ex_ast_to_js(ex_ast, format) do
ElixirScript.compile_quoted(ex_ast, %{ env: make_custom_env(), import_standard_libs: false, format: format })
end
def strip_spaces(js) do
js |> String.replace(~r/\s+/, "")
end
def assert_translation(ex_ast, js_code) do
assert_translation(ex_ast, js_code, :es)
end
def assert_translation(ex_ast, js_code, format) do
converted_code = ex_ast_to_js(ex_ast, format)
assert converted_code |> strip_spaces =~ strip_spaces(js_code), """
**Code Does Not Match **
***Expected***
#{js_code}
***Actual***
#{converted_code}
"""
end
def assert_js_matches(expected_js_code, actual_js_code) do
assert strip_spaces(hd(List.wrap(actual_js_code))) =~ strip_spaces(expected_js_code), """
**Code Does Not Match **
***Expected***
#{expected_js_code}
***Actual***
#{actual_js_code}
"""
end
def refute_translation(ex_ast, js_code) do
refute_translation(ex_ast, js_code, :es)
end
def refute_translation(ex_ast, js_code, format) do
converted_code = ex_ast_to_js(ex_ast, format)
refute converted_code |> strip_spaces =~ strip_spaces(js_code), """
**Code Does Not Match **
***Expected***
#{js_code}
***Actual***
#{converted_code}
"""
end
def generate_js(js_ast) do
Generator.generate(js_ast)
end
end