-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellformatter_test.go
131 lines (126 loc) · 3.24 KB
/
cellformatter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package retable
import (
"context"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestReflectCellFormatterFunc(t *testing.T) {
type args struct {
function any
rawResult bool
}
tests := []struct {
name string
args args
wantValType reflect.Type
wantErr bool
}{
{
name: "func(int) string",
args: args{
function: func(arg int) string { return fmt.Sprint(arg) },
rawResult: false,
},
wantValType: reflect.TypeOf(int(0)),
},
{
name: "func(int) (string, error)",
args: args{
function: func(arg int) (string, error) { return fmt.Sprint(arg), nil },
rawResult: false,
},
wantValType: reflect.TypeOf(int(0)),
},
{
name: "func(context.Context, int) string",
args: args{
function: func(_ context.Context, arg int) string { return fmt.Sprint(arg) },
rawResult: false,
},
wantValType: reflect.TypeOf(int(0)),
},
{
name: "func(context.Context, int) (string, error)",
args: args{
function: func(_ context.Context, arg int) (string, error) { return fmt.Sprint(arg), nil },
rawResult: false,
},
wantValType: reflect.TypeOf(int(0)),
},
{
name: "func() string",
args: args{
function: func() string { return "666" },
rawResult: false,
},
wantValType: nil,
},
{
name: "func() (string, error)",
args: args{
function: func() (string, error) { return "666", nil },
rawResult: false,
},
wantValType: nil,
},
// Invalid
{
name: "nil func",
args: args{
function: nil,
rawResult: false,
},
wantErr: true,
},
{
name: "func(int)",
args: args{
function: func(int) {},
rawResult: false,
},
wantErr: true,
},
{
name: "func(int) (error, string)",
args: args{
function: func(int) (error, string) { return nil, "" },
rawResult: false,
},
wantErr: true,
},
{
name: "func(context.Context, int) (error, string)",
args: args{
function: func(context.Context, int) (error, string) { return nil, "" },
rawResult: false,
},
wantErr: true,
},
{
name: "func(context.Context, int) (string, bool, error)",
args: args{
function: func(context.Context, int) (string, bool, error) { return "", false, nil },
rawResult: false,
},
wantErr: true,
},
}
view1int := &AnyValuesView{Cols: []string{"Col A"}, Rows: [][]any{{666}}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotFormatter, gotValType, err := ReflectCellFormatterFunc(tt.args.function, tt.args.rawResult)
require.Equalf(t, tt.wantErr, err != nil, "ReflectCellFormatterFunc() error = %v, wantErr %v", err, tt.wantErr)
if err != nil {
return
}
require.NotNil(t, gotFormatter, "ReflectCellFormatterFunc() gotFormatter = <nil>")
require.Equalf(t, tt.wantValType, gotValType, "ReflectCellFormatterFunc() gotValType = %v, want %v", gotValType, tt.wantValType)
gotStr, gotRaw, err := gotFormatter(context.Background(), view1int, 0, 0)
require.Equalf(t, tt.args.rawResult, gotRaw, "ReflectCellFormatterFunc() raw = %v, want %v", gotRaw, tt.args.rawResult)
require.Equalf(t, "666", gotStr, "ReflectCellFormatterFunc() str = %v, want %v", gotStr, "666")
require.NoErrorf(t, err, "gotFormatter() error = %v", err)
})
}
}