Created
October 4, 2012 22:39
-
-
Save scichelli/3836918 to your computer and use it in GitHub Desktop.
Collection Comparison
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
private void AssertStructure(string[] actualCollection, string[] expectedCollection) | |
{ | |
var actual = string.Join(",", actualCollection); | |
var expected = string.Join(",", expectedCollection.Select(s => s == "..." ? ".*" : s)).Replace(",.*", ".*").Replace(".*,", ".*"); | |
Assert.That(actual, Is.StringMatching(expected)); | |
} |
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
using NUnit.Framework; | |
using Should; | |
namespace Tests | |
{ | |
[TestFixture] | |
public class CollectionTester | |
{ | |
[Test] | |
public void AllMatch() | |
{ | |
var expected = new[] { "1", "2", "3" }; | |
var actual = new[] { "1", "2", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test, ExpectedException] | |
public void ActualIsMissingOne() | |
{ | |
var expected = new[] { "1", "2", "3" }; | |
var actual = new[] { "1", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test, ExpectedException] | |
public void ActualIsMissingOneAndExpectedHasEllipsis() | |
{ | |
var expected = new[] { "1", "2", "3", "..." }; | |
var actual = new[] { "1", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test, ExpectedException] | |
public void ActualHasTooMany() | |
{ | |
var expected = new[] { "1", "2", "3" }; | |
var actual = new[] { "1", "2", "4", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test, ExpectedException] | |
public void ActualHasWrongItem() | |
{ | |
var expected = new[] { "1", "2", "3" }; | |
var actual = new[] { "1", "4", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test] | |
public void ActualMatchesWithEllipsisReplacingOneItem() | |
{ | |
var expected = new[] { "1", "2", "...", "3" }; | |
var actual = new[] { "1", "2", "4", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test] | |
public void ActualMatchesWithEllipsisReplacingTwoItems() | |
{ | |
var expected = new[] { "1", "2", "...", "3" }; | |
var actual = new[] { "1", "2", "4", "5", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test] | |
public void ExpectedHasUnneededEllipsis() | |
{ | |
var expected = new[] { "1", "2", "...", "3" }; | |
var actual = new[] { "1", "2", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test] | |
public void ExpectedEndsWithEllipsis() | |
{ | |
var expected = new[] { "1", "2", "3", "..." }; | |
var actual = new[] { "1", "2", "3", "4" }; | |
AssertStructure(actual, expected); | |
} | |
[Test] | |
public void ExpectedHasTrailingEllipsis() | |
{ | |
var expected = new[] { "1", "2", "3", "..." }; | |
var actual = new[] { "1", "2", "3" }; | |
AssertStructure(actual, expected); | |
} | |
[Test, ExpectedException] | |
public void ExpectedHasItemsAfterEllipsis() | |
{ | |
var expected = new[] { "1", "2", "3", "...", "5" }; | |
var actual = new[] { "1", "2", "3", "4" }; | |
AssertStructure(actual, expected); | |
} | |
[Test, ExpectedException] | |
public void ExpectedHasItemsAfterEllipsisAndAnotherEllipsis() | |
{ | |
var expected = new[] { "1", "2", "3", "...", "5", "..." }; | |
var actual = new[] { "1", "2", "3", "4" }; | |
AssertStructure(actual, expected); | |
} | |
[Test] | |
public void ExpectedHasItemsAfterEllipsisAndAnotherEllipsisAndSomeMatches() | |
{ | |
var expected = new[] { "1", "2", "3", "...", "5", "..." }; | |
var actual = new[] { "1", "2", "3", "4", "5", "6" }; | |
AssertStructure(actual, expected); | |
} | |
private void AssertStructure(string[] actualCollection, string[] expectedCollection) | |
{ | |
int exp = 0; | |
int act = 0; | |
const string ellipsis = "..."; | |
while (act < actualCollection.Length && !(expectedCollection[exp] == ellipsis && exp == expectedCollection.Length - 1)) | |
{ | |
if (expectedCollection[exp] != ellipsis) | |
{ | |
Assert.That(actualCollection[act], Is.EqualTo(expectedCollection[exp])); | |
} | |
if (expectedCollection[exp] == ellipsis && actualCollection[act] == expectedCollection[exp + 1]) | |
{ | |
exp++; | |
continue; | |
} | |
if (expectedCollection[exp] == ellipsis && actualCollection[act] != expectedCollection[exp + 1]) | |
{ | |
act++; | |
Assert.That(act, Is.LessThan(actualCollection.Length), "Reached end of actuals without finding all expected."); | |
continue; | |
} | |
exp++; | |
act++; | |
} | |
} | |
} | |
} |
New revision, incorporating @handcraftsman's fixes: new test, and both replacements are needed. 130b29
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In that case, this test breaks it.