From 626505e9d9448f91084b719f0813ab419f4a3504 Mon Sep 17 00:00:00 2001 From: Adam Bar Date: Sat, 28 Dec 2013 21:44:46 +0100 Subject: [PATCH] fix for ShouldContainOnlyInOrder EnumerableAssertion extension that was broken when the collections were of different lengths. --- .../EnumerableAssertionExtensionsSpecs.cs | 24 +++++++++++++-- .../EnumerableAssertionExtensions.cs | 29 +++++++++++++++++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/source/developwithpassion.specification.specs/EnumerableAssertionExtensionsSpecs.cs b/source/developwithpassion.specification.specs/EnumerableAssertionExtensionsSpecs.cs index e09bb99..0873884 100755 --- a/source/developwithpassion.specification.specs/EnumerableAssertionExtensionsSpecs.cs +++ b/source/developwithpassion.specification.specs/EnumerableAssertionExtensionsSpecs.cs @@ -17,10 +17,28 @@ public class when_comparing_two_sets_of_items : concern Establish c = () => items = Enumerable.Range(1, 3).ToList(); - public class and_the_sets_are_different : when_comparing_two_sets_of_items + public class and_the_sets_have_different_items : when_comparing_two_sets_of_items { Because b = () => - spec.catch_exception(() => EnumerableAssertionExtensions.ShouldContainOnlyInOrder(items,3, 2, 1)); + spec.catch_exception(() => items.ShouldContainOnlyInOrder(3, 2, 1)); + + It should_get_an_exception_when_trying_to_make_an_assertion = () => + spec.exception_thrown.ShouldBeAn(); + } + + public class and_the_expected_set_is_longer_than_actual : when_comparing_two_sets_of_items + { + Because b = () => + spec.catch_exception(() => items.ShouldContainOnlyInOrder(1, 2, 3, 4)); + + It should_get_an_exception_when_trying_to_make_an_assertion = () => + spec.exception_thrown.ShouldBeAn(); + } + + public class and_the_expected_set_is_shorter_than_actual : when_comparing_two_sets_of_items + { + Because b = () => + spec.catch_exception(() => items.ShouldContainOnlyInOrder(1, 2)); It should_get_an_exception_when_trying_to_make_an_assertion = () => spec.exception_thrown.ShouldBeAn(); @@ -29,7 +47,7 @@ public class and_the_sets_are_different : when_comparing_two_sets_of_items public class and_the_sets_are_the_same : when_comparing_two_sets_of_items { Because b = () => - EnumerableAssertionExtensions.ShouldContainOnlyInOrder(items,1, 2, 3); + items.ShouldContainOnlyInOrder(1, 2, 3); } ; It should_not_get_an_exception_when_trying_to_make_the_assertion = () => { }; diff --git a/source/developwithpassion.specifications/extensions/EnumerableAssertionExtensions.cs b/source/developwithpassion.specifications/extensions/EnumerableAssertionExtensions.cs index d3bff16..64c27b0 100755 --- a/source/developwithpassion.specifications/extensions/EnumerableAssertionExtensions.cs +++ b/source/developwithpassion.specifications/extensions/EnumerableAssertionExtensions.cs @@ -15,11 +15,34 @@ public static void ShouldContainOnlyInOrder(this IEnumerable items, params public static void ShouldContainOnlyInOrder(this IEnumerable items, IEnumerable ordered_items) { var source = new List(items); - if (ordered_items.Where((item, index) => ! source[index].Equals(item)).Any()) + var it = ordered_items.GetEnumerator(); + var index = 0; + + while (it.MoveNext()) + { + if (index >= source.Count) + { + throw new SpecificationException( + "The set of items should only contain the items in the order {0}\r\nbut it is actually shorter and does not contain: {1}" + .format_using(ordered_items.EachToUsefulString(), ordered_items.Except(items).EachToUsefulString())); + } + + if (!source[index].Equals(it.Current)) + { + throw new SpecificationException( + "The set of items should only contain the items in the order {0}\r\nbut it actually contains the items: {1}" + .format_using(ordered_items.EachToUsefulString(), items.EachToUsefulString())); + } + + ++index; + } + + if (index < source.Count) { throw new SpecificationException( - "The set of items should only contain the items in the order {0}\r\nbut it actually contains the items:{1}" - .format_using(new object[] {ordered_items.EachToUsefulString(), items.EachToUsefulString()})); + "The set of items should only contain the items in the order {0}\r\nbut it is actually longer and additionally contains: {1}" + .format_using(ordered_items.EachToUsefulString(), items.Except(ordered_items).EachToUsefulString())); + } } }