diff --git a/src/xunit.assert/Asserts b/src/xunit.assert/Asserts index cac8b688c..3222c4c6b 160000 --- a/src/xunit.assert/Asserts +++ b/src/xunit.assert/Asserts @@ -1 +1 @@ -Subproject commit cac8b688c193c0f244a0bedf3bb60feeb32d377a +Subproject commit 3222c4c6b581f7510cbd44362171ab999ec71e18 diff --git a/test/test.xunit.assert/Asserts/EquivalenceAssertsTests.cs b/test/test.xunit.assert/Asserts/EquivalenceAssertsTests.cs index cc332fd53..5108f7701 100644 --- a/test/test.xunit.assert/Asserts/EquivalenceAssertsTests.cs +++ b/test/test.xunit.assert/Asserts/EquivalenceAssertsTests.cs @@ -1700,6 +1700,36 @@ public void FileInfoToDirectoryInfo_Failure_Embedded() ); } + // Uri + + public static TheoryData UriData = + [ + new Uri("https://xunit.net/"), + new Uri("a/b#c", UriKind.RelativeOrAbsolute), + ]; + + [Theory] + [MemberData(nameof(UriData))] + public void Uri_Success(Uri uri) + { + Assert.Equivalent(uri, new Uri(uri.OriginalString, UriKind.RelativeOrAbsolute)); + } + + [Theory] + [MemberData(nameof(UriData))] + public void Uri_Failure(Uri uri) + { + var ex = Record.Exception(() => Assert.Equivalent(uri, new Uri("hello/world", UriKind.RelativeOrAbsolute))); + + Assert.IsType(ex); + Assert.Equal( + "Assert.Equivalent() Failure" + Environment.NewLine + + "Expected: " + uri.OriginalString + Environment.NewLine + + "Actual: hello/world", + ex.Message + ); + } + // Ensuring we use reference equality for the circular reference hash sets [Theory] diff --git a/test/test.xunit.assert/Asserts/TypeAssertsTests.cs b/test/test.xunit.assert/Asserts/TypeAssertsTests.cs index ee89873cd..8efd22617 100644 --- a/test/test.xunit.assert/Asserts/TypeAssertsTests.cs +++ b/test/test.xunit.assert/Asserts/TypeAssertsTests.cs @@ -309,6 +309,71 @@ public void NullObject() } } + public class IsNotType_Generic_InexactMatch + { + [Fact] + public void NullObject() + { + Assert.IsNotType(null, exactMatch: false); + } + + [Fact] + public void SameType() + { + var ex = new InvalidCastException(); + + var result = Record.Exception(() => Assert.IsNotType(ex, exactMatch: false)); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine + + "Expected: typeof(System.InvalidCastException)" + Environment.NewLine + + "Actual: typeof(System.InvalidCastException)", + result.Message + ); + } + + [Fact] + public void BaseType() + { + var ex = new InvalidCastException(); + + var result = Record.Exception(() => Assert.IsNotType(ex, exactMatch: false)); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine + + "Expected: typeof(System.Exception)" + Environment.NewLine + + "Actual: typeof(System.InvalidCastException)", + result.Message + ); + } + + [Fact] + public void Interface() + { + var ex = new DisposableClass(); + +#pragma warning disable xUnit2018 // TODO: Temporary until xUnit2018 is updated for the new signatures + var result = Record.Exception(() => Assert.IsNotType(ex, exactMatch: false)); +#pragma warning restore xUnit2018 + + Assert.IsType(result); + Assert.Equal( + "Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine + + "Expected: typeof(System.IDisposable)" + Environment.NewLine + + "Actual: typeof(TypeAssertsTests+DisposableClass)", + result.Message + ); + } + + [Fact] + public void IncompatibleType() + { + Assert.IsNotType(new InvalidOperationException(), exactMatch: false); + } + } + #pragma warning disable xUnit2007 // Do not use typeof expression to check the type public class IsNotType_NonGeneric @@ -342,6 +407,69 @@ public void NullObject() } } + public class IsNotType_NonGeneric_InexactMatch + { + [Fact] + public void NullObject() + { + Assert.IsNotType(typeof(object), null, exactMatch: false); + } + + [Fact] + public void SameType() + { + var ex = new InvalidCastException(); + + var result = Record.Exception(() => Assert.IsNotType(typeof(InvalidCastException), ex, exactMatch: false)); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine + + "Expected: typeof(System.InvalidCastException)" + Environment.NewLine + + "Actual: typeof(System.InvalidCastException)", + result.Message + ); + } + + [Fact] + public void BaseType() + { + var ex = new InvalidCastException(); + + var result = Record.Exception(() => Assert.IsNotType(typeof(Exception), ex, exactMatch: false)); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine + + "Expected: typeof(System.Exception)" + Environment.NewLine + + "Actual: typeof(System.InvalidCastException)", + result.Message + ); + } + + [Fact] + public void Interface() + { + var ex = new DisposableClass(); + + var result = Record.Exception(() => Assert.IsNotType(typeof(IDisposable), ex, exactMatch: false)); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine + + "Expected: typeof(System.IDisposable)" + Environment.NewLine + + "Actual: typeof(TypeAssertsTests+DisposableClass)", + result.Message + ); + } + + [Fact] + public void IncompatibleType() + { + Assert.IsNotType(typeof(InvalidCastException), new InvalidOperationException(), exactMatch: false); + } + } + #pragma warning restore xUnit2007 // Do not use typeof expression to check the type public class IsType_Generic : TypeAssertsTests @@ -415,6 +543,76 @@ public void NullObject() } } + public class IsType_Generic_InexactMatch + { + [Fact] + public void NullObject() + { + var result = Record.Exception(() => Assert.IsType(null, exactMatch: false)); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsType() Failure: Value is null" + Environment.NewLine + + "Expected: typeof(object)" + Environment.NewLine + + "Actual: null", + result.Message + ); + } + + [Fact] + public void SameType() + { + var ex = new InvalidCastException(); + + Assert.IsType(ex, exactMatch: false); + } + + [Fact] + public void BaseType() + { + var ex = new InvalidCastException(); + + Assert.IsType(ex, exactMatch: false); + } + + [Fact] + public void Interface() + { + var ex = new DisposableClass(); + +#pragma warning disable xUnit2018 // TODO: Temporary until xUnit2018 is updated for the new signatures + Assert.IsType(ex, exactMatch: false); +#pragma warning restore xUnit2018 + } + + [Fact] + public void ReturnsCastObject() + { + var ex = new InvalidCastException(); + + var result = Assert.IsType(ex, exactMatch: false); + + Assert.Same(ex, result); + } + + [Fact] + public void IncompatibleType() + { + var result = + Record.Exception( + () => Assert.IsType(new InvalidOperationException(), exactMatch: false) + ); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsType() Failure: Value is an incompatible type" + Environment.NewLine + + "Expected: typeof(System.InvalidCastException)" + Environment.NewLine + + "Actual: typeof(System.InvalidOperationException)", + result.Message + ); + } + } + #pragma warning disable xUnit2007 // Do not use typeof expression to check the type public class IsType_NonGeneric : TypeAssertsTests @@ -480,6 +678,74 @@ public void NullObjectThrows() } } + public class IsType_NonGeneric_InexactMatch + { + [Fact] + public void NullObject() + { + var result = Record.Exception(() => Assert.IsType(typeof(object), null, exactMatch: false)); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsType() Failure: Value is null" + Environment.NewLine + + "Expected: typeof(object)" + Environment.NewLine + + "Actual: null", + result.Message + ); + } + + [Fact] + public void SameType() + { + var ex = new InvalidCastException(); + + Assert.IsType(typeof(InvalidCastException), ex, exactMatch: false); + } + + [Fact] + public void BaseType() + { + var ex = new InvalidCastException(); + + Assert.IsType(typeof(Exception), ex, exactMatch: false); + } + + [Fact] + public void Interface() + { + var ex = new DisposableClass(); + + Assert.IsType(typeof(IDisposable), ex, exactMatch: false); + } + + [Fact] + public void ReturnsCastObject() + { + var ex = new InvalidCastException(); + + var result = Assert.IsType(ex, exactMatch: false); + + Assert.Same(ex, result); + } + + [Fact] + public void IncompatibleType() + { + var result = + Record.Exception( + () => Assert.IsType(typeof(InvalidCastException), new InvalidOperationException(), exactMatch: false) + ); + + Assert.IsType(result); + Assert.Equal( + "Assert.IsType() Failure: Value is an incompatible type" + Environment.NewLine + + "Expected: typeof(System.InvalidCastException)" + Environment.NewLine + + "Actual: typeof(System.InvalidOperationException)", + result.Message + ); + } + } + #pragma warning restore xUnit2007 // Do not use typeof expression to check the type class DisposableClass : IDisposable