Made map IndexSet and IndexGet consistent. (#179)

This commit is contained in:
earncef 2019-04-09 04:54:02 +02:00 committed by Daniel
parent 8eabc23a45
commit 1a0c06b688
2 changed files with 23 additions and 2 deletions

View file

@ -76,13 +76,13 @@ func (o *Map) Equals(x Object) bool {
// IndexGet returns the value for the given key.
func (o *Map) IndexGet(index Object) (res Object, err error) {
strIdx, ok := index.(*String)
strIdx, ok := ToString(index)
if !ok {
err = ErrInvalidIndexType
return
}
val, ok := o.Value[strIdx.Value]
val, ok := o.Value[strIdx]
if !ok {
val = UndefinedValue
}

21
objects/map_test.go Normal file
View file

@ -0,0 +1,21 @@
package objects_test
import (
"testing"
"github.com/d5/tengo/assert"
"github.com/d5/tengo/objects"
)
func TestMap_Index(t *testing.T) {
m := &objects.Map{Value: make(map[string]objects.Object)}
k := &objects.Int{Value: 1}
v := &objects.String{Value: "abcdef"}
err := m.IndexSet(k, v)
assert.NoError(t, err)
res, err := m.IndexGet(k)
assert.NoError(t, err)
assert.Equal(t, v, res)
}