Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xunit: define codefixes for ExceptionAsserts #137

Closed
12 tasks
Tracked by #130
Meir017 opened this issue Jan 11, 2022 · 2 comments · Fixed by #284
Closed
12 tasks
Tracked by #130

xunit: define codefixes for ExceptionAsserts #137

Meir017 opened this issue Jan 11, 2022 · 2 comments · Fixed by #284

Comments

@Meir017
Copy link
Member

Meir017 commented Jan 11, 2022

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

  • Exception Throws(Type exceptionType, Action testCode);
  • Exception Throws(Type exceptionType, Func<object?> testCode);
  • T Throws<T>(Action testCode) where T : Exception;
  • T Throws<T>(Func<object?> testCode) where T : Exception;
  • T Throws<T>(string? paramName, Action testCode) where T : ArgumentException;
  • T Throws<T>(string? paramName, Func<object?> testCode);

  • T ThrowsAny<T>(Action testCode) where T : Exception;
  • T ThrowsAny<T>(Func<object?> testCode) where T : Exception;

  • async Task<T> ThrowsAnyAsync<T>(Func<Task> testCode) where T : Exception;

  • async Task<Exception> ThrowsAsync(Type exceptionType, Func<Task> testCode);
  • async Task<T> ThrowsAsync<T>(Func<Task> testCode) where T : Exception;
  • async Task<T> ThrowsAsync<T>(string? paramName, string paramName, Func<Task> testCode) where T : ArgumentException;
@Meir017
Copy link
Member Author

Meir017 commented Jan 11, 2022

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
Copy link
Member Author

Meir017 commented Jan 11, 2022

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant