github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/tests/map_js_test.go (about) 1 //go:build js && !wasm 2 // +build js,!wasm 3 4 package tests 5 6 import ( 7 "testing" 8 9 "github.com/gopherjs/gopherjs/js" 10 ) 11 12 func Test_MapWrapper(t *testing.T) { 13 // This tests that various map types, and a map as a function argument and return, 14 // wrap and unwrap correctly. 15 type Dummy struct { 16 Msg string 17 } 18 19 type StructWithMap struct { 20 StringMap map[string]string 21 IntMap map[int]int 22 DummyMap map[string]*Dummy 23 MapFunc func(map[string]string) map[string]string 24 } 25 26 dummyMap := map[string]*Dummy{"key": {Msg: "value"}} 27 swm := &StructWithMap{ 28 StringMap: map[string]string{"key": "value"}, 29 IntMap: map[int]int{1: 2}, 30 DummyMap: dummyMap, 31 MapFunc: func(m map[string]string) map[string]string { 32 return m 33 }, 34 } 35 swmWrapper := js.MakeFullWrapper(swm) 36 swmUnwrapped := swmWrapper.Interface().(*StructWithMap) 37 mapFuncArg := map[string]string{"key2": "value2"} 38 39 if got := swmWrapper.Get("StringMap").Get("key").String(); got != swm.StringMap["key"] { 40 t.Errorf("StringMap Got: %s, Want: %s", got, swm.StringMap["key"]) 41 } 42 if got := swmWrapper.Get("IntMap").Get("1").Int(); got != swm.IntMap[1] { 43 t.Errorf("IntMap Got: %d, Want: %d", got, swm.IntMap[1]) 44 } 45 if got := swmWrapper.Get("DummyMap").Get("key").Get("Msg").String(); got != swm.DummyMap["key"].Msg { 46 t.Errorf("DummyMap Got: %s, Want: %s", got, swm.DummyMap["key"].Msg) 47 } 48 if got := swmWrapper.Call("MapFunc", mapFuncArg).Get("key2").String(); got != mapFuncArg["key2"] { 49 t.Errorf("MapFunc Got: %s, Want: %s", got, mapFuncArg["key2"]) 50 } 51 52 if got := swmUnwrapped.StringMap["key"]; got != swm.StringMap["key"] { 53 t.Errorf("Unwrapped StringMap Got: %s, Want: %s", got, swm.StringMap["key"]) 54 } 55 if got := swmUnwrapped.IntMap[1]; got != swm.IntMap[1] { 56 t.Errorf("Unwrapped IntMap Got: %d, Want: %d", got, swm.IntMap[1]) 57 } 58 if got := swmUnwrapped.DummyMap["key"].Msg; got != swm.DummyMap["key"].Msg { 59 t.Errorf("Unwrapped DummyMap Got: %s, Want: %s", got, swm.DummyMap["key"].Msg) 60 } 61 if got := swmUnwrapped.MapFunc(mapFuncArg)["key2"]; got != swm.MapFunc(mapFuncArg)["key2"] { 62 t.Errorf("Unwrapped MapFunc Got: %s, Want: %s", got, swm.MapFunc(mapFuncArg)["key2"]) 63 } 64 } 65 66 func Test_MapStructObjectWrapper(t *testing.T) { 67 // This tests that maps work as expected when wrapping a Struct with js.Object field containing a map. 68 // js.Object fields' content should be passed to JS, so this is basically doubly-wrapping a map in two structs. 69 70 stringMap := map[string]string{"key": "value"} 71 72 // You cannot wrap a map directly, so put it in a struct. 73 type StructWithMap struct { 74 Map map[string]string 75 } 76 77 swm := &StructWithMap{Map: stringMap} 78 swmWrapped := js.MakeFullWrapper(swm) 79 80 // Now that map is wrapped in a struct, wrap the js.object in *another* struct. 81 type StructWithObject struct { 82 Wrappedswm *js.Object // This Object contains StructWithMap. 83 } 84 85 swo := &StructWithObject{Wrappedswm: swmWrapped} 86 swoWrapper := js.MakeFullWrapper(swo) 87 swmUnwrapped := swoWrapper.Interface().(*StructWithObject) 88 89 // Using "Get("Map")" shows that the first wrapping was unchanged. 90 if got := swoWrapper.Get("Wrappedswm").Get("Map").Get("key").String(); got != stringMap["key"] { 91 t.Errorf("Wrapped Wrappedswm value Got: %s, Want: %s", got, stringMap["key"]) 92 } 93 94 if got := swmUnwrapped.Wrappedswm.Get("Map").Get("key").String(); got != stringMap["key"] { 95 t.Errorf("Unwrapped Wrappedswm value Got: %s, Want: %s", got, stringMap["key"]) 96 } 97 } 98 99 func Test_MapEmbeddedObject(t *testing.T) { 100 o := js.Global.Get("JSON").Call("parse", `{"props": {"one": 1, "two": 2}}`) 101 102 type data struct { 103 *js.Object 104 Props map[string]int `js:"props"` 105 } 106 107 d := data{Object: o} 108 if d.Props["one"] != 1 { 109 t.Errorf("key 'one' value Got: %d, Want: %d", d.Props["one"], 1) 110 } 111 if d.Props["two"] != 2 { 112 t.Errorf("key 'two' value Got: %d, Want: %d", d.Props["two"], 2) 113 } 114 }