You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/08/24 18:29:49 UTC
[lucenenet] 02/13: Lucene.Net.Diagnostics: Added static
Debugging.Assert() overloads to allow assertions to be turned on and off in
the Release build
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 4b3a849a642fc47396866ba30ee5f585fddaa926
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Thu Aug 13 17:00:10 2020 +0700
Lucene.Net.Diagnostics: Added static Debugging.Assert() overloads to allow assertions to be turned on and off in the Release build
---
src/Lucene.Net/Support/Diagnostics/Debugging.cs | 57 +++++++++++++++++++++++--
1 file changed, 54 insertions(+), 3 deletions(-)
diff --git a/src/Lucene.Net/Support/Diagnostics/Debugging.cs b/src/Lucene.Net/Support/Diagnostics/Debugging.cs
index ecde166..e4ce467 100644
--- a/src/Lucene.Net/Support/Diagnostics/Debugging.cs
+++ b/src/Lucene.Net/Support/Diagnostics/Debugging.cs
@@ -1,4 +1,6 @@
using Lucene.Net.Util;
+using System;
+using System.Runtime.CompilerServices;
namespace Lucene.Net.Diagnostics
{
@@ -19,9 +21,9 @@ namespace Lucene.Net.Diagnostics
* limitations under the License.
*/
- // LUCENENET: This can only be named Debug if we merge it with the Debug
- // class from Lucene.Net.TestFramework because it is in the same namespace.
- // But that class is dependent upon AssertionException, which is only for testing.
+ /// <summary>
+ /// Provides a set of methods that help debug your code.
+ /// </summary>
internal static class Debugging
{
/// <summary>
@@ -33,5 +35,54 @@ namespace Lucene.Net.Diagnostics
/// <see cref="Index.TermVectorsConsumer"/>, and <see cref="Index.TermVectorsConsumerPerField"/>.
/// </summary>
public static bool AssertsEnabled { get; set; } = SystemProperties.GetPropertyAsBoolean("assert", false);
+
+ ///// <summary>
+ ///// Checks for a condition; if the condition is <c>false</c>, throws an <see cref="AssertionException"/>.
+ ///// </summary>
+ ///// <param name="condition">The conditional expression to evaluate. If the condition is <c>true</c>, no exception is thrown.</param>
+ //[MethodImpl(MethodImplOptions.AggressiveInlining)]
+ //public static void Assert(bool condition)
+ //{
+ // if (AssertsEnabled && !condition)
+ // throw new AssertionException();
+ //}
+
+ ///// <summary>
+ ///// Checks for a condition; if the <paramref name="condition"/> is <c>false</c>, throws an <see cref="AssertionException"/> with the specified <paramref name="message"/>.
+ ///// </summary>
+ ///// <param name="condition">The conditional expression to evaluate. If the condition is <c>true</c>, no exception is thrown.</param>
+ ///// <param name="messageFactory">A delegate to build the message to use.</param>
+ //[MethodImpl(MethodImplOptions.AggressiveInlining)]
+ //public static void Assert(bool condition, Func<string> messageFactory)
+ //{
+ // if (AssertsEnabled && !condition)
+ // throw new AssertionException(messageFactory());
+ //}
+
+ /// <summary>
+ /// Checks for a condition; if the condition is <c>false</c>, throws an <see cref="AssertionException"/>.
+ /// </summary>
+ /// <param name="conditionFactory">A delegate that returns the conditional expression to evaluate. If the condition is <c>true</c>, no exception is thrown.</param>
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void Assert(Func<bool> conditionFactory)
+ {
+ if (AssertsEnabled && !conditionFactory())
+ throw new AssertionException();
+ }
+
+ /// <summary>
+ /// Checks for a condition if asserts are enabled; if the <paramref name="conditionFactory"/>
+ /// returns <c>false</c>, throws an <see cref="AssertionException"/> with the message returned
+ /// from the specified <paramref name="messageFactory"/>.
+ /// </summary>
+ /// <param name="conditionFactory">A delegate that returns the conditional expression to evaluate. If the condition returned from the factory is <c>true</c>, no exception is thrown.</param>
+ /// <param name="messageFactory">A delegate to build the message to use.</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void Assert(Func<bool> conditionFactory, Func<string> messageFactory)
+ {
+ if (AssertsEnabled && !conditionFactory())
+ throw new AssertionException(messageFactory());
+ }
}
}