Case insensitive comparison in Contains under nUnit
Asked Answered
N

3

17

I'm trying to assert that a list contains a certain string. Since I'd need the condition to be evaluated case insensitively, I used a workaround (something along this blog post).

However, I'd like to know why there seems not to be a way to make the Assert.Contains method perform the comparison without regard of case sensitivity. Or is there a way to do that? (When I googled it, I only got hits on constraints for the Assert.That method on the official page for nUnit.)

Neddra answered 8/7, 2014 at 14:20 Comment(6)
Are you using nUnit? If so, it contains a StringAssert.AreEqualIgnoringCase method you could use here. Alternatively, you could ToUpper() both strings in your comparison as another bandaid.Bedsore
@Bedsore Hmm... That breaks the Assert.SomeSome looks... But if that's the recommended way to do that, then so be it. As to my question-in-depth: any idea why there's no Contains with an extra parameter to comparison control?Neddra
I have no clue, there may be an actual reason for it, but it could just as well be overlooked. If you really want to stick to the Assert.Some looks, you could use Assert.IsTrue(string.Equals(left, right, StringComparison.OrdinalIgnoreCase));Bedsore
@Bedsore That's what I'm doing already, hehe. I was just worry that there was a reason I didn't know about that would comer to bite me in the sitting device. Put in your thoughts as a reply so I can accept the answer if nobody else outshines you, please.Neddra
I have now, but you don't have to accept it if it doesn't fully answer your question.Bedsore
@Bedsore It does to 99%. The rest, I took the liberty to add to it. And woosh! Now, you're 10 points more trusted by SO. Congrats! :)Neddra
B
27

There is no way to specify ignoreCase in the Assert.Contains. Whether it's something that is overlooked or intended I do not know. You can, however, use

StringAssert.AreEqualIgnoringCase(left, right);

in your unit tests to achieve the same results.

Alternatively, if you wish to stick with the Assert.Foo() "theme", you could do something like this:

Assert.IsTrue(string.Equals(left, right, StringComparison.OrdinalIgnoreCase));

or, since Contains treats arrays:

Assert.IsTrue(list.Any(element => element.ToUpper() == "VILTERSTEN"));

where you call ToUpper() on both the left and right string operands, which effectively makes the comparison ignore case as well. OrdinalIgnoreCase is to ensure some corner cases (read: Turkish) of cultures do not cause unexpected results. If you are interested in reading up on that, have a look at the Turkey test.

Bedsore answered 8/7, 2014 at 18:20 Comment(1)
+1 for the turkey. I though you meant the bird, hehe. Very interesting article.Neddra
C
21

In NUnit 3, the following syntax can be used:

Assert.That(new[] {"red", "green", "blue"}, Does.Contain("RED").IgnoreCase);
Caye answered 13/11, 2017 at 22:53 Comment(1)
This is working fine, is readable, following framework mechanics and in my opinion it should be the accepted answer.Chefoo
F
14

nUnit does provide that and it reads:

Assert.That("red,green,blue".Split(','), Contains.Item("RED").IgnoreCase);
Facer answered 15/12, 2015 at 10:5 Comment(1)
Using 3.10 and .IgnoreCore is not supported anymore. Better to use the answer below with nunit 3.Uncaredfor

© 2022 - 2024 — McMap. All rights reserved.