Skip to content

Commit

Permalink
Update assertions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Nov 1, 2024
1 parent 3f9460d commit 5f7494f
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/xunit.assert/Asserts
30 changes: 30 additions & 0 deletions test/test.xunit.assert/Asserts/EquivalenceAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,36 @@ public void FileInfoToDirectoryInfo_Failure_Embedded()
);
}

// Uri

public static TheoryData<Uri> 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<EquivalentException>(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]
Expand Down
266 changes: 266 additions & 0 deletions test/test.xunit.assert/Asserts/TypeAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,71 @@ public void NullObject()
}
}

public class IsNotType_Generic_InexactMatch
{
[Fact]
public void NullObject()
{
Assert.IsNotType<object>(null, exactMatch: false);
}

[Fact]
public void SameType()
{
var ex = new InvalidCastException();

var result = Record.Exception(() => Assert.IsNotType<InvalidCastException>(ex, exactMatch: false));

Assert.IsType<IsNotTypeException>(result);
Assert.Equal<object>(
"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<Exception>(ex, exactMatch: false));

Assert.IsType<IsNotTypeException>(result);
Assert.Equal<object>(
"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<IDisposable>(ex, exactMatch: false));
#pragma warning restore xUnit2018

Assert.IsType<IsNotTypeException>(result);
Assert.Equal<object>(
"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<InvalidCastException>(new InvalidOperationException(), exactMatch: false);
}
}

#pragma warning disable xUnit2007 // Do not use typeof expression to check the type

public class IsNotType_NonGeneric
Expand Down Expand Up @@ -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<IsNotTypeException>(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<IsNotTypeException>(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<IsNotTypeException>(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
Expand Down Expand Up @@ -415,6 +543,76 @@ public void NullObject()
}
}

public class IsType_Generic_InexactMatch
{
[Fact]
public void NullObject()
{
var result = Record.Exception(() => Assert.IsType<object>(null, exactMatch: false));

Assert.IsType<IsTypeException>(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<InvalidCastException>(ex, exactMatch: false);
}

[Fact]
public void BaseType()
{
var ex = new InvalidCastException();

Assert.IsType<Exception>(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<IDisposable>(ex, exactMatch: false);
#pragma warning restore xUnit2018
}

[Fact]
public void ReturnsCastObject()
{
var ex = new InvalidCastException();

var result = Assert.IsType<InvalidCastException>(ex, exactMatch: false);

Assert.Same(ex, result);
}

[Fact]
public void IncompatibleType()
{
var result =
Record.Exception(
() => Assert.IsType<InvalidCastException>(new InvalidOperationException(), exactMatch: false)
);

Assert.IsType<IsTypeException>(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
Expand Down Expand Up @@ -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<IsTypeException>(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<InvalidCastException>(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<IsTypeException>(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
Expand Down

0 comments on commit 5f7494f

Please sign in to comment.