/
invocation_testcase.go
72 lines (56 loc) · 1.83 KB
/
invocation_testcase.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
66
67
68
69
70
71
72
package models
import (
"time"
"github.com/samber/lo"
"github.com/hashicorp/hcl/v2"
"github.com/v-braun/ratt/pkg/types"
"github.com/v-braun/ratt/pkg/utils"
)
var _ types.TestCaseInvocation = &invocationTestcaseModel{}
type invocationTestcaseModel struct {
name string
readModel *hcl.BodyContent
nested []types.Invocation
}
func BuildTestcaseInvocationModels(blocks []*hcl.Block, model types.RattModel, ctx types.RattContext) []types.Invocation {
targetBlocks := lo.Filter(blocks, func(b *hcl.Block, i int) bool {
return b.Type == "testcase"
})
result := utils.MapModels(targetBlocks, ctx, func(block *hcl.Block, ctx types.RattContext) types.Invocation {
return BuildTestcaseInvocationModel(block, model, ctx)
})
return result
}
func BuildTestcaseInvocationModel(block *hcl.Block, model types.RattModel, ctx types.RattContext) types.TestCaseInvocation {
nested, readModel := BuildInvocationBlockBody(block, model, TestcaseSchema(), ctx)
if ctx.Reporter().HasErrors() {
return nil
}
result := &invocationTestcaseModel{
readModel: readModel,
nested: nested,
name: block.Labels[0],
}
return result
}
func (ir *invocationTestcaseModel) Exec(ctx types.RattContext) {
subCtx := ctx.NewChild(ir.Name())
ctx.Bag()[types.IdBagEntry] = subCtx.Id()
subCtx.Reporter().Invocation(ir, types.RunningState, ctx)
start := time.Now()
for _, invocation := range ir.nested {
invocation.Exec(subCtx)
}
elapsed := time.Since(start)
ctx.Bag()[types.DurationBagEntry] = elapsed.Milliseconds()
subCtx.Reporter().Invocation(ir, types.EndState, ctx)
}
func (ir *invocationTestcaseModel) Name() string {
return ir.name
}
func (ir *invocationTestcaseModel) Type() types.InvocationType {
return types.TestcaseInvocationType
}
func (ir *invocationTestcaseModel) DefRange() *hcl.Range {
return &ir.readModel.MissingItemRange
}