This commit is contained in:
2021-06-13 10:28:03 +02:00
parent eb70603c85
commit df2d24cbd3
7487 changed files with 943244 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
using UnityEngine;
namespace Unity.VisualScripting
{
public static class Ensure
{
private static readonly EnsureThat instance = new EnsureThat();
public static bool IsActive { get; set; }
public static void Off() => IsActive = false;
public static void On() => IsActive = true;
public static EnsureThat That(string paramName)
{
instance.paramName = paramName;
return instance;
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void OnRuntimeMethodLoad()
{
IsActive = Application.isEditor || Debug.isDebugBuild;
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void IsTrue(bool value)
{
if (!Ensure.IsActive)
{
return;
}
if (!value)
{
throw new ArgumentException(ExceptionMessages.Booleans_IsTrueFailed, paramName);
}
}
public void IsFalse(bool value)
{
if (!Ensure.IsActive)
{
return;
}
if (value)
{
throw new ArgumentException(ExceptionMessages.Booleans_IsFalseFailed, paramName);
}
}
}
}

View File

@@ -0,0 +1,239 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void HasItems<T>(T value) where T : class, ICollection
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
if (value.Count < 1)
{
throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName);
}
}
public void HasItems<T>(ICollection<T> value)
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
if (value.Count < 1)
{
throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName);
}
}
public void HasItems<T>(T[] value)
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
if (value.Length < 1)
{
throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName);
}
}
public void HasNoNullItem<T>(T value) where T : class, IEnumerable
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
foreach (var item in value)
{
if (item == null)
{
throw new ArgumentException(ExceptionMessages.Collections_HasNoNullItemFailed, paramName);
}
}
}
public void HasItems<T>(IList<T> value) => HasItems(value as ICollection<T>);
public void HasItems<TKey, TValue>(IDictionary<TKey, TValue> value)
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
if (value.Count < 1)
{
throw new ArgumentException(ExceptionMessages.Collections_HasItemsFailed, paramName);
}
}
public void SizeIs<T>(T[] value, int expected)
{
if (!Ensure.IsActive)
{
return;
}
if (value.Length != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Length), paramName);
}
}
public void SizeIs<T>(T[] value, long expected)
{
if (!Ensure.IsActive)
{
return;
}
if (value.Length != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Length), paramName);
}
}
public void SizeIs<T>(T value, int expected) where T : ICollection
{
if (!Ensure.IsActive)
{
return;
}
if (value.Count != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName);
}
}
public void SizeIs<T>(T value, long expected) where T : ICollection
{
if (!Ensure.IsActive)
{
return;
}
if (value.Count != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName);
}
}
public void SizeIs<T>(ICollection<T> value, int expected)
{
if (!Ensure.IsActive)
{
return;
}
if (value.Count != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName);
}
}
public void SizeIs<T>(ICollection<T> value, long expected)
{
if (!Ensure.IsActive)
{
return;
}
if (value.Count != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName);
}
}
public void SizeIs<T>(IList<T> value, int expected) => SizeIs(value as ICollection<T>, expected);
public void SizeIs<T>(IList<T> value, long expected) => SizeIs(value as ICollection<T>, expected);
public void SizeIs<TKey, TValue>(IDictionary<TKey, TValue> value, int expected)
{
if (!Ensure.IsActive)
{
return;
}
if (value.Count != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName);
}
}
public void SizeIs<TKey, TValue>(IDictionary<TKey, TValue> value, long expected)
{
if (!Ensure.IsActive)
{
return;
}
if (value.Count != expected)
{
throw new ArgumentException(ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count), paramName);
}
}
public void IsKeyOf<TKey, TValue>(IDictionary<TKey, TValue> value, TKey expectedKey, string keyLabel = null)
{
if (!Ensure.IsActive)
{
return;
}
if (!value.ContainsKey(expectedKey))
{
throw new ArgumentException(ExceptionMessages.Collections_ContainsKey_Failed.Inject(expectedKey, keyLabel ?? paramName.Prettify()), paramName);
}
}
public void Any<T>(IList<T> value, Func<T, bool> predicate) => Any(value as ICollection<T>, predicate);
public void Any<T>(ICollection<T> value, Func<T, bool> predicate)
{
if (!Ensure.IsActive)
{
return;
}
if (!value.Any(predicate))
{
throw new ArgumentException(ExceptionMessages.Collections_Any_Failed, paramName);
}
}
public void Any<T>(T[] value, Func<T, bool> predicate)
{
if (!Ensure.IsActive)
{
return;
}
if (!value.Any(predicate))
{
throw new ArgumentException(ExceptionMessages.Collections_Any_Failed, paramName);
}
}
}
}

View File

@@ -0,0 +1,103 @@
using System;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void Is<T>(T param, T expected) where T : struct, IComparable<T>
{
if (!Ensure.IsActive)
{
return;
}
if (!param.IsEq(expected))
{
throw new ArgumentException(ExceptionMessages.Comp_Is_Failed.Inject(param, expected), paramName);
}
}
public void IsNot<T>(T param, T expected) where T : struct, IComparable<T>
{
if (!Ensure.IsActive)
{
return;
}
if (param.IsEq(expected))
{
throw new ArgumentException(ExceptionMessages.Comp_IsNot_Failed.Inject(param, expected), paramName);
}
}
public void IsLt<T>(T param, T limit) where T : struct, IComparable<T>
{
if (!Ensure.IsActive)
{
return;
}
if (!param.IsLt(limit))
{
throw new ArgumentException(ExceptionMessages.Comp_IsNotLt.Inject(param, limit), paramName);
}
}
public void IsLte<T>(T param, T limit) where T : struct, IComparable<T>
{
if (!Ensure.IsActive)
{
return;
}
if (param.IsGt(limit))
{
throw new ArgumentException(ExceptionMessages.Comp_IsNotLte.Inject(param, limit), paramName);
}
}
public void IsGt<T>(T param, T limit) where T : struct, IComparable<T>
{
if (!Ensure.IsActive)
{
return;
}
if (!param.IsGt(limit))
{
throw new ArgumentException(ExceptionMessages.Comp_IsNotGt.Inject(param, limit), paramName);
}
}
public void IsGte<T>(T param, T limit) where T : struct, IComparable<T>
{
if (!Ensure.IsActive)
{
return;
}
if (param.IsLt(limit))
{
throw new ArgumentException(ExceptionMessages.Comp_IsNotGte.Inject(param, limit), paramName);
}
}
public void IsInRange<T>(T param, T min, T max) where T : struct, IComparable<T>
{
if (!Ensure.IsActive)
{
return;
}
if (param.IsLt(min))
{
throw new ArgumentException(ExceptionMessages.Comp_IsNotInRange_ToLow.Inject(param, min), paramName);
}
if (param.IsGt(max))
{
throw new ArgumentException(ExceptionMessages.Comp_IsNotInRange_ToHigh.Inject(param, max), paramName);
}
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void IsNotEmpty(Guid value)
{
if (!Ensure.IsActive)
{
return;
}
if (value.Equals(Guid.Empty))
{
throw new ArgumentException(ExceptionMessages.Guids_IsNotEmpty_Failed, paramName);
}
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void IsNotNull<T>(T? value) where T : struct
{
if (!Ensure.IsActive)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(paramName, ExceptionMessages.Common_IsNotNull_Failed);
}
}
}
}

View File

@@ -0,0 +1,34 @@
using System;
using JetBrains.Annotations;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void IsNull<T>([NoEnumeration] T value)
{
if (!Ensure.IsActive)
{
return;
}
if (value != null)
{
throw new ArgumentNullException(paramName, ExceptionMessages.Common_IsNull_Failed);
}
}
public void IsNotNull<T>([NoEnumeration] T value)
{
if (!Ensure.IsActive)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(paramName, ExceptionMessages.Common_IsNotNull_Failed);
}
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void HasAttribute(Type param, Type attributeType)
{
if (!Ensure.IsActive)
{
return;
}
if (!param.HasAttribute(attributeType))
{
throw new ArgumentException(ExceptionMessages.Reflection_HasAttribute_Failed.Inject(param.ToString(), attributeType.ToString()), paramName);
}
}
public void HasAttribute<TAttribute>(Type param) where TAttribute : Attribute => HasAttribute(param, typeof(TAttribute));
private void HasConstructorAccepting(Type param, Type[] parameterTypes, bool nonPublic)
{
if (!Ensure.IsActive)
{
return;
}
if (param.GetConstructorAccepting(parameterTypes, nonPublic) == null)
{
var message = nonPublic ? ExceptionMessages.Reflection_HasConstructor_Failed : ExceptionMessages.Reflection_HasPublicConstructor_Failed;
throw new ArgumentException(message.Inject(param.ToString(), parameterTypes.ToCommaSeparatedString()), paramName);
}
}
public void HasConstructorAccepting(Type param, params Type[] parameterTypes) => HasConstructorAccepting(param, parameterTypes, true);
public void HasPublicConstructorAccepting(Type param, params Type[] parameterTypes) => HasConstructorAccepting(param, parameterTypes, false);
}
}

View File

@@ -0,0 +1,188 @@
using System;
using System.Text.RegularExpressions;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void IsNotNullOrWhiteSpace(string value)
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
if (StringUtility.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(ExceptionMessages.Strings_IsNotNullOrWhiteSpace_Failed, paramName);
}
}
public void IsNotNullOrEmpty(string value)
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(ExceptionMessages.Strings_IsNotNullOrEmpty_Failed, paramName);
}
}
public void IsNotNull(string value)
{
if (!Ensure.IsActive)
{
return;
}
if (value == null)
{
throw new ArgumentNullException(paramName, ExceptionMessages.Common_IsNotNull_Failed);
}
}
public void IsNotEmpty(string value)
{
if (!Ensure.IsActive)
{
return;
}
if (string.Empty.Equals(value))
{
throw new ArgumentException(ExceptionMessages.Strings_IsNotEmpty_Failed, paramName);
}
}
public void HasLengthBetween(string value, int minLength, int maxLength)
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
var length = value.Length;
if (length < minLength)
{
throw new ArgumentException(ExceptionMessages.Strings_HasLengthBetween_Failed_ToShort.Inject(minLength, maxLength, length), paramName);
}
if (length > maxLength)
{
throw new ArgumentException(ExceptionMessages.Strings_HasLengthBetween_Failed_ToLong.Inject(minLength, maxLength, length), paramName);
}
}
public void Matches(string value, string match) => Matches(value, new Regex(match));
public void Matches(string value, Regex match)
{
if (!Ensure.IsActive)
{
return;
}
if (!match.IsMatch(value))
{
throw new ArgumentException(ExceptionMessages.Strings_Matches_Failed.Inject(value, match), paramName);
}
}
public void SizeIs(string value, int expected)
{
if (!Ensure.IsActive)
{
return;
}
IsNotNull(value);
if (value.Length != expected)
{
throw new ArgumentException(ExceptionMessages.Strings_SizeIs_Failed.Inject(expected, value.Length), paramName);
}
}
public void IsEqualTo(string value, string expected)
{
if (!Ensure.IsActive)
{
return;
}
if (!StringEquals(value, expected))
{
throw new ArgumentException(ExceptionMessages.Strings_IsEqualTo_Failed.Inject(value, expected), paramName);
}
}
public void IsEqualTo(string value, string expected, StringComparison comparison)
{
if (!Ensure.IsActive)
{
return;
}
if (!StringEquals(value, expected, comparison))
{
throw new ArgumentException(ExceptionMessages.Strings_IsEqualTo_Failed.Inject(value, expected), paramName);
}
}
public void IsNotEqualTo(string value, string expected)
{
if (!Ensure.IsActive)
{
return;
}
if (StringEquals(value, expected))
{
throw new ArgumentException(ExceptionMessages.Strings_IsNotEqualTo_Failed.Inject(value, expected), paramName);
}
}
public void IsNotEqualTo(string value, string expected, StringComparison comparison)
{
if (!Ensure.IsActive)
{
return;
}
if (StringEquals(value, expected, comparison))
{
throw new ArgumentException(ExceptionMessages.Strings_IsNotEqualTo_Failed.Inject(value, expected), paramName);
}
}
public void IsGuid(string value)
{
if (!Ensure.IsActive)
{
return;
}
if (!StringUtility.IsGuid(value))
{
throw new ArgumentException(ExceptionMessages.Strings_IsGuid_Failed.Inject(value), paramName);
}
}
private bool StringEquals(string x, string y, StringComparison? comparison = null)
{
return comparison.HasValue
? string.Equals(x, y, comparison.Value)
: string.Equals(x, y);
}
}
}

View File

@@ -0,0 +1,37 @@
using System;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void IsOfType<T>(T param, Type expectedType)
{
if (!Ensure.IsActive)
{
return;
}
if (!expectedType.IsAssignableFrom(param))
{
throw new ArgumentException(ExceptionMessages.Types_IsOfType_Failed.Inject(expectedType.ToString(), param?.GetType().ToString() ?? "null"), paramName);
}
}
public void IsOfType(Type param, Type expectedType)
{
if (!Ensure.IsActive)
{
return;
}
if (!expectedType.IsAssignableFrom(param))
{
throw new ArgumentException(ExceptionMessages.Types_IsOfType_Failed.Inject(expectedType.ToString(), param.ToString()), paramName);
}
}
public void IsOfType<T>(object param) => IsOfType(param, typeof(T));
public void IsOfType<T>(Type param) => IsOfType(param, typeof(T));
}
}

View File

@@ -0,0 +1,20 @@
using System;
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
public void IsNotDefault<T>(T param) where T : struct
{
if (!Ensure.IsActive)
{
return;
}
if (default(T).Equals(param))
{
throw new ArgumentException(ExceptionMessages.ValueTypes_IsNotDefault_Failed, paramName);
}
}
}
}

View File

@@ -0,0 +1,7 @@
namespace Unity.VisualScripting
{
public partial class EnsureThat
{
internal string paramName;
}
}

View File

@@ -0,0 +1,47 @@
namespace Unity.VisualScripting
{
public static class ExceptionMessages
{
public static string Common_IsNull_Failed { get; } = "Value must be null.";
public static string Common_IsNotNull_Failed { get; } = "Value cannot be null.";
public static string Booleans_IsTrueFailed { get; } = "Expected an expression that evaluates to true.";
public static string Booleans_IsFalseFailed { get; } = "Expected an expression that evaluates to false.";
public static string Collections_Any_Failed { get; } = "The predicate did not match any elements.";
public static string Collections_ContainsKey_Failed { get; } = "{1} '{0}' was not found.";
public static string Collections_HasItemsFailed { get; } = "Empty collection is not allowed.";
public static string Collections_HasNoNullItemFailed { get; } = "Collection with null items is not allowed.";
public static string Collections_SizeIs_Failed { get; } = "Expected size '{0}' but found '{1}'.";
public static string Comp_Is_Failed { get; } = "Value '{0}' is not '{1}'.";
public static string Comp_IsNot_Failed { get; } = "Value '{0}' is '{1}', which was not expected.";
public static string Comp_IsNotLt { get; } = "Value '{0}' is not lower than limit '{1}'.";
public static string Comp_IsNotLte { get; } = "Value '{0}' is not lower than or equal to limit '{1}'.";
public static string Comp_IsNotGt { get; } = "Value '{0}' is not greater than limit '{1}'.";
public static string Comp_IsNotGte { get; } = "Value '{0}' is not greater than or equal to limit '{1}'.";
public static string Comp_IsNotInRange_ToLow { get; } = "Value '{0}' is < min '{1}'.";
public static string Comp_IsNotInRange_ToHigh { get; } = "Value '{0}' is > max '{1}'.";
public static string Guids_IsNotEmpty_Failed { get; } = "An empty GUID is not allowed.";
public static string Strings_IsEqualTo_Failed { get; } = "Value '{0}' is not '{1}'.";
public static string Strings_IsNotEqualTo_Failed { get; } = "Value '{0}' is '{1}', which was not expected.";
public static string Strings_SizeIs_Failed { get; } = "Expected length '{0}' but got '{1}'.";
public static string Strings_IsNotNullOrWhiteSpace_Failed { get; } = "The string can't be left empty, null or consist of only whitespaces.";
public static string Strings_IsNotNullOrEmpty_Failed { get; } = "The string can't be null or empty.";
public static string Strings_HasLengthBetween_Failed_ToShort { get; } = "The string is not long enough. Must be between '{0}' and '{1}' but was '{2}' characters long.";
public static string Strings_HasLengthBetween_Failed_ToLong { get; } = "The string is too long. Must be between '{0}' and '{1}'. Must be between '{0}' and '{1}' but was '{2}' characters long.";
public static string Strings_Matches_Failed { get; } = "Value '{0}' does not match '{1}'";
public static string Strings_IsNotEmpty_Failed { get; } = "Empty String is not allowed.";
public static string Strings_IsGuid_Failed { get; } = "Value '{0}' is not a valid GUID.";
public static string Types_IsOfType_Failed { get; } = "Expected a '{0}' but got '{1}'.";
public static string Reflection_HasAttribute_Failed { get; } = "Type '{0}' does not define the [{1}] attribute.";
public static string Reflection_HasConstructor_Failed { get; } = "Type '{0}' does not provide a constructor accepting ({1}).";
public static string Reflection_HasPublicConstructor_Failed { get; } = "Type '{0}' does not provide a public constructor accepting ({1}).";
public static string ValueTypes_IsNotDefault_Failed { get; } = "The param was expected to not be of default value.";
}
}

View File

@@ -0,0 +1,22 @@
using System;
namespace Unity.VisualScripting
{
internal static class XComparable
{
internal static bool IsLt<T>(this IComparable<T> x, T y)
{
return x.CompareTo(y) < 0;
}
internal static bool IsEq<T>(this IComparable<T> x, T y)
{
return x.CompareTo(y) == 0;
}
internal static bool IsGt<T>(this IComparable<T> x, T y)
{
return x.CompareTo(y) > 0;
}
}
}

View File

@@ -0,0 +1,17 @@
using System.Linq;
namespace Unity.VisualScripting
{
internal static class XString
{
internal static string Inject(this string format, params object[] formattingArgs)
{
return string.Format(format, formattingArgs);
}
internal static string Inject(this string format, params string[] formattingArgs)
{
return string.Format(format, formattingArgs.Select(a => a as object).ToArray());
}
}
}