Created
June 2, 2012 00:56
-
-
Save LukeWinikates/2856000 to your computer and use it in GitHub Desktop.
Sometimes, IEnumerable<T>.Any() is the opposite of what you want...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EqualityExtender | |
{ | |
/// <summary> | |
/// Sometimes you want to say this.Name = "Bob" or "Fran". Linq lets you do: | |
/// new[]{"Fran", "Bob"}.Any(n=> n == this.Name); | |
/// | |
/// but isn't it nicer to do: | |
/// | |
/// this.Name.EqualsAnyOf(new[]{"Bob", "Fran"}); | |
/// </summary> | |
public static bool EqualsAnyOf<T>(this T source, IEnumerable<T> others) | |
{ | |
return others.Any(t => t.Equals(source)); | |
} | |
} | |
[TestFixture] | |
public class EqualityExtenderTests { | |
[TestCase("a")] | |
[TestCase("b")] | |
[TestCase("z")] | |
public void AlphabetIncludesLetters(string letter) { | |
Assert.That(letter.EqualsAnyOf(new[] { | |
"a", "b", "z" | |
})); | |
} | |
[TestCase(-1)] | |
public void PositiveNumbersDontIncludeNegatives(int number) { | |
Assert.False(number.EqualsAnyOf(new[] {1, 2, 3})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment