/// <summary> /// Asserts the string to match a list of expected values, separated by new-lines. /// </summary> /// <param name="actual">actual string</param> /// <param name="expected">expected objects - optionally ExpectTo enum to indicate the type of string assertions</param> /// <returns>The remaining strings that haven't been asserted yet. It can be chained to the next assertions.</returns> /// <see cref="ExpectTo"/> public static string Assert(this string actual, params object[] expected) { ExpectTo expects = ExpectTo.Equal; bool continueSearching = ExpectTo.AssertContinuously == (expects & ExpectTo.AssertContinuously); var actuals = actual.TrimEnd().Split('\n').Select( s => s.TrimEnd() ).ToArray(); int lastLine = actuals.Length; int i = 0; int expectedLine = 1; string message; if (expected.Length == 0) { expected = new[] { "" }; } for (int line = 1; line <= lastLine; line++) { string act = actuals[line - 1]; var a = act.TrimEnd(); var o = expected[i]; if (o is ExpectTo) { expects = (ExpectTo)o; if (ExpectTo.Go == (expects & ExpectTo.Go)) { return(string.Join(Environment.NewLine, actuals.Skip(line - 1))); } continueSearching = ExpectTo.AssertContinuously == (expects & ExpectTo.AssertContinuously); expects = expects & (ExpectTo)65535; i++; line--; if (i >= expected.Length) { UnitTestingAssert.Fail($"Expected string has reached to the end of line. Actual at Line {line}: {a}"); return(""); } continue; } if (i >= expected.Length) { if (continueSearching) { return(string.Join(Environment.NewLine, actuals.Skip(line - 1))); } UnitTestingAssert.Fail($"Expected string has reached to the end of line. Actual at Line {line}: {a}"); return(""); } if (!continueSearching || line >= lastLine || expects >= (ExpectTo)ExpectTo.NotEqual) { message = line == (i + 1) ? $"Line {line}" : $"Expected Line {expectedLine}, Actual Line {line}"; } else { message = null; } string e = Convert.ToString(o); if (continueSearching) { bool assertion = Assert(a, expects, e, message); if (assertion && expects >= (ExpectTo)ExpectTo.NotEqual) { continue; } if (!assertion) { if (message == null) { continue; } else { return(string.Join(Environment.NewLine, actuals.Skip(line))); } } } else { bool asserted = Assert(a, expects, e, message); if (!asserted) { return(""); } } i++; expectedLine++; } if (expects < ExpectTo.NotEqual && i < expected.Length) { UnitTestingAssert.Fail($"Expected string has reached to the end of line. Expected line {expectedLine}: {expected[i]}"); } return(""); }
private static bool Assert(string actual, ExpectTo expectsTo, string expected, string message) { expected = expected.TrimEnd(); actual = actual.TrimEnd(); bool r; switch (expectsTo) { case ExpectTo.Contain: r = actual == null && expected == null || actual != null && expected != null && actual.Contains(expected); if (!r && message != null) { StringAssert.Contains(expected, actual, $"{message} - The actual string should contain the expected string."); } return(r); case ExpectTo.EndWith: r = expected == null && actual == null || actual != null && expected != null && actual.EndsWith(expected, StringComparison.Ordinal); if (!r && message != null) { StringAssert.EndsWith(expected, actual, $"{message} - The actual string should end with the expected string."); } return(r); case ExpectTo.Equal: r = expected == actual; if (!r && message != null) { UnitTestingAssert.AreEqual(expected, actual, $"{message} - The actual string should equal to the expected string."); } return(r); case ExpectTo.EqualIgnoringCase: r = string.Equals(expected, actual, StringComparison.InvariantCultureIgnoreCase); if (!r && message != null) { StringAssert.AreEqualIgnoringCase(expected, actual, $"{message}: The actual string does not equal ignoring case to the expected string."); } return(r); case ExpectTo.Match: r = expected == null && actual == null || expected != null && actual != null && Regex.IsMatch(actual, expected); if (!r && message != null) { StringAssert.IsMatch(expected, actual, $"{message} - The actual string should match the expected string pattern."); } return(r); case ExpectTo.NotEndWith: r = (actual == null) != (expected == null) || expected != null && actual != null && !actual.EndsWith(expected, StringComparison.Ordinal); if (!r && message != null) { StringAssert.DoesNotEndWith(expected, actual, $"{message} - The actual string should not end with the expected string."); } return(r); case ExpectTo.NotEqual: r = actual != expected; if (!r && message != null) { UnitTestingAssert.AreNotEqual(expected, actual, $"{message}: The actual string should not equal to the expected string."); } return(r); case ExpectTo.NotEqualIgnoringCase: r = (expected == null) != (actual == null) || expected != null && actual != null && !expected.Equals(actual, StringComparison.OrdinalIgnoreCase); if (!r && message != null) { StringAssert.AreNotEqualIgnoringCase(expected, actual, $"{message} - The actual string should not equal ignoring case to the expected string."); } return(r); case ExpectTo.NotMatch: r = (expected == null) != (actual == null) || expected != null && actual != null && !Regex.IsMatch(actual, expected); if (!r && message != null) { StringAssert.DoesNotMatch(expected, actual, $"{message} - The actual string should not match the expected string."); } return(r); case ExpectTo.NotStartWith: r = (expected == null) != (actual == null) || actual != null && expected != null && !actual.StartsWith(expected, StringComparison.Ordinal); if (!r && message != null) { StringAssert.DoesNotStartWith(expected, actual, $"{message} - The actual string should not start with the expected string."); } return(r); case ExpectTo.StartWith: r = (expected == null) && (actual == null) || actual != null && expected != null && actual.StartsWith(expected, StringComparison.Ordinal); if (!r && message != null) { StringAssert.StartsWith(expected, actual, $"{message} - The actual string should start with the expected string."); } return(r); default: throw new InvalidOperationException($"Expect to {expectsTo} is invalid."); } }
/// <summary> /// Asserts the string to match a list of expected values, separated by new-lines. /// </summary> /// <param name="actual">actual string</param> /// <param name="expected">expected objects - optionally ExpectTo enum to indicate the type of string assertions</param> /// <returns>The remaining strings that haven't been asserted yet. It can be chained to the next assertions.</returns> /// <see cref="ExpectTo"/> public static string Assert(this string actual, params object[] expected) { ExpectTo expects = ExpectTo.Equal; bool continueSearching = ExpectTo.AssertContinuously == (expects & ExpectTo.AssertContinuously); var actuals = actual.TrimEnd().Split('\n').Select( s => s.TrimEnd() ).ToArray(); int lastLine = actuals.Length; int i = 0; int expectedLine = 1; string message; if (expected.Length == 0) { expected = new[] { "" }; } for (int line = 1; line <= lastLine; line++) { string act = actuals[line - 1]; var a = act.TrimEnd(); if (i >= expected.Length) { if (continueSearching) { return(string.Join(Environment.NewLine, actuals.Skip(line - 1))); } else { UnitTestingAssert.AreEqual(null, a, $"Actual line {line} should end at the same time as the expected line."); return(""); } } var o = expected[i]; if (o is ExpectTo) { expects = (ExpectTo)o; continueSearching = ExpectTo.AssertContinuously == (expects & ExpectTo.AssertContinuously); expects = expects & (ExpectTo)65535; i++; line--; if (!continueSearching && i >= expected.Length) { UnitTestingAssert.AreEqual(null, a, $"Actual line {line} should end at the same time as the expected line."); return(""); } continue; } if (i >= expected.Length) { if (continueSearching) { return(string.Join(Environment.NewLine, actuals.Skip(line - 1))); } UnitTestingAssert.AreEqual(null, a, $"Actual line {line} should end at the same time as the expected line."); return(""); } if (!continueSearching || line >= lastLine || expects >= (ExpectTo)ExpectTo.NotEqual) { message = line == (i + 1) ? $"Line {line}" : $"Expected Line {expectedLine}, Actual Line {line}"; } else { message = null; } string e = Convert.ToString(o); if (continueSearching) { bool assertion = Assert(a, expects, e, message); if (assertion && expects >= (ExpectTo)ExpectTo.NotEqual) { continue; } if (!assertion) { if (message == null) { continue; } else { return(string.Join(Environment.NewLine, actuals.Skip(line))); } } } else { bool asserted = Assert(a, expects, e, message); if (!asserted) { return(""); } } i++; expectedLine++; } if (!continueSearching && i < expected.Length) { UnitTestingAssert.AreEqual(expected[i], null, $"The Actual line has passed the end, but there is at least one {expects} at expected line no. {expectedLine}."); } return(""); }