diff --git a/src/IF.Lastfm.Core.Tests/TestHelper.cs b/src/IF.Lastfm.Core.Tests/TestHelper.cs index 1fbcede..3767969 100644 --- a/src/IF.Lastfm.Core.Tests/TestHelper.cs +++ b/src/IF.Lastfm.Core.Tests/TestHelper.cs @@ -26,15 +26,22 @@ public static string TestSerialise(this T poco) return JsonConvert.SerializeObject(poco, Formatting.Indented, _testSerialiserSettings); } + public static string DifferencesTo(this IEnumerable expected, IEnumerable actual) + { + var first = String.Join(Environment.NewLine, expected); + var second = String.Join(Environment.NewLine, actual); + + return first.DifferencesTo(second); + } + public static string DifferencesTo(this string first, string second) { - const string start = "\n\nDifferences:\n"; + const string lineDiffTemplate = "{0}A: {1}\n{0}B: {2}"; + var start = Environment.NewLine + Environment.NewLine + "Differences:" + Environment.NewLine; var sb = new StringBuilder(start); - var sr1 = new StringReader(first); var sr2 = new StringReader(second); - int count = 1; using (sr1) { @@ -42,18 +49,26 @@ public static string DifferencesTo(this string first, string second) { while (true) { - string line1, line2; - var lineWasRead = sr1.LineRead(out line1); - lineWasRead &= sr2.LineRead(out line2); + var line1 = sr1.ReadLine(); + var line1Read = !String.IsNullOrEmpty(line1); + var line2 = sr2.ReadLine(); + var line2Read = !String.IsNullOrEmpty(line2); - if (!lineWasRead) + if (line1Read && line2Read) + { + if (line1 != line2) + { + var line = string.Format(lineDiffTemplate, count, line1, line2); + sb.AppendLine(line); + } + } + else if (!line1Read && !line2Read) { break; } - - if (line1 != line2) + else // one string still has lines { - var line = string.Format("{0}A: {1}\n{0}B: {2}", count, line1, line2); + var line = string.Format(lineDiffTemplate, count, line1, line2); sb.AppendLine(line); } @@ -61,7 +76,6 @@ public static string DifferencesTo(this string first, string second) } } } - return sb.ToString(); } @@ -81,18 +95,23 @@ public static DateTime RoundToNearestSecond(this DateTime dt) { var ms = dt.Millisecond; - return ms < 500 + return ms < 500 ? dt.AddMilliseconds(-ms) : dt.AddMilliseconds(1000 - ms); } - public static void AssertValues(this PageResponse pageResponse, bool success, int totalItems, int pageSize, int page, int totalPages) - where T : new() + public static void AssertValues( + this PageResponse pageResponse, + bool success, + int totalItems, + int pageSize, + int page, + int totalPages) where T : new() { const string messageFormat = "Page response:\n{0}\n\nExpected {1} to equal {2}"; var json = pageResponse.TestSerialise(); Func testMessage = (property, count) => string.Format(messageFormat, json, property, count); - + Assert.IsTrue(pageResponse.Success == success, testMessage("success", success)); Assert.IsTrue(pageResponse.TotalItems == totalItems, testMessage("totalitems", totalItems)); Assert.IsTrue(pageResponse.PageSize == pageSize, testMessage("pagesize", pageSize));