| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package datatest |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
type TestHandler func(t *testing.T, tc map[string]any) |
| 17 |
|
| 18 |
|
| 19 |
type TestRunner struct { |
| 20 |
handlers map[string]TestHandler |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
func NewTestRunner() *TestRunner { |
| 25 |
return &TestRunner{ |
| 26 |
handlers: make(map[string]TestHandler), |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
func (r *TestRunner) RegisterHandler(testType string, handler TestHandler) { |
| 32 |
r.handlers[testType] = handler |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func (r *TestRunner) RunWithCases(t *testing.T, cases []map[string]any) { |
| 37 |
t.Helper() |
| 38 |
|
| 39 |
for _, tc := range cases { |
| 40 |
tc := tc |
| 41 |
|
| 42 |
|
| 43 |
name, _ := tc["name"].(string) |
| 44 |
if name == "" { |
| 45 |
name = "unnamed" |
| 46 |
} |
| 47 |
|
| 48 |
testType, _ := tc["type"].(string) |
| 49 |
if testType == "" { |
| 50 |
t.Fatalf("Test case %q missing 'type' field", name) |
| 51 |
} |
| 52 |
|
| 53 |
t.Run(name, func(t *testing.T) { |
| 54 |
handler, ok := r.handlers[testType] |
| 55 |
if !ok { |
| 56 |
t.Fatalf("Unknown test type: %s", testType) |
| 57 |
} |
| 58 |
handler(t, tc) |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
func RunTestCases(t *testing.T, loadFunc func() ([]map[string]any, error), handlers map[string]TestHandler) { |
| 66 |
t.Helper() |
| 67 |
|
| 68 |
cases, err := loadFunc() |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("Failed to load test cases: %v", err) |
| 71 |
} |
| 72 |
|
| 73 |
runner := NewTestRunner() |
| 74 |
for testType, handler := range handlers { |
| 75 |
runner.RegisterHandler(testType, handler) |
| 76 |
} |
| 77 |
runner.RunWithCases(t, cases) |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func GetString(tc map[string]any, key string) (string, bool) { |
| 82 |
val, ok := tc[key] |
| 83 |
if !ok { |
| 84 |
return "", false |
| 85 |
} |
| 86 |
str, ok := val.(string) |
| 87 |
return str, ok |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func GetInt(tc map[string]any, key string) (int, bool) { |
| 92 |
val, ok := tc[key] |
| 93 |
if !ok { |
| 94 |
return 0, false |
| 95 |
} |
| 96 |
intVal, ok := val.(int) |
| 97 |
return intVal, ok |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func GetBool(tc map[string]any, key string) (bool, bool) { |
| 102 |
val, ok := tc[key] |
| 103 |
if !ok { |
| 104 |
return false, false |
| 105 |
} |
| 106 |
boolVal, ok := val.(bool) |
| 107 |
return boolVal, ok |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
func GetSlice(tc map[string]any, key string) ([]any, bool) { |
| 112 |
val, ok := tc[key] |
| 113 |
if !ok { |
| 114 |
return nil, false |
| 115 |
} |
| 116 |
slice, ok := val.([]any) |
| 117 |
return slice, ok |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func GetMap(tc map[string]any, key string) (map[string]any, bool) { |
| 122 |
val, ok := tc[key] |
| 123 |
if !ok { |
| 124 |
return nil, false |
| 125 |
} |
| 126 |
m, ok := val.(map[string]any) |
| 127 |
return m, ok |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func RequireString(t *testing.T, tc map[string]any, key string) string { |
| 132 |
t.Helper() |
| 133 |
val, ok := GetString(tc, key) |
| 134 |
if !ok { |
| 135 |
t.Fatalf("Required field %q missing or not a string", key) |
| 136 |
} |
| 137 |
return val |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
func RequireInt(t *testing.T, tc map[string]any, key string) int { |
| 142 |
t.Helper() |
| 143 |
val, ok := GetInt(tc, key) |
| 144 |
if !ok { |
| 145 |
t.Fatalf("Required field %q missing or not an int", key) |
| 146 |
} |
| 147 |
return val |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
func RequireSlice(t *testing.T, tc map[string]any, key string) []any { |
| 152 |
t.Helper() |
| 153 |
val, ok := GetSlice(tc, key) |
| 154 |
if !ok { |
| 155 |
t.Fatalf("Required field %q missing or not a slice", key) |
| 156 |
} |
| 157 |
return val |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
func UnmarshalTestCase(tc map[string]any, target any) error { |
| 162 |
return UnmarshalStruct(target, tc) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
func AssertEqual(t *testing.T, expected, actual any) { |
| 167 |
t.Helper() |
| 168 |
if fmt.Sprintf("%v", expected) != fmt.Sprintf("%v", actual) { |
| 169 |
t.Errorf("Expected:\n%v\nGot:\n%v", expected, actual) |
| 170 |
} |
| 171 |
} |