@@ -5,7 +5,6 @@ defmodule ElixirScript.Translate.Forms.Test do
55 alias ESTree.Tools.Builder , as: J
66 use ExUnitProperties
77
8-
98 setup_all do
109 { :ok , pid } = ElixirScript.State . start_link ( % { } )
1110
@@ -17,13 +16,15 @@ defmodule ElixirScript.Translate.Forms.Test do
1716 [ state: state ]
1817 end
1918
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 . integer ( ) ,
23- StreamData . boolean ( ) ,
24- StreamData . binary ( ) ,
25- StreamData . uniform_float ( )
26- ] ) do
19+ property "integers, floats, binaries, and booleans translates to a literal JavaScript AST node" ,
20+ % { state: state } do
21+ check all value <-
22+ StreamData . one_of ( [
23+ StreamData . integer ( ) ,
24+ StreamData . boolean ( ) ,
25+ StreamData . binary ( ) ,
26+ StreamData . uniform_float ( )
27+ ] ) do
2728 { js_ast , _ } = Form . compile ( value , state )
2829 assert js_ast == J . literal ( value )
2930 end
@@ -32,29 +33,33 @@ defmodule ElixirScript.Translate.Forms.Test do
3233 property "atom translates to Symbol.for call" , % { state: state } do
3334 check all atom <- StreamData . unquoted_atom ( ) do
3435 { js_ast , _ } = Form . compile ( atom , state )
35- assert js_ast == J . call_expression (
36- J . member_expression (
37- J . identifier ( "Symbol" ) ,
38- J . identifier ( "for" )
39- ) ,
40- [ J . literal ( atom ) ]
41- )
36+
37+ assert js_ast ==
38+ J . call_expression (
39+ J . member_expression (
40+ J . identifier ( "Symbol" ) ,
41+ J . identifier ( "for" )
42+ ) ,
43+ [ J . literal ( atom ) ]
44+ )
4245 end
4346 end
4447
4548 property "tuple translates to new Tuple object" , % { state: state } do
4649 check all tuple <- StreamData . tuple ( { StreamData . integer ( ) , StreamData . binary ( ) } ) do
4750 { js_ast , _ } = Form . compile ( tuple , state )
48- assert js_ast == J . new_expression (
49- J . member_expression (
50- J . member_expression (
51- J . identifier ( "ElixirScript" ) ,
52- J . identifier ( "Core" )
53- ) ,
54- J . identifier ( "Tuple" )
55- ) ,
56- [ J . literal ( elem ( tuple , 0 ) ) , J . literal ( elem ( tuple , 1 ) ) ]
57- )
51+
52+ assert js_ast ==
53+ J . new_expression (
54+ J . member_expression (
55+ J . member_expression (
56+ J . identifier ( "ElixirScript" ) ,
57+ J . identifier ( "Core" )
58+ ) ,
59+ J . identifier ( "Tuple" )
60+ ) ,
61+ [ J . literal ( elem ( tuple , 0 ) ) , J . literal ( elem ( tuple , 1 ) ) ]
62+ )
5863 end
5964 end
6065
@@ -72,16 +77,16 @@ defmodule ElixirScript.Translate.Forms.Test do
7277 end
7378
7479 property "local function call translates to local JavaScript function call" , % { state: state } do
75- check all func <- StreamData . filter ( StreamData . unquoted_atom ( ) , fn ( x ) -> not ( x in [ :fn ] ) end ) ,
80+ check all func <- StreamData . filter ( StreamData . unquoted_atom ( ) , fn x -> x not in [ :fn ] end ) ,
7681 params <- StreamData . list_of ( StreamData . binary ( ) ) do
77-
7882 ast = { func , [ ] , params }
7983
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
84+ str_func =
85+ if func in ElixirScript.Translate.Identifier . js_reserved_words ( ) do
86+ "__#{ to_string ( func ) } __"
87+ else
88+ to_string ( func )
89+ end
8590
8691 { js_ast , _ } = Form . compile ( ast , state )
8792 assert js_ast . type == "CallExpression"
@@ -99,15 +104,15 @@ defmodule ElixirScript.Translate.Forms.Test do
99104 property "super function call translates to local JavaScript function call" do
100105 check all func <- StreamData . unquoted_atom ( ) ,
101106 params <- StreamData . list_of ( StreamData . binary ( ) ) do
102-
103107 ast = { :super , [ ] , [ { :def , func } ] ++ params }
104108 state = % { function: { func , nil } , vars: % { } }
105109
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
110+ str_func =
111+ if func in ElixirScript.Translate.Identifier . js_reserved_words ( ) do
112+ "__#{ to_string ( func ) } __"
113+ else
114+ to_string ( func )
115+ end
111116
112117 { js_ast , _ } = Form . compile ( ast , state )
113118 assert js_ast . type == "CallExpression"
@@ -125,10 +130,24 @@ defmodule ElixirScript.Translate.Forms.Test do
125130 test "module" , % { state: state } do
126131 ast = IO
127132
133+ ElixirScript.State . put_module ( state . pid , IO , % { } )
134+
128135 { js_ast , _ } = Form . compile ( ast , state )
129136 assert js_ast == % ESTree.Identifier { loc: nil , name: "$IO$" , type: "Identifier" }
130137 end
131138
139+ test "unknown module" , % { state: state } do
140+ ast = Enum
141+
142+ { js_ast , _ } = Form . compile ( ast , state )
143+
144+ assert js_ast == % ESTree.ObjectExpression {
145+ loc: nil ,
146+ properties: [ ] ,
147+ type: "ObjectExpression"
148+ }
149+ end
150+
132151 test "function returning an array" do
133152 ast = { :fn , [ ] , [ { :foo , [ ] , [ ] , [ 1 , 2 , 3 ] } ] }
134153 state = % { function: { :something , nil } }
@@ -137,37 +156,48 @@ defmodule ElixirScript.Translate.Forms.Test do
137156
138157 return_statement = Enum . at ( Enum . at ( hd ( js_ast . body . body ) . body . body , 1 ) . consequent . body , 1 )
139158
140- assert return_statement . argument == J . array_expression ( [
141- J . literal ( 1 ) ,
142- J . literal ( 2 ) ,
143- J . literal ( 3 )
144- ] )
159+ assert return_statement . argument ==
160+ J . array_expression ( [
161+ J . literal ( 1 ) ,
162+ J . literal ( 2 ) ,
163+ J . literal ( 3 )
164+ ] )
145165 end
146166
147167 test "calling field on field" do
148- ast = { { :. , [ line: 16 ] ,
149- [ { { :. , [ line: 16 ] , [ { :map , [ line: 16 ] , nil } , :token_count ] } , [ line: 16 ] ,
150- [ ] } , :toLocaleString ] } , [ line: 16 ] , [ ] }
168+ ast =
169+ {
170+ { :. , [ line: 16 ] , [
171+ { { :. , [ line: 16 ] , [ { :map , [ line: 16 ] , nil } , :token_count ] } , [ line: 16 ] , [ ] } ,
172+ :toLocaleString
173+ ] } ,
174+ [ line: 16 ] ,
175+ [ ]
176+ }
151177
152178 state = % { function: { :something , nil } , vars: % { } }
153179
154180 { js_ast , _ } = Form . compile ( ast , state )
155181
156- assert js_ast == Helpers . call (
157- ElixirScript.Translate.Forms.JS . call_property ( ) ,
158- [
159- Helpers . call (
160- ElixirScript.Translate.Forms.JS . call_property ( ) ,
161- [ J . identifier ( "map" ) , J . literal ( "token_count" ) ]
162- ) ,
163- J . literal ( "toLocaleString" )
164- ]
165- )
182+ assert js_ast ==
183+ Helpers . call ( ElixirScript.Translate.Forms.JS . call_property ( ) , [
184+ Helpers . call ( ElixirScript.Translate.Forms.JS . call_property ( ) , [
185+ J . identifier ( "map" ) ,
186+ J . literal ( "token_count" )
187+ ] ) ,
188+ J . literal ( "toLocaleString" )
189+ ] )
166190 end
167191
168192 test "make sure counter used in guard" , % { state: state } do
169- state = Map . merge ( state , % { anonymous_fn: false , function: { :filter_names_in_guards , nil } , in_guard: true ,
170- module: Integration , vars: % { "has__qmark__" => 0 } } )
193+ state =
194+ Map . merge ( state , % {
195+ anonymous_fn: false ,
196+ function: { :filter_names_in_guards , nil } ,
197+ in_guard: true ,
198+ module: Integration ,
199+ vars: % { "has__qmark__" => 0 }
200+ } )
171201
172202 ast = { { :. , [ ] , [ :erlang , :== ] } , [ line: 29 ] , [ { :has? , [ line: 29 ] , nil } , 5 ] }
173203
@@ -177,9 +207,10 @@ defmodule ElixirScript.Translate.Forms.Test do
177207
178208 test "multi bind" , % { state: state } do
179209 ast =
180- { := , [ line: 35 ] ,
181- [ [ { :| , [ line: 35 ] , [ { :a , [ line: 35 ] , nil } , { :_ , [ line: 35 ] , nil } ] } ] ,
182- { := , [ line: 35 ] , [ { :b , [ line: 35 ] , nil } , [ 1 , 2 , 3 , 4 , 5 ] ] } ] }
210+ { := , [ line: 35 ] , [
211+ [ { :| , [ line: 35 ] , [ { :a , [ line: 35 ] , nil } , { :_ , [ line: 35 ] , nil } ] } ] ,
212+ { := , [ line: 35 ] , [ { :b , [ line: 35 ] , nil } , [ 1 , 2 , 3 , 4 , 5 ] ] }
213+ ] }
183214
184215 { js_ast , _ } = Form . compile ( ast , state )
185216
0 commit comments