105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
package basic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
// executor is the subset of sdk.Tool used in tests so concrete tools can be
|
|
// exercised without importing the SDK into the test table.
|
|
type executor interface {
|
|
Execute(context.Context, any) (any, error)
|
|
}
|
|
|
|
// run executes e with args against a background context.
|
|
func run(t *testing.T, e executor, args map[string]any) (any, error) {
|
|
t.Helper()
|
|
return e.Execute(context.Background(), args)
|
|
}
|
|
|
|
// assertIs fails when err does not wrap target.
|
|
func assertIs(t *testing.T, err, target error) {
|
|
t.Helper()
|
|
if !errors.Is(err, target) {
|
|
t.Fatalf("errors.Is(%v, %v) = false, want true", err, target)
|
|
}
|
|
}
|
|
|
|
// assertNoErr fails when err is non-nil.
|
|
func assertNoErr(t *testing.T, err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
// mapResult asserts got is a map[string]any and returns it.
|
|
func mapResult(t *testing.T, got any) map[string]any {
|
|
t.Helper()
|
|
m, ok := got.(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("result is %T, want map[string]any", got)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// fieldString returns m[key] as a string, failing otherwise.
|
|
func fieldString(t *testing.T, m map[string]any, key string) string {
|
|
t.Helper()
|
|
v, ok := m[key].(string)
|
|
if !ok {
|
|
t.Fatalf("%s field is %T (%v), want string", key, m[key], m[key])
|
|
}
|
|
return v
|
|
}
|
|
|
|
// fieldFloat returns m[key] as a float64, failing otherwise.
|
|
func fieldFloat(t *testing.T, m map[string]any, key string) float64 {
|
|
t.Helper()
|
|
v, ok := m[key].(float64)
|
|
if !ok {
|
|
t.Fatalf("%s field is %T (%v), want float64", key, m[key], m[key])
|
|
}
|
|
return v
|
|
}
|
|
|
|
// fieldInt returns m[key] as an int, failing otherwise.
|
|
func fieldInt(t *testing.T, m map[string]any, key string) int {
|
|
t.Helper()
|
|
v, ok := m[key].(int)
|
|
if !ok {
|
|
t.Fatalf("%s field is %T (%v), want int", key, m[key], m[key])
|
|
}
|
|
return v
|
|
}
|
|
|
|
// fieldInt64 returns m[key] as an int64, failing otherwise.
|
|
func fieldInt64(t *testing.T, m map[string]any, key string) int64 {
|
|
t.Helper()
|
|
v, ok := m[key].(int64)
|
|
if !ok {
|
|
t.Fatalf("%s field is %T (%v), want int64", key, m[key], m[key])
|
|
}
|
|
return v
|
|
}
|
|
|
|
// fieldBool returns m[key] as a bool, failing otherwise.
|
|
func fieldBool(t *testing.T, m map[string]any, key string) bool {
|
|
t.Helper()
|
|
v, ok := m[key].(bool)
|
|
if !ok {
|
|
t.Fatalf("%s field is %T (%v), want bool", key, m[key], m[key])
|
|
}
|
|
return v
|
|
}
|
|
|
|
// assertApprox fails when got and want differ by more than 1e-9.
|
|
func assertApprox(t *testing.T, got, want float64) {
|
|
t.Helper()
|
|
if math.Abs(got-want) > 1e-9 {
|
|
t.Fatalf("got %v, want %v", got, want)
|
|
}
|
|
}
|