Skip to content
Closed
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
56 changes: 56 additions & 0 deletions TUnit.Assertions.Tests/TypeOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,60 @@ public async Task Returns_Casted_Object()

await Assert.That(result).IsNotNull();
}

[Test]
public async Task IsTypeOf_Works_With_NonObject_Type()
{
// This test verifies the fix for the issue where IsTypeOf only worked with object types
string str = "Hello";

var result = await Assert.That(str).IsTypeOf<string, string>();

await Assert.That(result).IsEqualTo("Hello");
}

[Test]
public async Task IsTypeOf_Works_With_Object_SingleTypeParameter()
{
object obj = "Hello";

var result = await Assert.That(obj).IsTypeOf<string>();

await Assert.That(result).IsEqualTo("Hello");
}

[Test]
public async Task IsTypeOf_Works_With_NullableObject()
{
object? nullableObj = new StringBuilder("Test");

var result = await Assert.That(nullableObj).IsTypeOf<StringBuilder>();

await Assert.That(result.ToString()).IsEqualTo("Test");
}

[Test]
public async Task IsTypeOf_Fails_When_Type_Mismatch()
{
object obj = "Hello";

await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(async () =>
{
await Assert.That(obj).IsTypeOf<int>();
});
}

[Test]
public async Task IsTypeOf_TwoTypeParameters_Works_With_AnySourceType()
{
// Test with string as source type
string str = "Hello";
var result1 = await Assert.That(str).IsTypeOf<string, string>();
await Assert.That(result1).IsEqualTo("Hello");

// Test with int as source type
int number = 42;
var result2 = await Assert.That(number).IsTypeOf<int, int>();
await Assert.That(result2).IsEqualTo(42);
}
}
6 changes: 3 additions & 3 deletions TUnit.Assertions/Extensions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public static DictionaryAssertion<TKey, TValue> That<TKey, TValue>(
}

/// <summary>
/// Creates an assertion for an array.
/// Creates an assertion for an array (nullable or non-nullable).
/// This overload enables better type inference for collection operations like IsInOrder, All, ContainsOnly.
/// Example: await Assert.That(array).IsInOrder();
/// </summary>
public static CollectionAssertion<TItem> That<TItem>(
TItem[] value,
TItem[]? value,
[CallerArgumentExpression(nameof(value))] string? expression = null)
{
return new CollectionAssertion<TItem>(value, expression);
return new CollectionAssertion<TItem>(value!, expression);
}

/// <summary>
Expand Down
19 changes: 10 additions & 9 deletions TUnit.Assertions/Extensions/AssertionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,26 @@ public static Chaining.OrAssertion<TValue> IsNotEqualTo<TValue>(
// IsTypeOf(Type), IsAssignableTo, IsNotAssignableTo are now generated by AssertionExtensionGenerator

/// <summary>
/// Asserts that the value is of the specified type and returns an assertion on the casted value (specialized for object).
/// Asserts that the value is of the specified type and returns an assertion on the casted value.
/// Example: await Assert.That(obj).IsTypeOf<StringBuilder>();
/// This generic overload works with any source type.
/// </summary>
public static TypeOfAssertion<object, TExpected> IsTypeOf<TExpected>(
this IAssertionSource<object> source)
public static TypeOfAssertion<TValue, TExpected> IsTypeOf<TExpected, TValue>(
this IAssertionSource<TValue> source)
{
source.Context.ExpressionBuilder.Append($".IsTypeOf<{typeof(TExpected).Name}>()");
return new TypeOfAssertion<object, TExpected>(source.Context);
return new TypeOfAssertion<TValue, TExpected>(source.Context);
}

/// <summary>
/// Asserts that the value is of the specified type and returns an assertion on the casted value.
/// Asserts that the value is of the specified type and returns an assertion on the casted value (specialized for object).
/// Example: await Assert.That(obj).IsTypeOf<StringBuilder>();
/// </summary>
public static TypeOfAssertion<TFrom, TTo> IsTypeOf<TTo, TFrom>(
this IAssertionSource<TFrom> source)
public static TypeOfAssertion<object, TExpected> IsTypeOf<TExpected>(
this IAssertionSource<object> source)
{
source.Context.ExpressionBuilder.Append($".IsTypeOf<{typeof(TTo).Name}>()");
return new TypeOfAssertion<TFrom, TTo>(source.Context);
source.Context.ExpressionBuilder.Append($".IsTypeOf<{typeof(TExpected).Name}>()");
return new TypeOfAssertion<object, TExpected>(source.Context);
}

/// <summary>
Expand Down
Loading