diff --git a/EasyAssertions/MessageHelper.cs b/EasyAssertions/MessageHelper.cs index 0058d95..a07c329 100644 --- a/EasyAssertions/MessageHelper.cs +++ b/EasyAssertions/MessageHelper.cs @@ -156,56 +156,55 @@ public static string Sample(ICollection items) return "empty."; int remainingItems = SampleSize; - List sample = InnerSample(items, ref remainingItems); + List sample = BuildSample(items); return sample.Count == 1 ? $"[ {sample[0]} ]" : "[" + sample.Select(i => Environment.NewLine + " " + i).Join("") + Environment.NewLine + "]"; - } - - private static List InnerSample(IEnumerable items, ref int remainingItems) - { - List sample = new List(); - foreach (object item in items) + List BuildSample(IEnumerable innerItems) { - if (sample.Any()) - sample[sample.Count - 1] += ","; + List innerSample = new List(); - if (remainingItems == 0) + foreach (object item in innerItems) { - sample.Add("..."); - break; - } + if (innerSample.Any()) + innerSample[innerSample.Count - 1] += ","; - IEnumerable enumerable = item as IEnumerable; - if (enumerable != null) - { - List innerSample = InnerSample(enumerable, ref remainingItems); - switch (innerSample.Count) + if (remainingItems == 0) { - case 0: - sample.Add("[]"); - remainingItems--; - break; - case 1: - sample.Add($"[ {innerSample[0]} ]"); - break; - default: - sample.Add("["); - sample.AddRange(innerSample.Select(s => " " + s)); - sample.Add("]"); - break; + innerSample.Add("..."); + break; + } + + if (item is IEnumerable enumerableItem && !(item is string)) + { + List itemSample = BuildSample(enumerableItem); + switch (itemSample.Count) + { + case 0: + innerSample.Add("[]"); + remainingItems--; + break; + case 1: + innerSample.Add($"[ {itemSample[0]} ]"); + break; + default: + innerSample.Add("["); + innerSample.AddRange(itemSample.Select(s => " " + s)); + innerSample.Add("]"); + break; + } + } + else + { + innerSample.Add(Value(item)); + remainingItems--; } } - else - { - sample.Add(Value(item)); - remainingItems--; - } - } - return sample; + return innerSample; + } } /// diff --git a/UnitTests/MessageHelperTests.cs b/UnitTests/MessageHelperTests.cs index 2feedc3..95347fb 100644 --- a/UnitTests/MessageHelperTests.cs +++ b/UnitTests/MessageHelperTests.cs @@ -57,6 +57,17 @@ public void Sample_NestedEnumerables_OutputsInnerItems() ]", result); } + [Test] + public void Sample_Strings_OutputsWholeStrings() + { + string result = MessageHelper.Sample(new[] { "foo", "bar" }); + + Assert.AreEqual(@"[ + ""foo"", + ""bar"" +]", result); + } + [Test] public void Sample_NestedEnumerable_LimitsTo10Items() {