diff --git a/operator.go b/operator.go index 5de18d1f..bc419f9d 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.stepMap, o.steps[len(o.store.stepMap)-1].key) } - o.store.stepMap[o.steps[len(o.store.stepMap)].key] = v + k := o.steps[len(o.store.stepMap)].key + o.store.recordToMap(k, v) } func (o *operator) Close() { @@ -784,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 52300eca..1fb76ff5 100644 --- a/store.go +++ b/store.go @@ -5,18 +5,48 @@ 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{}) { + if !s.useMap { + panic("recordToMap can only be used if useMap = true") + } + s.stepMap[k] = v + s.latestMapKey = k +} + +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) 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{} { 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