testing/md/params.go

79 lines
1.7 KiB
Go
Raw Normal View History

2024-05-08 20:28:36 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package mockdata
import (
"io/ioutil"
"path/filepath"
"gopkg.in/yaml.v2"
"log"
"fmt"
"gitea.cv.mazarbul.net/rosskeen.house/testing/fixture"
)
// Return a structure to load the mock data into
type MockObjectFn func() interface{}
type MockParam struct {
param fixture.Param
mock MockObjectFn
}
type Parameters struct {
*fixture.Parameters
name string
basepath string
mockdata MockObjectFn
}
func P(name string, mockdata MockObjectFn) *Parameters { return &Parameters{ name: name, mockdata: mockdata } }
// mockdata.BasePath('./testdata/mock_{testname}_')
func (p *Parameters) BasePath() string {
if p.basepath == "" {
g := fmt.Sprintf("mock_%s_", p.name)
p.basepath = filepath.Join("./testdata", g)
}
return p.basepath
}
// mockdata.Paths(base string)
func (p *Parameters) Paths() []string {
df, e := filepath.Glob(p.BasePath() + "*")
if e != nil {
log.Fatal(e)
}
return df
}
func (p *Parameters) Values() []fixture.Param {
// return TestData files
paths := p.Paths()
r := make([]fixture.Param, len(paths))
for i := range(paths) {
r[i] = MockParam { param: paths[i], mock: p.mockdata }
}
return r
}
func Read(path string, mockdata interface{}) interface{} {
d,e := ioutil.ReadFile(path)
if e != nil {
log.Fatalf("Error reading fixture data from %s: %s", path, e)
}
err := yaml.Unmarshal([]byte(d), mockdata)
if err != nil {
log.Printf("%s", err)
}
return mockdata
}
func ReadRaw(path string) interface{} {
d,e := ioutil.ReadFile(path)
if e != nil {
log.Fatalf("Error reading fixture data from %s: %s", path, e)
}
return d
}