franela / goblin

Minimal and Beautiful Go testing framework
MIT License
887 stars 79 forks source link

Add .Exists() to assertions #46

Open xetorthio opened 10 years ago

xetorthio commented 10 years ago

So we can do:

  g.Assert(foo).Exists()

This should decide what to do for each type.

t3hmrman commented 9 years ago

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 commented 9 years ago

@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 commented 9 years ago

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 commented 8 years ago

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

marcosnils commented 8 years ago

@ammario there's an opened PR actually for this but the author never got back. Would you like to push it forward? https://github.com/franela/goblin/pull/49