From 4dda2270d9d374aff7fcf018f18e5e0a194c5638 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sat, 27 Aug 2022 11:34:16 +0900 Subject: [PATCH 1/2] tmp --- operator.go | 5 +++-- store.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/operator.go b/operator.go index 37f2099c..244fa92f 100644 --- a/operator.go +++ b/operator.go @@ -114,7 +114,7 @@ func (o *operator) recordToArray(v map[string]interface{}) { // delete values of prevous loop o.store.steps = o.store.steps[:len(o.store.steps)-1] } - o.store.steps = append(o.store.steps, v) + o.store.recordToArray(v) } func (o *operator) recordToMap(v map[string]interface{}) { @@ -122,7 +122,8 @@ func (o *operator) recordToMap(v map[string]interface{}) { // delete values of prevous loop delete(o.store.stepMaps, o.steps[len(o.store.stepMaps)-1].key) } - o.store.stepMaps[o.steps[len(o.store.stepMaps)].key] = v + k := o.steps[len(o.store.stepMaps)].key + o.store.recordToMap(k, v) } func (o *operator) Close() { diff --git a/store.go b/store.go index 9045a847..3d9b6c46 100644 --- a/store.go +++ b/store.go @@ -19,6 +19,20 @@ type store struct { loopIndex *int } +func (s *store) recordToMap(k string, v map[string]interface{}) { + if !s.useMap { + panic("recordToMap can only be used if useMap = true") + } + s.stepMaps[k] = v +} + +func (s *store) recordToArray(v map[string]interface{}) { + if s.useMap { + panic("recordToMap can only be used if useMap = false") + } + s.steps = append(s.steps, v) +} + func (s *store) toNormalizedMap() map[string]interface{} { store := map[string]interface{}{} for k := range s.funcs { From dd79e63fe7b3e81ccfeedb85490ec806b786fdd1 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sun, 28 Aug 2022 07:33:22 +0900 Subject: [PATCH 2/2] Support `current` variable Fix #113 --- operator.go | 2 +- store.go | 32 ++++++++++++++++++++++++-------- test.go | 5 ++++- testdata/book/github.yml | 12 ++++++++---- testdata/book/github_map.yml | 17 +++++++++-------- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/operator.go b/operator.go index 826a6d99..bc419f9d 100644 --- a/operator.go +++ b/operator.go @@ -785,7 +785,7 @@ func (o *operator) runInternal(ctx context.Context) error { return nil } o.Debugf(cyan("Run '%s' on %s\n"), testRunnerKey, o.stepName(i)) - if err := s.testRunner.Run(ctx, s.testCond); err != nil { + if err := s.testRunner.Run(ctx, s.testCond, runned); err != nil { return fmt.Errorf("test failed on %s: %v", o.stepName(i), err) } if !runned { diff --git a/store.go b/store.go index c0d85184..1fb76ff5 100644 --- a/store.go +++ b/store.go @@ -5,18 +5,20 @@ const ( storeStepsKey = "steps" storeParentKey = "parent" storeIncludedKey = "included" + storeCurrentKey = "current" storeFuncValue = "[func]" ) type store struct { - steps []map[string]interface{} - stepMap map[string]map[string]interface{} - vars map[string]interface{} - funcs map[string]interface{} - bindVars map[string]interface{} - parentVars map[string]interface{} - useMap bool // Use map syntax in `steps:`. - loopIndex *int + steps []map[string]interface{} + stepMap map[string]map[string]interface{} + vars map[string]interface{} + funcs map[string]interface{} + bindVars map[string]interface{} + parentVars map[string]interface{} + useMap bool // Use map syntax in `steps:`. + loopIndex *int + latestMapKey string } func (s *store) recordToMap(k string, v map[string]interface{}) { @@ -24,6 +26,7 @@ func (s *store) recordToMap(k string, v map[string]interface{}) { panic("recordToMap can only be used if useMap = true") } s.stepMap[k] = v + s.latestMapKey = k } func (s *store) recordToArray(v map[string]interface{}) { @@ -33,6 +36,19 @@ func (s *store) recordToArray(v map[string]interface{}) { s.steps = append(s.steps, v) } +func (s *store) latest() map[string]interface{} { + if !s.useMap { + if len(s.steps) == 0 { + return nil + } + return s.steps[len(s.steps)-1] + } + if v, ok := s.stepMap[s.latestMapKey]; ok { + return v + } + return nil +} + func (s *store) toNormalizedMap() map[string]interface{} { store := map[string]interface{}{} for k := range s.funcs { diff --git a/test.go b/test.go index a0b6a30a..6514175e 100644 --- a/test.go +++ b/test.go @@ -24,8 +24,11 @@ func newTestRunner(o *operator) (*testRunner, error) { }, nil } -func (rnr *testRunner) Run(ctx context.Context, cond string) error { +func (rnr *testRunner) Run(ctx context.Context, cond string, runned bool) error { store := rnr.operator.store.toMap() + if runned { + store[storeCurrentKey] = rnr.operator.store.latest() + } t := buildTree(cond, store) rnr.operator.Debugln("-----START TEST CONDITION-----") rnr.operator.Debugf("%s", t) diff --git a/testdata/book/github.yml b/testdata/book/github.yml index 95001930..4dbad828 100644 --- a/testdata/book/github.yml +++ b/testdata/book/github.yml @@ -15,7 +15,7 @@ steps: body: application/json: null - test: 'steps[0].res.status == 200' + test: 'current.res.status == 200' - req: /orgs/golang/repos?per_page=30&page=1: @@ -25,7 +25,7 @@ steps: body: application/json: null - test: 'steps[1].res.status == 200' + test: 'current.res.status == 200' - req: /orgs/golang/repos?per_page=30&page=2: @@ -35,6 +35,10 @@ steps: body: application/json: null - test: 'steps[2].res.status == 200' + test: 'current.res.status == 200' - - test: 'len(steps[1].res.body) + len(steps[2].res.body) == 53' + test: | + steps[0].res.status == 200 && + steps[1].res.status == 200 && + steps[2].res.status == 200 && + len(steps[1].res.body) + len(steps[2].res.body) == 53 diff --git a/testdata/book/github_map.yml b/testdata/book/github_map.yml index 58355afb..f8571e47 100644 --- a/testdata/book/github_map.yml +++ b/testdata/book/github_map.yml @@ -1,4 +1,4 @@ -desc: Test using GitHub ( named steps ) +desc: Test using GitHub ( map syntax ) vars: user: k1LoW token: ${GITHUB_TOKEN} @@ -12,8 +12,7 @@ steps: headers: Authorization: "token {{ vars.token }}" body: null - test0: - test: 'steps.req0.res.status == 200' + test: 'current.res.status == 200' req1: req: /orgs/golang/repos?per_page=30&page=1: @@ -21,8 +20,7 @@ steps: headers: Authorization: "token {{ vars.token }}" body: null - test1: - test: 'steps.req1.res.status == 200' + test: 'current.res.status == 200' req2: req: /orgs/golang/repos?per_page=30&page=2: @@ -30,8 +28,11 @@ steps: headers: Authorization: "token {{ vars.token }}" body: null - test2: - test: 'steps.req2.res.status == 200' + test: 'current.res.status == 200' test3: - test: 'len(steps.req1.res.body) + len(steps.req2.res.body) == 53' + test: | + steps.req0.res.status == 200 && + steps.req1.res.status == 200 && + steps.req2.res.status == 200 && + len(steps.req1.res.body) + len(steps.req2.res.body) == 53