/
executor.go
65 lines (60 loc) · 1.71 KB
/
executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package actions
import (
"github.com/wesovilabs/orion/context"
"github.com/wesovilabs/orion/helper"
"github.com/wesovilabs/orion/internal/errors"
)
// ExecuteFn defined type to be implemented by the plugins.
type ExecuteFn func(ctx context.FeatureContext) errors.Error
// Execute Given a plugin and the context is executed.
func Execute(ctx context.FeatureContext, action *Base, fn ExecuteFn) errors.Error {
// ctx.StartAction()
evalCtx := ctx.EvalContext()
when, err := helper.GetExpressionValueAsBool(evalCtx, action.when, true)
if err != nil {
return err
}
if !when {
return nil
}
if action.count != nil && !action.count.Range().Empty() {
return doCount(ctx, action, fn)
}
if action.while != nil && !action.while.Range().Empty() {
return doWhile(ctx, action, fn)
}
return fn(ctx)
}
func doWhile(ctx context.FeatureContext, plugin *Base, fn ExecuteFn) errors.Error {
evalCtx := ctx.EvalContext()
index := 0
for {
ctx.Variables().SetIndex(index)
ctx.Variables().SetToContext(ctx.EvalContext())
if while, err := helper.GetExpressionValueAsBool(evalCtx, plugin.while, true); err != nil || !while {
return err
}
if err := fn(ctx); err != nil {
return err
}
index++
}
}
func doCount(ctx context.FeatureContext, plugin *Base, fn ExecuteFn) errors.Error {
evalCtx := ctx.EvalContext()
count, err := helper.GetExpressionValueAsInt(evalCtx, plugin.count, 1)
if err != nil {
return err
}
for index := 0; index < count; index++ {
ctx.Variables().SetIndex(index)
ctx.Variables().SetToContext(ctx.EvalContext())
if while, err := helper.GetExpressionValueAsBool(evalCtx, plugin.while, true); err != nil || !while {
return err
}
if err := fn(ctx); err != nil {
return err
}
}
return nil
}