public void StringChecks() { var valueToTest = "Abc Def Xyz Bin"; // Constraint-style asserts: Assert.That("", Is.Empty); Assert.That(valueToTest, Is.Not.Empty); Assert.That(valueToTest, Does.Contain("Def")); Assert.That(valueToTest, Does.Not.Contain("Bang")); Assert.That(valueToTest, Does.StartWith("Abc")); Assert.That(valueToTest, Does.Not.StartWith("Def")); Assert.That(valueToTest, Does.EndWith("Bin")); Assert.That(valueToTest, Does.Not.EndWith("Xyz")); Assert.That(valueToTest, Is.EqualTo("abc def xyz bin").IgnoreCase); Assert.That(valueToTest, Is.Not.EqualTo("something else").IgnoreCase); Assert.That(valueToTest, Does.Match("^Abc.*Bin$")); Assert.That(valueToTest, Does.Not.Match("^Abc.*Def$")); // Classic-style asserts: StringAssert.Contains("Def", valueToTest); StringAssert.DoesNotContain("Bang", valueToTest); StringAssert.StartsWith("Abc", valueToTest); StringAssert.DoesNotStartWith("Def", valueToTest); StringAssert.EndsWith("Bin", valueToTest); StringAssert.DoesNotEndWith("Xyz", valueToTest); StringAssert.AreEqualIgnoringCase("abc def xyz bin", valueToTest); StringAssert.AreNotEqualIgnoringCase("something else", valueToTest); StringAssert.IsMatch("^Abc.*Bin$", valueToTest); //first param is a regex pattern StringAssert.DoesNotMatch("^Abc.*Def$", valueToTest); //first param is a regex pattern }
public void StringChecks() { var valueToTest = "Foo Bar Baz Bin"; // Constraint-style asserts: Assert.That("", Is.Empty); Assert.That(valueToTest, Is.Not.Empty); Assert.That(valueToTest, Does.Contain("Bar")); Assert.That(valueToTest, Does.Not.Contain("Bang")); Assert.That(valueToTest, Does.StartWith("Foo")); Assert.That(valueToTest, Does.Not.StartWith("Bar")); Assert.That(valueToTest, Does.EndWith("Bin")); Assert.That(valueToTest, Does.Not.EndWith("Baz")); Assert.That(valueToTest, Is.EqualTo("foo bar baz bin").IgnoreCase); Assert.That(valueToTest, Is.Not.EqualTo("something else").IgnoreCase); Assert.That(valueToTest, Does.Match("^Foo.*Bin$")); // param is a regex pattern Assert.That(valueToTest, Does.Not.Match("^Foo.*Bar$")); // param is a regex pattern // Classic-style asserts: StringAssert.Contains("Bar", valueToTest); StringAssert.DoesNotContain("Bang", valueToTest); StringAssert.StartsWith("Foo", valueToTest); StringAssert.DoesNotStartWith("Bar", valueToTest); StringAssert.EndsWith("Bin", valueToTest); StringAssert.DoesNotEndWith("Baz", valueToTest); StringAssert.AreEqualIgnoringCase("foo bar baz bin", valueToTest); StringAssert.AreNotEqualIgnoringCase("something else", valueToTest); StringAssert.IsMatch("^Foo.*Bin$", valueToTest); //first param is a regex pattern StringAssert.DoesNotMatch("^Foo.*Bar$", valueToTest); //first param is a regex pattern }
public void StringEndWith() { // You can test ending substrings. var text = "this is the end"; StringAssert.EndsWith("the end", text); StringAssert.DoesNotEndWith("fini", text); }
public void GetsWordsString() { var words = WordGenerator.GetRandomWordString(10); Assert.IsNotNull(words); Assert.Greater(words.Length, 24); StringAssert.DoesNotStartWith(" ", words); StringAssert.DoesNotEndWith(" ", words); }
public void EndsWith_Test() { string actual = "dcc"; string expected = "c"; StringAssert.EndsWith(expected, actual); expected = "d"; StringAssert.DoesNotEndWith(expected, actual); }
public void stringNotEndWith_Test() { var testor = new TestorTheBarbarian(); //Nunit Classic Model StringAssert.DoesNotEndWith("ra", testor.Name); //NUnit Constraint model Assert.That(testor.Name, Is.Not.StringEnding("ra")); //shouldly syntax testor.Name.ShouldNotEndWith("ra"); }
public void testStrings() { string actual = "ABCDEFG"; StringAssert.Contains("BCD", actual); StringAssert.DoesNotContain("DCB", actual); StringAssert.StartsWith("ABC", actual); StringAssert.DoesNotStartWith("BCD", actual); StringAssert.EndsWith("FG", actual); StringAssert.DoesNotEndWith("BCD", actual); StringAssert.AreEqualIgnoringCase("abcdefg", actual); StringAssert.AreNotEqualIgnoringCase("aaaaaaa", actual); StringAssert.IsMatch("A.+", actual); StringAssert.DoesNotMatch("K", actual); }
public void StringAssertsTest() { string s = "The best way to explain it is to do it"; StringAssert.Contains("explain", s, "No match found"); StringAssert.DoesNotContain("Alice", s); StringAssert.StartsWith("The", s, "No {0} found in {1}", new string[2] { "this", "that" }); StringAssert.DoesNotStartWith("An", s); StringAssert.EndsWith("do it", s, "Incorrect ending"); StringAssert.DoesNotEndWith("do that", s); string x = "ThE bEst WaY to eXplaiN iT iS tO dO iT"; StringAssert.AreEqualIgnoringCase(x, s, "No match found"); }
public void StringAssertsTest() { StringAssert.Contains("world", "Hello world!"); StringAssert.DoesNotContain("word", "Hello world!"); StringAssert.StartsWith("Hello", "Hello world!"); StringAssert.DoesNotStartWith("Hell0", "Hello world!"); StringAssert.EndsWith("world!", "Hello world!"); StringAssert.DoesNotEndWith("world?", "Hello world!"); StringAssert.AreEqualIgnoringCase("Hello world!", "hello WORLD!"); StringAssert.AreNotEqualIgnoringCase("Hello world!", "hello WORD!"); StringAssert.IsMatch(@"\w*\s\w*", "Hello world!"); StringAssert.DoesNotMatch(@"\w\s\s\w", "Hello world!"); }
public void UpdateCopyrightFileWorks() { var copyrights = ReadCopyrightFileFromString(_readTestData); Assert.AreEqual(7, copyrights.Paragraphs.Count); var ackDict = SIL.Acknowledgements.AcknowledgementsProvider.CollectAcknowledgements(); Assert.IsNotNull(ackDict); Assert.IsNotNull(ackDict.Keys); foreach (var key in ackDict.Keys) { copyrights.AddOrUpdateParagraphFromAcknowledgement(ackDict[key], ""); } // Find all License paragraphs and ensure no duplicates var licensesDescribed = new List <string>(); foreach (var p in copyrights.Paragraphs) { if (p.Fields.Count == 1 && p.Fields[0].Tag == "License") { CollectionAssert.DoesNotContain(licensesDescribed, p.Fields[0].Value); licensesDescribed.Add(p.Fields[0].Value); } } // Find all Files paragraphs and ensure no duplicates var fileParas = new List <string>(); foreach (var p in copyrights.Paragraphs) { if (p.Fields.Count >= 3 && p.Fields[0].Tag == "Files") { CollectionAssert.DoesNotContain(fileParas, p.Fields[0].Value); // but we may have a file-only spec that would match a complete pathname, // we need to check for that case as well. foreach (var file in fileParas) { StringAssert.DoesNotEndWith("/" + p.Fields[0].Value, file); } fileParas.Add(p.Fields[0].Value); } } }
public void TestKeyValueParse() { // Given string[] pairs = { "key=value", "k=v", "except=bro", "awesome====123123", " keytrimmed = value trimmed " }; // When IReadOnlyDictionary <string, object> args = MetadataParser.Parse(pairs); // Then Assert.AreEqual(pairs.Length, args.Count); foreach (KeyValuePair <string, object> arg in args) { Assert.NotNull(arg.Value, "Argument value should not be null."); StringAssert.DoesNotStartWith(" ", arg.Key, "Arguments key should be trimmed."); StringAssert.DoesNotEndWith(" ", arg.Key, "Arguments key should be trimmed."); StringAssert.DoesNotStartWith(" ", (string)arg.Value, "Arguments value should be trimmed."); StringAssert.DoesNotEndWith(" ", (string)arg.Value, "Arguments value should be trimmed."); } }
public void TestKeyValueParse() { // Given var pairs = new string[] { "key=value", "k=v", "except=bro", "awesome====123123", " keytrimmed = value trimmed " }; // When var args = _parser.Parse(pairs); // Then Assert.AreEqual(pairs.Length, args.Count); foreach (var arg in args) { Assert.NotNull(arg.Value, "Argument value should not be null."); StringAssert.DoesNotStartWith(" ", arg.Key, "Arguments key should be trimmed."); StringAssert.DoesNotEndWith(" ", arg.Key, "Arguments key should be trimmed."); StringAssert.DoesNotStartWith(" ", (string)arg.Value, "Arguments value should be trimmed."); StringAssert.DoesNotEndWith(" ", (string)arg.Value, "Arguments value should be trimmed."); } }
public void stringNotEndWith_Test() { var testor = new TestorTheBarbarian(); //comment above and uncomment below to fail test //var testor = new Enemy() { Name = "Bugra" }; //Nunit Classic Model StringAssert.DoesNotEndWith("ra", testor.Name); // -->Expected: not String ending with "do" But was: "Master Codo" //NUnit Constraint model Assert.That(testor.Name, Is.Not.StringEnding("ra")); // -->Expected: not String ending with "do" But was: "Master Codo" //FluentAssertions syntax testor.Name.Should().NotEndWith("ra"); // --> Result Message: Expected string "Bugra" not to end with "ra". testor.Name.Should().NotEndWith("do", because: "object is a testor"); // --> Result Message: Expected string "Bugra" not to end with "ra" because object is a testor. }
public void DoesNotEndWithFails() { Assert.Throws <AssertionException>(() => StringAssert.DoesNotEndWith("abc", "***abc")); }
public void DoesNotEndWith() { StringAssert.DoesNotEndWith("x", "abc"); }
public void DoesNotEndWithFails() { StringAssert.DoesNotEndWith("abc", "***abc"); }
/// <summary> /// Tests whether the specified string ends with the specified substring /// and throws an exception if the test string does not end with the /// substring. /// </summary> /// <param name="assert">The <see cref="StringAssert"/> instance to extend.</param> /// <param name="value"> /// The string that is expected to end with <paramref name="substring"/>. /// </param> /// <param name="substring"> /// The string expected to be a suffix of <paramref name="value"/>. /// </param> /// <param name="message"> /// The message to include in the exception when <paramref name="value"/> /// does not end with <paramref name="substring"/>. The message is /// shown in test results. /// </param> /// <param name="comparisonType"> /// The comparison method to compare strings <paramref name="comparisonType"/>. /// </param> /// <exception cref="AssertFailedException"> /// Thrown if <paramref name="value"/> does not end with /// <paramref name="substring"/>. /// </exception> public static void DoesNotEndWith(this StringAssert assert, string value, string substring, string message, StringComparison comparisonType) { assert.DoesNotEndWith(value, substring, message, comparisonType, Empty); }
/// <summary> /// Tests whether the specified string ends with the specified substring /// and throws an exception if the test string does not end with the /// substring. /// </summary> /// <param name="assert">The <see cref="StringAssert"/> instance to extend.</param> /// <param name="value"> /// The string that is expected to end with <paramref name="substring"/>. /// </param> /// <param name="substring"> /// The string expected to be a suffix of <paramref name="value"/>. /// </param> /// <param name="message"> /// The message to include in the exception when <paramref name="value"/> /// does not end with <paramref name="substring"/>. The message is /// shown in test results. /// </param> /// <param name="parameters"> /// An array of parameters to use when formatting <paramref name="message"/>. /// </param> /// <exception cref="AssertFailedException"> /// Thrown if <paramref name="value"/> does not end with /// <paramref name="substring"/>. /// </exception> public static void DoesNotEndWith(this StringAssert assert, string value, string substring, string message, params object?[] parameters) { assert.DoesNotEndWith(value, substring, message, StringComparison.Ordinal, parameters); }
/// <summary> /// Tests whether the specified string ends with the specified substring /// and throws an exception if the test string does not end with the /// substring. /// </summary> /// <param name="assert">The <see cref="StringAssert"/> instance to extend.</param> /// <param name="value"> /// The string that is expected to end with <paramref name="substring"/>. /// </param> /// <param name="substring"> /// The string expected to be a suffix of <paramref name="value"/>. /// </param> /// <param name="message"> /// The message to include in the exception when <paramref name="value"/> /// does not end with <paramref name="substring"/>. The message is /// shown in test results. /// </param> /// <exception cref="AssertFailedException"> /// Thrown if <paramref name="value"/> does not end with /// <paramref name="substring"/>. /// </exception> public static void DoesNotEndWith(this StringAssert assert, string value, string substring, string message) { assert.DoesNotEndWith(value, substring, message, StringComparison.Ordinal); }
/// <summary> /// Tests whether the specified string ends with the specified substring /// and throws an exception if the test string does not end with the /// substring. /// </summary> /// <param name="assert">The <see cref="StringAssert"/> instance to extend.</param> /// <param name="value"> /// The string that is expected to end with <paramref name="substring"/>. /// </param> /// <param name="substring"> /// The string expected to be a suffix of <paramref name="value"/>. /// </param> /// <exception cref="AssertFailedException"> /// Thrown if <paramref name="value"/> does not end with /// <paramref name="substring"/>. /// </exception> public static void DoesNotEndWith(this StringAssert assert, string value, string substring) { assert.DoesNotEndWith(value, substring, String.Empty, StringComparison.Ordinal); }
[TestCase("hoge", "aaaHOGE")] // Test OK. public void DoesNotEndWithTest(string expected, string actual) { StringAssert.DoesNotEndWith(expected, actual); }
public void TestAssertions() { #region Condition assertions Assert.True(true, "Assert.True and Assert.IsTrue test that the specified condition is true. "); Assert.IsTrue(true); Assert.False(false, "Assert.False and Assert.IsFalse test that the specified condition is false."); Assert.IsFalse(false); Assert.Null(null); Assert.IsNull(null, "Assert.Null and Assert.IsNull test that the specified object is null."); Assert.IsNotNull("10"); Assert.NotNull("10", ""); Assert.IsNaN(Double.NaN, "Assert.IsNaN tests that the specified double value is NaN (Not a number)."); Assert.IsEmpty(""); Assert.IsEmpty(new List <object>()); Assert.IsNotEmpty(new List <object> { 1 }); Assert.IsNotEmpty("MyTestString"); #endregion #region Equality Assert.AreEqual(true, true, "Assert.AreEqual tests whether the two arguments are equal."); Assert.AreNotEqual(true, false); #endregion #region Identity Assert.AreSame(string.Empty, string.Empty, "Assert.AreNotSame tests that the two arguments do not reference the same object."); Assert.AreNotSame(new object(), new object()); //both objects are refering to same object object a = new object(); object b = a; Assert.AreSame(a, b); #endregion #region Comparision //Contrary to the normal order of Asserts, these methods are designed to be read in the "natural" English-language or mathematical order. //Thus Assert.Greater(x, y) asserts that x is greater than y (x > y). Assert.Greater(Decimal.MaxValue, Decimal.MinValue, "Assert.Greater tests whether one object is greater than than another"); Assert.GreaterOrEqual(Decimal.MinValue, Decimal.MinValue); Assert.Less(Decimal.MinValue, Decimal.MaxValue); Assert.LessOrEqual(Decimal.MinValue, Decimal.MinValue); #endregion #region Types Assert.IsInstanceOf <decimal>(decimal.MinValue, "Assert.IsInstanceOf succeeds if the object provided as an actual value is an instance of the expected type."); Assert.IsNotInstanceOf <int>(decimal.MinValue); Assert.IsNotAssignableFrom(typeof(List <Type>), string.Empty, "Assert.IsAssignableFrom succeeds if the object provided may be assigned a value of the expected type."); Assert.IsAssignableFrom(typeof(List <decimal>), new List <decimal>()); Assert.IsNotAssignableFrom <List <Type> >(string.Empty); Assert.IsAssignableFrom <List <decimal> >(new List <decimal>()); #endregion #region Strings //The StringAssert class provides a number of methods that are useful when examining string values StringAssert.Contains("london", "london"); StringAssert.StartsWith("Food", "FoodPanda"); StringAssert.EndsWith("rangers", "Powerrangers"); StringAssert.AreEqualIgnoringCase("DOG", "dog"); StringAssert.IsMatch("[10-29]", "15"); StringAssert.DoesNotContain("abc", "defghijk"); StringAssert.DoesNotEndWith("abc", "abcdefgh"); StringAssert.DoesNotMatch("abc", "def"); string a1 = "abc"; string b1 = "abcd"; //StringAssert.ReferenceEquals(a1, b1); need to debug why it's failing Assert.Contains(string.Empty, new List <object> { string.Empty }, "Assert.Contains is used to test whether an object is contained in a collection."); #endregion #region Collections //The CollectionAssert class provides a number of methods that are useful when examining collections and //their contents or for comparing two collections. These methods may be used with any object implementing IEnumerable. //The AreEqual overloads succeed if the corresponding elements of the two collections are equal. //AreEquivalent tests whether the collection contents are equal, but without regard to order. //In both cases, elements are compared using NUnit's default equality comparison. CollectionAssert.AllItemsAreInstancesOfType(new List <decimal> { 0m }, typeof(decimal)); CollectionAssert.AllItemsAreNotNull(new List <decimal> { 0m }); CollectionAssert.AllItemsAreUnique(new List <decimal> { 0m, 1m }); CollectionAssert.AreEqual(new List <decimal> { 0m }, new List <decimal> { 0m }); CollectionAssert.AreEquivalent(new List <decimal> { 0m, 1m }, new List <decimal> { 1m, 0m }); // Same as AreEqual though order does not mater CollectionAssert.AreNotEqual(new List <decimal> { 0m }, new List <decimal> { 1m }); CollectionAssert.AreNotEquivalent(new List <decimal> { 0m, 1m }, new List <decimal> { 1m, 2m }); // Same as AreNotEqual though order does not matter CollectionAssert.Contains(new List <decimal> { 0m, 1m }, 1m); CollectionAssert.DoesNotContain(new List <decimal> { 0m, 1m }, 2m); CollectionAssert.IsSubsetOf(new List <decimal> { 1m }, new List <decimal> { 0m, 1m }); // {1} is considered a SubSet of {1,2} CollectionAssert.IsNotSubsetOf(new List <decimal> { 1m, 2m }, new List <decimal> { 0m, 1m }); CollectionAssert.IsEmpty(new List <decimal>()); CollectionAssert.IsNotEmpty(new List <decimal> { 1m }); CollectionAssert.IsOrdered(new List <decimal> { 1m, 2m, 3m }); CollectionAssert.IsOrdered(new List <string> { "a", "A", "b" }, StringComparer.CurrentCultureIgnoreCase); CollectionAssert.IsOrdered(new List <int> { 3, 2, 1 }, "The list is ordered"); // Only supports ICompare and not ICompare<T> as of version 2.6 #endregion #region File Assert //Various ways to compare a stream or file. //The FileAssert class provides methods for comparing or verifying the existence of files //Files may be provided as Streams, as FileInfos or as strings giving the path to each file. FileAssert.AreEqual(new MemoryStream(), new MemoryStream()); FileAssert.AreEqual(new FileInfo("MyFile.txt"), new FileInfo("MyFile.txt")); FileAssert.AreEqual("MyFile.txt", "MyFile.txt"); FileAssert.AreNotEqual(new FileInfo("MyFile.txt"), new FileInfo("MyFile2.txt")); FileAssert.AreNotEqual("MyFile.txt", "MyFile2.txt"); FileAssert.AreNotEqual(new FileStream("MyFile.txt", FileMode.Open), new FileStream("MyFile2.txt", FileMode.Open)); FileAssert.Equals(new FileInfo("MyFile.txt"), new FileInfo("MyFile.txt")); FileAssert.ReferenceEquals(new FileInfo("MyFile.txt"), new FileInfo("MyFile.txt")); #endregion #region Utilities // Used to stop test execution and mark them pass or fail or skip if (Convert.ToInt32(DateTime.Now.Second) > 30) { Console.WriteLine("Exaple on Utilities assertions"); Assert.Ignore(); } if (Convert.ToInt32(DateTime.Now.Second) < 30) { Assert.Inconclusive(); } // Defining the failed message Assert.IsTrue(true, "A failed message here"); Assert.Pass(); Assert.Fail(); #endregion }
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."); } }