2019-04-20 02:44:11 +08:00
|
|
|
package vars
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/vals"
|
2019-04-20 02:44:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var elementTests = []struct {
|
|
|
|
name string
|
2022-03-20 23:50:25 +08:00
|
|
|
oldContainer any
|
|
|
|
indices []any
|
|
|
|
elemValue any
|
|
|
|
newContainer any
|
2019-04-20 02:44:11 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"single level",
|
|
|
|
vals.MakeMap("k1", "v1", "k2", "v2"),
|
2022-03-20 23:50:25 +08:00
|
|
|
[]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"),
|
2022-03-20 23:50:25 +08:00
|
|
|
[]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
|
2022-03-20 23:50:25 +08:00
|
|
|
oldContainer any
|
|
|
|
indices []any
|
|
|
|
newContainer any
|
2019-04-20 02:44:11 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"single level",
|
|
|
|
vals.MakeMap("k1", "v1", "k2", "v2"),
|
2022-03-20 23:50:25 +08:00
|
|
|
[]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"),
|
2022-03-20 23:50:25 +08:00
|
|
|
[]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",
|
2022-01-01 05:05:07 +08:00
|
|
|
vals.ReprPlain(m),
|
|
|
|
vals.ReprPlain(test.newContainer))
|
2019-04-20 02:44:11 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|