-
-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added utility class TypeDescriptionUtility.cs, along with unit tests …
…in TypeDescriptionUtilitySpecs.cs. Refactored AssertionScope.FailWith to produce new utility method AssertionScope.FormatFailureMessage. Updated DataTableCollectionAssertionExtensions.cs, DataColumnCollectionAssertionExtensions.cs and DataRowCollectionAssertionExtensions.cs to throw InvalidOperationException when BeSameAs or NotBeSameAs is called with a subject of the wrong type, using the new AssertionScope.FormatFailureMessage method to give the exception an appropriate message. Updated DataTableCollectionAssertionSpecs.cs, DataColumnCollectionAssertionSpecs.cs and DataRowCollectionAssertionSpecs.cs to account for the change in the exception type & message in the When_generic_collection_is_tested_against_typed_collection_it_should_fail test methods.
- Loading branch information
Showing
9 changed files
with
213 additions
and
28 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace FluentAssertions | ||
{ | ||
internal static class TypeDescriptionUtility | ||
{ | ||
public static string GetDescriptionOfObjectType(object obj) | ||
{ | ||
return (obj is null) ? "<null>" : GetTypeDescription(obj.GetType()); | ||
} | ||
|
||
public static string GetTypeDescription(Type type) | ||
{ | ||
return | ||
((type.Namespace == "System.Linq") && type.IsGenericType) | ||
? "an anonymous iterator from a LINQ expression with element type " + type.GetGenericArguments()[0].FullName | ||
: (type.IsValueType ? "a value of type " : "an instance of ") + type.FullName; | ||
} | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
Tests/FluentAssertions.Specs/TypeDescriptionUtilitySpecs.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,108 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Xunit; | ||
|
||
namespace FluentAssertions.Specs | ||
{ | ||
public class TypeDescriptionUtilitySpecs | ||
{ | ||
[Fact] | ||
public void When_object_is_null_it_should_work() | ||
{ | ||
// Act & Assert | ||
TypeDescriptionUtility.GetDescriptionOfObjectType(null).Should().Be("<null>"); | ||
} | ||
|
||
[Fact] | ||
public void When_object_is_value_type_it_should_work() | ||
{ | ||
// Act & Assert | ||
TypeDescriptionUtility.GetDescriptionOfObjectType(37).Should().Be("a value of type System.Int32"); | ||
} | ||
|
||
[Fact] | ||
public void When_object_is_reference_type_it_should_work() | ||
{ | ||
// Act & Assert | ||
TypeDescriptionUtility.GetDescriptionOfObjectType(new object()).Should().Be("an instance of System.Object"); | ||
} | ||
|
||
[Fact] | ||
public void When_object_is_generic_value_type_it_should_work() | ||
{ | ||
// Arrange | ||
var box = new Box<int>() { Value = 37 }; | ||
|
||
// Act & Assert | ||
TypeDescriptionUtility.GetDescriptionOfObjectType(box).Should() | ||
.Match("a value of type *+Box`1[[System.Int32*]]"); | ||
} | ||
|
||
[Fact] | ||
public void When_object_is_generic_reference_type_it_should_work() | ||
{ | ||
// Act & Assert | ||
TypeDescriptionUtility.GetDescriptionOfObjectType(new List<int>()).Should() | ||
.Match("an instance of System.Collections.Generic.List`1[[System.Int32*]]"); | ||
} | ||
|
||
[Fact] | ||
public void When_object_is_LINQ_anonymous_iterator_type_it_should_work() | ||
{ | ||
// Arrange | ||
var value = new[] { 1, 2, 3 }.Select(x => 2 * x); | ||
|
||
TypeDescriptionUtility.GetDescriptionOfObjectType(value).Should().Be( | ||
"an anonymous iterator from a LINQ expression with element type System.Int32"); | ||
} | ||
|
||
[Fact] | ||
public void When_type_is_value_type_it_should_work() | ||
{ | ||
// Act & Assert | ||
TypeDescriptionUtility.GetTypeDescription(typeof(int)).Should().Be("a value of type System.Int32"); | ||
} | ||
|
||
[Fact] | ||
public void When_type_is_reference_type_it_should_work() | ||
{ | ||
// Act & Assert | ||
TypeDescriptionUtility.GetTypeDescription(typeof(object)).Should().Be("an instance of System.Object"); | ||
} | ||
|
||
[Fact] | ||
public void When_type_is_generic_value_type_it_should_work() | ||
{ | ||
// Arrange | ||
var value = new Box<int>() { Value = 37 }; | ||
|
||
// Act & Assert | ||
TypeDescriptionUtility.GetTypeDescription(value.GetType()).Should() | ||
.Match("a value of type *+Box`1[[System.Int32*]]"); | ||
} | ||
|
||
[Fact] | ||
public void When_type_is_generic_reference_type_it_should_work() | ||
{ | ||
// Act & Assert | ||
TypeDescriptionUtility.GetTypeDescription(typeof(List<int>)).Should() | ||
.Match("an instance of System.Collections.Generic.List`1[[System.Int32*]]"); | ||
} | ||
|
||
[Fact] | ||
public void When_type_is_LINQ_anonymous_iterator_type_it_should_work() | ||
{ | ||
// Arrange | ||
var value = new[] { 1, 2, 3 }.Select(x => 2 * x); | ||
|
||
TypeDescriptionUtility.GetTypeDescription(value.GetType()).Should().Be( | ||
"an anonymous iterator from a LINQ expression with element type System.Int32"); | ||
} | ||
|
||
private struct Box<T> | ||
{ | ||
public T Value { get; set; } | ||
} | ||
} | ||
} |