github.com/elves/elvish@v0.15.0/pkg/eval/vals/concat_test.go (about) 1 package vals 2 3 import ( 4 "errors" 5 "testing" 6 7 . "github.com/elves/elvish/pkg/tt" 8 ) 9 10 // An implementation for Concatter that accepts strings, returns a special 11 // error when rhs is a float64, and returns ErrConcatNotImplemented when rhs is 12 // of other types. 13 type concatter struct{} 14 15 func (concatter) Concat(rhs interface{}) (interface{}, error) { 16 switch rhs := rhs.(type) { 17 case string: 18 return "concatter " + rhs, nil 19 case float64: 20 return nil, errors.New("float64 is bad") 21 default: 22 return nil, ErrConcatNotImplemented 23 } 24 } 25 26 // An implementation of RConcatter that accepts all types. 27 type rconcatter struct{} 28 29 func (rconcatter) RConcat(lhs interface{}) (interface{}, error) { 30 return "rconcatter", nil 31 } 32 33 func TestConcat(t *testing.T) { 34 Test(t, Fn("Concat", Concat), Table{ 35 Args("foo", "bar").Rets("foobar", nil), 36 Args("foo", 2.0).Rets("foo2", nil), 37 Args(2.0, "foo").Rets("2foo", nil), 38 39 // LHS implements Concatter and succeeds 40 Args(concatter{}, "bar").Rets("concatter bar", nil), 41 // LHS implements Concatter but returns ErrConcatNotImplemented; RHS 42 // does not implement RConcatter 43 Args(concatter{}, 12).Rets(nil, cannotConcat{"!!vals.concatter", "!!int"}), 44 // LHS implements Concatter but returns another error 45 Args(concatter{}, 12.0).Rets(nil, errors.New("float64 is bad")), 46 47 // LHS does not implement Concatter but RHS implements RConcatter 48 Args(12, rconcatter{}).Rets("rconcatter", nil), 49 }) 50 }