Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remaining list of assertion not implemented #4

Open
1 of 2 tasks
dicko2 opened this issue Aug 22, 2023 · 6 comments
Open
1 of 2 tasks

Remaining list of assertion not implemented #4

dicko2 opened this issue Aug 22, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@dicko2
Copy link
Collaborator

dicko2 commented Aug 22, 2023

Feature request

Type

  • - Enhancement - completely new feature
  • - Improvement - make what we have better

Asserts TODO

Remaining list of assertion not implemented in a code fix

  • Assert.Greater
  • Assert.GreaterOrEqual
  • Assert.Less
  • Assert.LessOrEqual
  • Assert.Positive
  • Assert.Negative
  • Assert.IsNotAssignableFrom
  • Assert.Catch
  • Assert.CatchAsync
  • Assert.Pass
  • Assert.Fail
  • Assert.Ignore
  • Assert.Inconclusive
  • String Assert
  • Collection Assert
  • File Assert
  • Directory Assert
  • CollectionAssert.AreEquivalent
  • Assert.IsNotNull
@dicko2 dicko2 added the enhancement New feature or request label Aug 22, 2023
@dicko2
Copy link
Collaborator Author

dicko2 commented Nov 1, 2023

IsInstanceOf
IsNotNull

Are common ones that are missing

IsTrue seems to have a bug

Assert.IsTrue(actual.Items.Count > 0);
// converts to
actual.Items.Count > 0.ShouldBeTrue();

@dicko2
Copy link
Collaborator Author

dicko2 commented Mar 13, 2024

var BoosterPropertyItems = new List<int>();
BoosterPropertyItems.Add(1548);

            Assert.NotNull(BoosterPropertyItems.SingleOrDefault(o => o == 1548));
            Assert.Null(BoosterPropertyItems.SingleOrDefault(o => o == 6542));
// converts to
BoosterPropertyItems.SingleOrDefault(o => o == 1548).ShouldNotBeNull();
BoosterPropertyItems.SingleOrDefault(o => o == 6542).ShouldBeNull();

@joeldickson
Copy link
Contributor

joeldickson commented Sep 5, 2024

Assert.Contains(expectedPermission.Id, mappedPermissionIds);

This didnt work, both are ints inside a for loop

should be

mappedPermissionIds.ShouldContain(expectedPermission.Id);

@joeldickson
Copy link
Contributor

  1. Assert.Greater / Assert.GreaterOrEqual
// NUnit
Assert.Greater(5, 3);
Assert.GreaterOrEqual(5, 5);

// Shouldly
5.ShouldBeGreaterThan(3);
5.ShouldBeGreaterThanOrEqualTo(5);
  1. Assert.Less / Assert.LessOrEqual
// NUnit
Assert.Less(3, 5);
Assert.LessOrEqual(5, 5);

// Shouldly
3.ShouldBeLessThan(5);
5.ShouldBeLessThanOrEqualTo(5);
  1. Assert.Positive / Assert.Negative
// NUnit
Assert.Positive(5);
Assert.Negative(-5);

// Shouldly
5.ShouldBePositive();
(-5).ShouldBeNegative();
  1. Assert.IsNotAssignableFrom
// NUnit
Assert.IsNotAssignableFrom<string>(new object());

// Shouldly
new object().ShouldNotBeAssignableTo<string>();
  1. Assert.Catch / Assert.CatchAsync
// NUnit
Assert.Catch<ArgumentException>(() => throw new ArgumentException());
Assert.CatchAsync<ArgumentException>(async () => await ThrowsExceptionAsync());

// Shouldly
Should.Throw<ArgumentException>(() => throw new ArgumentException());
Should.ThrowAsync<ArgumentException>(async () => await ThrowsExceptionAsync());
  1. Assert.Pass / Assert.Fail / Assert.Ignore / Assert.Inconclusive
    Shouldly doesn't have direct equivalents for these NUnit control flow assertions. These are typically handled differently in test frameworks that use Shouldly.

  2. String Assert

// NUnit
StringAssert.Contains("expected", "unexpected");
StringAssert.StartsWith("start", "starting");
StringAssert.EndsWith("end", "ending");

// Shouldly
"unexpected".ShouldContain("expected");
"starting".ShouldStartWith("start");
"ending".ShouldEndWith("end");
  1. Collection Assert
// NUnit
CollectionAssert.Contains(new[] { 1, 2, 3 }, 2);
CollectionAssert.IsSubsetOf(new[] { 1, 2 }, new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2, 3 }.ShouldContain(2);
new[] { 1, 2 }.ShouldBeSubsetOf(new[] { 1, 2, 3 });
  1. File Assert / Directory Assert
    Shouldly doesn't have built-in file/directory assertions. You'd typically use System.IO methods with Shouldly assertions.

  2. CollectionAssert.AreEquivalent

// NUnit
CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, new[] { 3, 2, 1 });

// Shouldly
new[] { 1, 2, 3 }.ShouldBe(new[] { 3, 2, 1 }, ignoreOrder: true);
  1. Assert.IsNotNull
// NUnit
Assert.IsNotNull(new object());

// Shouldly
new object().ShouldNotBeNull();

@joeldickson
Copy link
Contributor

  1. CollectionAssert.AllItemsAreInstancesOf
// NUnit
CollectionAssert.AllItemsAreInstancesOf(new[] { "a", "b", "c" }, typeof(string));

// Shouldly
new[] { "a", "b", "c" }.ShouldAllBe(item => item.ShouldBeOfType<string>());
  1. CollectionAssert.AllItemsAreNotNull
// NUnit
CollectionAssert.AllItemsAreNotNull(new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2, 3 }.ShouldNotContainNull();
  1. CollectionAssert.AllItemsAreUnique
// NUnit
CollectionAssert.AllItemsAreUnique(new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2, 3 }.ShouldBeUnique();
  1. CollectionAssert.AreEqual
// NUnit
CollectionAssert.AreEqual(new[] { 1, 2, 3 }, new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2, 3 }.ShouldBe(new[] { 1, 2, 3 });
  1. CollectionAssert.AreNotEqual
// NUnit
CollectionAssert.AreNotEqual(new[] { 1, 2, 3 }, new[] { 3, 2, 1 });

// Shouldly
new[] { 1, 2, 3 }.ShouldNotBe(new[] { 3, 2, 1 });
  1. CollectionAssert.Contains
// NUnit
CollectionAssert.Contains(new[] { 1, 2, 3 }, 2);

// Shouldly
new[] { 1, 2, 3 }.ShouldContain(2);
  1. CollectionAssert.DoesNotContain
// NUnit
CollectionAssert.DoesNotContain(new[] { 1, 2, 3 }, 4);

// Shouldly
new[] { 1, 2, 3 }.ShouldNotContain(4);
  1. CollectionAssert.IsEmpty
// NUnit
CollectionAssert.IsEmpty(new int[] { });

// Shouldly
new int[] { }.ShouldBeEmpty();
  1. CollectionAssert.IsNotEmpty
// NUnit
CollectionAssert.IsNotEmpty(new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2, 3 }.ShouldNotBeEmpty();
  1. CollectionAssert.IsOrdered
// NUnit
CollectionAssert.IsOrdered(new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2, 3 }.ShouldBeInOrder();
  1. CollectionAssert.IsSubsetOf
// NUnit
CollectionAssert.IsSubsetOf(new[] { 1, 2 }, new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2 }.ShouldBeSubsetOf(new[] { 1, 2, 3 });
  1. CollectionAssert.IsNotSubsetOf
// NUnit
CollectionAssert.IsNotSubsetOf(new[] { 1, 2, 4 }, new[] { 1, 2, 3 });

// Shouldly
new[] { 1, 2, 4 }.ShouldNotBeSubsetOf(new[] { 1, 2, 3 });

@joeldickson
Copy link
Contributor

NUnit Assertions and Shouldly Equivalents with Examples

NUnit Assertion Shouldly Equivalent Example
Assert.AreEqual(expected, actual) actual.ShouldBe(expected) ```csharp
int result = 5 + 5;
// NUnit
Assert.AreEqual(10, result);
// Shouldly
result.ShouldBe(10);
| `Assert.AreNotEqual(expected, actual)` | `actual.ShouldNotBe(expected)` | ```csharp
int result = 5 + 6;
// NUnit
Assert.AreNotEqual(10, result);
// Shouldly
result.ShouldNotBe(10);
``` |
| `Assert.IsTrue(condition)` | `condition.ShouldBeTrue()` | ```csharp
bool isEven = (10 % 2 == 0);
// NUnit
Assert.IsTrue(isEven);
// Shouldly
isEven.ShouldBeTrue();
``` |
| `Assert.IsFalse(condition)` | `condition.ShouldBeFalse()` | ```csharp
bool isOdd = (10 % 2 != 0);
// NUnit
Assert.IsFalse(isOdd);
// Shouldly
isOdd.ShouldBeFalse();
``` |
| `Assert.IsNull(object)` | `object.ShouldBeNull()` | ```csharp
string str = null;
// NUnit
Assert.IsNull(str);
// Shouldly
str.ShouldBeNull();
``` |
| `Assert.IsNotNull(object)` | `object.ShouldNotBeNull()` | ```csharp
string str = "Hello";
// NUnit
Assert.IsNotNull(str);
// Shouldly
str.ShouldNotBeNull();
``` |
| `Assert.AreSame(expected, actual)` | `actual.ShouldBeSameAs(expected)` | ```csharp
var obj1 = new object();
var obj2 = obj1;
// NUnit
Assert.AreSame(obj1, obj2);
// Shouldly
obj2.ShouldBeSameAs(obj1);
``` |
| `Assert.AreNotSame(expected, actual)` | `actual.ShouldNotBeSameAs(expected)` | ```csharp
var obj1 = new object();
var obj2 = new object();
// NUnit
Assert.AreNotSame(obj1, obj2);
// Shouldly
obj2.ShouldNotBeSameAs(obj1);
``` |
| `Assert.IsInstanceOf<T>(object)` | `object.ShouldBeOfType<T>()` | ```csharp
var str = "Hello";
// NUnit
Assert.IsInstanceOf<string>(str);
// Shouldly
str.ShouldBeOfType<string>();
``` |
| `Assert.IsNotInstanceOf<T>(object)` | `object.ShouldNotBeOfType<T>()` | ```csharp
var num = 5;
// NUnit
Assert.IsNotInstanceOf<string>(num);
// Shouldly
num.ShouldNotBeOfType<string>();
``` |
| `Assert.Contains(expected, collection)` | `collection.ShouldContain(expected)` | ```csharp
var list = new List<int> { 1, 2, 3 };
// NUnit
Assert.Contains(2, list);
// Shouldly
list.ShouldContain(2);
``` |
| `Assert.DoesNotContain(expected, collection)` | `collection.ShouldNotContain(expected)` | ```csharp
var list = new List<int> { 1, 2, 3 };
// NUnit
Assert.DoesNotContain(4, list);
// Shouldly
list.ShouldNotContain(4);
``` |
| `Assert.IsEmpty(collection)` | `collection.ShouldBeEmpty()` | ```csharp
var list = new List<int>();
// NUnit
Assert.IsEmpty(list);
// Shouldly
list.ShouldBeEmpty();
``` |
| `Assert.IsNotEmpty(collection)` | `collection.ShouldNotBeEmpty()` | ```csharp
var list = new List<int> { 1, 2, 3 };
// NUnit
Assert.IsNotEmpty(list);
// Shouldly
list.ShouldNotBeEmpty();
``` |
| `Assert.Greater(actual, expected)` | `actual.ShouldBeGreaterThan(expected)` | ```csharp
int result = 10;
// NUnit
Assert.Greater(result, 5);
// Shouldly
result.ShouldBeGreaterThan(5);
``` |
| `Assert.GreaterOrEqual(actual, expected)` | `actual.ShouldBeGreaterThanOrEqualTo(expected)` | ```csharp
int result = 10;
// NUnit
Assert.GreaterOrEqual(result, 10);
// Shouldly
result.ShouldBeGreaterThanOrEqualTo(10);
``` |
| `Assert.Less(actual, expected)` | `actual.ShouldBeLessThan(expected)` | ```csharp
int result = 5;
// NUnit
Assert.Less(result, 10);
// Shouldly
result.ShouldBeLessThan(10);
``` |
| `Assert.LessOrEqual(actual, expected)` | `actual.ShouldBeLessThanOrEqualTo(expected)` | ```csharp
int result = 10;
// NUnit
Assert.LessOrEqual(result, 10);
// Shouldly
result.ShouldBeLessThanOrEqualTo(10);
``` |
| `Assert.IsNaN(number)` | `number.ShouldBeNaN()` | ```csharp
double result = double.NaN;
// NUnit
Assert.IsNaN(result);
// Shouldly
result.ShouldBeNaN();
``` |
| `Assert.IsEmpty(string)` | `string.ShouldBeEmpty()` | ```csharp
string str = "";
// NUnit
Assert.IsEmpty(str);
// Shouldly
str.ShouldBeEmpty();
``` |
| `Assert.IsNotEmpty(string)` | `string.ShouldNotBeEmpty()` | ```csharp
string str = "Hello";
// NUnit
Assert.IsNotEmpty(str);
// Shouldly
str.ShouldNotBeEmpty();
``` |
| `StringAssert.StartsWith(expected, actual)` | `actual.ShouldStartWith(expected)` | ```csharp
string str = "Hello, World!";
// NUnit
StringAssert.StartsWith("Hello", str);
// Shouldly
str.ShouldStartWith("Hello");
``` |
| `StringAssert.EndsWith(expected, actual)` | `actual.ShouldEndWith(expected)` | ```csharp
string str = "Hello, World!";
// NUnit
StringAssert.EndsWith("World!", str);
// Shouldly
str.ShouldEndWith("World!");
``` |
| `StringAssert.Contains(expected, actual)` | `actual.ShouldContain(expected)` | ```csharp
string str = "Hello, World!";
// NUnit
StringAssert.Contains("World", str);
// Shouldly
str.ShouldContain("World");
``` |
| `Assert.Throws<T>(() => { ... })` | `Should.Throw<T>(() => { ... })` | ```csharp
// NUnit
Assert.Throws<ArgumentException>(() => throw new ArgumentException());
// Shouldly
Should.Throw<ArgumentException>(() => throw new ArgumentException());
``` |
| `Assert.DoesNotThrow(() => { ... })` | `Should.NotThrow(() => { ... })` | ```csharp
// NUnit
Assert.DoesNotThrow(() => { /* Some code */ });
// Shouldly
Should.NotThrow(() => { /* Some code */ });
``` |
| `CollectionAssert.AreEqual(expected, actual)` | `actual.ShouldBe(expected)` | ```csharp
var expected = new List<int> { 1, 2, 3 };
var actual = new List<int> { 1, 2, 3 };
// NUnit
CollectionAssert.AreEqual(expected, actual);
// Shouldly
actual.ShouldBe(expected);
``` |
| `CollectionAssert.AreEquivalent(expected, actual)` | `actual.ShouldBe(expected, ignoreOrder: true)` | ```csharp
var expected = new List<int> { 1, 2, 3 };
var actual = new List<int> { 3, 2, 1 };
// NUnit
CollectionAssert.AreEquivalent(expected, actual);
// Shouldly
actual.ShouldBe(expected, ignoreOrder: true);
``` |
| `CollectionAssert.AllItemsAreInstancesOfType(collection, type)` | `collection.ShouldAllBe(item => item.ShouldBeOfType(type))` | ```csharp
var list = new List<string> { "a", "b", "c" };
// NUnit
CollectionAssert.AllItemsAreInstancesOfType(list, typeof(string));
// Shouldly
list.ShouldAllBe(item => item.ShouldBeOfType<string>());
``` |
| `CollectionAssert.AllItemsAreNotNull(collection)` | `collection.ShouldNotContain(null)` | ```csharp
var list = new List<string> { "a", "b", "c" };
// NUnit
CollectionAssert.AllItemsAreNotNull(list);
// Shouldly
list.ShouldNotContain(null);
``` |
| `CollectionAssert.AllItemsAreUnique(collection)` | `collection.ShouldBeUnique()` | ```csharp
var list = new List<int> { 1, 2, 3 };
// NUnit
CollectionAssert.AllItemsAreUnique(list);
// Shouldly
list.ShouldBeUnique();
``` |
| `Assert.That(actual, Is.EqualTo(expected))` | `actual.ShouldBe(expected)` | ```csharp
int result = 5 + 5;
// NUnit
Assert.That(result, Is.EqualTo(10));
// Shouldly
result.ShouldBe(10);
``` |
| `Assert.That(actual, Is.Not.EqualTo(expected))` | `actual.ShouldNotBe(expected)` | ```csharp
int result = 5 + 6;
// NUnit
Assert.That(result, Is.Not.EqualTo(10));
// Shouldly
result.ShouldNotBe(10);
``` |
| `Assert.That(collection, Has.Member(expected))` | `collection.ShouldContain(expected)` | ```csharp
var list = new List<int> { 1, 2, 3 };
// NUnit
Assert.That(list, Has.Member(2));
// Shouldly
list.ShouldContain(2);
``` |
| `Assert.That(collection, Has.No.Member(expected))` | `collection.ShouldNotContain(expected)` | ```csharp
var list = new List<int> { 1, 2, 3 };
// NUnit
Assert.That(list, Has.No.Member(4));
// Shouldly
list.ShouldNotContain(4);
``` |
| `Assert.That(actual, Is.GreaterThan(expected))` | `actual.ShouldBeGreaterThan(expected)` | ```csharp
int result = 10;
// NUnit
Assert.That(result, Is.GreaterThan(5));
// Shouldly
result.ShouldBeGreaterThan(5);
``` |
| `Assert.That(actual, Is.LessThan(expected))` | `actual.ShouldBeLessThan(expected)` | ```csharp
int result = 5;
// NUnit
Assert.That(result, Is.LessThan(10));
// Shouldly
result.ShouldBeLessThan(10);
``` |
| `Assert.That(collection, Is.Unique)` | `collection.ShouldBeUnique()` | ```csharp
var list = new List<int> { 1, 2, 3 };
// NUnit
Assert.That(list, Is.Unique);
// Shouldly
list.ShouldBeUnique();
``` |
| `Assert.That(actual, Is.StringContaining(expected))` | `actual.ShouldContain(expected)` | ```csharp
string str = "Hello, World!";
// NUnit
Assert.That(str, Is.StringContaining("World"));
// Shouldly
str.ShouldContain("World");
``` |
| `Assert.That(actual, Is.StringStarting(expected))` | `actual.ShouldStartWith(expected)` | ```csharp
string str = "Hello, World!";
// NUnit
Assert.That(str, Is.StringStarting("Hello"));
// Shouldly
str.ShouldStartWith("Hello");
``` |
| `Assert.That(actual, Is.StringEnding(expected))` | `actual.ShouldEndWith(expected)` | ```csharp
string str = "Hello, World!";
// NUnit
Assert.That(str, Is.StringEnding("World!"));
// Shouldly
str.ShouldEndWith("World!");
``` |
| `Assert.That(actual, Is.Empty)` | `actual.ShouldBeEmpty()` | ```csharp
var list = new List<int>();
// NUnit
Assert.That(list, Is.Empty);
// Shouldly
list.ShouldBeEmpty();
``` |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants