Last active
September 29, 2021 06:36
-
-
Save jmcd/e56f4212f237162d90e1f1a4074988a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyApp.Domain | |
{ | |
using Ardalis.GuardClauses; | |
public class CountryCode | |
{ | |
public CountryCode(string value) => Value = Guard.Against.UnknownCountryCode(value, nameof(value)); | |
public string Value { get; } | |
} | |
public class Extension | |
{ | |
public Extension(string value) => Value = Guard.Against.InvalidExtension(value, nameof(value)); | |
public string Value { get; } | |
} | |
public class AreaCode | |
{ | |
public AreaCode(string value) => Value = Guard.Against.UnknownAreaCode(value, nameof(value)); | |
public string Value { get; } | |
} | |
public class PhoneNumber | |
{ | |
public PhoneNumber(CountryCode countryCode, AreaCode areaCode, Extension extension) | |
{ | |
CountryCode = countryCode; | |
AreaCode = areaCode; | |
Extension = extension; | |
} | |
public CountryCode CountryCode { get; } | |
public AreaCode AreaCode { get; } | |
public Extension Extension { get; } | |
} | |
} | |
namespace TestProject2 | |
{ | |
using System; | |
using FluentAssertions; | |
using MyApp.Domain; | |
using Xunit; | |
public class UnitTest1 | |
{ | |
[Theory] | |
[InlineData("A", "1", "2")] | |
[InlineData("1", "A", "2")] | |
[InlineData("1", "2", "A")] | |
public void All_parts_exceptions_on_invalid(string countryCodeValue, string areaCodeValue, string extensionValue) => | |
FluentActions | |
.Invoking(() => | |
{ | |
var countryCode = new CountryCode(countryCodeValue); | |
var areaCode = new AreaCode(areaCodeValue); | |
var extension = new Extension(extensionValue); | |
return new PhoneNumber(countryCode, areaCode, extension); | |
}) | |
.Should().Throw<Exception>(); | |
} | |
} | |
namespace Ardalis.GuardClauses | |
{ | |
using System; | |
using System.Linq; | |
public static class CountryCodeGuard | |
{ | |
private static readonly string[] KnownCountryCodeValues = { "44", "01" }; | |
private static readonly string Message = $"Should be one of {string.Join(", ", KnownCountryCodeValues)}"; | |
public static string UnknownCountryCode(this IGuardClause guardClause, string input, string parameterName) | |
{ | |
if (!KnownCountryCodeValues.Contains(input)) | |
{ | |
throw new ArgumentException(Message, parameterName); | |
} | |
return input; | |
} | |
} | |
} | |
namespace Ardalis.GuardClauses | |
{ | |
using System; | |
using System.Linq; | |
public static class AreaCodeGuard | |
{ | |
private static readonly string[] KnownAreaCodeValues = { "0131", "0141" }; | |
private static readonly string Message = $"Should be one of {string.Join(", ", KnownAreaCodeValues)}"; | |
public static string UnknownAreaCode(this IGuardClause guardClause, string input, string parameterName) | |
{ | |
if (!KnownAreaCodeValues.Contains(input)) | |
{ | |
throw new ArgumentException(Message, parameterName); | |
} | |
return input; | |
} | |
} | |
} | |
namespace Ardalis.GuardClauses | |
{ | |
using System; | |
using System.Linq; | |
public static class ExtensionGuard | |
{ | |
public static string InvalidExtension(this IGuardClause guardClause, string input, string parameterName) | |
{ | |
if (input.Length != 4) | |
{ | |
throw new ArgumentException("Should be length of exactly 4", parameterName); | |
} | |
if (!input.All(char.IsDigit)) | |
{ | |
throw new ArgumentException("Should contain only digits", parameterName); | |
} | |
return input; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment