-
Notifications
You must be signed in to change notification settings - Fork 187
/
property_assignment_test_cases.go
88 lines (73 loc) · 2.66 KB
/
property_assignment_test_cases.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/functions"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/testcases"
"github.com/pkg/errors"
"golang.org/x/net/context"
kerrors "k8s.io/apimachinery/pkg/util/errors"
)
// InjectPropertyAssignmentTestsID is the unique identifier for this stage
const InjectPropertyAssignmentTestsID = "injectPropertyAssignmentTestCases"
func InjectPropertyAssignmentTests(idFactory astmodel.IdentifierFactory) *Stage {
stage := NewStage(
InjectPropertyAssignmentTestsID,
"Add test cases to verify PropertyAssignment functions",
func(ctx context.Context, state *State) (*State, error) {
factory := makePropertyAssignmentTestCaseFactory(idFactory)
modifiedDefs := make(astmodel.TypeDefinitionSet)
var errs []error
for _, d := range state.Definitions() {
if factory.NeedsTest(d) {
updated, err := factory.AddTestTo(d)
if err != nil {
errs = append(errs, err)
} else {
modifiedDefs[updated.Name()] = updated
}
}
}
if len(errs) > 0 {
return nil, kerrors.NewAggregate(errs)
}
return state.WithOverlaidDefinitions(modifiedDefs), nil
})
stage.RequiresPrerequisiteStages(
InjectPropertyAssignmentFunctionsStageID, // Need PropertyAssignmentFunctions to test
InjectJsonSerializationTestsID) // We reuse the generators from the JSON tests
return stage
}
type propertyAssignmentTestCaseFactory struct {
injector *astmodel.TestCaseInjector
idFactory astmodel.IdentifierFactory
}
func makePropertyAssignmentTestCaseFactory(idFactory astmodel.IdentifierFactory) propertyAssignmentTestCaseFactory {
return propertyAssignmentTestCaseFactory{
injector: astmodel.NewTestCaseInjector(),
idFactory: idFactory,
}
}
func (s *propertyAssignmentTestCaseFactory) NeedsTest(def astmodel.TypeDefinition) bool {
container, ok := astmodel.AsFunctionContainer(def.Type())
if !ok {
return false
}
for _, fn := range container.Functions() {
if _, ok := fn.(*functions.PropertyAssignmentFunction); ok {
return true
}
}
return false
}
func (s *propertyAssignmentTestCaseFactory) AddTestTo(def astmodel.TypeDefinition) (astmodel.TypeDefinition, error) {
container, ok := astmodel.AsFunctionContainer(def.Type())
if !ok {
return astmodel.TypeDefinition{}, errors.Errorf("expected %s to be a function container", def.Name())
}
testCase := testcases.NewPropertyAssignmentTestCase(def.Name(), container, s.idFactory)
return s.injector.Inject(def, testCase)
}