From fe6cca362c59aca5b4128c55cda615b0bfe5d761 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 22 Sep 2021 18:41:28 +0800 Subject: [PATCH] refactor: IStep interface --- boomer.go | 5 ++--- extract.go | 12 +++++++++--- models.go | 3 ++- step.go | 20 +++++++++++++++----- step_test.go | 4 ++-- validate.go | 16 +++++++++++----- 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/boomer.go b/boomer.go index 5e7cd8b..e15bc92 100644 --- a/boomer.go +++ b/boomer.go @@ -32,11 +32,10 @@ func convertBoomerTask(testcase *TestCase) *boomer.Task { err := step.Run() elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond) - tStep := step.ToStruct() if err == nil { - boomer.RecordSuccess(string(tStep.Request.Method), tStep.Name, elapsed, int64(0)) + boomer.RecordSuccess(step.Type(), step.Name(), elapsed, int64(0)) } else { - boomer.RecordFailure(string(tStep.Request.Method), tStep.Name, elapsed, err.Error()) + boomer.RecordFailure(step.Type(), step.Name(), elapsed, err.Error()) } } }, diff --git a/extract.go b/extract.go index d30f7de..aff300b 100644 --- a/extract.go +++ b/extract.go @@ -1,5 +1,7 @@ package httpboomer +import "fmt" + // implements IStep interface type stepRequestExtraction struct { runner *Runner @@ -13,12 +15,16 @@ func (s *stepRequestExtraction) WithJmesPath(jmesPath string, varName string) *s func (s *stepRequestExtraction) Validate() *stepRequestValidation { return &stepRequestValidation{ - TStep: s.step, + step: s.step, } } -func (s *stepRequestExtraction) ToStruct() *TStep { - return s.step +func (s *stepRequestExtraction) Name() string { + return s.step.Name +} + +func (s *stepRequestExtraction) Type() string { + return fmt.Sprintf("request-%v", s.step.Request.Method) } func (s *stepRequestExtraction) Run() error { diff --git a/models.go b/models.go index 696b8f2..c7b9a87 100644 --- a/models.go +++ b/models.go @@ -61,7 +61,8 @@ type TStep struct { // interface for all types of steps type IStep interface { - ToStruct() *TStep + Name() string + Type() string Run() error } diff --git a/step.go b/step.go index 5a0c277..4a945fd 100644 --- a/step.go +++ b/step.go @@ -1,5 +1,7 @@ package httpboomer +import "fmt" + func Step(name string) *step { return &step{ runner: defaultRunner, @@ -167,7 +169,7 @@ func (s *requestWithOptionalArgs) TeardownHook(hook string) *requestWithOptional func (s *requestWithOptionalArgs) Validate() *stepRequestValidation { return &stepRequestValidation{ runner: s.runner, - TStep: s.step, + step: s.step, } } @@ -178,8 +180,12 @@ func (s *requestWithOptionalArgs) Extract() *stepRequestExtraction { } } -func (s *requestWithOptionalArgs) ToStruct() *TStep { - return s.step +func (s *requestWithOptionalArgs) Name() string { + return s.step.Name +} + +func (s *requestWithOptionalArgs) Type() string { + return fmt.Sprintf("request-%v", s.step.Request.Method) } func (s *requestWithOptionalArgs) Run() error { @@ -202,8 +208,12 @@ func (s *testcaseWithOptionalArgs) Export(names ...string) *testcaseWithOptional return s } -func (s *testcaseWithOptionalArgs) ToStruct() *TStep { - return s.step +func (s *testcaseWithOptionalArgs) Name() string { + return s.step.Name +} + +func (s *testcaseWithOptionalArgs) Type() string { + return "testcase" } func (s *testcaseWithOptionalArgs) Run() error { diff --git a/step_test.go b/step_test.go index 9d64380..88b10c7 100644 --- a/step_test.go +++ b/step_test.go @@ -23,7 +23,7 @@ var ( ) func TestRunRequestGetToStruct(t *testing.T) { - tStep := stepGET.ToStruct() + tStep := stepGET.step if tStep.Request.Method != GET { t.Fatalf("tStep.Request.Method != GET") } @@ -45,7 +45,7 @@ func TestRunRequestGetToStruct(t *testing.T) { } func TestRunRequestPostDataToStruct(t *testing.T) { - tStep := stepPOSTData.ToStruct() + tStep := stepPOSTData.step if tStep.Request.Method != POST { t.Fatalf("tStep.Request.Method != POST") } diff --git a/validate.go b/validate.go index 065266f..9b68ade 100644 --- a/validate.go +++ b/validate.go @@ -1,9 +1,11 @@ package httpboomer +import "fmt" + // implements IStep interface type stepRequestValidation struct { runner *Runner - TStep *TStep + step *TStep } func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation { @@ -13,14 +15,18 @@ func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{ Expect: expected, Message: msg, } - s.TStep.Validators = append(s.TStep.Validators, validator) + s.step.Validators = append(s.step.Validators, validator) return s } -func (s *stepRequestValidation) ToStruct() *TStep { - return s.TStep +func (s *stepRequestValidation) Name() string { + return s.step.Name +} + +func (s *stepRequestValidation) Type() string { + return fmt.Sprintf("request-%v", s.step.Request.Method) } func (s *stepRequestValidation) Run() error { - return s.runner.runStep(s.TStep) + return s.runner.runStep(s.step) }