Skip to content
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 .Exists() to assertions #46

Open
xetorthio opened this issue Nov 26, 2014 · 5 comments
Open

Add .Exists() to assertions #46

xetorthio opened this issue Nov 26, 2014 · 5 comments

Comments

@xetorthio
Copy link
Contributor

So we can do:

  g.Assert(foo).Exists()

This should decide what to do for each type.

@t3hmrman
Copy link

t3hmrman commented Dec 7, 2014

A question -- what exactly would this test? is it a non-null check?

I don't know that the Exists test makes sense for Go... Usually it means a check if variable exists at all in the current scope, correct? Pretty hard to get something like that past the Go compiler, unless there's some obvious case I'm overlooking

@marcosnils
Copy link
Member

@t3hmrman we came up with two scenarios where this may be useful:

  1. Instead of doing g.Assert(err != nul).IsTrue() we can just use this new approach
  2. We can provide different implementation per type such as:

strings having >0 length
numbers being greater than 0
etc.

What do you think?

@t3hmrman
Copy link

t3hmrman commented Dec 9, 2014

So I think the only place it makes sense is when dealing with pointers -- that's the only place it feels like to me that checking if something "exists" makes sense. You get a pointer to the thing, and you want to make sure that the thing is actually there (and not null/nil, the empty thing), then you check if it "exists".

If it were to take on only that meaning, it would be synonymous with a not-null check. But then again, if that were the case, then I would rather write something like g.Assert(err).IsNotNull() and get that easy readability...

I think a lot of the point of matchers is the readability they add, and that has a lot to do with the semantics of the statement. If you have something like g.Assert(str).Exists(), I don't think it would be as clear as something like g.Assert(len(str)).IsGreaterThanZero() or something like that. Matchers give us that declarative use, but I think you lose some of that for Exists on other types.

I am hung up on the english semantics of it -- exists just doesn't feel like something I would need to check in Go.

The functionality you're suggesting though would go great with just the general Assert function.

like if I could do g.Assert(x) by itself, and be assured that it had all those other implementations I could do stuff like:
g.Assert(strThatShouldNotBeEmpty)
g.Assert(numberThatShouldNotBeZero)
g.Assert(!err)

or something, but even that's a little cludgy, and not really declarative enough.

@ammario
Copy link

ammario commented Sep 16, 2016

Maybe something like IsNil() or IsNotNil() ? I'm a little annoyed with the g.Assert(err != nil).IsTrue() pattern.

@marcosnils
Copy link
Member

@ammario there's an opened PR actually for this but the author never got back. Would you like to push it forward? #49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants