public static void ShouldResultToStringHaveLines(this string @this, ToStringContentType toStringContentType, params string[] expectedLines) { TestResultToString(@this, toStringContentType, expectedLines).ThrowExceptionIfFailed(); }
public static TestResult TestResultToString(string toStringOutput, ToStringContentType toStringContentType, params string[] expectedLines) { ThrowHelper.NullArgument(toStringOutput, nameof(toStringOutput)); ThrowHelper.NullArgument(expectedLines, nameof(expectedLines)); if (expectedLines.Length == 0) { throw new ArgumentException("Empty list of expected lines", nameof(expectedLines)); } if (toStringContentType == ToStringContentType.Codes) { if (expectedLines.Length != 1) { throw new ArgumentException($"Expected codes only (all in the single line), but found lines: {expectedLines.Length}", nameof(expectedLines)); } } if (toStringContentType == ToStringContentType.MessagesAndCodes) { if (expectedLines.Length < 3) { throw new ArgumentException($"Expected codes and messages (so at least 3 lines), but found lines: {expectedLines.Length}", nameof(expectedLines)); } if (!string.IsNullOrEmpty(expectedLines[1])) { throw new ArgumentException($"Expected codes and messages (divided by a single empty line), but found in second line: {expectedLines[1]}", nameof(expectedLines)); } if (expectedLines.Skip(2).Any(string.IsNullOrEmpty)) { throw new ArgumentException($"Expected codes and messages (divided by a single empty line), also another empty line", nameof(expectedLines)); } } if (toStringContentType == ToStringContentType.Messages) { if (expectedLines.Any(string.IsNullOrEmpty)) { throw new ArgumentException($"Expected messages only, but found empty line", nameof(expectedLines)); } } var hasCodes = toStringContentType == ToStringContentType.Codes || toStringContentType == ToStringContentType.MessagesAndCodes; var lines = toStringOutput.Split(new[] { Environment.NewLine }, StringSplitOptions.None); if (lines.Length != expectedLines.Length) { return(TestResult.Failed($"Expected amount of lines: {expectedLines.Length}, but found: {lines.Length}")); } if (hasCodes) { var codes = lines[0].Split(new[] { ", " }, StringSplitOptions.None); var expectedCodes = expectedLines[0].Split(new[] { ", " }, StringSplitOptions.None); var missingCodes = expectedCodes.Where(expectedCode => codes.All(c => !string.Equals(c, expectedCode, StringComparison.Ordinal))).OrderBy(a => a).ToArray(); if (missingCodes.Any()) { return(TestResult.Failed($"Expected codes that are missing: {string.Join(", ", missingCodes)}")); } if (codes.Length != expectedCodes.Length) { return(TestResult.Failed($"Expected amount of codes: {expectedCodes.Length}, but found: {codes.Length}")); } } if (toStringContentType == ToStringContentType.MessagesAndCodes) { if (!string.IsNullOrEmpty(lines[1])) { return(TestResult.Failed($"Expected codes and messages (divided by a single line), but found in second line: {lines[1]}")); } } var messageLines = toStringContentType == ToStringContentType.Messages ? lines : lines.Skip(2).ToArray(); var expectedMessageLines = toStringContentType == ToStringContentType.Messages ? expectedLines : expectedLines.Skip(2).ToArray(); var missingMessages = expectedMessageLines.Where(expectedMessageLine => messageLines.All(c => !string.Equals(c, expectedMessageLine, StringComparison.Ordinal))).ToArray(); if (missingMessages.Any()) { return(TestResult.Failed($"Expected messages that are missing: {string.Join(", ", missingMessages.OrderBy(s => s).Select(s => $"`{s}`"))}")); } return(TestResult.Passed()); }