Skip to content

Commit

Permalink
Order sensitive comparison should display missing rows if there are m…
Browse files Browse the repository at this point in the history
…ore expected rows in the table than were returned (#1098)

* Add test showing failure case in sensitive comparison.

* Note missing items in order-sensitive compare when less items in set than table.

* Refactor to only generate absent items once.

"Results are present but there should be no results" is a subcondition
of "extra results exist".
  • Loading branch information
midgleyc authored and SabotageAndi committed Jul 17, 2018
1 parent 1d95f26 commit 71511d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
46 changes: 24 additions & 22 deletions TechTalk.SpecFlow/Assist/SetComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,29 @@ private SafetyTableDiffExceptionBuilder<T> BuildTheTableDiffExceptionBuilder()
return makeSureTheFormattingWorkAboveDoesNotResultInABadException;
}

private IEnumerable<int> GetExpectedItemsNotFoundInTheData(IEnumerable<T> set, bool sequentialEquality)
{
var getListOfExpectedItemsThatCouldNotBeFound =
sequentialEquality
? new Func<IEnumerable<T>, IEnumerable<int>>(GetListOfExpectedItemsThatCouldNotBeFoundOrderSensitive)
: new Func<IEnumerable<T>, IEnumerable<int>>(GetListOfExpectedItemsThatCouldNotBeFoundOrderInsensitive);

var expectedItemsNotFoundInTheData = getListOfExpectedItemsThatCouldNotBeFound(set);
return expectedItemsNotFoundInTheData;
}

public void CompareToSet(IEnumerable<T> set, bool sequentialEquality)
{
AssertThatAllColumnsInTheTableMatchToPropertiesOnTheType();

if (ThereAreNoResultsAndNoExpectedResults(set))
return;

var getListOfExpectedItemsThatCouldNotBeFound =
sequentialEquality ?
new Func<IEnumerable<T>, IEnumerable<int>>(GetListOfExpectedItemsThatCouldNotBeFoundOrderSensitive) :
new Func<IEnumerable<T>, IEnumerable<int>>(GetListOfExpectedItemsThatCouldNotBeFoundOrderInsensitive);

if (ThereAreResultsWhenThereShouldBeNone(set))
ThrowAnExpectedNoResultsError(set, getListOfExpectedItemsThatCouldNotBeFound(set));
var expectedItemsNotFoundInTheData = GetExpectedItemsNotFoundInTheData(set, sequentialEquality);

AssertThatTheItemsMatchTheExpectedResults(set, getListOfExpectedItemsThatCouldNotBeFound);
AssertThatTheItemsMatchTheExpectedResults(set, expectedItemsNotFoundInTheData);

AssertThatNoExtraRowsExist(set, getListOfExpectedItemsThatCouldNotBeFound(set));
AssertThatNoExtraRowsExist(set, expectedItemsNotFoundInTheData);
}

private void AssertThatNoExtraRowsExist(IEnumerable<T> set, IEnumerable<int> listOfMissingItems)
Expand All @@ -52,25 +57,13 @@ private void AssertThatNoExtraRowsExist(IEnumerable<T> set, IEnumerable<int> lis
ThrowAnErrorDetailingWhichItemsAreMissing(listOfMissingItems);
}

private void ThrowAnExpectedNoResultsError(IEnumerable<T> set, IEnumerable<int> listOfMissingItems)
{
ThrowAnErrorDetailingWhichItemsAreMissing(listOfMissingItems);
}

private bool ThereAreResultsWhenThereShouldBeNone(IEnumerable<T> set)
{
return set.Any() && !table.Rows.Any();
}

private bool ThereAreNoResultsAndNoExpectedResults(IEnumerable<T> set)
{
return !set.Any() && !table.Rows.Any();
}

private void AssertThatTheItemsMatchTheExpectedResults(IEnumerable<T> set, Func<IEnumerable<T>, IEnumerable<int>> getListOfExpectedItemsThatCouldNotBeFound)
private void AssertThatTheItemsMatchTheExpectedResults(IEnumerable<T> set, IEnumerable<int> listOfMissingItems)
{
var listOfMissingItems = getListOfExpectedItemsThatCouldNotBeFound(set);

if (ExpectedItemsCouldNotBeFound(listOfMissingItems))
ThrowAnErrorDetailingWhichItemsAreMissing(listOfMissingItems);
}
Expand Down Expand Up @@ -119,6 +112,15 @@ private IEnumerable<int> GetListOfExpectedItemsThatCouldNotBeFoundOrderSensitive
actualItems.Skip(table.RowCount).Select(i => new TableDifferenceItem<T>(i)))
.ToList();

var extraTableItems = table.Rows.Count() - actualItems.Count;
if (extraTableItems > 0)
{
for (var index = actualItems.Count; index < table.Rows.Count(); index++)
{
listOfMissingItems.Add(index + 1);
}
}

return listOfMissingItems;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ public void Returns_a_descriptive_error_when_three_results_exist_when_one_expect
".AgnosticLineBreak());
}

[Test]
public void Returns_a_descriptive_error_when_no_results_exist_when_one_expected()
{
var table = new Table("StringProperty");
table.AddRow("orange");

var items = new SetComparisonTestObject[] { };

var exception = GetTheExceptionThrowByComparingThese(table, items);

exception.Message.AgnosticLineBreak().Should().Be(@"
| StringProperty |
- | orange |
".AgnosticLineBreak());
}

protected ComparisonException GetTheExceptionThrowByComparingThese(Table table, SetComparisonTestObject[] items)
{
try
Expand Down

0 comments on commit 71511d8

Please sign in to comment.