1
exercism fetch csharp error-handling

ErrorHandlingTest.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using Xunit;

public class ErrorHandlingTest
{
    // Read more about exception handling here:
    // https://msdn.microsoft.com/en-us/library/ms173162.aspx?f=255&MSPPError=-2147217396
    [Fact]
    public void ThrowException()
    {
        Assert.Throws<Exception>(() => ErrorHandling.HandleErrorByThrowingException());
    }

    // Read more about nullable types here:
    // https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx?f=255&MSPPError=-2147217396
    [Fact(Skip = "Remove to run test")]
    public void ReturnNullableType()
    {
        var successfulResult = ErrorHandling.HandleErrorByReturningNullableType("1");
        Assert.Equal(1, successfulResult);

        var failureResult = ErrorHandling.HandleErrorByReturningNullableType("a");
        Assert.Null(failureResult);
    }

    // Read more about out parameters here:
    // https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx?f=255&MSPPError=-2147217396
    [Fact(Skip = "Remove to run test")]
    public void ReturnWithOutParameter()
    {
        int result;
        var successfulResult = ErrorHandling.HandleErrorWithOutParam("1", out result);
        Assert.True(successfulResult);
        Assert.Equal(1, result);
        
        var failureResult = ErrorHandling.HandleErrorWithOutParam("a", out result);
        Assert.False(failureResult);
        // The value of result is meaningless here (it could be anything) so it shouldn't be used and it's not validated 
    }

    private class DisposableResource : IDisposable
    {
        public bool IsDisposed { get; private set; }

        public void Dispose()
        {
            IsDisposed = true;
        }
    }

    // Read more about IDisposable here:
    // https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx
    [Fact(Skip = "Remove to run test")]
    public void DisposableObjectsAreDisposedWhenThrowingAnException()
    {
        var disposableResource = new DisposableResource();

        Assert.Throws<Exception>(() => ErrorHandling.DisposableResourcesAreDisposedWhenExceptionIsThrown(disposableResource));
        Assert.True(disposableResource.IsDisposed);
    }
}