refactor: convert API

This commit is contained in:
debugtalk
2021-10-11 12:15:24 +08:00
parent 188b5598d5
commit 5a74b12499
5 changed files with 32 additions and 32 deletions
+3 -3
View File
@@ -35,12 +35,12 @@ func (b *Boomer) SetDebug(debug bool) *Boomer {
func (b *Boomer) Run(testcases ...ITestCase) { func (b *Boomer) Run(testcases ...ITestCase) {
var taskSlice []*boomer.Task var taskSlice []*boomer.Task
for _, testcase := range testcases { for _, iTestCase := range testcases {
tcStruct, err := testcase.ToStruct() testcase, err := iTestCase.ToTestCase()
if err != nil { if err != nil {
panic(err) panic(err)
} }
task := b.convertBoomerTask(tcStruct) task := b.convertBoomerTask(testcase)
taskSlice = append(taskSlice, task) taskSlice = append(taskSlice, task)
} }
boomer.Run(taskSlice...) boomer.Run(taskSlice...)
+19 -13
View File
@@ -11,25 +11,24 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
func (tc *TestCase) toStruct() *TCase { func (tc *TestCase) ToTCase() (*TCase, error) {
tcStruct := TCase{ tCase := TCase{
Config: tc.Config, Config: tc.Config,
} }
for _, step := range tc.TestSteps { for _, step := range tc.TestSteps {
tcStruct.TestSteps = append(tcStruct.TestSteps, step.ToStruct()) tCase.TestSteps = append(tCase.TestSteps, step.ToStruct())
} }
return &tcStruct return &tCase, nil
} }
func (tc *TestCase) dump2JSON(path string) error { func (tc *TCase) Dump2JSON(path string) error {
path, err := filepath.Abs(path) path, err := filepath.Abs(path)
if err != nil { if err != nil {
log.Printf("convert absolute path error: %v, path: %v", err, path) log.Printf("convert absolute path error: %v, path: %v", err, path)
return err return err
} }
log.Printf("dump testcase to json path: %s", path) log.Printf("dump testcase to json path: %s", path)
tcStruct := tc.toStruct() file, _ := json.MarshalIndent(tc, "", " ")
file, _ := json.MarshalIndent(tcStruct, "", " ")
err = ioutil.WriteFile(path, file, 0644) err = ioutil.WriteFile(path, file, 0644)
if err != nil { if err != nil {
log.Printf("dump json path error: %v", err) log.Printf("dump json path error: %v", err)
@@ -38,7 +37,7 @@ func (tc *TestCase) dump2JSON(path string) error {
return nil return nil
} }
func (tc *TestCase) dump2YAML(path string) error { func (tc *TCase) Dump2YAML(path string) error {
path, err := filepath.Abs(path) path, err := filepath.Abs(path)
if err != nil { if err != nil {
log.Printf("convert absolute path error: %v, path: %v", err, path) log.Printf("convert absolute path error: %v, path: %v", err, path)
@@ -52,8 +51,7 @@ func (tc *TestCase) dump2YAML(path string) error {
encoder.SetIndent(4) encoder.SetIndent(4)
// encode // encode
tcStruct := tc.toStruct() err = encoder.Encode(tc)
err = encoder.Encode(tcStruct)
if err != nil { if err != nil {
return err return err
} }
@@ -106,7 +104,7 @@ func loadFromYAML(path string) (*TCase, error) {
return tc, err return tc, err
} }
func convertTestCase(tc *TCase) (*TestCase, error) { func (tc *TCase) ToTestCase() (*TestCase, error) {
testCase := &TestCase{ testCase := &TestCase{
Config: tc.Config, Config: tc.Config,
} }
@@ -128,7 +126,7 @@ func convertTestCase(tc *TCase) (*TestCase, error) {
var ErrUnsupportedFileExt = fmt.Errorf("unsupported testcase file extension") var ErrUnsupportedFileExt = fmt.Errorf("unsupported testcase file extension")
func loadTestFile(path *TestCasePath) (*TestCase, error) { func (path *TestCasePath) ToTestCase() (*TestCase, error) {
var tc *TCase var tc *TCase
var err error var err error
@@ -145,9 +143,17 @@ func loadTestFile(path *TestCasePath) (*TestCase, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
testcase, err := convertTestCase(tc) testcase, err := tc.ToTestCase()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return testcase, nil return testcase, nil
} }
func (path *TestCasePath) ToTCase() (*TCase, error) {
testcase, err := path.ToTestCase()
if err != nil {
return nil, err
}
return testcase.ToTCase()
}
+4 -3
View File
@@ -56,12 +56,13 @@ var (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
tCase, _ := demoTestCase.ToTCase()
// setup, prepare demo json/yaml testcase file path // setup, prepare demo json/yaml testcase file path
err := demoTestCase.dump2JSON(demoTestCaseJSONPath) err := tCase.Dump2JSON(demoTestCaseJSONPath)
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
err = demoTestCase.dump2YAML(demoTestCaseYAMLPath) err = tCase.Dump2YAML(demoTestCaseYAMLPath)
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
@@ -118,7 +119,7 @@ func TestLoadYAMLCase(t *testing.T) {
func TestLoadJSONAndRun(t *testing.T) { func TestLoadJSONAndRun(t *testing.T) {
jsonPath := &TestCasePath{demoTestCaseJSONPath} jsonPath := &TestCasePath{demoTestCaseJSONPath}
testcase, err := loadTestFile(jsonPath) testcase, err := jsonPath.ToTestCase()
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.Fail() t.Fail()
} }
+3 -10
View File
@@ -68,7 +68,8 @@ type IStep interface {
} }
type ITestCase interface { type ITestCase interface {
ToStruct() (*TestCase, error) ToTestCase() (*TestCase, error)
ToTCase() (*TCase, error)
} }
// used for testcase runner // used for testcase runner
@@ -77,7 +78,7 @@ type TestCase struct {
TestSteps []IStep TestSteps []IStep
} }
func (tc *TestCase) ToStruct() (*TestCase, error) { func (tc *TestCase) ToTestCase() (*TestCase, error) {
return tc, nil return tc, nil
} }
@@ -85,14 +86,6 @@ type TestCasePath struct {
Path string Path string
} }
func (path *TestCasePath) ToStruct() (*TestCase, error) {
testcase, err := loadTestFile(path)
if err != nil {
return nil, err
}
return testcase, nil
}
type TestCaseSummary struct{} type TestCaseSummary struct{}
type StepData struct { type StepData struct {
+3 -3
View File
@@ -38,13 +38,13 @@ func (r *Runner) SetDebug(debug bool) *Runner {
} }
func (r *Runner) Run(testcases ...ITestCase) error { func (r *Runner) Run(testcases ...ITestCase) error {
for _, testcase := range testcases { for _, iTestCase := range testcases {
tcStruct, err := testcase.ToStruct() testcase, err := iTestCase.ToTestCase()
if err != nil { if err != nil {
log.Printf("[Run] testcase.ToStruct() error: %v", err) log.Printf("[Run] testcase.ToStruct() error: %v", err)
return err return err
} }
if err := r.runCase(tcStruct); err != nil { if err := r.runCase(testcase); err != nil {
log.Printf("[Run] runCase error: %v", err) log.Printf("[Run] runCase error: %v", err)
return err return err
} }