@@ -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