-
Notifications
You must be signed in to change notification settings - Fork 204
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
Create Test Package #1567
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92a9f58
Move AssertFileGeneratesExpectedCode() into new package
theunrepentantgeek 48394a6
Move FakeFunction into test package
theunrepentantgeek 4b4049f
Rename FakeFunction file
theunrepentantgeek f052256
Add utility function to create FileDefinitions
theunrepentantgeek 1a0ae70
Use CreateFileDefinition() from existing tests
theunrepentantgeek 17eea54
Code gardening
theunrepentantgeek 9dfa0a1
Merge branch 'master' into feature/test-package
theunrepentantgeek 0b6c838
Merge branch 'master' into feature/test-package
theunrepentantgeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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. 🤷
There was a problem hiding this comment.
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 theFunction
interface, so I couldn't useReturnType
for the member name here.