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 Test Package #1567

Merged
merged 8 commits into from
Jun 17, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
package conversions

import (
"bytes"
"testing"

"github.com/sebdah/goldie/v2"

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

. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -77,8 +75,8 @@ func CreatePropertyAssignmentFunctionTestCases() []*StorageConversionPropertyTes
optionalNextAgeProperty := astmodel.NewPropertyDefinition("Age", "age", astmodel.NewOptionalType(nextAge.Name()))

idFactory := astmodel.NewIdentifierFactory()
ageFunction := NewFakeFunction("Age", idFactory)
ageFunction.returnType = astmodel.IntType
ageFunction := test.NewFakeFunction("Age", idFactory)
ageFunction.TypeReturned = astmodel.IntType

nastyProperty := astmodel.NewPropertyDefinition(
"nasty",
Expand Down Expand Up @@ -216,32 +214,6 @@ func runTestPropertyAssignmentFunction_AsFunc(c *StorageConversionPropertyTestCa

receiverDefinition := c.currentObject.WithType(currentType.WithFunction(convertFrom).WithFunction(convertTo))

defs := []astmodel.TypeDefinition{receiverDefinition}
packages := make(map[astmodel.PackageReference]*astmodel.PackageDefinition)

currentPackage := receiverDefinition.Name().PackageReference.(astmodel.LocalPackageReference)

packageDefinition := astmodel.NewPackageDefinition(currentPackage.Group(), currentPackage.PackageName(), "1")
packageDefinition.AddDefinition(receiverDefinition)

packages[currentPackage] = packageDefinition

// put all definitions in one file, regardless.
// the package reference isn't really used here.
fileDef := astmodel.NewFileDefinition(currentPackage, defs, packages)

assertFileGeneratesExpectedCode(t, fileDef, c.name)
}

func assertFileGeneratesExpectedCode(t *testing.T, fileDef *astmodel.FileDefinition, testName string) {
g := goldie.New(t)

buf := &bytes.Buffer{}
fileWriter := astmodel.NewGoSourceFileWriter(fileDef)
err := fileWriter.SaveToWriter(buf)
if err != nil {
t.Fatalf("could not generate file: %v", err)
}

g.Assert(t, testName, buf.Bytes())
fileDef := test.CreateFileDefinition(receiverDefinition)
test.AssertFileGeneratesExpectedCode(t, fileDef, c.name)
}
30 changes: 30 additions & 0 deletions hack/generator/pkg/test/asserts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package test

import (
"bytes"
"testing"

"github.com/sebdah/goldie/v2"

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

// AssertFileGeneratesExpectedCode serialises the given FileDefintion as a golden file test, checking that the expected
// results are generated
func AssertFileGeneratesExpectedCode(t *testing.T, fileDef *astmodel.FileDefinition, testName string) {
g := goldie.New(t)

buf := &bytes.Buffer{}
fileWriter := astmodel.NewGoSourceFileWriter(fileDef)
err := fileWriter.SaveToWriter(buf)
if err != nil {
t.Fatalf("could not generate file: %v", err)
}

g.Assert(t, testName, buf.Bytes())
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT license.
*/

package conversions
package test

import (
"github.com/dave/dst"
Expand All @@ -13,12 +13,14 @@ import (
)

// FakeFunction is a fake function that can be used for testing purposes
// Note that there's another FakeFunction in astmodel but we can't eliminate that one because it would cause a package
// reference cycle
type FakeFunction struct {
name string
Imported *astmodel.PackageReferenceSet
Referenced astmodel.TypeNameSet
returnType astmodel.Type
idFactory astmodel.IdentifierFactory
name string
Imported *astmodel.PackageReferenceSet
Referenced astmodel.TypeNameSet
TypeReturned astmodel.Type
Copy link
Member

@babbageclunk babbageclunk Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super minor: I have a slight preference for ReturnType here.
Oh, I see that you did this to make it consistent with Imported and Referenced. But actually present tense (Imports, References) feels more natural for those too, and I quite like that they can also be read as plural nouns as well since they're sets. 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the function ReturnType() is on the Function interface, so I couldn't use ReturnType for the member name here.

idFactory astmodel.IdentifierFactory
}

func NewFakeFunction(name string, idFactory astmodel.IdentifierFactory) *FakeFunction {
Expand Down Expand Up @@ -47,16 +49,16 @@ func (fake *FakeFunction) References() astmodel.TypeNameSet {
return fake.Referenced
}

func (fake *FakeFunction) AsFunc(generationContext *astmodel.CodeGenerationContext, reciever astmodel.TypeName) *dst.FuncDecl {
recieverName := fake.idFactory.CreateIdentifier(reciever.Name(), astmodel.NotExported)
func (fake *FakeFunction) AsFunc(generationContext *astmodel.CodeGenerationContext, receiver astmodel.TypeName) *dst.FuncDecl {
receiverName := fake.idFactory.CreateIdentifier(receiver.Name(), astmodel.NotExported)
details := astbuilder.FuncDetails{
ReceiverIdent: recieverName,
ReceiverType: astmodel.NewOptionalType(reciever).AsType(generationContext),
ReceiverIdent: receiverName,
ReceiverType: astmodel.NewOptionalType(receiver).AsType(generationContext),
Name: fake.name,
}

if fake.returnType != nil {
details.AddReturn(fake.returnType.AsType(generationContext))
if fake.TypeReturned != nil {
details.AddReturn(fake.TypeReturned.AsType(generationContext))
}

return details.DefineFunc()
Expand Down Expand Up @@ -96,5 +98,5 @@ func (fake *FakeFunction) Equals(f astmodel.Function) bool {
}

func (fake *FakeFunction) ReturnType() astmodel.Type {
return fake.returnType
return fake.TypeReturned
}
34 changes: 34 additions & 0 deletions hack/generator/pkg/test/file_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package test

import (
"github.com/pkg/errors"

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

func CreateFileDefinition(definitions ...astmodel.TypeDefinition) *astmodel.FileDefinition {
packages := make(map[astmodel.PackageReference]*astmodel.PackageDefinition)

// Use the package reference of the first definition for the whole file
ref, err := astmodel.PackageAsLocalPackage(definitions[0].Name().PackageReference)
if err != nil {
panic(errors.Wrap(err, "Expected first definition to have a local package reference - fix your test!"))
}

pkgDefinition := astmodel.NewPackageDefinition(ref.Group(), ref.PackageName(), ref.Version())
for _, def := range definitions {
pkgDefinition.AddDefinition(def)
}

packages[ref] = pkgDefinition

// put all definitions in one file, regardless.
// the package reference isn't really used here.
fileDef := astmodel.NewFileDefinition(ref, definitions, packages)
return fileDef
}