-
Notifications
You must be signed in to change notification settings - Fork 346
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
Add subtests support to template. #28
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
Changes Unknown when pulling fdb7375 on itsjamie:master into * on cweill:master*. |
I had a quick look over, and looks good so far. Could you please add a couple test files? |
CLAs look good, thanks! |
Changes Unknown when pulling b6eda04 on itsjamie:master into * on cweill:master*. |
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.
Can you please add at least one test case for the subtests scenario?
Also instead of duplicating the render files, make two files in the render
package with the build flags and with a constant for enabling subtests. For example: const enableSubtests = true
.
Hey, sorry about the delay, looks like there are some artifacts in the PR that are unrelated to the final approach I took, which are the duplicated render files you're mentioning. I'll comment where it changes. |
return tmpls.ExecuteTemplate(w, "function", struct { | ||
*models.Function | ||
PrintInputs bool | ||
Subtests bool |
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.
New subtest implementation passes a Subtests bool always set to true for Go 1.7.
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.
Changes Unknown when pulling c1fbe95 on itsjamie:master into * on cweill:master*. |
@cweill I'm struggling with the tests.. when I comment out the line that removes the temporary file that is generated to compare, that file is actually empty. So the error I'm receiving is:
Here is the models.Function:
I would expect to get an error from the TestFunction or Header if the output of the generated test was empty? |
Changes Unknown when pulling 4c565bb on itsjamie:master into * on cweill:master*. |
@@ -42,11 +42,51 @@ func {{.TestName}}(t *testing.T) { | |||
// TODO: Add test cases. | |||
} | |||
for {{if not .IsNaked}} _, tt := {{end}} range tests { | |||
{{if .Subtests}} |
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.
Please prefix with {{- if
@@ -42,11 +42,51 @@ func {{.TestName}}(t *testing.T) { | |||
// TODO: Add test cases. | |||
} | |||
for {{if not .IsNaked}} _, tt := {{end}} range tests { | |||
{{if .Subtests}} | |||
t.Run(tt.name, func(t *testing.T) { |
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.
You should only wrap the code that is different with if .Subtests
. Otherwise there is too much duplication.
return err | ||
} | ||
|
||
func TestFunction(w io.Writer, f *models.Function, printInputs bool) error { |
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.
Instead of having two render files, actually you should add another parameter to TestFunction:
func TestFunction(w io.Writer, f *models.Function, printInputs, useSubtests bool) error
This parameter should be passed all the way from the gotests
package, and the main
file should determine this value. I think it should be enabled by default in Go 1.7, but be disable-able via a flag.
return tmpls.ExecuteTemplate(w, "function", struct { | ||
*models.Function | ||
PrintInputs bool | ||
Subtests bool |
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.
If you pass |
Support -subtests flag on command line, defaults to false to maintain backwards compatibility. Setting to true will cause gotests to utilize the Go 1.7 subtests feature via `t.Run`.
Changes Unknown when pulling 7b394c2 on itsjamie:master into * on cweill:master*. |
Oops. I landed pretty close to what you suggested, however I defaulted to false. I'm not sure why the test is failing to be honest, I ran the generated file in a diff checker and it found no difference. I went through it with my eyes and I couldn't see any difference. I'll minimize the duplication in the template... |
Hm, could it be an extra newline at the end of the output? |
I just saw your latests updates. Mostly looks good except for the one comment about minimizing code duplication in the subtests template. |
Changes Unknown when pulling 4dd98ea on itsjamie:master into * on cweill:master*. |
1 similar comment
Changes Unknown when pulling 4dd98ea on itsjamie:master into * on cweill:master*. |
Changes Unknown when pulling 4c514ca on itsjamie:master into * on cweill:master*. |
You were right @cweill, it was a missing newline at the end of the golden. Tests pass now on my local for 1.7.3 on darwin/amd64. |
@@ -38,6 +40,7 @@ var ( | |||
exportedFuncs = flag.Bool("exported", false, `generate tests for exported functions and methods. Takes precedence over -only and -all`) | |||
allFuncs = flag.Bool("all", false, "generate tests for all functions and methods") | |||
printInputs = flag.Bool("i", false, "print test inputs in error messages") | |||
subtests = flag.Bool("subtests", false, "generate tests using the Go 1.7 subtests feature") |
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 we should enable this by default on Go 1.7. Subtests is one of the key features that makes table driven tests nice to use. As such, we should encourage its usage as much as possible, hence enabling it by default.
We should call the flag nosubtests
which defaults to false, but when provided disables subtests.
@@ -50,6 +53,7 @@ func main() { | |||
ExportedFuncs: *exportedFuncs, | |||
AllFuncs: *allFuncs, | |||
PrintInputs: *printInputs, | |||
Subtests: *subtests, |
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 should use the build protection markup to only include the flag in Go 1.7+. It doesn't make sense to include it in anything below. In Go 1.6 and below, we can just use a const subtests = &false
so that this compiles. How does that sound?
Sounds good to me. I won't get to it tonight but I should tomorrow night.
|
Sounds good. Thank you! |
This changes subtests to be enabled by default.
Changes Unknown when pulling 6ae68cc on itsjamie:master into * on cweill:master*. |
Changes Unknown when pulling 2fb048f on itsjamie:master into * on cweill:master*. |
@@ -0,0 +1,7 @@ | |||
// +build !go1.7 |
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 this file is unnecessary since subtests
will default to false
.
Just one last comment. Otherwise everything looks good to me. |
Fixs a logic error where setting nosubtests to true enabled subtests.
Changes Unknown when pulling 67a2920 on itsjamie:master into * on cweill:master*. |
Changes Unknown when pulling 802f0b3 on itsjamie:master into * on cweill:master*. |
Fixes #23. |
Used an if block and duplicated because of wanting to keep the tabbing
consistent. There might be some way to make it so you don't need to
duplicate?