fluentassertions.analyzers icon indicating copy to clipboard operation
fluentassertions.analyzers copied to clipboard

xunit: define codefixes for ExceptionAsserts

Open Meir017 opened this issue 2 years ago • 2 comments

The asserts defined in https://github.com/xunit/assert.xunit/blob/main/ExceptionAsserts.cs

Meir017 avatar Jan 11 '22 05:01 Meir017

The simple assertions methods:

public T Throws<T>(Action testCode) where T : Exception
{
    Assert.NotNull(Assert.Throws<T>(testCode));
    testCode.Should().ThrowExactly<T>().Which.Should().NotBeNull();

    Assert.Throws<T>(testCode);
    testCode.Should().ThrowExactly<T>();

    return default;
}

public async Task<T> ThrowsAsync<T>(Func<Task> testCode) where T : Exception
{
    Assert.NotNull(await Assert.ThrowsAsync<T>(testCode));
    (await testCode.Should().ThrowExactlyAsync<T>()).Which.Should().NotBeNull();

    await Assert.ThrowsAsync<T>(testCode);
    await testCode.Should().ThrowExactlyAsync<T>();

    return default;
}

public T ThrowsAny<T>(Action testCode) where T : Exception
{   
    Assert.NotNull(Assert.ThrowsAny<T>(testCode));
    testCode.Should().Throw<T>().Which.Should().NotBeNull();

    Assert.ThrowsAny<T>(testCode);
    testCode.Should().Throw<T>();

    

    return default;
}

public async Task<T> ThrowsAnyAsync<T>(Func<Task> testCode) where T : Exception
{
    Assert.NotNull(await Assert.ThrowsAnyAsync<T>(testCode));
    (await testCode.Should().ThrowAsync<T>()).Which.Should().NotBeNull();

    await Assert.ThrowsAnyAsync<T>(testCode);
    await testCode.Should().ThrowAsync<T>();

    return default;
}

Meir017 avatar Jan 11 '22 20:01 Meir017

not sure about the methods:

public static T Throws<T>(string paramName, Action testCode) where T : ArgumentException {}
public static T Throws<T>(string paramName, Func<object> testCode) where T : ArgumentException {}
public static async Task<T> ThrowsAsync<T>(string paramName, Func<Task> testCode) where T : ArgumentException {}
public static async ValueTask<T> ThrowsAsync<T>(string paramName, Func<ValueTask> testCode) where T : ArgumentException {}

maybe something like this:

public async Task<T> ThrowsAnyAsync<T>(Func<Task> testCode) where T : Exception
{
    Assert.NotNull(await Assert.ThrowsAnyAsync<T>(testCode));
    (await testCode.Should().ThrowAsync<T>()).Which.Should().NotBeNull();

    await Assert.ThrowsAnyAsync<T>(testCode);
    await testCode.Should().ThrowAsync<T>();

    return default;
}

public static async Task<T> ThrowsAsync<T>(string paramName, Func<Task> testCode) where T : ArgumentException
{
    Assert.NotNull(await Assert.ThrowsAsync<T>(paramName, testCode));
    (await testCode.Should().ThrowExactlyAsync<T>().WithParameterName(paramName)).Which.Should().NotBeNull();

    await Assert.ThrowsAsync<T>(paramName, testCode);
    await testCode.Should().ThrowExactlyAsync<T>().WithParameterName(paramName);

    return default;
}

Meir017 avatar Jan 11 '22 20:01 Meir017