-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move AssertFileGeneratesExpectedCode() into new package * Move FakeFunction into test package * Rename FakeFunction file * Add utility function to create FileDefinitions * Use CreateFileDefinition() from existing tests * Code gardening
- Loading branch information
1 parent
2ceb10b
commit 69e8589
Showing
4 changed files
with
84 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |