Skip to content

Commit

Permalink
Merge pull request #119 from k1LoW/current
Browse files Browse the repository at this point in the history
Support `current` variable
  • Loading branch information
k1LoW authored Aug 28, 2022
2 parents 2e19b14 + e3a5dd8 commit 41d08d6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
7 changes: 4 additions & 3 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ 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{}) {
if o.store.loopIndex != nil && *o.store.loopIndex > 0 {
// 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() {
Expand Down Expand Up @@ -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 {
Expand Down
46 changes: 38 additions & 8 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
5 changes: 4 additions & 1 deletion test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions testdata/book/github.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
17 changes: 9 additions & 8 deletions testdata/book/github_map.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
desc: Test using GitHub ( named steps )
desc: Test using GitHub ( map syntax )
vars:
user: k1LoW
token: ${GITHUB_TOKEN}
Expand All @@ -12,26 +12,27 @@ 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:
get:
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:
get:
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

0 comments on commit 41d08d6

Please sign in to comment.