Implement True/False guards
ADD
WalletWasabi.Tests/UnitTests/GuardTests.cs
+using System;
+using System.Collections.Generic;
+using System.Text;
+using WalletWasabi.Helpers;
+using Xunit;
+
+namespace WalletWasabi.Tests.UnitTests
+{
+ public class GuardTests
+ {
+ [Fact]
+ public void True()
+ {
+ Guard.True("foo", true);
+ Assert.Throws<ArgumentNullException>(() => Guard.True(null, true));
+ Assert.Throws<ArgumentException>(() => Guard.True("", true));
+ Assert.Throws<ArgumentException>(() => Guard.True(" ", true));
+ Assert.Throws<ArgumentOutOfRangeException>(() => Guard.True("foo", false));
+ Assert.Throws<ArgumentNullException>(() => Guard.True("foo", null));
+ }
+
+ [Fact]
+ public void False()
+ {
+ Guard.False("foo", false);
+ Assert.Throws<ArgumentNullException>(() => Guard.False(null, false));
+ Assert.Throws<ArgumentException>(() => Guard.False("", false));
+ Assert.Throws<ArgumentException>(() => Guard.False(" ", false));
+ Assert.Throws<ArgumentOutOfRangeException>(() => Guard.False("foo", true));
+ Assert.Throws<ArgumentNullException>(() => Guard.False("foo", null));
+ }
+ }
+}

MODIFY
WalletWasabi/Helpers/Guard.cs
WalletWasabi/Helpers/Guard.cs
@@ -6,6 +6,24 @@ namespace WalletWasabi.Helpers
{
public static class Guard
{
+ public static bool True(string parameterName, bool? value)
+ => AssertBool(parameterName, true, value);
+
+ public static bool False(string parameterName, bool? value)
+ => AssertBool(parameterName, false, value);
+
+ private static bool AssertBool(string parameterName, bool expectedValue, bool? value)
+ {
+ NotNull(parameterName, value);
+
+ if (value != expectedValue)
+ {
+ throw new ArgumentOutOfRangeException(parameterName, value, $"Parameter must be {expectedValue}.");
+ }
+
+ return (bool)value;
+ }
+
public static T NotNull<T>(string parameterName, T value)
{
AssertCorrectParameterName(parameterName);

Implement True/False guards