Add more DebugTools assert variants (#4425)

This commit is contained in:
Leon Friedrich
2023-09-18 13:18:35 +12:00
committed by GitHub
parent 10f25faabf
commit 9576d0739f

View File

@@ -1,8 +1,10 @@
using System;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Robust.Shared.Utility
{
@@ -36,6 +38,67 @@ namespace Robust.Shared.Utility
throw new DebugAssertException();
}
[Conditional("DEBUG")]
[AssertionMethod]
public static void AssertEqual(object? objA, object? objB)
{
if (ReferenceEquals(objA, objB))
return;
if (objA == null || !objA.Equals(objB))
throw new DebugAssertException($"Expected: {objB ?? "null"} but was {objA ?? "null"}");
}
[Conditional("DEBUG")]
[AssertionMethod]
public static void AssertEqual(object? objA, object? objB, string message)
{
if (ReferenceEquals(objA, objB))
return;
if (objA == null || !objA.Equals(objB))
throw new DebugAssertException($"{message}\nExpected: {objB ?? "null"} but was {objA ?? "null"}");
}
[Conditional("DEBUG")]
[AssertionMethod]
public static void AssertNotEqual(object? objA, object? objB)
{
if (ReferenceEquals(objA, objB))
throw new DebugAssertException($"Expected: not {objB ?? "null"}");
if (objA == null || !objA.Equals(objB))
return;
throw new DebugAssertException($"Expected: not {objB}");
}
[Conditional("DEBUG")]
[AssertionMethod]
public static void AssertNotEqual(object? objA, object? objB, string message)
{
if (ReferenceEquals(objA, objB))
throw new DebugAssertException($"{message}\nExpected: not {objB ?? "null"}");
if (objA == null || !objA.Equals(objB))
return;
throw new DebugAssertException($"{message}\nExpected: not {objB}");
}
[Conditional("DEBUG")]
[AssertionMethod]
public static void AssertOwner(EntityUid? uid, IComponent component)
{
if (uid == null)
throw new DebugAssertException($"Null entity uid cannot own a component. Component: {component.GetType().Name}");
// Whenever .owner is removed this will need to be replaced by something.
// We need some way to ensure that people don't mix up uids & components when calling methods.
if (component.Owner != uid)
throw new DebugAssertException($"Entity {uid} is not the owner of the component. Component: {component.GetType().Name}");
}
/// <summary>
/// An assertion that will <see langword="throw" /> an exception if the
/// <paramref name="condition" /> is not true.