-
Notifications
You must be signed in to change notification settings - Fork 50
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
Issues implementing a "Not" comparison #147
Comments
https://godoc.org/gotest.tools/assert has some examples of how Since you are comparing against a fixed value, and the failure message will contain the literal source code used in the assertion, you should be able to use If the expected value is a variable then I guess the literal is less useful. Is that the case? |
Negating a As an example of a use case I had, if I had a string assert.Check(t, cmp.Contains(myText, "foo"))
assert.Check(t, !strings.Contains(myText, "bar")) It's workable, but the error messaging on the |
The use of `!strings.Contains()` in one assertion, rather than `!cmp.Contains()` is due to gotestyourself/gotest.tools#147 Signed-off-by: Ian Campbell <ijc@docker.com>
The use of `!strings.Contains()` in one assertion, rather than `!cmp.Contains()` is due to gotestyourself/gotest.tools#147 Signed-off-by: Ian Campbell <ijc@docker.com>
The use of `!strings.Contains()` in one assertion, rather than `!cmp.Contains()` is due to gotestyourself/gotest.tools#147 Signed-off-by: Ian Campbell <ijc@docker.com>
The use of `!strings.Contains()` in one assertion, rather than `!cmp.Contains()` is due to gotestyourself/gotest.tools#147 Signed-off-by: Ian Campbell <ijc@docker.com>
FWIW I implemented something like it here: https://github.com/tiborvass/docker/blob/c03078863fca6f37fe852b36c19d48e2ac025f44/integration-cli/checker/checker.go#L38-L50 which required the creation of another type whose |
i recently tried to do this with the following code, it worked well when it passed, and produced an unhelpful message when it failed. func not(c cmp.Comparison) cmp.Comparison {
return func() cmp.Result {
return InvertedResult{c()}
}
}
type InvertedResult struct {
cmp.Result
}
func (r InvertedResult) Success() bool {
return !r.Result.Success()
} If the FailureMessage interfaces were public, it might be possible to wrap generically enough to prefix the messages with |
func (r InvertedResult) FailureMessage(args []ast.Expr) string {
if f, ok := r.Result.(interface{
FailureMessage() string
}); ok {
return "NOT " + f.FailureMessage()
}
if f, ok := r.Result.(interface{
FailureMessage(args []ast.Expr) string
}); ok {
return "NOT " + f.FailureMessage(args)
}
return "failed, but I do not know why"
} I think something along those lines should work. |
Sorry for not replying to this comment! I agree. In cases where the default message is not great I will add more context at the end: assert.Assert(t, !strings.Contains(myText, "bar"), "got: %v", myText) doer, ok := something.(Doer)
assert.Assert(t, ok, "got: %T", something) Some examples of this in the intro docs would probably be a good addition. It's a bit more typing, but hopefully negative assertions are relatively rare. I think there are even advantages to this approach over of a Comparison. It encourages adding additional context to assertions, and keeps the interface small. |
Yep, that makes sense - thanks. |
Hi, i stumbled over this issue while trying to create something like a "NotDeepEqual" comparision. Thanks |
Hi @eloo , could you share more about your use case? What is the test trying to verify? The reason I ask is because |
Hi @dnephin |
Got this same problem while using require.NotContains(t, fields, "tag_id") |
now that generics exist, could this be revisited? lacking a |
Basically, I tried implementing a generic
Not
comparison likeassert.Assert(t, Not(cmp.Equal(foo, bar))
.The interface here seems a bit limiting as I do not have access to variables, formatting or anything from my
Not
comparison.It seems right now I would have to implement a
NotEqual
comparison in order to gain access variables and the like.I'm wondering if some additional interface could help here? Maybe something that a comparison could optionally implement to make it easier to wrap?
The text was updated successfully, but these errors were encountered: