forked from ethereum/go-ethereum
-
Couldn't load subscription status.
- Fork 5
chore(core/types): add Test_Body_hasOptionalFields to enforce hasLaterOptionalField is up to date
#113
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
Closed
Closed
chore(core/types): add Test_Body_hasOptionalFields to enforce hasLaterOptionalField is up to date
#113
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77f1419
chore(core/types): add Test_Body_hasOptionalFields to enforce `hasLat…
qdm12 1f3f31e
Simplify rlp tag extraction code
qdm12 3c70266
Rename `hasOptionalField` -> `hasAnyOptionalFieldSet`
qdm12 92d44f2
Rename `Test_Body_hasAnyOptionalFieldSet` -> `TestBody_hasAnyOptional…
qdm12 24b3aec
Call v.Type().Field(i) directly
qdm12 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestBody_hasAnyOptionalFieldSet(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var body Body | ||
|
|
||
| assert.False(t, body.hasAnyOptionalFieldSet(), "body has no optional field set") | ||
|
|
||
| v := reflect.ValueOf(&body).Elem() | ||
| for i := 0; i < v.NumField(); i++ { | ||
| fieldType := v.Type().Field(i) | ||
| tag := fieldType.Tag.Get("rlp") | ||
| if !slices.Contains(strings.Split(tag, ","), "optional") { | ||
| continue | ||
| } | ||
|
|
||
| field := v.Field(i) | ||
| before := field.Interface() | ||
| switch field.Kind() { | ||
| case reflect.Slice: | ||
| slice := reflect.MakeSlice(field.Type(), 1, 1) | ||
| field.Set(slice) | ||
| default: | ||
| t.Errorf("unexpected field kind %q for field %q", field.Kind(), fieldType.Name) | ||
| } | ||
|
|
||
| assert.Truef(t, body.hasAnyOptionalFieldSet(), "body has the optional field %q set", fieldType.Name) | ||
| field.Set(reflect.ValueOf(before)) // reset the field | ||
| } | ||
| } | ||
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
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.
This needs support for
reflect.Pointeras the optional fields can be struct pointers.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.
We can just add them later on a add-when-needed basis. If it's not defined in the switch, the test would fail