hardayal / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Add IsEmptyString matcher #45

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi

Could you please add a matcher for checking if provided string is empty? By
empty I mean null or zero length.

I am aware that this check might be easily done by combining two of the
existing matchers, but I think this is a very often used matcher and it is
worth making it easier for developers.

Please see attached proposal.

Thanks.

Original issue reported on code.google.com by jaroslav.kuruc on 4 Sep 2008 at 10:01

Attachments:

GoogleCodeExporter commented 8 years ago
Null and the empty string are not the same concept (despite what Oracle will 
tell
you).  This should not be called IsEmptyString.

Original comment by nat.pr...@gmail.com on 21 Sep 2008 at 11:58

GoogleCodeExporter commented 8 years ago
I like the idea, but not the name.  My usecase is actually for the opposite 
concept.  I want to ensure that a string 
"has content".  This would be particularly useful for tests that expect 
exceptions to be thrown with meaningful 
messages.  E.g.:
try  {...} catch (NullPointerException e) { assertThat(e.getMessage(), 
hasContent()); }
or written in the opposite form:
try  {...} catch (NullPointerException e) { assertThat(e.getMessage(), 
is(not(nullOrEmptyString()))); }

Original comment by gk5...@gmail.com on 2 Oct 2008 at 4:24

GoogleCodeExporter commented 8 years ago
My usecase was also to test that string is not empty, but than can be achieved 
very
easily by: not(isEmptyString())

Original comment by jaroslav.kuruc on 2 Oct 2008 at 4:38

GoogleCodeExporter commented 8 years ago
A "postive" matcher, hasContents or something, would be better than a matcher
isEmptyString that has a misleaing name.

Original comment by nat.pr...@gmail.com on 2 Oct 2008 at 8:17

GoogleCodeExporter commented 8 years ago
so, the test for empty or null would be: not(hasContents())

personally, I suspect that a test for the empty case (whatever it's called) is 
more common. Usually, if I want a 
string to have a value, I'll want to say something about what's in it.

Original comment by smgfree...@gmail.com on 2 Oct 2008 at 9:43

GoogleCodeExporter commented 8 years ago

Original comment by nat.pr...@gmail.com on 24 Oct 2008 at 12:32

GoogleCodeExporter commented 8 years ago
Renamed IsNullOrEmptyString to IsEmptyString and adjusted matching as 
appropriate. Added a 
isEmptyOrNullString method that combines it with an IsNull matcher

Original comment by smgfree...@gmail.com on 20 Nov 2008 at 8:34

GoogleCodeExporter commented 8 years ago

Original comment by smgfree...@gmail.com on 22 Nov 2008 at 12:16