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++; | |
} | |
} | |
} | |
} |
handcraftsman
commented
Oct 18, 2012
Cool, thank you, @handcraftsman. My penultimate revision passes your test. It comes down to whether you replace ",." or ".," when cleaning up the list. I had some reason for flipping that, and now I'm irritated with myself for not making a better note of why I had to flip it, since reverting it back still passes all my scenarios, including my real ones.
Oh, markdown converted my asterisks. Should be: whether you replace comma dot star or dot star comma.
In that case, this test breaks it.
[Test]
public void ExpectedHasTrailingEllipsis()
{
var expected = new[] { "1", "2", "3", "..." };
var actual = new[] { "1", "2", "3" };
AssertStructure(actual, expected);
}
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