From 50230adb543155aded50168e02da2872a39d88ed Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 19 Aug 2024 21:37:32 +0800 Subject: [PATCH] refactor: ITestCase, GetTestCase --- hrp/loader.go | 4 ++-- hrp/step_request.go | 2 +- hrp/testcase.go | 21 ++++++--------------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/hrp/loader.go b/hrp/loader.go index d8bb8c03..42245a74 100644 --- a/hrp/loader.go +++ b/hrp/loader.go @@ -25,7 +25,7 @@ func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) { return nil, errors.New("invalid iTestCase type") } - casePath := tcPath.GetPath() + casePath := string(*tcPath) err := fs.WalkDir(os.DirFS(casePath), ".", func(path string, dir fs.DirEntry, e error) error { if dir == nil { // casePath is a file other than a dir @@ -46,7 +46,7 @@ func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) { // filtered testcases testCasePath := TestCasePath(path) - tc, err := testCasePath.ToTestCase() + tc, err := testCasePath.GetTestCase() if err != nil { log.Warn().Err(err).Str("path", path).Msg("load testcase failed") return nil diff --git a/hrp/step_request.go b/hrp/step_request.go index 08574fd4..f00e5eac 100644 --- a/hrp/step_request.go +++ b/hrp/step_request.go @@ -680,7 +680,7 @@ func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs { // CallRefCase calls a referenced testcase. func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs { var err error - s.step.TestCase, err = tc.ToTestCase() + s.step.TestCase, err = tc.GetTestCase() if err != nil { log.Error().Err(err).Msg("failed to load testcase") os.Exit(code.GetErrorCode(err)) diff --git a/hrp/testcase.go b/hrp/testcase.go index 78b67017..5bb267a7 100644 --- a/hrp/testcase.go +++ b/hrp/testcase.go @@ -17,21 +17,16 @@ import ( // ITestCase represents interface for testcases, // includes TestCase and TestCasePath. type ITestCase interface { - GetPath() string - ToTestCase() (*TestCase, error) + GetTestCase() (*TestCase, error) } // TestCasePath implements ITestCase interface. type TestCasePath string -func (path *TestCasePath) GetPath() string { - return fmt.Sprintf("%v", *path) -} - -// ToTestCase loads testcase path and convert to *TestCase -func (path *TestCasePath) ToTestCase() (*TestCase, error) { +// GetTestCase loads testcase path and convert to *TestCase +func (path *TestCasePath) GetTestCase() (*TestCase, error) { tc := &TestCase{} - casePath := path.GetPath() + casePath := string(*path) err := builtin.LoadFile(casePath, tc) if err != nil { return nil, err @@ -56,11 +51,7 @@ type TestCase struct { TestSteps []IStep `json:"-" yaml:"-"` } -func (tc *TestCase) GetPath() string { - return tc.Config.Path -} - -func (tc *TestCase) ToTestCase() (*TestCase, error) { +func (tc *TestCase) GetTestCase() (*TestCase, error) { return tc, nil } @@ -208,7 +199,7 @@ func (tc *TestCase) loadISteps() (*TestCase, error) { } refTestCase := TestCasePath(path) - tc, err := refTestCase.ToTestCase() + tc, err := refTestCase.GetTestCase() if err != nil { return nil, err }