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

docs: fluent assertions string assertions #396

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 212 additions & 13 deletions docs/FluentAssertionsAnalyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser

# FluentAssertions Analyzer Docs

- [StringShouldStartWith](#scenario-stringshouldstartwith) - `actual.Should().StartWith(expected);`
- [StringShouldEndWith](#scenario-stringshouldendwith) - `actual.Should().EndWith(expected);`
- [StringShouldNotBeNullOrEmpty](#scenario-stringshouldnotbenullorempty) - `actual.Should().NotBeNullOrEmpty();`
- [StringShouldBeNullOrEmpty](#scenario-stringshouldbenullorempty) - `actual.Should().BeNullOrEmpty();`
- [StringShouldBeNullOrWhiteSpace](#scenario-stringshouldbenullorwhitespace) - `actual.Should().BeNullOrWhiteSpace();`
- [StringShouldNotBeNullOrWhiteSpace](#scenario-stringshouldnotbenullorwhitespace) - `actual.Should().NotBeNullOrWhiteSpace();`
- [StringShouldHaveLength](#scenario-stringshouldhavelength) - `actual.Should().HaveLength(expected);`
- [CollectionShouldNotBeEmpty](#scenario-collectionshouldnotbeempty) - `collection.Should().NotBeEmpty();`
- [CollectionShouldBeEmpty](#scenario-collectionshouldbeempty) - `collection.Should().BeEmpty();`
- [CollectionShouldNotContainCondition](#scenario-collectionshouldnotcontaincondition) - `collection.Should().NotContain(i => i == 4);`
Expand Down Expand Up @@ -44,6 +51,198 @@ This is a generated file, please edit src\FluentAssertions.Analyzers.FluentAsser

## Scenarios

### scenario: StringShouldStartWith

```cs
// arrange
var actual = "actual";
var expected = "act";

// old assertion:
actual.StartsWith(expected).Should().BeTrue();

// new assertion:
actual.Should().StartWith(expected);
```

#### Failure messages

```cs
// arrange
var actual = "actual";
var expected = "wrong";

// old assertion:
actual.StartsWith(expected).Should().BeTrue(); // fail message: Expected actual.StartsWith(expected) to be True, but found False.

// new assertion:
actual.Should().StartWith(expected); // fail message: Expected actual to start with "wrong", but "actual" differs near "act" (index 0).
```

### scenario: StringShouldEndWith

```cs
// arrange
var actual = "actual";
var expected = "ual";

// old assertion:
actual.EndsWith(expected).Should().BeTrue();

// new assertion:
actual.Should().EndWith(expected);
```

#### Failure messages

```cs
// arrange
var actual = "actual";
var expected = "wrong";

// old assertion:
actual.EndsWith(expected).Should().BeTrue(); // fail message: Expected actual.EndsWith(expected) to be True, but found False.

// new assertion:
actual.Should().EndWith(expected); // fail message: Expected actual "actual" to end with "wrong".
```

### scenario: StringShouldNotBeNullOrEmpty

```cs
// arrange
var actual = "actual";

// old assertion:
string.IsNullOrEmpty(actual).Should().BeFalse();
actual.Should().NotBeNull().And.NotBeEmpty();
actual.Should().NotBeEmpty().And.NotBeNull();

// new assertion:
actual.Should().NotBeNullOrEmpty();
```

#### Failure messages

```cs
// arrange
var actual = string.Empty;

// old assertion:
string.IsNullOrEmpty(actual).Should().BeFalse(); // fail message: Expected string.IsNullOrEmpty(actual) to be False, but found True.
actual.Should().NotBeNull().And.NotBeEmpty(); // fail message: Did not expect actual to be empty.
actual.Should().NotBeEmpty().And.NotBeNull(); // fail message: Did not expect actual to be empty.

// new assertion:
actual.Should().NotBeNullOrEmpty(); // fail message: Expected actual not to be <null> or empty, but found "".
```

### scenario: StringShouldBeNullOrEmpty

```cs
// arrange
var actual = string.Empty;

// old assertion:
string.IsNullOrEmpty(actual).Should().BeTrue();

// new assertion:
actual.Should().BeNullOrEmpty();
```

#### Failure messages

```cs
// arrange
var actual = "actual";

// old assertion:
string.IsNullOrEmpty(actual).Should().BeTrue(); // fail message: Expected string.IsNullOrEmpty(actual) to be True, but found False.

// new assertion:
actual.Should().BeNullOrEmpty(); // fail message: Expected actual to be <null> or empty, but found "actual".
```

### scenario: StringShouldBeNullOrWhiteSpace

```cs
// arrange
var actual = string.Empty;

// old assertion:
string.IsNullOrWhiteSpace(actual).Should().BeTrue();

// new assertion:
actual.Should().BeNullOrWhiteSpace();
```

#### Failure messages

```cs
// arrange
var actual = "actual";

// old assertion:
string.IsNullOrWhiteSpace(actual).Should().BeTrue(); // fail message: Expected string.IsNullOrWhiteSpace(actual) to be True, but found False.

// new assertion:
actual.Should().BeNullOrWhiteSpace(); // fail message: Expected actual to be <null> or whitespace, but found "actual".
```

### scenario: StringShouldNotBeNullOrWhiteSpace

```cs
// arrange
var actual = "actual";

// old assertion:
string.IsNullOrWhiteSpace(actual).Should().BeFalse();

// new assertion:
actual.Should().NotBeNullOrWhiteSpace();
```

#### Failure messages

```cs
// arrange
var actual = string.Empty;

// old assertion:
string.IsNullOrWhiteSpace(actual).Should().BeFalse(); // fail message: Expected string.IsNullOrWhiteSpace(actual) to be False, but found True.

// new assertion:
actual.Should().NotBeNullOrWhiteSpace(); // fail message: Expected actual not to be <null> or whitespace, but found "".
```

### scenario: StringShouldHaveLength

```cs
// arrange
var actual = "actual";
var expected = 6;

// old assertion:
actual.Length.Should().Be(expected);

// new assertion:
actual.Should().HaveLength(expected);
```

#### Failure messages

```cs
// arrange
var actual = "actual";
var expected = 5;

// old assertion:
actual.Length.Should().Be(expected); // fail message: Expected actual.Length to be 5, but found 6.

// new assertion:
actual.Should().HaveLength(expected); // fail message: Expected actual with length 5, but found string "actual" with length 6.
```

### scenario: CollectionShouldNotBeEmpty

```cs
Expand All @@ -64,7 +263,7 @@ collection.Should().NotBeEmpty();
var collection = new List<int> { };

// old assertion:
collection.Any().Should().BeTrue(); // fail message: Expected collection.Any() to be true, but found False.
collection.Any().Should().BeTrue(); // fail message: Expected collection.Any() to be True, but found False.

// new assertion:
collection.Should().NotBeEmpty(); // fail message: Expected collection not to be empty.
Expand Down Expand Up @@ -93,13 +292,13 @@ collection.Should().BeEmpty();
var collection = new List<int> { 1, 2, 3 };

// old assertion:
collection.Any().Should().BeFalse(); // fail message: Expected collection.Any() to be false, but found True.
collection.Any().Should().BeFalse(); // fail message: Expected collection.Any() to be False, but found True.
collection.Count().Should().Be(0); // fail message: Expected collection.Count() to be 0, but found 3 (difference of 3).
collection.Count.Should().Be(0); // fail message: Expected collection.Count to be 0, but found 3 (difference of 3).
collection.Should().HaveCount(0); // fail message: Expected collection to contain 0 item(s), but found 3: {1, 2, 3}.

// new assertion:
collection.Should().BeEmpty(); // fail message: Expected collection to be empty, but found {1, 2, 3}.
collection.Should().BeEmpty(); // fail message: Expected collection to be empty, but found at least one item {1}.
```

### scenario: CollectionShouldNotContainCondition
Expand All @@ -123,8 +322,8 @@ collection.Should().NotContain(i => i == 4);
var collection = new List<int> { 1, 2, 3, 4, 5 };

// old assertion:
collection.Any(i => i == 4).Should().BeFalse(); // fail message: Expected collection.Any(i => i == 4) to be false, but found True.
collection.Where(i => i == 4).Should().BeEmpty(); // fail message: Expected collection.Where(i => i == 4) to be empty, but found {4}.
collection.Any(i => i == 4).Should().BeFalse(); // fail message: Expected collection.Any(i => i == 4) to be False, but found True.
collection.Where(i => i == 4).Should().BeEmpty(); // fail message: Expected collection.Where(i => i == 4) to be empty, but found at least one item {4}.

// new assertion:
collection.Should().NotContain(i => i == 4); // fail message: Expected collection {1, 2, 3, 4, 5} to not have any items matching (i == 4), but found {4}.
Expand All @@ -150,7 +349,7 @@ collection.Should().NotContain(4);
var collection = new List<int> { 1, 2, 3, 4, 5 };

// old assertion:
collection.Contains(4).Should().BeFalse(); // fail message: Expected collection.Contains(4) to be false, but found True.
collection.Contains(4).Should().BeFalse(); // fail message: Expected collection.Contains(4) to be False, but found True.

// new assertion:
collection.Should().NotContain(4); // fail message: Expected collection {1, 2, 3, 4, 5} to not contain 4.
Expand All @@ -176,7 +375,7 @@ collection.Should().OnlyContain(x => x > 0);
var collection = new List<int> { 1, 2, 3, -1 };

// old assertion:
collection.All(x => x > 0).Should().BeTrue(); // fail message: Expected collection.All(x => x > 0) to be true, but found False.
collection.All(x => x > 0).Should().BeTrue(); // fail message: Expected collection.All(x => x > 0) to be True, but found False.

// new assertion:
collection.Should().OnlyContain(x => x > 0); // fail message: Expected collection to contain only items matching (x > 0), but {-1} do(es) not match.
Expand All @@ -202,7 +401,7 @@ collection.Should().Contain(2);
var collection = new List<int> { 1, 3, 4, 5 };

// old assertion:
collection.Contains(2).Should().BeTrue(); // fail message: Expected collection.Contains(2) to be true, but found False.
collection.Contains(2).Should().BeTrue(); // fail message: Expected collection.Contains(2) to be True, but found False.

// new assertion:
collection.Should().Contain(2); // fail message: Expected collection {1, 3, 4, 5} to contain 2.
Expand All @@ -229,7 +428,7 @@ collection.Should().Contain(i => i == 2);
var collection = new List<int> { 3, 4, 5 };

// old assertion:
collection.Any(i => i == 2).Should().BeTrue(); // fail message: Expected collection.Any(i => i == 2) to be true, but found False.
collection.Any(i => i == 2).Should().BeTrue(); // fail message: Expected collection.Any(i => i == 2) to be True, but found False.
collection.Where(i => i == 2).Should().NotBeEmpty(); // fail message: Expected collection.Where(i => i == 2) not to be empty.

// new assertion:
Expand Down Expand Up @@ -580,7 +779,7 @@ dictionary.Should().ContainKey("two");
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["three"] = 3 };

// old assertion:
dictionary.ContainsKey("two").Should().BeTrue(); // fail message: Expected dictionary.ContainsKey("two") to be true, but found False.
dictionary.ContainsKey("two").Should().BeTrue(); // fail message: Expected dictionary.ContainsKey("two") to be True, but found False.

// new assertion:
dictionary.Should().ContainKey("two"); // fail message: Expected dictionary {["one"] = 1, ["three"] = 3} to contain key "two".
Expand All @@ -606,7 +805,7 @@ dictionary.Should().NotContainKey("four");
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4 };

// old assertion:
dictionary.ContainsKey("four").Should().BeFalse(); // fail message: Expected dictionary.ContainsKey("four") to be false, but found True.
dictionary.ContainsKey("four").Should().BeFalse(); // fail message: Expected dictionary.ContainsKey("four") to be False, but found True.

// new assertion:
dictionary.Should().NotContainKey("four"); // fail message: Expected dictionary {["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4} not to contain key "four", but found it anyhow.
Expand All @@ -632,7 +831,7 @@ dictionary.Should().ContainValue(2);
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3 };

// old assertion:
dictionary.ContainsValue(4).Should().BeTrue(); // fail message: Expected dictionary.ContainsValue(4) to be true, but found False.
dictionary.ContainsValue(4).Should().BeTrue(); // fail message: Expected dictionary.ContainsValue(4) to be True, but found False.

// new assertion:
dictionary.Should().ContainValue(4); // fail message: Expected dictionary {["one"] = 1, ["two"] = 2, ["three"] = 3} to contain value 4.
Expand All @@ -658,7 +857,7 @@ dictionary.Should().NotContainValue(4);
var dictionary = new Dictionary<string, int> { ["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4 };

// old assertion:
dictionary.ContainsValue(4).Should().BeFalse(); // fail message: Expected dictionary.ContainsValue(4) to be false, but found True.
dictionary.ContainsValue(4).Should().BeFalse(); // fail message: Expected dictionary.ContainsValue(4) to be False, but found True.

// new assertion:
dictionary.Should().NotContainValue(4); // fail message: Expected dictionary {["one"] = 1, ["two"] = 2, ["three"] = 3, ["four"] = 4} not to contain value 4, but found it anyhow.
Expand Down
14 changes: 4 additions & 10 deletions docs/MsTestAnalyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var flag = false;
Assert.IsTrue(flag); /* fail message: Assert.IsTrue failed. */

// new assertion:
flag.Should().BeTrue(); /* fail message: Expected flag to be true, but found False. */
flag.Should().BeTrue(); /* fail message: Expected flag to be True, but found False. */
```

### scenario: AssertIsFalse
Expand All @@ -95,7 +95,7 @@ var flag = true;
Assert.IsFalse(flag); /* fail message: Assert.IsFalse failed. */

// new assertion:
flag.Should().BeFalse(); /* fail message: Expected flag to be false, but found True. */
flag.Should().BeFalse(); /* fail message: Expected flag to be False, but found True. */
```

### scenario: AssertIsNull
Expand Down Expand Up @@ -974,10 +974,7 @@ static void ThrowException() => throw new InvalidOperationException();
Action action = ThrowException;

// old assertion:
Assert.ThrowsException<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
Exception Message: Operation is not valid due to the current state of the object.
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsException_Failure_OldAssertion>g__ThrowException|109_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1298
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException[T](Action action, String message, Object[] parameters) */
Assert.ThrowsException<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Expected exception type:<System.ArgumentException>. Actual exception type:<System.InvalidOperationException>. */

// new assertion:
action.Should().ThrowExactly<ArgumentException>(); /* fail message: Expected type to be System.ArgumentException, but found System.InvalidOperationException. */
Expand All @@ -1004,10 +1001,7 @@ static Task ThrowExceptionAsync() => throw new InvalidOperationException();
Func<Task> action = ThrowExceptionAsync;

// old assertion:
await Assert.ThrowsExceptionAsync<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Threw exception InvalidOperationException, but exception ArgumentException was expected.
Exception Message: Operation is not valid due to the current state of the object.
Stack Trace: at FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.MsTestAnalyzerTests.<AssertThrowsExceptionAsync_Failure_OldAssertion>g__ThrowExceptionAsync|112_0() in /Users/runner/work/fluentassertions.analyzers/src/FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs/MsTestAnalyzerTests.cs:line 1334
at Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsExceptionAsync[T](Func`1 action, String message, Object[] parameters) */
await Assert.ThrowsExceptionAsync<ArgumentException>(action); /* fail message: Assert.ThrowsException failed. Expected exception type:<System.ArgumentException>. Actual exception type:<System.InvalidOperationException>. */

// new assertion:
await action.Should().ThrowExactlyAsync<ArgumentException>(); /* fail message: Expected type to be System.ArgumentException, but found System.InvalidOperationException. */
Expand Down
6 changes: 3 additions & 3 deletions docs/Nunit3Analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Assert.That(flag, Is.Not.False); /* fail message: Expected: not False
*/

// new assertion:
flag.Should().BeTrue(); /* fail message: Expected flag to be true, but found False. */
flag.Should().BeTrue(); /* fail message: Expected flag to be True, but found False. */
```

### scenario: AssertIsFalse
Expand Down Expand Up @@ -111,7 +111,7 @@ Assert.That(flag, Is.Not.True); /* fail message: Expected: not True
*/

// new assertion:
flag.Should().BeFalse(); /* fail message: Expected flag to be false, but found True. */
flag.Should().BeFalse(); /* fail message: Expected flag to be False, but found True. */
```

### scenario: AssertNull
Expand Down Expand Up @@ -224,7 +224,7 @@ CollectionAssert.IsEmpty(collection); /* fail message: Expected: <empty>
*/

// new assertion:
collection.Should().BeEmpty(); /* fail message: Expected collection to be empty, but found {1, 2, 3}. */
collection.Should().BeEmpty(); /* fail message: Expected collection to be empty, but found at least one item {1}. */
```

### scenario: AssertIsNotEmpty
Expand Down
Loading
Loading