| 46 | } |
| 47 | |
| 48 | func TestCallLocalMethod(t *testing.T) { |
| 49 | sample := &callLocalMethodSample{} |
| 50 | cases := []struct { |
| 51 | name string |
| 52 | method string |
| 53 | in []reflect.Value |
| 54 | assertErr func(t *testing.T, err error) |
| 55 | assertOut func(t *testing.T, out []reflect.Value) |
| 56 | }{ |
| 57 | { |
| 58 | name: "call success", |
| 59 | method: "Sum", |
| 60 | in: []reflect.Value{reflect.ValueOf(sample), reflect.ValueOf(1), reflect.ValueOf(2)}, |
| 61 | assertErr: func(t *testing.T, err error) { |
| 62 | assert.NoError(t, err) |
| 63 | }, |
| 64 | assertOut: func(t *testing.T, out []reflect.Value) { |
| 65 | assert.Len(t, out, 1) |
| 66 | assert.Equal(t, 3, out[0].Interface()) |
| 67 | }, |
| 68 | }, |
| 69 | { |
| 70 | name: "panic with error", |
| 71 | method: "PanicError", |
| 72 | in: []reflect.Value{reflect.ValueOf(sample)}, |
| 73 | assertErr: func(t *testing.T, err error) { |
| 74 | assert.EqualError(t, err, "boom") |
| 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | name: "panic with string", |
| 79 | method: "PanicString", |
| 80 | in: []reflect.Value{reflect.ValueOf(sample)}, |
| 81 | assertErr: func(t *testing.T, err error) { |
| 82 | assert.EqualError(t, err, "boom str") |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | name: "panic with unknown type", |
| 87 | method: "PanicUnknown", |
| 88 | in: []reflect.Value{reflect.ValueOf(sample)}, |
| 89 | assertErr: func(t *testing.T, err error) { |
| 90 | assert.EqualError(t, err, "invoke function error, unknow exception: 123") |
| 91 | }, |
| 92 | }, |
| 93 | } |
| 94 | |
| 95 | for _, tt := range cases { |
| 96 | t.Run(tt.name, func(t *testing.T) { |
| 97 | m, ok := reflect.TypeOf(sample).MethodByName(tt.method) |
| 98 | if !ok { |
| 99 | t.Fatalf("method %s not found", tt.method) |
| 100 | } |
| 101 | out, err := callLocalMethod(m, tt.in) |
| 102 | if tt.assertErr != nil { |
| 103 | tt.assertErr(t, err) |
| 104 | } |
| 105 | if tt.assertOut != nil { |