diff options
author | Damian Gryski <[email protected]> | 2023-03-18 10:10:20 -0700 |
---|---|---|
committer | Ron Evans <[email protected]> | 2023-03-25 22:32:29 +0100 |
commit | 6cb7f29d9b14e3b84bda857da2256388d1a44914 (patch) | |
tree | dbcd0a0f791bbbf704afc674a0a817857c893e20 | |
parent | 21527353f7c4e096d0646fff68e0aae5e2540559 (diff) | |
download | tinygo-6cb7f29d9b14e3b84bda857da2256388d1a44914.tar.gz tinygo-6cb7f29d9b14e3b84bda857da2256388d1a44914.zip |
reflect: add tests for map interface lookup fixes
-rw-r--r-- | src/reflect/value_test.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/reflect/value_test.go b/src/reflect/value_test.go index 56a6e54c9..618088b99 100644 --- a/src/reflect/value_test.go +++ b/src/reflect/value_test.go @@ -133,6 +133,49 @@ func TestTinyMap(t *testing.T) { if six.Interface().(int) != 6 { t.Errorf("m[hello, 4]=%v, want 6", six) } + + // make sure we can look up interface keys with reflection + type unmarshalerText struct { + A, B string + } + + ut := make(map[unmarshalerText]bool) + + refut := ValueOf(ut) + + // put in a key with the compiler + ut[unmarshalerText{"x", "y"}] = true + + // make sure we can get it out + v2 := refut.MapIndex(ValueOf(unmarshalerText{"x", "y"})) + if !v2.IsValid() || !v2.Bool() { + t.Errorf("Failed to look up map struct key with refection") + } + + // put in a key with reflection + refut.SetMapIndex(ValueOf(unmarshalerText{"y", "z"}), ValueOf(true)) + + // make sure we can get it out with the compiler + if !ut[unmarshalerText{"y", "z"}] { + t.Errorf("Failed to look up up reflect-set map key with compiler") + } + + utKeys := refut.MapKeys() + + // make sure keys extracted via reflection have the correct ype + if _, ok := utKeys[0].Interface().(unmarshalerText); !ok { + t.Errorf("Map keys via MapKeys() have wrong type: %v", utKeys[0].Type().String()) + } + + // and via iteration + + utIter := refut.MapRange() + utIter.Next() + utIterKey := utIter.Key() + + if _, ok := utIterKey.Interface().(unmarshalerText); !ok { + t.Errorf("Map keys via MapIter() have wrong type: %v", utIterKey.Type().String()) + } } func TestTinySlice(t *testing.T) { |