github.com/elves/elvish@v0.15.0/pkg/eval/vals/assoc_test.go (about) 1 package vals 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/elves/elvish/pkg/eval/errs" 8 . "github.com/elves/elvish/pkg/tt" 9 ) 10 11 type customAssocer struct{} 12 13 var errCustomAssoc = errors.New("custom assoc error") 14 15 func (a customAssocer) Assoc(k, v interface{}) (interface{}, error) { 16 return "custom result", errCustomAssoc 17 } 18 19 func TestAssoc(t *testing.T) { 20 Test(t, Fn("Assoc", Assoc), Table{ 21 Args("0123", "0", "foo").Rets("foo123", nil), 22 Args("0123", "1:3", "bar").Rets("0bar3", nil), 23 Args("0123", "1:3", 12).Rets(nil, errReplacementMustBeString), 24 Args("0123", "x", "y").Rets(nil, errIndexMustBeInteger), 25 26 Args(MakeList("0", "1", "2", "3"), "0", "foo").Rets( 27 Eq(MakeList("foo", "1", "2", "3")), nil), 28 Args(MakeList("0", "1", "2", "3"), 0.0, "foo").Rets( 29 Eq(MakeList("foo", "1", "2", "3")), nil), 30 Args(MakeList("0"), MakeList("0"), "1").Rets(nil, errIndexMustBeInteger), 31 Args(MakeList("0"), "1", "x").Rets(nil, errs.OutOfRange{ 32 What: "index here", ValidLow: "0", ValidHigh: "0", Actual: "1"}), 33 // TODO: Support list assoc with slice 34 Args(MakeList("0", "1", "2", "3"), "1:3", MakeList("foo")).Rets( 35 nil, errAssocWithSlice), 36 37 Args(MakeMap("k", "v", "k2", "v2"), "k", "newv").Rets( 38 Eq(MakeMap("k", "newv", "k2", "v2")), nil), 39 Args(MakeMap("k", "v"), "k2", "v2").Rets( 40 Eq(MakeMap("k", "v", "k2", "v2")), nil), 41 42 Args(customAssocer{}, "x", "y").Rets("custom result", errCustomAssoc), 43 44 Args(struct{}{}, "x", "y").Rets(nil, errAssocUnsupported), 45 }) 46 }