-
Notifications
You must be signed in to change notification settings - Fork 9
/
Throw.DebugAssert.cs
54 lines (51 loc) · 2.55 KB
/
Throw.DebugAssert.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
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace CK.Core;
public partial class Throw
{
/// <summary>
/// <see cref="Debug.Assert(bool)"/> replacement that captures the expression, file path and
/// line number and throws a <see cref="CKException"/>.
/// </summary>
/// <param name="valid">Must be true.</param>
/// <param name="exp">Automatically captures <paramref name="valid"/> expression source text.</param>
/// <param name="filePath">Automatically captures the source code file path.</param>
/// <param name="lineNumber">Automatically captures the source code line number.</param>
/// <exception cref="CKException">Whenever <paramref name="valid"/> is false.</exception>
[StackTraceHidden]
[Conditional( "DEBUG" )]
public static void DebugAssert( [DoesNotReturnIf( false )] bool valid,
[CallerArgumentExpression( "valid" )] string? exp = null,
[CallerFilePath] string? filePath = null,
[CallerLineNumber] int lineNumber = 0 )
{
if( !valid )
{
Throw.CKException( $"Debug Assertion failed '{exp}', {filePath}@{lineNumber}." );
}
}
/// <summary>
/// <see cref="Debug.Assert(bool)"/> replacement that captures the expression, file path and
/// line number and throws a <see cref="CKException"/>.
/// </summary>
/// <param name="message">Additional message that will appear in the exception.</param>
/// <param name="valid">Must be true.</param>
/// <param name="exp">Automatically captures <paramref name="valid"/> expression source text.</param>
/// <param name="filePath">Automatically captures the source code file path.</param>
/// <param name="lineNumber">Automatically captures the source code line number.</param>
/// <exception cref="CKException">Whenever <paramref name="valid"/> is false.</exception>
[StackTraceHidden]
[Conditional( "DEBUG" )]
public static void DebugAssert( string message,
[DoesNotReturnIf( false )] bool valid,
[CallerArgumentExpression( "valid" )] string? exp = null,
[CallerFilePath] string? filePath = null,
[CallerLineNumber] int lineNumber = 0 )
{
if( !valid )
{
Throw.CKException( $"Debug Assertion failed: {message} - '{exp}', {filePath}@{lineNumber}." );
}
}
}