-
Notifications
You must be signed in to change notification settings - Fork 48
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
Remove testify/assert from Validator Tests #158
Changes from 6 commits
0bee411
1f50ffa
ec81b76
51f7b3c
c489d43
ef86be1
58a5451
3305326
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/davecgh/go-spew/spew" | ||
) | ||
|
||
type myNonzeroInt int | ||
|
@@ -64,6 +64,8 @@ var errZeroTest = errors.New("value must not be 0") | |
var errEmptyTest = errors.New("value must not be empty") | ||
var errMoreTest = errors.New("value must have more than 1 element") | ||
|
||
var validateErrorTemplate = "config:%s test:%s error:%v" | ||
|
||
func (m myNonzeroInt) Validate() error { | ||
return testZeroErr(int(m)) | ||
} | ||
|
@@ -310,7 +312,9 @@ func TestValidationPass(t *testing.T) { | |
for i, test := range tests { | ||
t.Run(fmt.Sprintf("Test config (%v): %#v", i, test), func(t *testing.T) { | ||
err := c.Unpack(test) | ||
assert.NoError(t, err) | ||
if err != nil { | ||
t.Fatalf(validateErrorTemplate, spew.Sdump(c), spew.Sdump(test), err) | ||
} | ||
}) | ||
} | ||
} | ||
|
@@ -535,7 +539,9 @@ func TestValidationFail(t *testing.T) { | |
for i, test := range tests { | ||
t.Run(fmt.Sprintf("Test config (%v): %#v", i, test), func(t *testing.T) { | ||
err := c.Unpack(test) | ||
assert.True(t, err != nil) | ||
if err == nil { | ||
t.Fatalf(validateErrorTemplate, spew.Sdump(c), spew.Sdump(test), err) | ||
} | ||
}) | ||
} | ||
} | ||
|
@@ -632,7 +638,11 @@ func TestValidateRequiredFailing(t *testing.T) { | |
|
||
t.Logf("Unpack returned error: %v", err) | ||
err = err.(Error).Reason() | ||
assert.Equal(t, test.err, err) | ||
logTmpl := "expected:%q got:%q" | ||
if test.err != err { | ||
t.Fatalf(logTmpl, test.err, err) | ||
} | ||
t.Logf(logTmpl, test.err, err) | ||
}) | ||
} | ||
} | ||
|
@@ -751,7 +761,11 @@ func TestValidateNonzeroFailing(t *testing.T) { | |
|
||
t.Logf("Unpack returned error: %v", err) | ||
err = err.(Error).Reason() | ||
assert.Equal(t, test.err, err) | ||
logTmpl := "expected:%q got:%q" | ||
if test.err != err { | ||
t.Fatalf(logTmpl, test.err, err) | ||
} | ||
t.Logf(logTmpl, test.err, err) | ||
}) | ||
} | ||
} | ||
|
@@ -973,7 +987,10 @@ func TestValidationFailOnDefaults(t *testing.T) { | |
for i, test := range tests { | ||
t.Run(fmt.Sprintf("Test config (%v): %#v", i, test), func(t *testing.T) { | ||
err := c.Unpack(test) | ||
assert.True(t, err != nil) | ||
if err == nil { | ||
t.Fatalf(validateErrorTemplate, spew.Sdump(c), spew.Sdump(test), err) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have this pattern in 3 places now, maybe more? Consider to provide a helper function like this (also guarntees that the message is and keeps unified between tests in the future):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm keeping that in mind, for now I've removed some redundancy with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes the C programmer in me sad. Fortunately Printf is more secure in go. :) For Printf style function I'd prefer to not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this in two functions, |
||
|
||
}) | ||
} | ||
} |
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 don't think we always need to log. Appropos want vs. got, I like to use go-cmp in my own projects. Asserting becomes this then:
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.
Non-error test logging only happens when
go test
is given the-v
option, does that change your concern at all?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 run most tests with
-v
and the output is quite verbose already. Just noticed that the go-ucfg travis file does not use-c
, but I will add it to be consistent with other projects (need to update the travis file because I'm adding go modules support anyways).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.
Removed.