Debian Codegraph @

internal/testutil/datatest/runner.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Test runner for data-driven test execution.
5 // Manages test handlers and orchestrates YAML-based test execution.
6
7 package datatest
8
9 import (
10 "fmt"
11 "testing"
12 )
13
14 // TestHandler is a function that runs a single test case.
15 // The test case is passed as a map[string]interface{}.
16 type TestHandler func(t *testing.T, tc map[string]any)
17
18 // TestRunner manages test execution with handlers for different test types.
19 type TestRunner struct {
20 handlers map[string]TestHandler
21 }
22
23 // NewTestRunner creates a new test runner.
24 func NewTestRunner() *TestRunner {
25 return &TestRunner{
26 handlers: make(map[string]TestHandler),
27 }
28 }
29
30 // RegisterHandler registers a handler for a specific test type.
31 func (r *TestRunner) RegisterHandler(testType string, handler TestHandler) {
32 r.handlers[testType] = handler
33 }
34
35 // RunWithCases executes test cases loaded from a slice of maps.
36 func (r *TestRunner) RunWithCases(t *testing.T, cases []map[string]any) {
37 t.Helper()
38
39 for _, tc := range cases {
40 tc := tc // capture loop variable
41
42 // Extract test name and type
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 // RunTestCases is a convenience function that creates a runner and executes test cases.
64 // The loadFunc should be provided by the calling package to load test data using its preferred YAML parser.
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 // GetString extracts a string field from a test case map.
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 // GetInt extracts an int field from a test case map.
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 // GetBool extracts a bool field from a test case map.
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 // GetSlice extracts a slice field from a test case map.
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 // GetMap extracts a map field from a test case map.
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 // RequireString extracts a string field, failing the test if not present.
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 // RequireInt extracts an int field, failing the test if not present.
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 // RequireSlice extracts a slice field, failing the test if not present.
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 // UnmarshalTestCase unmarshals a test case map into a struct.
161 func UnmarshalTestCase(tc map[string]any, target any) error {
162 return UnmarshalStruct(target, tc)
163 }
164
165 // AssertEqual is a helper for comparing expected and actual values in test handlers.
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 }