-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
service/dap: avoid hardcoded []byte slices in DAP tests #1881
Conversation
service/dap/daptest/client.go
Outdated
} | ||
|
||
func (c *Client) ExpectContinueResponse(t *testing.T) *dap.ContinueResponse { | ||
m, err := dap.ReadProtocolMessage(c.reader) |
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 think you would be better served by defining a helper function:
func mustReadProtocolMessage(t *testing.T, c *Client) dap.Message {
m, err := dap.ReadProtocolMessage(c.reader)
if err != nil {
t.Error(err)
}
return m
}
then you wouldn't even need these, you could just mustReadProtocolMessage(t, c).(*dap.ConcreteResponseType)
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.
Thanks for the suggestion. Just to make sure I understand precisely, so you mean I should use (1) this helper function to simplify the Expect*
methods, or (2) replace all calls to Expect*
with this?
E.g., replace:
contResp := client.ExpectContinueResponse(t)
With:
contResp := mustReadProtocolMessage(t, client).(*dap.ContinueResponse)
The tradeoff, IMHO is:
- The original has cleaner call sites in tests, so the tests themselves are somewhat easier to read.
- We have some custom logic in some
Expect*
methods (e.g.ExpectInitializeResponse
) and may add more such logic in the future. Removing the helpers means this logic needs to be duplicated at call sites, or have specific helpers just for relevant methods.
On the other hand, your proposal helps remove a bunch of repetitive methods, which is an advantage.
A middle way is (1), replace:
func (c *Client) ExpectContinueResponse(t *testing.T) *dap.ContinueResponse {
m, err := dap.ReadProtocolMessage(c.reader)
if err != nil {
t.Error(err)
}
return m.(*dap.ContinueResponse)
}
By:
func (c *Client) ExpectContinueResponse(t *testing.T) *dap.ContinueResponse {
return c.mustReadProtocolMessage().(*dap.ContinueResponse)
}
This keeps all the Expect*
methods but makes them one-liners, except those where extra logic is needed. The call sites in tests remain short and readable, but the amount of repetitive code is reduced.
WDYT?
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 was thinking just:
contResp := mustReadProtocolMessage(t, client).(*dap.ContinueResponse)
but if you think you'll need to add extra logic then keeping the Expect methods makes sense.
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 like the idea of keeping the Expect methods, but factoring out all the common parts into a mustRead helper.
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.
Except for consistency it should be called ExpectProtocolMessage or the other one should be called MustReadFooRequest.
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.
Done
service/dap/daptest/client.go
Outdated
// ReadBaseMessage reads and returns a json-encoded DAP message. | ||
func (c *Client) ReadBaseMessage() ([]byte, error) { | ||
message, err := dap.ReadBaseMessage(c.reader) | ||
func (c *Client) ExpectDisconnectResponse(t *testing.T) *dap.DisconnectResponse { |
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.
Should these be calling t.Helper()?
(Just learned about it from PR #1882)
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.
Done
Mark t.Helper() where appropriate
The 1.12 macOS builder errors out with:
Is this known flakiness? (timeout?) Is there a way to kick a build without pushing a new commit? |
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.
LGTM
PS. I think the timeout is a problem with travis, actually. |
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.
LGTM
* Remove hardcoded tests * Cleanup - consistent naming, simplify some code, remove unused func * Address review comments * Add Seq checks back * Address more review comments * Remove hardcoded tests * Cleanup - consistent naming, simplify some code, remove unused func * Address review comments * Add Seq checks back * Address more review comments * Make more consistent * More consistency * Simplify Except* methods with a helper Mark t.Helper() where appropriate
* Remove hardcoded tests * Cleanup - consistent naming, simplify some code, remove unused func * Address review comments * Add Seq checks back * Address more review comments * Remove hardcoded tests * Cleanup - consistent naming, simplify some code, remove unused func * Address review comments * Add Seq checks back * Address more review comments * Make more consistent * More consistency * Simplify Except* methods with a helper Mark t.Helper() where appropriate
DAP tests will check server responses by analyzing interesting fields on a field-by-field basis rather than comparing serialized messages with
[]byte
golden strings.Updates #1515