MCPcopy Index your code
hub / github.com/gopherjs/gopherjs / Test_MapStructObjectWrapper

Function Test_MapStructObjectWrapper

tests/map_js_test.go:65–96  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

63}
64
65func Test_MapStructObjectWrapper(t *testing.T) {
66 // This tests that maps work as expected when wrapping a Struct with js.Object field containing a map.
67 // js.Object fields' content should be passed to JS, so this is basically doubly-wrapping a map in two structs.
68
69 stringMap := map[string]string{"key": "value"}
70
71 // You cannot wrap a map directly, so put it in a struct.
72 type StructWithMap struct {
73 Map map[string]string
74 }
75
76 swm := &StructWithMap{Map: stringMap}
77 swmWrapped := js.MakeFullWrapper(swm)
78
79 // Now that map is wrapped in a struct, wrap the js.object in *another* struct.
80 type StructWithObject struct {
81 Wrappedswm *js.Object // This Object contains StructWithMap.
82 }
83
84 swo := &StructWithObject{Wrappedswm: swmWrapped}
85 swoWrapper := js.MakeFullWrapper(swo)
86 swmUnwrapped := swoWrapper.Interface().(*StructWithObject)
87
88 // Using "Get("Map")" shows that the first wrapping was unchanged.
89 if got := swoWrapper.Get("Wrappedswm").Get("Map").Get("key").String(); got != stringMap["key"] {
90 t.Errorf("Wrapped Wrappedswm value Got: %s, Want: %s", got, stringMap["key"])
91 }
92
93 if got := swmUnwrapped.Wrappedswm.Get("Map").Get("key").String(); got != stringMap["key"] {
94 t.Errorf("Unwrapped Wrappedswm value Got: %s, Want: %s", got, stringMap["key"])
95 }
96}
97
98func Test_MapEmbeddedObject(t *testing.T) {
99 o := js.Global.Get("JSON").Call("parse", `{"props": {"one": 1, "two": 2}}`)

Callers

nothing calls this directly

Calls 4

MakeFullWrapperFunction · 0.92
StringMethod · 0.65
GetMethod · 0.65
InterfaceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…