Skip to content

Commit

Permalink
refactor: IStep interface
Browse files Browse the repository at this point in the history
  • Loading branch information
debugtalk committed Sep 22, 2021
1 parent 57a1daf commit fe6cca3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
5 changes: 2 additions & 3 deletions boomer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
},
Expand Down
12 changes: 9 additions & 3 deletions extract.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package httpboomer

import "fmt"

// implements IStep interface
type stepRequestExtraction struct {
runner *Runner
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ type TStep struct {

// interface for all types of steps
type IStep interface {
ToStruct() *TStep
Name() string
Type() string
Run() error
}

Expand Down
20 changes: 15 additions & 5 deletions step.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package httpboomer

import "fmt"

func Step(name string) *step {
return &step{
runner: defaultRunner,
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
16 changes: 11 additions & 5 deletions validate.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
}

0 comments on commit fe6cca3

Please sign in to comment.