Skip to content

Commit f7ed05f

Browse files
committed
Using StreamData property testing for some tests
Removing hopefully the last reference to the watcher from the CLI module
1 parent 7030cf8 commit f7ed05f

File tree

8 files changed

+168
-156
lines changed

8 files changed

+168
-156
lines changed

lib/elixir_script/cli.ex

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ defmodule ElixirScript.CLI do
77
output: :string,
88
help: :boolean,
99
version: :boolean,
10-
watch: :boolean,
1110
root: :string
1211
]
1312

@@ -66,20 +65,13 @@ defmodule ElixirScript.CLI do
6665
end
6766

6867
def do_process(input, options) do
69-
{watch, options} = Keyword.pop(options, :watch, false)
70-
7168
compile_opts = [
7269
output: Keyword.get(options, :output, :stdout),
7370
root: Keyword.get(options, :root, ".")
7471
]
7572

7673
input = handle_input(input)
7774
ElixirScript.Compiler.compile(input, compile_opts)
78-
79-
if watch do
80-
ElixirScript.Watcher.start_link(input, compile_opts)
81-
:timer.sleep :infinity
82-
end
8375
end
8476

8577
defp options_contains_unknown_values(options) do

lib/elixir_script/passes/translate/identifier.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@ defmodule ElixirScript.Translate.Identifier do
7575
J.identifier(name)
7676
end
7777

78+
def js_reserved_words() do
79+
@js_reserved_words
80+
end
81+
7882
end

mix.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule ElixirScript.Mixfile do
2222

2323
def application do
2424
[
25-
applications: [:logger, :estree]
25+
extra_applications: [:logger]
2626
]
2727
end
2828

@@ -31,7 +31,8 @@ defmodule ElixirScript.Mixfile do
3131
{:estree, "~> 2.6"},
3232
{:ex_doc, "~> 0.16", only: :dev},
3333
{:excoveralls, "~> 0.7", only: :test},
34-
{:credo, "~> 0.8", only: [:dev, :test]}
34+
{:credo, "~> 0.8", only: [:dev, :test]},
35+
{:stream_data, "~> 0.1", only: :test}
3536
]
3637
end
3738

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], [], "hexpm"},
1313
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], [], "hexpm"},
1414
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], [], "hexpm"},
15+
"stream_data": {:hex, :stream_data, "0.1.0", "858a6d1f1fe9415c6877fde1140b6702a47c0bebd28599e26a83dfc96020835a", [], [], "hexpm"},
1516
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"}}

test/passes/translate/form_test.exs

Lines changed: 94 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule ElixirScript.Translate.Forms.Test do
33
alias ElixirScript.Translate.Form
44
alias ElixirScript.Translate.Identifier
55
alias ESTree.Tools.Builder, as: J
6+
require StreamData
7+
import PropertyTest
68

79

810
setup_all do
@@ -15,107 +17,119 @@ defmodule ElixirScript.Translate.Forms.Test do
1517
[state: state]
1618
end
1719

18-
test "integer" do
19-
ast = 1
20-
state = %{}
21-
22-
{js_ast, _} = Form.compile(ast, state)
23-
assert js_ast == J.literal(1)
24-
end
25-
26-
test "boolean" do
27-
ast = true
28-
state = %{}
29-
30-
{js_ast, _} = Form.compile(ast, state)
31-
assert js_ast == J.literal(true)
32-
end
33-
34-
test "float" do
35-
ast = 1.0
36-
state = %{}
37-
38-
{js_ast, _} = Form.compile(ast, state)
39-
assert js_ast == J.literal(1.0)
20+
property "integers, floats, binaries, and booleans translates to a literal JavaScript AST node", %{state: state} do
21+
check all value <- StreamData.one_of([
22+
StreamData.int(),
23+
StreamData.boolean(),
24+
StreamData.binary(),
25+
StreamData.uniform_float()
26+
]) do
27+
{js_ast, _} = Form.compile(value, state)
28+
assert js_ast == J.literal(value)
29+
end
4030
end
4131

42-
test "binary" do
43-
ast = "hello"
44-
state = %{}
45-
46-
{js_ast, _} = Form.compile(ast, state)
47-
assert js_ast == J.literal("hello")
48-
end
49-
50-
test "atom" do
51-
ast = :tiger
52-
state = %{}
53-
54-
{js_ast, _} = Form.compile(ast, state)
55-
assert js_ast == J.call_expression(
32+
property "atom translates to Symbol.for call", %{state: state} do
33+
check all atom <- StreamData.unquoted_atom() do
34+
{js_ast, _} = Form.compile(atom, state)
35+
assert js_ast == J.call_expression(
5636
J.member_expression(
5737
J.identifier("Symbol"),
5838
J.identifier("for")
5939
),
60-
[J.literal(:tiger)]
40+
[J.literal(atom)]
6141
)
42+
end
6243
end
6344

64-
test "module", %{state: state} do
65-
ast = IO
66-
67-
{js_ast, _} = Form.compile(ast, state)
68-
assert js_ast == J.call_expression(
69-
Identifier.make_namespace_members(["Elixir", "IO", "__load"]),
70-
[J.identifier("Elixir")]
71-
)
72-
end
73-
74-
test "tuple" do
75-
ast = {1, 2}
76-
state = %{}
77-
78-
{js_ast, _} = Form.compile(ast, state)
79-
assert js_ast == J.new_expression(
80-
J.member_expression(
45+
property "tuple translates to new Tuple object", %{state: state} do
46+
check all tuple <- StreamData.tuple({StreamData.int(), StreamData.binary()}) do
47+
{js_ast, _} = Form.compile(tuple, state)
48+
assert js_ast == J.new_expression(
8149
J.member_expression(
82-
J.identifier("ElixirScript"),
83-
J.identifier("Core")
50+
J.member_expression(
51+
J.identifier("ElixirScript"),
52+
J.identifier("Core")
53+
),
54+
J.identifier("Tuple")
8455
),
85-
J.identifier("Tuple")
86-
),
87-
[J.literal(1), J.literal(2)]
88-
)
56+
[J.literal(elem(tuple, 0)), J.literal(elem(tuple, 1))]
57+
)
58+
end
8959
end
9060

91-
test "list" do
92-
ast = [1, 2]
93-
state = %{}
94-
95-
{js_ast, _} = Form.compile(ast, state)
96-
assert js_ast == J.array_expression([J.literal(1), J.literal(2)])
61+
property "list translates to a JavaScript Array", %{state: state} do
62+
check all list <- StreamData.list_of(StreamData.binary()) do
63+
{js_ast, _} = Form.compile(list, state)
64+
assert js_ast.type == "ArrayExpression"
65+
assert length(js_ast.elements) == length(list)
66+
67+
Enum.zip(js_ast.elements, list)
68+
|> Enum.each(fn {ast, original} ->
69+
assert ast == J.literal(original)
70+
end)
71+
end
9772
end
9873

99-
test "super" do
100-
ast = {:super, [], [{:def, :my_function}, 1]}
101-
state = %{function: {:my_function, nil}, vars: %{}}
74+
property "local function call translates to local JavaScript function call", %{state: state} do
75+
check all func <- StreamData.unquoted_atom(),
76+
params <- StreamData.list_of(StreamData.binary()) do
77+
78+
ast = {func, [], params}
79+
80+
str_func = if func in ElixirScript.Translate.Identifier.js_reserved_words() do
81+
"__#{to_string(func)}__"
82+
else
83+
to_string(func)
84+
end
85+
86+
{js_ast, _} = Form.compile(ast, state)
87+
assert js_ast.type == "CallExpression"
88+
assert length(js_ast.arguments) == length(params)
89+
assert js_ast.callee.type == "Identifier"
90+
assert js_ast.callee.name == str_func
91+
92+
Enum.zip(js_ast.arguments, params)
93+
|> Enum.each(fn {ast, original} ->
94+
assert ast == J.literal(original)
95+
end)
96+
end
97+
end
10298

103-
{js_ast, _} = Form.compile(ast, state)
104-
assert js_ast == J.call_expression(
105-
J.identifier("my_function"),
106-
[J.literal(1)]
107-
)
99+
property "super function call translates to local JavaScript function call" do
100+
check all func <- StreamData.unquoted_atom(),
101+
params <- StreamData.list_of(StreamData.binary()) do
102+
103+
ast = {:super, [], [{:def, func}] ++ params}
104+
state = %{function: {func, nil}, vars: %{}}
105+
106+
str_func = if func in ElixirScript.Translate.Identifier.js_reserved_words() do
107+
"__#{to_string(func)}__"
108+
else
109+
to_string(func)
110+
end
111+
112+
{js_ast, _} = Form.compile(ast, state)
113+
assert js_ast.type == "CallExpression"
114+
assert length(js_ast.arguments) == length(params)
115+
assert js_ast.callee.type == "Identifier"
116+
assert js_ast.callee.name == str_func
117+
118+
Enum.zip(js_ast.arguments, params)
119+
|> Enum.each(fn {ast, original} ->
120+
assert ast == J.literal(original)
121+
end)
122+
end
108123
end
109124

110-
test "local function" do
111-
ast = {:my_function, [], [1]}
112-
state = %{}
125+
test "module", %{state: state} do
126+
ast = IO
113127

114128
{js_ast, _} = Form.compile(ast, state)
115129
assert js_ast == J.call_expression(
116-
J.identifier("my_function"),
117-
[J.literal(1)]
118-
)
130+
Identifier.make_namespace_members(["Elixir", "IO", "__load"]),
131+
[J.identifier("Elixir")]
132+
)
119133
end
120134

121135
test "function returning an array" do

0 commit comments

Comments
 (0)