-
Notifications
You must be signed in to change notification settings - Fork 79
proof of concept of the more type-safe way of defining the predicates. #1073
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
Draft
metlos
wants to merge
12
commits into
codeready-toolchain:master
Choose a base branch
from
metlos:new-predicates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c751223
proof of concept of the more type-safe way of defining the predicates.
metlos 9d62475
remove the duplicated "explain" test
metlos ad47f65
Merge branch 'master' into new-predicates
MatousJobanek 6205908
Adding a variant in the assertions package that reuses assert.* metho…
metlos 4dfd0ce
Experiment to add diffing
metlos ddd3eb0
Merge branch 'master' into new-predicates
mfrancisc bfc25b6
merge master
metlos 097f981
Simplify the whole assertion setup. Provide utility functions for ass…
metlos 55109b4
remove the gotest.tools from go.mod. Added by mistake.
metlos 50e68f2
small additions, improved naming, fixed typos
metlos 8fd7da8
merge master
metlos 7e24fa9
Merge branch 'master' into new-predicates
MatousJobanek 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| package assertions | ||
|
|
||
| type deepCopy[T any] interface { | ||
| DeepCopy() T | ||
| } | ||
|
|
||
| func copyObject[T any](obj any) T { | ||
| if dc, ok := obj.(deepCopy[T]); ok { | ||
| return dc.DeepCopy() | ||
| } | ||
| // TODO: should we go into attempting cloning slices and maps? | ||
| return obj.(T) | ||
| } |
This file contains hidden or 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,60 @@ | ||
| package assertions | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| var ( | ||
| _ Assertion[bool] = (*AssertAndFixFunc[bool])(nil) | ||
| _ AssertionFixer[bool] = (*AssertAndFixFunc[bool])(nil) | ||
| ) | ||
|
|
||
| func Explain[T any, A WithAssertions[T]](obj T, assertions A) string { | ||
| cpy := copyObject[T](obj) | ||
|
|
||
| nonFixingAssertions := []string{} | ||
| nonFixingAssertionsIndices := []int{} | ||
| for i, a := range assertions.Assertions() { | ||
| if f, ok := a.(AssertionFixer[T]); ok { | ||
| f.AdaptToMatch(cpy) | ||
| } else { | ||
| nonFixingAssertions = append(nonFixingAssertions, fmt.Sprintf("%T", a)) | ||
| nonFixingAssertionsIndices = append(nonFixingAssertionsIndices, i) | ||
| } | ||
| } | ||
|
|
||
| sb := strings.Builder{} | ||
| sb.WriteString(cmp.Diff(obj, cpy)) | ||
| for i := range nonFixingAssertions { | ||
| sb.WriteRune('\n') | ||
| sb.WriteString(fmt.Sprintf("the %dth assertion was not able to modify the object to match it", nonFixingAssertionsIndices[i])) | ||
| } | ||
|
|
||
| return sb.String() | ||
| } | ||
|
|
||
| type AssertionFixer[T any] interface { | ||
| AdaptToMatch(object T) T | ||
| } | ||
|
|
||
| type AssertAndFixFunc[T any] struct { | ||
| Assert func(t AssertT, obj T) | ||
| Fix func(obj T) T | ||
| } | ||
|
|
||
| func (a *AssertAndFixFunc[T]) Test(t AssertT, obj T) { | ||
| t.Helper() | ||
| if a.Assert != nil { | ||
| a.Assert(t, obj) | ||
| } | ||
| } | ||
|
|
||
| func (a *AssertAndFixFunc[T]) AdaptToMatch(object T) T { | ||
| if a.Fix != nil { | ||
| return a.Fix(object) | ||
| } | ||
| return object | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| package assertions | ||
|
|
||
| import ( | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| type AssertT interface { | ||
| assert.TestingT | ||
| Helper() | ||
| Logf(format string, args ...any) | ||
| } | ||
|
|
||
| type RequireT interface { | ||
| require.TestingT | ||
| Helper() | ||
| Logf(format string, args ...any) | ||
| } | ||
|
|
||
| type failureTrackingT struct { | ||
| AssertT | ||
| failed bool | ||
| } | ||
|
|
||
| func (t *failureTrackingT) Errorf(format string, args ...interface{}) { | ||
| t.failed = true | ||
| t.AssertT.Errorf(format, args...) | ||
| } |
Oops, something went wrong.
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.
related to the other comment, the MetadataAssertions should be common for any kind/resource we execute the logic for. I believe that including it into the fluent-api chain of methods would be better for an easy use and reference:
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.
See my comment above - I'm personally like the "down and right" approach better because it can visually separate different parts of the object definition similarly to how the object definition looks like when you initialize a struct.
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.
I understand your "down and right" point, but it's all just a criteria for the resource, it doesn't matter if it's part of
metadata,spec,status,dataor any other sub-section of any of the before-mentioned ones.It's also pretty clear what they represent from the name, there is no confusion