-
-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #368 from ssiko/ShouldHaveSingleItem
Added ShouldHaveSingleItem for Enumerables.
- Loading branch information
Showing
11 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
...mples/EnumerableShouldHaveSingleItemExamples.ShouldHaveSingleItem.codeSample.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var maggie = new Person() { Name = "Maggie" }; | ||
var homer = new Person() { Name = "Homer" }; | ||
var simpsonsBabies = new List<Person>() { homer, maggie }; | ||
simpsonsBabies.ShouldHaveSingleItem(); |
5 changes: 5 additions & 0 deletions
5
...es/EnumerableShouldHaveSingleItemExamples.ShouldHaveSingleItem.exceptionText.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
simpsonsBabies | ||
should have single item but had | ||
2 | ||
items and was | ||
[Homer, Maggie] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/DocumentationExamples/EnumerableShouldHaveSingleItemExamples.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
using Shouldly; | ||
using Simpsons; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace DocumentationExamples | ||
{ | ||
public class EnumerableShouldHaveSingleItemExamples | ||
{ | ||
readonly ITestOutputHelper _testOutputHelper; | ||
|
||
public EnumerableShouldHaveSingleItemExamples(ITestOutputHelper testOutputHelper) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact] | ||
public void ShouldHaveSingleItem() | ||
{ | ||
DocExampleWriter.Document(() => | ||
{ | ||
var maggie = new Person() { Name = "Maggie" }; | ||
var homer = new Person() { Name = "Homer" }; | ||
var simpsonsBabies = new List<Person>() { homer, maggie }; | ||
simpsonsBabies.ShouldHaveSingleItem(); | ||
}, _testOutputHelper); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Shouldly.Shared/MessageGenerators/ShouldHaveSingleItemMessageGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using Shouldly.DifferenceHighlighting; | ||
using System.Collections; | ||
using System.Linq; | ||
|
||
namespace Shouldly.MessageGenerators | ||
{ | ||
internal class ShouldHaveSingleItemMessageGenerator : ShouldlyMessageGenerator | ||
{ | ||
const string ShouldBeAssertion = "ShouldHaveSingleItem"; | ||
|
||
public override bool CanProcess(IShouldlyAssertionContext context) | ||
{ | ||
return context.ShouldMethod.Equals(ShouldBeAssertion, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
public override string GenerateErrorMessage(IShouldlyAssertionContext context) | ||
{ | ||
var codePart = context.CodePart; | ||
var expected = context.Expected.ToStringAwesomely(); | ||
var count = (context.Expected ?? Enumerable.Empty<object>()).As<IEnumerable>().Cast<object>().Count(); | ||
if (codePart != "null") | ||
{ | ||
return | ||
$@"{codePart} | ||
{context.ShouldMethod.PascalToSpaced()} but had | ||
{count} | ||
items and was | ||
{expected}"; | ||
} | ||
else | ||
{ | ||
return | ||
$@"{expected} | ||
{context.ShouldMethod.PascalToSpaced()} but had | ||
{count} | ||
items"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Shouldly.Tests.Shared/ShouldHaveSingleItem/ArrayScenario.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Xunit; | ||
using System.Collections.Generic; | ||
using Shouldly.Tests.Strings; | ||
|
||
namespace Shouldly.Tests.Shared.ShouldHaveSingleItem | ||
{ | ||
public class ArrayScenario | ||
{ | ||
|
||
[Fact] | ||
public void ArrayScenarioShouldFail() | ||
{ | ||
Verify.ShouldFail(() => | ||
new[] { 1, 2 }.ShouldHaveSingleItem("Some additional context"), | ||
|
||
errorWithSource: | ||
@"new[] { 1, 2 } | ||
should have single item but had | ||
2 | ||
items and was | ||
[1, 2] | ||
Additional Info: | ||
Some additional context", | ||
|
||
errorWithoutSource: | ||
@"[1, 2] | ||
should have single item but had | ||
2 | ||
items | ||
Additional Info: | ||
Some additional context"); | ||
} | ||
|
||
[Fact] | ||
public void ShouldPass() | ||
{ | ||
int result = new[] { 1 }.ShouldHaveSingleItem(); | ||
result.ShouldBe(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters