Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create testcases package #1557

Merged
merged 5 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion hack/generator/pkg/astmodel/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"sort"
"strings"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astbuilder"
"github.com/dave/dst"
"github.com/pkg/errors"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astbuilder"
)

// ObjectType represents an (unnamed) object type
Expand All @@ -36,6 +37,9 @@ var _ PropertyContainer = &ObjectType{}
// Ensure ObjectType implements the FunctionContainer interface correctly
var _ FunctionContainer = &ObjectType{}

// Ensure ObjectType implements the TestCaseContainer interface correctly
var _ TestCaseContainer = &ObjectType{}

// NewObjectType is a factory method for creating a new ObjectType
func NewObjectType() *ObjectType {
return &ObjectType{
Expand Down
13 changes: 13 additions & 0 deletions hack/generator/pkg/astmodel/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ var _ PropertyContainer = &ResourceType{}
// Ensure ResourceType implements the FunctionContainer interface correctly
var _ FunctionContainer = &ResourceType{}

// Ensure ResourceType implements the TestCaseContainer interface correctly
var _ TestCaseContainer = &ResourceType{}

// SpecType returns the type used for specification
func (resource *ResourceType) SpecType() Type {
return resource.spec
Expand Down Expand Up @@ -207,6 +210,16 @@ func (resource *ResourceType) WithTestCase(testcase TestCase) *ResourceType {
return result
}

// TestCases returns a new slice containing all the test cases associated with this resource
func (resource *ResourceType) TestCases() []TestCase {
var result []TestCase
for _, tc := range resource.testcases {
result = append(result, tc)
}

return result
}

// AsType always panics because a resource has no direct AST representation
func (resource *ResourceType) AsType(_ *CodeGenerationContext) dst.Expr {
panic("a resource cannot be used directly as a type")
Expand Down
4 changes: 3 additions & 1 deletion hack/generator/pkg/astmodel/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type TestCase interface {
Equals(f TestCase) bool
}

type TestCaseDefiner interface {
// TestCaseContainer represents types that can contain test cases
// These types allow us to generate tests to verify the generated code does the right thing
type TestCaseContainer interface {
TestCases() []TestCase
}
4 changes: 2 additions & 2 deletions hack/generator/pkg/astmodel/test_file_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (file *TestFileDefinition) AsAst() (*dst.File, error) {
// Emit all test cases:
var testcases []dst.Decl
for _, s := range file.definitions {
definer, ok := s.Type().(TestCaseDefiner)
definer, ok := s.Type().(TestCaseContainer)
if !ok {
continue
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (file *TestFileDefinition) generateImports() *PackageImportSet {
var requiredImports = NewPackageImportSet()

for _, s := range file.definitions {
definer, ok := s.Type().(TestCaseDefiner)
definer, ok := s.Type().(TestCaseContainer)
if !ok {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"context"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
"github.com/Azure/azure-service-operator/hack/generator/pkg/testcases"

kerrors "k8s.io/apimachinery/pkg/util/errors"
)

Expand Down Expand Up @@ -62,7 +64,7 @@ func (s *objectSerializationTestCaseFactory) AddTestTo(def astmodel.TypeDefiniti
func (s *objectSerializationTestCaseFactory) injectTestCase(
_ *astmodel.TypeVisitor, objectType *astmodel.ObjectType, ctx interface{}) (astmodel.Type, error) {
name := ctx.(astmodel.TypeName)
testcase := astmodel.NewObjectSerializationTestCase(name, objectType, s.idFactory)
testcase := testcases.NewObjectSerializationTestCase(name, objectType, s.idFactory)
result := objectType.WithTestCase(testcase)
return result, nil
}
Loading