-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Change testJSONMarshal #2708
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Change testJSONMarshal #2708
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2708 +/- ##
==========================================
+ Coverage 98.13% 98.16% +0.03%
==========================================
Files 148 148
Lines 12783 12783
==========================================
+ Hits 12544 12548 +4
+ Misses 164 160 -4
Partials 75 75 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Thanks, @exageraldo ! Please remove "Draft" status when you would like me to review this PR. |
…ces before comparison (inside testJSONMarshal)
…itError_Marshal and TestAbuseRateLimitError_Marshal
Some tests are no longer
Since we do a tag validation in the
Theoretically it is just an explicit way of leaving the structure as it already was (did that make any sense?) |
In some cases, I had to use part, _ := json.Marshal(">= 2.0.0, < 2.0.2") github/teams_discussions_test.go bodyHTML, _ := json.Marshal(`<p>test</p>`) In both cases, the idea is just to convert " Do you think we can keep it that way, or can we add two other want = strings.Replace(want, ">", "\u003e", -1)
want = strings.Replace(want, "<", "\u003c", -1) I could not find any other simple way to handle this. And now a new case: contentValue, _ := json.Marshal([]byte{1}) |
Yeah, those cases look a bit odd. |
We'll give this PR a couple weeks to get a reply and have the conflicts resolved, then if we haven't got any updates, it will be closed as abandoned. |
I'll fix these things by this weekend 😄 |
Awesome - @exageraldo - thank you for the update! |
hey, sorry for the delay. |
Why are you changing GitHub workflows? |
I didn't change anything in the workflows. I ended up opting for a "merge" instead of a "rebase" because some changes were disappearing and I didn't know how to solve it. If you look in the "Files Changed" tab, you'll see that all the changes are related to tests only. |
I'll look again when I'm not on my android phone later today. |
OK, I must have been looking only at a merge commit... looking now and it is looking much better, thanks. |
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.
Wow, thank you, @exageraldo !
A have a few questions...
} | ||
|
||
if diff := cmp.Diff(string(w), string(got)); diff != "" { | ||
t.Errorf("json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v", got, w, diff) | ||
// Remove all spaces and new lines from the JSON strings. |
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.
// Remove all spaces and new lines from the JSON strings. | |
// Remove all tabs and newlines from the JSON strings. |
want = strings.Replace(want, "\t", "", -1) | ||
want = strings.Replace(want, "\n", "", -1) | ||
|
||
// Replace the "<" and ">" characters with their unicode escape sequences. |
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 seems odd - why was this necessary?
func TestRateLimitError_Marshal(t *testing.T) { | ||
testJSONMarshal(t, &RateLimitError{}, "{}") | ||
|
||
u := &RateLimitError{ | ||
Rate: Rate{ | ||
Limit: 1, | ||
Remaining: 1, | ||
Reset: Timestamp{referenceTime}, | ||
}, | ||
Message: "msg", | ||
} | ||
|
||
want := `{ | ||
"Rate": { | ||
"limit": 1, | ||
"remaining": 1, | ||
"reset": ` + referenceTimeStr + ` | ||
}, | ||
"message": "msg" | ||
}` | ||
|
||
testJSONMarshal(t, u, want) | ||
} | ||
|
||
func TestAbuseRateLimitError_Marshal(t *testing.T) { | ||
testJSONMarshal(t, &AbuseRateLimitError{}, "{}") | ||
|
||
u := &AbuseRateLimitError{ | ||
Message: "msg", | ||
} | ||
|
||
want := `{ | ||
"message": "msg" | ||
"reason":"reason", | ||
"created_at":` + referenceTimeStr + ` | ||
}` | ||
|
||
testJSONMarshal(t, u, want) | ||
} |
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.
Why were these removed?
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.
wow. It wasn't supposed to happen.
I think it was when I sync the branch. I'll add it again!
@@ -16,7 +16,7 @@ type GetAuditLogOptions struct { | |||
Include *string `url:"include,omitempty"` // Event type includes. Can be one of "web", "git", "all". Default: "web". (Optional.) | |||
Order *string `url:"order,omitempty"` // The order of audit log events. Can be one of "asc" or "desc". Default: "desc". (Optional.) | |||
|
|||
ListCursorOptions | |||
ListCursorOptions `url:",omitempty"` |
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 seems odd - why is this necessary for an embedded struct?
@@ -54,7 +54,7 @@ type SearchOptions struct { | |||
// Whether to retrieve text match metadata with a query | |||
TextMatch bool `url:"-"` | |||
|
|||
ListOptions | |||
ListOptions `url:",omitempty"` |
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 also seems odd - why is this needed?
@@ -17,7 +17,7 @@ type TeamListTeamMembersOptions struct { | |||
// values are "all", "member", "maintainer". Default is "all". | |||
Role string `url:"role,omitempty"` | |||
|
|||
ListOptions | |||
ListOptions `url:",omitempty"` |
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.
Same thing
@@ -196,7 +196,7 @@ type UserListOptions struct { | |||
// Note: Pagination is powered exclusively by the Since parameter, | |||
// ListOptions.Page has no effect. | |||
// ListOptions.PerPage controls an undocumented GitHub API parameter. | |||
ListOptions | |||
ListOptions `url:",omitempty"` |
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.
ditto
We could simplify this by changing testJSONMarshal to this: // Test whether the marshaling of v produces JSON that corresponds
// to the want string.
func testJSONMarshal(t *testing.T, v interface{}, want string) {
t.Helper()
got, err := json.Marshal(v)
if err != nil {
t.Errorf("Unable to marshal JSON for %#v", v)
}
got = normalizeJSON(t, got)
wantBytes := normalizeJSON(t, []byte(want))
diff := cmp.Diff(string(wantBytes), string(got))
if diff != "" {
t.Errorf("json.Marshal returned:\n%s\nwant:\n%s\ndiff:\n%v", string(got), string(wantBytes), diff)
}
}
// normalizeJSON normalizes the JSON in b by unmarshaling and marshaling it
// again.
func normalizeJSON(t *testing.T, b []byte) []byte {
t.Helper()
var v interface{}
err := json.Unmarshal(b, &v)
if err != nil {
t.Errorf("Unable to unmarshal JSON for %v: %v", string(b), err)
}
w, err := json.MarshalIndent(v, "", " ")
if err != nil {
t.Errorf("Unable to marshal JSON for %#v", v)
}
return w
} That way differences like casing are failures but it isn't required to have the same whitespace or order of fields. This is similar to what testify does with |
I will close this PR around the end of January, 2025 as "abandoned" if there is no further response. |
I'll organize the branch to push up something like @WillAbides suggested! |
@WillAbides 's solution worked very well! Thanks a lot! Maybe we can close this PR. |
SGTM. |
Fixes: #2699