elvish/pkg/eval/vars/element_test.go

90 lines
1.8 KiB
Go
Raw Normal View History

2019-04-20 02:44:11 +08:00
package vars
import (
"testing"
"src.elv.sh/pkg/eval/vals"
2019-04-20 02:44:11 +08:00
)
var elementTests = []struct {
name string
oldContainer any
indices []any
elemValue any
newContainer any
2019-04-20 02:44:11 +08:00
}{
{
"single level",
vals.MakeMap("k1", "v1", "k2", "v2"),
[]any{"k1"}, "new v1",
2019-04-20 02:44:11 +08:00
vals.MakeMap("k1", "new v1", "k2", "v2"),
},
{
"multi level",
vals.MakeMap(
"k1", vals.MakeMap("k1a", "v1a", "k1b", "v1b"), "k2", "v2"),
[]any{"k1", "k1a"}, "new v1a",
2019-04-20 02:44:11 +08:00
vals.MakeMap(
"k1", vals.MakeMap("k1a", "new v1a", "k1b", "v1b"), "k2", "v2"),
},
}
func TestElement(t *testing.T) {
for _, test := range elementTests {
t.Run(test.name, func(t *testing.T) {
m := test.oldContainer
2020-10-06 02:35:52 +08:00
elemVar, err := MakeElement(FromPtr(&m), test.indices)
2019-04-20 02:44:11 +08:00
if err != nil {
t.Errorf("MakeElement -> error %v, want nil", err)
}
elemVar.Set(test.elemValue)
if !vals.Equal(m, test.newContainer) {
t.Errorf("Value after Set is %v, want %v", m, test.newContainer)
}
if elemVar.Get() != test.elemValue {
t.Errorf("elemVar.Get() -> %v, want %v",
elemVar.Get(), test.elemValue)
}
})
}
}
var delElementTests = []struct {
name string
oldContainer any
indices []any
newContainer any
2019-04-20 02:44:11 +08:00
}{
{
"single level",
vals.MakeMap("k1", "v1", "k2", "v2"),
[]any{"k1"},
2019-04-20 02:44:11 +08:00
vals.MakeMap("k2", "v2"),
},
{
"multi level",
vals.MakeMap(
"k1", vals.MakeMap("k1a", "v1a", "k1b", "v1b"), "k2", "v2"),
[]any{"k1", "k1a"},
2019-04-20 02:44:11 +08:00
vals.MakeMap("k1", vals.MakeMap("k1b", "v1b"), "k2", "v2"),
},
}
func TestDelElement(t *testing.T) {
for _, test := range delElementTests {
t.Run(test.name, func(t *testing.T) {
m := test.oldContainer
2020-10-06 02:35:52 +08:00
DelElement(FromPtr(&m), test.indices)
2019-04-20 02:44:11 +08:00
if !vals.Equal(m, test.newContainer) {
t.Errorf("After deleting, map is %v, want %v",
vals.ReprPlain(m),
vals.ReprPlain(test.newContainer))
2019-04-20 02:44:11 +08:00
}
})
}
}