1+ defmodule ElixirScript.Translate.Forms.Map.Test do
2+ use ExUnit.Case
3+ alias ElixirScript.Translate.Form
4+ alias ESTree.Tools.Builder , as: J
5+
6+ test "map with atom key" do
7+ properties = [ a: 1 ]
8+ ast = { :%{} , [ ] , properties }
9+ state = % { }
10+
11+ { js_ast , _ } = Form . compile ( ast , state )
12+ assert js_ast == J . object_expression ( [
13+ J . property (
14+ J . call_expression (
15+ J . member_expression (
16+ J . identifier ( "Symbol" ) ,
17+ J . identifier ( "for" )
18+ ) ,
19+ [ J . literal ( :a ) ]
20+ ) ,
21+ J . literal ( 1 ) ,
22+ :init , false , false , true
23+ )
24+ ] )
25+ end
26+
27+ test "map with string key" do
28+ properties = [ { "a" , 1 } ]
29+ ast = { :%{} , [ ] , properties }
30+ state = % { }
31+
32+ { js_ast , _ } = Form . compile ( ast , state )
33+ assert js_ast == J . object_expression ( [
34+ J . property (
35+ J . identifier ( "a" ) ,
36+ J . literal ( 1 )
37+ )
38+ ] )
39+ end
40+
41+
42+ test "map update" do
43+ properties = [ { "a" , 1 } ]
44+ map_ast = { :%{} , [ ] , properties }
45+ new_values = [ { "a" , 2 } ]
46+ state = % { }
47+
48+ ast = { :%{} , [ ] , [ { :| , [ ] , [ map_ast , new_values ] } ] }
49+
50+ { js_ast , _ } = Form . compile ( ast , state )
51+ assert js_ast == J . call_expression (
52+ J . member_expression (
53+ J . identifier ( "Object" ) ,
54+ J . identifier ( "assign" )
55+ ) ,
56+ [
57+ J . object_expression ( [ ] ) ,
58+ J . object_expression ( [
59+ J . property (
60+ J . identifier ( "a" ) ,
61+ J . literal ( 1 )
62+ )
63+ ] ) ,
64+ J . object_expression ( [
65+ J . property (
66+ J . identifier ( "a" ) ,
67+ J . literal ( 2 )
68+ )
69+ ] )
70+ ]
71+ )
72+ end
73+
74+ end
0 commit comments