• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AlamoEngine-Tools / PetroglyphTools / 8252849769

12 Mar 2024 05:02PM UTC coverage: 74.489% (+0.8%) from 73.737%
8252849769

Pull #387

github

web-flow
Merge f7095f970 into f4ad7514b
Pull Request #387: Use CommonUtilites as dependency

653 of 788 branches covered (82.87%)

Branch coverage included in aggregate %.

80 of 143 new or added lines in 17 files covered. (55.94%)

15 existing lines in 4 files now uncovered.

1645 of 2297 relevant lines covered (71.62%)

73.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.25
/PG.Testing/ExceptionUtilities.cs
1
// Copyright (c) Alamo Engine Tools and contributors. All rights reserved.
2
// Licensed under the MIT license. See LICENSE file in the project root for details.
3

4
using System;
5
using System.Linq;
6
using System.Reflection;
7
using Microsoft.VisualStudio.TestTools.UnitTesting;
8

9
namespace PG.Testing;
10

11
public static class ExceptionUtilities
12
{
13
    public static T AssertDoesNotThrowException<T>(Func<T> action)
14
    {
15
        try
16
        {
17
            return action();
30✔
18
        }
19
        catch (Exception e)
×
20
        {
21
            Assert.Fail($"Expected no exception to be thrown but got '{e.GetType().Name}' instead", e);
×
22
            return default;
×
23
        }
24
    }
30✔
25

26
    public static void AssertDoesNotThrowException(Action action)
27
    {
28
        AssertDoesNotThrowException(() => action);
44✔
29
    }
22✔
30

31
    public static void AssertThrowsException<T>(Type type, Func<T> action)
32
    {
33
        AssertThrowsException(type, () =>
1✔
34
        {
1✔
35
            action();
1✔
36
        });
1✔
37
    }
1✔
38

39
    public static void AssertThrowsException(Type type, Action action)
40
    {
41
        var exception = Record(action);
1✔
42
        if (exception is null)
1!
43
            throw new AssertFailedException($"Expected an exception of type '{type.Name}' but none was thrown.");
×
44
        if (exception.GetType() != type)
1!
45
            throw new AssertFailedException($"Expected an exception of type '{type.Name}' but '{exception.GetType().Name}' was thrown.");
×
46
    }
1✔
47

48
    public static void AssertThrows(Type[] exceptionTypes, Action testCode)
49
    {
UNCOV
50
        var exception = Record(testCode);
×
UNCOV
51
        if (exception is null)
×
52
            throw new AssertFailedException("Expected an exception but none was thrown.");
×
UNCOV
53
        var exceptionType = exception.GetType();
×
UNCOV
54
        if (!exceptionTypes.Contains(exceptionType))
×
55
            throw new AssertFailedException($"Caught wrong exception: {exceptionType.Name}");
×
UNCOV
56
    }
×
57

58
    public static void AssertThrowsAny(Action testCode)
59
    {
60
        AssertThrowsAnyOfType<Exception>(testCode);
8✔
61
    }
8✔
62

63
    public static void AssertThrowsAnyOfType<T>(Action testCode) where T : Exception
64
    {
65
        var exception = Record(testCode);
8✔
66
        if (exception is null)
8!
67
            throw new AssertFailedException("Expected an exception but none was thrown.");
×
68
        if (exception is not T)
8!
69
            throw new AssertFailedException($"Expected any exception of type {typeof(T).Name} but got {exception.GetType().Name}");
×
70
    }
8✔
71

72
    public static Exception? Record(Action testCode)
73
    {
74
        try
75
        {
76
            testCode();
9✔
77
            return null;
×
78
        }
79
        catch (Exception ex)
80
        {
81
            return ex;
9✔
82
        }
83
    }
9✔
84

85
    public static void AssertThrowsException_IgnoreTargetInvocationException<T>(Action action) where T : Exception
86
    {
87
        AssertThrowsException_IgnoreTargetInvocationException(typeof(T), action);
×
88
    }
×
89

90
    public static void AssertThrowsException_IgnoreTargetInvocationException(Type type, Action action)
91
    {
92
        AssertThrowsException_IgnoreTargetInvocationException(type, () => action);
×
93
    }
×
94

95
    public static void AssertThrowsException_IgnoreTargetInvocationException<T>(Func<object?> action) where T : Exception
96
    {
97
        AssertThrowsException_IgnoreTargetInvocationException(typeof(T), action);
2✔
98
    }
2✔
99

100
    public static void AssertThrowsException_IgnoreTargetInvocationException(Type expectedException, Func<object?> action)
101
    {
102
        if (expectedException.IsAssignableFrom(typeof(Exception)))
2!
103
            throw new ArgumentException("Type argument must be assignable from System.Exception", nameof(expectedException));
×
104
        try
105
        {
106
            action();
2✔
107
        }
×
108
        catch (TargetInvocationException e)
2✔
109
        {
110
            if (e.InnerException?.GetType() != expectedException)
2!
111
                Assert.Fail($"Expected exception of type {expectedException.Name} but got {e.InnerException?.GetType().Name}");
×
112
            return;
2✔
113
        }
114
        catch (Exception e)
×
115
        {
116
            if (e.GetType() == expectedException)
×
117
                return;
×
118
            Assert.Fail($"Expected exception of type {expectedException.Name} but got {e.GetType().Name}");
×
119
        }
×
120
        Assert.Fail($"Excepted exception of type {expectedException.Name} but non was thrown.");
×
121
    }
2✔
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc