testing/fixtures/testdata.go

42 lines
898 B
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package fixtures
import (
"testing"
"io/ioutil"
"path/filepath"
"log"
"fmt"
"strings"
"strconv"
)
func (f *F) FixtureTestDataBasePath() string {
g := fmt.Sprintf("fixture_%s_", f.Name())
p := filepath.Join("./testdata", g)
d := filepath.Join(g,p)
return d
}
func (f *F) FixtureTestDataPaths() []string {
p := f.FixtureTestDataBasePath()
df, e := filepath.Glob(p + "*")
if e != nil {
log.Fatal(e)
}
return df
}
func (f *F) FixtureTestData() {
bp := f.FixtureTestDataBasePath()
df := f.FixtureTestDataPaths()
for i, d := range(df) {
subtestname := strings.TrimPrefix(d, bp)
if subtestname == d {
subtestname = strconv.Itoa(i)
}
data,_ := ioutil.ReadFile(d)
f.Run(f.Name() + "." + subtestname, func (t *testing.T) { f.RunWith(f.T, data) })
}
}