|
1 |
| -namespace Logitar.Identity.Core; |
| 1 | +using FluentValidation; |
| 2 | +using Logitar.Security.Cryptography; |
| 3 | +using System.Globalization; |
| 4 | + |
| 5 | +namespace Logitar.Identity.Core; |
2 | 6 |
|
3 | 7 | [Trait(Traits.Category, Categories.Unit)]
|
4 | 8 | public class LocaleTests
|
5 | 9 | {
|
6 |
| - // TODO(fpion): implement |
| 10 | + [Fact(DisplayName = "ctor: it should construct the correct instance given a valid value.")] |
| 11 | + public void Given_ValidValue_When_ctor_Then_ConstructedCorrectly() |
| 12 | + { |
| 13 | + CultureInfo culture = CultureInfo.GetCultureInfo("fr-CA"); |
| 14 | + Locale locale = new($" {culture} "); |
| 15 | + Assert.Equal(culture.Name, locale.Code); |
| 16 | + Assert.Equal(culture, locale.Culture); |
| 17 | + } |
| 18 | + |
| 19 | + [Theory(DisplayName = "ctor: it should throw ValidationException given an empty or white-space value.")] |
| 20 | + [InlineData("")] |
| 21 | + [InlineData(" ")] |
| 22 | + public void Given_EmptyOrWhiteSpace_When_ctor_Then_ValidationException(string value) |
| 23 | + { |
| 24 | + var exception = Assert.Throws<ValidationException>(() => new Locale(value)); |
| 25 | + |
| 26 | + Assert.Equal(2, exception.Errors.Count()); |
| 27 | + Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code"); |
| 28 | + Assert.Contains(exception.Errors, e => e.ErrorCode == "NotEmptyValidator" && e.PropertyName == "Code"); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact(DisplayName = "ctor: it should throw ValidationException given an invalid value.")] |
| 32 | + public void Given_InvalidValue_When_ctor_Then_ValidationException() |
| 33 | + { |
| 34 | + string value = RandomStringGenerator.GetString(999); |
| 35 | + var exception = Assert.Throws<ValidationException>(() => new Locale(value)); |
| 36 | + |
| 37 | + Assert.Equal(2, exception.Errors.Count()); |
| 38 | + Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code"); |
| 39 | + Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Code"); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact(DisplayName = "ToString: it should return the correct string representation.")] |
| 43 | + public void Given_Locale_When_ToString_Then_CorrectString() |
| 44 | + { |
| 45 | + CultureInfo culture = CultureInfo.GetCultureInfo("fr-CA"); |
| 46 | + Locale locale = new(culture); |
| 47 | + Assert.Equal(string.Format("{0} ({1})", culture.DisplayName, culture.Name), locale.ToString()); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact(DisplayName = "TryCreate: it should return a new instance given a valid value.")] |
| 51 | + public void Given_ValidValue_When_TryCreate_Then_InstanceReturned() |
| 52 | + { |
| 53 | + string value = " fr-CA "; |
| 54 | + Locale? locale = Locale.TryCreate(value); |
| 55 | + Assert.NotNull(locale); |
| 56 | + Assert.Equal(value.Trim(), locale.Culture.Name); |
| 57 | + } |
| 58 | + |
| 59 | + [Theory(DisplayName = "TryCreate: it should return null given a null, empty, or white-space value.")] |
| 60 | + [InlineData(null)] |
| 61 | + [InlineData("")] |
| 62 | + [InlineData(" ")] |
| 63 | + public void Given_NullEmptyOrWhiteSpace_When_TryCreate_Then_NullReturned(string? value) |
| 64 | + { |
| 65 | + Assert.Null(Locale.TryCreate(value)); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact(DisplayName = "TryCreate: it should throw ValidationException given an invalid value.")] |
| 69 | + public void Given_InvalidValue_When_TryCreate_Then_ValidationException() |
| 70 | + { |
| 71 | + string value = RandomStringGenerator.GetString(999); |
| 72 | + var exception = Assert.Throws<ValidationException>(() => Locale.TryCreate(value)); |
| 73 | + |
| 74 | + Assert.Equal(2, exception.Errors.Count()); |
| 75 | + Assert.Contains(exception.Errors, e => e.ErrorCode == "LocaleValidator" && e.PropertyName == "Code"); |
| 76 | + Assert.Contains(exception.Errors, e => e.ErrorCode == "MaximumLengthValidator" && e.PropertyName == "Code"); |
| 77 | + } |
7 | 78 | }
|
0 commit comments