Skip to content

Instantly share code, notes, and snippets.

@scichelli
Created October 4, 2012 22:39
Show Gist options
  • Save scichelli/3836918 to your computer and use it in GitHub Desktop.
Save scichelli/3836918 to your computer and use it in GitHub Desktop.
Collection Comparison
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));
}
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++;
}
}
}
}
@scichelli
Copy link
Author

Oh, markdown converted my asterisks. Should be: whether you replace comma dot star or dot star comma.

@handcraftsman
Copy link

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);
}

@scichelli
Copy link
Author

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