From 21cf2d006084043685f0db30fbbc2bf854403ecc Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 13:48:42 +0200 Subject: [PATCH 01/37] Fix benchmarks - Pass the config - Fix `Xtz.StronglyTyped.Benchmark.Models.IntStructId.IsValid()` --- .../Xtz.StronglyTyped.Benchmark/Models/IntStructId.cs | 2 +- src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs | 6 +++--- .../SystemTextJsonSerializationGuidIds.cs | 6 +++--- .../SystemTextJsonSerializationIntIds.cs | 8 ++++---- .../SystemTextJsonSerializationMacAddress.cs | 10 +++++----- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Models/IntStructId.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/Models/IntStructId.cs index f4f10a9..8897d0e 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Models/IntStructId.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Models/IntStructId.cs @@ -8,7 +8,7 @@ public partial struct IntStructId private bool IsValid(int value) { // ID must be greater than 0 - return value <= 0; + return value > 0; } } } \ No newline at end of file diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs index afa19ab..7074e4f 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs @@ -16,9 +16,9 @@ public static void Main(string[] args) #endif BenchmarkRunner.Run(config); - BenchmarkRunner.Run(); - BenchmarkRunner.Run(); - BenchmarkRunner.Run(); + BenchmarkRunner.Run(config); + BenchmarkRunner.Run(config); + BenchmarkRunner.Run(config); } } } \ No newline at end of file diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs index 5c12fb1..3ac1dd7 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs @@ -38,21 +38,21 @@ public string SerializeEmailStrings() } [Benchmark(Description = "StronglyTyped>")] - public string SerializeStronglyTypedStringStructs() + public string SerializeStronglyTypedGuidStructs() { var result = JsonSerializer.Serialize(_stronglyTypedGuidStructs); return result; } [Benchmark(Description = "StronglyTyped")] - public string SerializeStronglyTypedEmails() + public string SerializeStronglyTypedGuids() { var result = JsonSerializer.Serialize(_employeeGuidIds); return result; } [Benchmark(Description = "Other StronglyTyped")] - public string SerializeOtherStronglyTypedEmails() + public string SerializeOtherStronglyTypedGuids() { var result = JsonSerializer.Serialize(_otherEmployeeGuidIds); return result; diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs index f3ad3ff..d46a690 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs @@ -32,28 +32,28 @@ public SystemTextJsonSerializationIntIds() } [Benchmark(Baseline = true, Description = "int")] - public string SerializeEmailStrings() + public string SerializeInts() { var result = JsonSerializer.Serialize(_ints); return result; } [Benchmark(Description = "StronglyTyped>")] - public string SerializeStronglyTypedStringStructs() + public string SerializeStronglyTypedIntStructs() { var result = JsonSerializer.Serialize(_stronglyTypedIntStructs); return result; } [Benchmark(Description = "StronglyTyped")] - public string SerializeStronglyTypedEmails() + public string SerializeStronglyTypedIntIds() { var result = JsonSerializer.Serialize(_companyIntIds); return result; } [Benchmark(Description = "Other StronglyTyped")] - public string SerializeOtherStronglyTypedEmails() + public string SerializeOtherStronglyTypedIntIds() { var result = JsonSerializer.Serialize(_otherCompanyIntIds); return result; diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs index 501d693..7582cd3 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs @@ -40,7 +40,7 @@ public SystemTextJsonSerializationMacAddress() } [Benchmark(Baseline = true, Description = "string")] - public string SerializeEmailStrings() + public string SerializeMacAddressStrings() { var result = JsonSerializer.Serialize(_strings); return result; @@ -60,15 +60,15 @@ public string SerializeStronglyTypedStringStructs() return result; } - [Benchmark(Description = "StronglyTyped")] - public string SerializeStronglyTypedEmails() + [Benchmark(Description = "StronglyTyped")] + public string SerializeStronglyTypedMacAddresses() { var result = JsonSerializer.Serialize(_macAddresses); return result; } - [Benchmark(Description = "Other StronglyTyped")] - public string SerializeOtherStronglyTypedEmails() + [Benchmark(Description = "Other StronglyTyped")] + public string SerializeOtherStronglyTypedMacAddresses() { var result = JsonSerializer.Serialize(_otherMacAddresses); return result; From f3123fa2923e0039b62713794598f93ac723aaaa Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 13:51:20 +0200 Subject: [PATCH 02/37] Minor refactoring in `Xtz.StronglyTyped.BogusAutoFixture.UnitTests` --- .../AddressAutoDataTests.cs | 6 ++--- .../CommerceAutoDataTests.cs | 26 ++++++++++++++----- .../CompanyAutoDataTests.cs | 6 ++--- .../Extensions/Extensions.cs | 15 +++++++++++ .../FinanceAutoDataTests.cs | 6 ++--- .../InternetAutoDataTests.cs | 12 ++++++--- .../NameAutoDataTests.cs | 13 ++++++---- .../NumberAutoDataTests.cs | 6 ++--- .../PhoneAutoDataTests.cs | 6 ++--- .../VehicleAutoDataTests.cs | 6 ++--- ...glyTyped.BogusAutoFixture.UnitTests.csproj | 4 +++ 11 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Extensions/Extensions.cs diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs index 2ddd3bb..d4d041d 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.Address; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; @@ -44,10 +45,7 @@ public void ShouldGenerateStronglyTypedValues( postalCode, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); - - Assert.IsEmpty(nonBogusValues); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs index e4c0ee8..b910e33 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Commerce; @@ -14,13 +15,11 @@ public void ShouldGenerateStronglyTypedValues( Department department, Ean13 ean13, Ean8 ean8, - Price price, ProductShortName productShortName, ProductAdjective productAdjective, ProductCategory productCategory, ProductColor productColor, ProductMaterial productMaterial, - ProductFullName productFullName, SubDepartment subDepartment) { var values = new object[] @@ -28,20 +27,33 @@ public void ShouldGenerateStronglyTypedValues( department, ean13, ean8, - price, productShortName, productAdjective, productCategory, productColor, productMaterial, - productFullName, subDepartment, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + } - Assert.IsEmpty(nonBogusValues); + [Test] + [StrongAutoData] + public void ShouldGenerateStronglyTypedPrice(Price value) + { + Assert.IsFalse(value.Currency.Code.ToString().IsBogusGeneratedValue()); + Assert.IsFalse(value.Currency.Name.ToString().IsBogusGeneratedValue()); + Assert.IsFalse(value.Currency.Symbol?.ToString().IsBogusGeneratedValue()); + } + + [Test] + [StrongAutoData] + public void ShouldGenerateStronglyTypedProductFullName(ProductFullName value) + { + Assert.IsFalse(value.ProductAdjective.ToString().IsBogusGeneratedValue()); + Assert.IsFalse(value.ProductMaterial.ToString().IsBogusGeneratedValue()); + Assert.IsFalse(value.ProductShortName.ToString().IsBogusGeneratedValue()); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs index 799f2b9..6e076c8 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Company; @@ -38,10 +39,7 @@ public void ShouldGenerateStronglyTypedValues( siteCode }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); - - Assert.IsEmpty(nonBogusValues); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Extensions/Extensions.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Extensions/Extensions.cs new file mode 100644 index 0000000..5e07a8b --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Extensions/Extensions.cs @@ -0,0 +1,15 @@ +using System.Text.RegularExpressions; + +namespace Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions +{ + public static class Extensions + { + /// value0edafc90-698d-4804-9267-3c5e73f4aa94 + private static readonly Regex BOGUS_VALUE_REGEX = new Regex("value[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + public static bool IsBogusGeneratedValue(this string value) + { + return BOGUS_VALUE_REGEX.IsMatch(value); + } + } +} diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs index f3ff94f..224c3e6 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Finance; @@ -32,10 +33,7 @@ public void ShouldGenerateStronglyTypedValues( transactionType, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); - - Assert.IsEmpty(nonBogusValues); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs index b59f1cf..7d6f09a 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Internet; @@ -44,10 +46,14 @@ public void ShouldGenerateStronglyTypedValues( websiteProtocol, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + } - Assert.IsEmpty(nonBogusValues); + [Test] + [StrongAutoData] + public void ShouldGenerateStronglyTypedCollection(IEnumerable values) + { + Assert.IsNotEmpty(values); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs index 3a3415b..9d65d20 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Name; @@ -13,7 +14,6 @@ public class NameAutoDataTests public void ShouldGenerateStronglyTypedValues( DisplayName displayName, FirstName firstName, - FullName fullName, LastName lastName, NamePrefix namePrefix, NameSuffix nameSuffix) @@ -22,16 +22,19 @@ public void ShouldGenerateStronglyTypedValues( { displayName, firstName, - fullName, lastName, namePrefix, nameSuffix, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + } - Assert.IsEmpty(nonBogusValues); + [Test] + [StrongAutoData] + public void ShouldGenerateStronglyTypedFullName(FullName value) + { + Assert.IsFalse(value.FirstName.ToString().IsBogusGeneratedValue()); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs index 97f4e1e..b5f5aba 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Numbers; @@ -40,10 +41,7 @@ public void ShouldGenerateStronglyTypedValues( positiveInt64, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse((string?) x.ToString()![^36..], out _)); - - Assert.IsEmpty(nonBogusValues); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs index 7a59f94..206d8fa 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Phone; @@ -18,10 +19,7 @@ public void ShouldGenerateStronglyTypedValues( phoneNumber, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); - - Assert.IsEmpty(nonBogusValues); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs index 8e71c81..354e272 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using NUnit.Framework; +using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; using Xtz.StronglyTyped.BuiltinTypes.Vehicle; @@ -26,10 +27,7 @@ public void ShouldGenerateStronglyTypedValues( vin, }; - var nonBogusValues = values - .Where(x => x.ToString()!.Length >= 36 && Guid.TryParse(x.ToString()![^36..], out _)); - - Assert.IsEmpty(nonBogusValues); + Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj index 14bd2bf..4d317cb 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj @@ -10,6 +10,10 @@ $(BaseIntermediateOutputPath)\GeneratedFiles + + + + From 6ed668516ac435630b38d28cfe918ac3d7d21ea9 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 13:52:44 +0200 Subject: [PATCH 03/37] Fix `ShouldThrowIfEmpty()` in `StronglyTyped<>` class form `decimal inner type` --- src/Xtz.StronglyTyped/StronglyTyped.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xtz.StronglyTyped/StronglyTyped.cs b/src/Xtz.StronglyTyped/StronglyTyped.cs index 669106e..4644645 100644 --- a/src/Xtz.StronglyTyped/StronglyTyped.cs +++ b/src/Xtz.StronglyTyped/StronglyTyped.cs @@ -29,7 +29,7 @@ private void ThrowIfInvalid(TInnerType value) Throw($" value is invalid for type {GetType()}"); } - if (ShouldThrowIfEmpty() && !typeof(TInnerType).IsPrimitive) + if (ShouldThrowIfEmpty() && !(typeof(TInnerType).IsPrimitive || typeof(TInnerType) == typeof(decimal))) { if (Equals(value, string.Empty) || object.Equals(value, default(TInnerType))) { From 512a398ae99b6bd85d34bcf8778263d00ea33130 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 13:54:19 +0200 Subject: [PATCH 04/37] Consolidate NuGet package versions --- ...tz.StronglyTyped.EntityFramework.IntegrationTests.csproj | 6 +++--- .../Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj | 6 +++--- .../Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj index 8375fe5..24fc097 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj @@ -9,9 +9,9 @@ - - - + + + diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj index b8584f2..47d985d 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj @@ -8,9 +8,9 @@ - - - + + + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index 03aa0ab..b5cf811 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -45,6 +45,7 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + From 417464cda9424d5a41c698d49df06322528a9095 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:00:38 +0200 Subject: [PATCH 05/37] Update `Xtz.StronglyTyped.BuiltinTypes.AutoFixture` - Add support for collection generation --- .../Abstract/BaseFakerSpecimenBuilder.cs | 56 ++++++++++++++++++- .../Abstract/ConvertedEnumerable.cs | 26 +++++++++ .../Abstract/TypeExtensions.cs | 30 ++++++++++ .../StrongAutoDataAttribute.cs | 11 +++- 4 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/ConvertedEnumerable.cs create mode 100644 src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/TypeExtensions.cs diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs index fa54ef7..fc32df2 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Reflection; +using AutoFixture; using AutoFixture.Kernel; using Bogus; @@ -9,19 +10,72 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture { public abstract class BaseFakerSpecimenBuilder : ISpecimenBuilder { + private static readonly int DEFAULT_REPEAT_COUNT = 3; private NoSpecimen NoSpecimen { get; } = new(); protected abstract Dictionary> FakerFactories { get; } + // KLUDGE: Don't be surprised if you run single test without AutoData but it creates test values for other tests (which you are not trying to run at the moment) public object Create(object request, ISpecimenContext context) { if (request is not ParameterInfo parameterInfo) { - return NoSpecimen; + switch (request) + { + case MultipleRequest {Request: SeededRequest { Request: Type multipleType }}: + return CreateArray(multipleType, context); + case SeededRequest { Request: Type type } when type.TryGetSingleGenericTypeArgument(typeof(IEnumerable<>), out Type? enumerableType): + return CreateEnumerable(enumerableType, context); + default: + return NoSpecimen; + } } var parameterType = parameterInfo.ParameterType; + return Create(parameterType); + } + + private object CreateEnumerable(Type? enumerableType, ISpecimenContext context) + { + if (enumerableType is null) return NoSpecimen; + + var specimen = context.Resolve(new MultipleRequest(new SeededRequest(enumerableType, null))); + if (specimen is OmitSpecimen) return specimen; + + var typedAdapterType = typeof(ConvertedEnumerable<>).MakeGenericType(enumerableType); + return Activator.CreateInstance(typedAdapterType, specimen); + } + + private object CreateArray(Type multipleType, ISpecimenContext context) + { + var firstElement = Create(multipleType); + if (firstElement is NoSpecimen) return firstElement; + + var count = DEFAULT_REPEAT_COUNT; + if (context is SpecimenContext {Builder: Fixture fixture}) + { + count = fixture.RepeatCount; + if (count < 1) + { + count = DEFAULT_REPEAT_COUNT; + } + } + + var result = new object[count]; + result[0] = firstElement; + for (var i = 1; i < count; i++) + { + result[i] = Create(multipleType); + } + + return result; + } + + private object Create(Type? parameterType) + { + if (parameterType is null) return NoSpecimen; + var fakerObject = GetFaker(parameterType); if (fakerObject == null) return NoSpecimen; diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/ConvertedEnumerable.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/ConvertedEnumerable.cs new file mode 100644 index 0000000..02078d2 --- /dev/null +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/ConvertedEnumerable.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture +{ + public class ConvertedEnumerable : IEnumerable + { + private readonly IEnumerable _enumerable; + + public ConvertedEnumerable(IEnumerable enumerable) + { + _enumerable = enumerable ?? throw new ArgumentNullException(nameof(enumerable)); + } + + public IEnumerator GetEnumerator() + { + foreach (var item in _enumerable) + { + if (item is T variable) yield return variable; + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/TypeExtensions.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/TypeExtensions.cs new file mode 100644 index 0000000..a8b321c --- /dev/null +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/TypeExtensions.cs @@ -0,0 +1,30 @@ +using System; +using System.Reflection; + +namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture +{ + public static class TypeExtensions + { + public static bool TryGetSingleGenericTypeArgument( + this Type currentType, + Type expectedGenericDefinition, + out Type? enumerableType) + { + if (!expectedGenericDefinition.GetTypeInfo().IsGenericTypeDefinition) throw new ArgumentException("Must be a generic type definition", nameof(expectedGenericDefinition)); + + var typeInfo = currentType.GetTypeInfo(); + if (typeInfo.IsGenericType && currentType.GetGenericTypeDefinition() == expectedGenericDefinition) + { + var typeArguments = typeInfo.GenericTypeArguments; + if (typeArguments.Length == 1) + { + enumerableType = typeArguments[0]; + return true; + } + } + + enumerableType = null; + return false; + } + } +} \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/StrongAutoDataAttribute.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/StrongAutoDataAttribute.cs index 1301230..cd5b116 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/StrongAutoDataAttribute.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/StrongAutoDataAttribute.cs @@ -8,12 +8,19 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture [AttributeUsage(AttributeTargets.Method)] public class StrongAutoDataAttribute : AutoDataAttribute { + private static readonly Lazy FIXTURE = new(BuildFixture); + public StrongAutoDataAttribute() - : base(CreateFixture) + : base(FixtureFunc) + { + } + + private static IFixture FixtureFunc() { + return FIXTURE.Value; } - private static IFixture CreateFixture() + private static IFixture BuildFixture() { var fixture = new Fixture(); fixture.Customizations.Add(new AddressFakerSpecimenBuilder()); From 359d00e2480345dcb8f3203b4ce61d52342eb459 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:03:19 +0200 Subject: [PATCH 06/37] Update `StronglyTypedGenerator` - Fix `.ThrowIfInvalid()` for struct when inner type is `string` - Add custom `.ToString()` for `DateTime` and `TimeSpan` - Minor refactoring --- .../StronglyTypedGenerator.cs | 60 +++++++++++++------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs index 7839218..397391f 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs @@ -348,7 +348,7 @@ private void WriteStructThrowIfInvalid(CodeWriter writer, StronglyTypedWorkItem } } - if (workItem.InnerType == typeof (string)) + if (workItem.InnerType == typeof(string) && !workItem.ExtraFeatures.DoesAllowEmpty) { writer.AppendLine(); using (writer.BeginScope("if (value == string.Empty)")) @@ -413,35 +413,57 @@ private static void WriteEquatableEquals(CodeWriter writer, StronglyTypedWorkIte private static void TryWriteToString(CodeWriter writer, StronglyTypedWorkItem workItem) { - if (workItem.Kind == WorkItemKind.Struct && !workItem.ExtraFeatures.HasToString) + if (!workItem.ExtraFeatures.HasToString) { - WriteXmlSummary(writer, "Returns a string that represents inner value."); - WriteXmlReturns(writer, "A string that represents inner value."); - using (writer.BeginScope("public override string ToString()")) + // Order of ifs is important + + if (workItem.InnerType == typeof(Guid)) { - if (workItem.InnerType == typeof(Guid)) + WriteXmlSummary(writer, "Returns a string that represents inner ."); + WriteXmlReturns(writer, "A string that represents inner ."); + using (writer.BeginScope("public override string ToString()")) { writer.AppendLine("return $\"{Value:D}\";"); } - else + writer.AppendLine(); + return; + } + + if (workItem.InnerType == typeof(DateTime)) + { + WriteXmlSummary(writer, "Returns an ISO-8601 string that represents inner ."); + WriteXmlReturns(writer, "An ISO-8601 string that represents inner ."); + using (writer.BeginScope("public override string ToString()")) { - writer.AppendLine("return $\"{Value}\";"); + writer.AppendLine("return $\"{Value.ToUniversalTime():s}Z\";"); } + writer.AppendLine(); + return; } - writer.AppendLine(); - return; - } - if (workItem.Kind == WorkItemKind.Class && !workItem.ExtraFeatures.HasToString && workItem.InnerType == typeof(Guid)) - { - WriteXmlSummary(writer, "Returns a string that represents inner ."); - WriteXmlReturns(writer, "A string that represents inner ."); - using (writer.BeginScope("public override string ToString()")) + if (workItem.InnerType == typeof(TimeSpan)) { - writer.AppendLine("return $\"{Value:D}\";"); + WriteXmlSummary(writer, "Returns an ISO-8601 string that represents inner ."); + WriteXmlReturns(writer, "An ISO-8601 string that represents inner ."); + using (writer.BeginScope("public override string ToString()")) + { + writer.AppendLine("return $\"{System.Xml.XmlConvert.ToString(Value)}\";"); + } + writer.AppendLine(); + return; + } + + if (workItem.Kind == WorkItemKind.Struct) + { + WriteXmlSummary(writer, "Returns a string that represents inner value."); + WriteXmlReturns(writer, "A string that represents inner value."); + using (writer.BeginScope("public override string ToString()")) + { + writer.AppendLine("return $\"{Value}\";"); + } + writer.AppendLine(); + return; } - writer.AppendLine(); - return; } } From 6c806cf008a63cbbab6dc33f2a8d71af53e71b3c Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:05:14 +0200 Subject: [PATCH 07/37] Add more tests to `Xtz.StronglyTyped.SourceGenerator.IntegrationTests` - Add tests for structs with all supported inner types --- .../InnerTypesGeneratorTests.cs | 87 ++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/InnerTypesGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/InnerTypesGeneratorTests.cs index 5f73b6b..46dcb0d 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/InnerTypesGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/InnerTypesGeneratorTests.cs @@ -25,7 +25,7 @@ public class InnerTypesGeneratorTests : GeneratorTestsBase [TestCase(typeof(uint))] [TestCase(typeof(ulong))] [TestCase(typeof(ushort))] - public void ShouldGenerate_ForPrimitiveTypes(Type innerType) + public void ShouldGenerateClass_ForPrimitiveTypes(Type innerType) { //// Arrange @@ -63,7 +63,7 @@ public partial class WithInnerType [TestCase(typeof(MailAddress))] [TestCase(typeof(IPAddress))] [TestCase(typeof(PhysicalAddress))] - public void ShouldGenerate_ForKnownTypes(Type innerType) + public void ShouldGenerateClass_ForKnownTypes(Type innerType) { //// Arrange @@ -92,5 +92,88 @@ public partial class WithInnerType AssertGenerationSuccess(4, diagnostics, outputCompilation, driver.GetRunResult()); } + + [Test] + [TestCase(typeof(bool))] + [TestCase(typeof(byte))] + [TestCase(typeof(char))] + [TestCase(typeof(decimal))] + [TestCase(typeof(double))] + [TestCase(typeof(float))] + [TestCase(typeof(int))] + [TestCase(typeof(long))] + [TestCase(typeof(sbyte))] + [TestCase(typeof(short))] + [TestCase(typeof(string))] + [TestCase(typeof(uint))] + [TestCase(typeof(ulong))] + [TestCase(typeof(ushort))] + public void ShouldGenerateStruct_ForPrimitiveTypes(Type innerType) + { + //// Arrange + + var sourceCode = $@" +namespace IntegrationTests.Generated +{{ + using Xtz.StronglyTyped.SourceGenerator; + + [StrongType(typeof({innerType.FullName}))] + public partial struct WithInnerType + {{ + }} +}} + "; + + var inputCompilation = CreateCompilation(sourceCode, OutputKind.DynamicallyLinkedLibrary); + + //// Act + + GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); + + // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls + driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + + //// Assert + + AssertGenerationSuccess(4, diagnostics, outputCompilation, driver.GetRunResult()); + } + + [Test] + [TestCase(typeof(DateTime))] + [TestCase(typeof(TimeSpan))] + [TestCase(typeof(Guid))] + [TestCase(typeof(Uri))] + [TestCase(typeof(MailAddress))] + [TestCase(typeof(IPAddress))] + [TestCase(typeof(PhysicalAddress))] + public void ShouldGenerateStruct_ForKnownTypes(Type innerType) + { + //// Arrange + + var sourceCode = $@" +namespace IntegrationTests.Generated +{{ + using Xtz.StronglyTyped.SourceGenerator; + + [StrongType(typeof({innerType.FullName}))] + public partial struct WithInnerType + {{ + }} +}} + "; + + var inputCompilation = CreateCompilation(sourceCode, OutputKind.DynamicallyLinkedLibrary); + + //// Act + + GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); + + // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls + driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + + //// Assert + + AssertGenerationSuccess(4, diagnostics, outputCompilation, driver.GetRunResult()); + } } } \ No newline at end of file From 559cb97fbfbc89ed876539394d1726208c79928a Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:16:09 +0200 Subject: [PATCH 08/37] Implement strongly-typed JSON converter for NewtonsoftJson - Extract `StronglyTypedNewtonsoftConverter` to a separate NuGet package - Implement custom tricky conversions when reader reads value of a type different from a target type - `long` -> `ushort` - `long` -> `int` - `long` -> `decimal` - `double` -> `decimal` - `double` -> `float` - `BigInteger` -> `decimal` - `BigInteger` -> `ushort` (not sure if it's the case) - `BigInteger` -> `int` (not sure if it's the case) - `BigInteger` -> `long` (not sure if it's the case) - Implement custom TimeSpan parsing from ISO-8601 format using `XmlConvert` - Implement custom serialization for numbers - Implement custom serialization for `bool` --- .../WebApi/Startup.cs | 1 + .../StronglyTypedNewtonsoftConverter.cs | 36 --- ...onglyTyped.Api_3_1.IntegrationTests.csproj | 3 +- .../NewtonsoftJsonConverterException.cs | 14 + .../StronglyTypedNewtonsoftConverter.cs | 242 ++++++++++++++++++ .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 50 ++++ 6 files changed, 309 insertions(+), 37 deletions(-) delete mode 100644 src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedNewtonsoftConverter.cs create mode 100644 src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs create mode 100644 src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs create mode 100644 src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs index 8c5503c..1533d7b 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Xtz.StronglyTyped.NewtonsoftJson; namespace Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApi { diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedNewtonsoftConverter.cs b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedNewtonsoftConverter.cs deleted file mode 100644 index a09efc2..0000000 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedNewtonsoftConverter.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.ComponentModel; -using Newtonsoft.Json; - -namespace Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApi -{ - public class StronglyTypedNewtonsoftConverter : JsonConverter - { - public override bool CanConvert(Type objectType) - { - return typeof(IStronglyTyped).IsAssignableFrom(objectType); - } - - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - var stringValue = reader.ReadAsString(); - var typeConverter = TypeDescriptor.GetConverter(objectType); - - return (IStronglyTyped)typeConverter.ConvertFrom(stringValue); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - if (value is null) - { - writer.WriteValue("null"); - return; - } - - var typeConverter = TypeDescriptor.GetConverter(value.GetType()); - - var stringValue = typeConverter.ConvertTo(value, typeof(string)) as string; - writer.WriteValue(stringValue); - } - } -} \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj index 47d985d..9ee43d8 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -15,6 +15,7 @@ + diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs new file mode 100644 index 0000000..e44b0e0 --- /dev/null +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs @@ -0,0 +1,14 @@ +using System; +using System.Diagnostics.CodeAnalysis; + +namespace Xtz.StronglyTyped.NewtonsoftJson +{ + [ExcludeFromCodeCoverage] + public class NewtonsoftJsonConverterException : StronglyTypedException + { + public NewtonsoftJsonConverterException(Type type, string errorMessage) + : base(type, errorMessage) + { + } + } +} \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs new file mode 100644 index 0000000..ece0e62 --- /dev/null +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs @@ -0,0 +1,242 @@ +using System; +using System.ComponentModel; +using System.Numerics; +using System.Xml; +using Newtonsoft.Json; +using Xtz.StronglyTyped.TypeConverters; + +namespace Xtz.StronglyTyped.NewtonsoftJson +{ + public class StronglyTypedNewtonsoftConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return typeof(IStronglyTyped).IsAssignableFrom(objectType); + } + + public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + var typeConverter = TypeDescriptor.GetConverter(objectType); + + if (reader.Value is long longValue) + { + return ConvertFromLong(longValue, typeConverter as ICustomTypeConverter); + } + + if (reader.Value is double doubleValue) + { + return ConvertFromDouble(doubleValue, typeConverter as ICustomTypeConverter); + } + + if (reader.Value is BigInteger bigIntValue) + { + return ConvertFromBigInt(bigIntValue, typeConverter as ICustomTypeConverter); + } + + if (typeConverter is ICustomTypeConverter customTypeConverter && customTypeConverter.InnerType == typeof(TimeSpan)) + { + return ConvertToTimeSpan(reader.Value, typeConverter); + } + + return (IStronglyTyped)typeConverter.ConvertFrom(reader.Value); + } + + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + { + if (value is null) + { + writer.WriteNull(); + return; + } + + var typeConverter = TypeDescriptor.GetConverter(value.GetType()); + if (typeConverter is ICustomTypeConverter customTypeConverter) + { + if (customTypeConverter.InnerType == typeof(bool)) + { + var boolValue = (value as IStronglyTyped)?.Value ?? default(bool); + writer.WriteValue(boolValue); + return; + } + + if (TryWriteNumber(value, customTypeConverter.InnerType, writer)) + { + return; + } + } + + var stringValue = typeConverter.ConvertTo(value, typeof(string)) as string; + writer.WriteValue(stringValue); + } + + private bool TryWriteNumber(object value, Type innerType, JsonWriter writer) + { + if (innerType == typeof(decimal)) + { + var decimalValue = (value as IStronglyTyped)?.Value ?? default(decimal); + writer.WriteValue(decimalValue); + return true; + } + + if (innerType == typeof(double)) + { + var doubleValue = (value as IStronglyTyped)?.Value ?? default(double); + writer.WriteValue(doubleValue); + return true; + } + + if (innerType == typeof(float)) + { + var floatValue = (value as IStronglyTyped)?.Value ?? default(float); + writer.WriteValue(floatValue); + return true; + } + + if (innerType == typeof(int)) + { + var intValue = (value as IStronglyTyped)?.Value ?? default(int); + writer.WriteValue(intValue); + return true; + } + + if (innerType == typeof(long)) + { + var longValue = (value as IStronglyTyped)?.Value ?? default(long); + writer.WriteValue(longValue); + return true; + } + + if (innerType == typeof(uint)) + { + var uintValue = (value as IStronglyTyped)?.Value ?? default(uint); + writer.WriteValue(uintValue); + return true; + } + + if (innerType == typeof(ulong)) + { + var ulongValue = (value as IStronglyTyped)?.Value ?? default(ulong); + writer.WriteValue(ulongValue); + return true; + } + + if (innerType == typeof(byte)) + { + var byteValue = (value as IStronglyTyped)?.Value ?? default(byte); + writer.WriteValue(byteValue); + return true; + } + + if (innerType == typeof(sbyte)) + { + var sbyteValue = (value as IStronglyTyped)?.Value ?? default(sbyte); + writer.WriteValue(sbyteValue); + return true; + } + + if (innerType == typeof(short)) + { + var shortValue = (value as IStronglyTyped)?.Value ?? default(short); + writer.WriteValue(shortValue); + return true; + } + + if (innerType == typeof(ushort)) + { + var ushortValue = (value as IStronglyTyped)?.Value ?? default(ushort); + writer.WriteValue(ushortValue); + return true; + } + + return false; + } + + private IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter typeConverter) + { + if (typeConverter.InnerType == typeof(ushort)) + { + var value = unchecked((ushort)longValue); + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + if (typeConverter.InnerType == typeof(int)) + { + var value = unchecked((int)longValue); + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + if (typeConverter.InnerType == typeof(decimal)) + { + var value = (decimal)longValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(long)}' to '{typeConverter.StrongType.FullName}'"); + } + + private IStronglyTyped ConvertFromDouble(double doubleValue, ICustomTypeConverter typeConverter) + { + if (typeConverter.InnerType == typeof(decimal)) + { + var value = (decimal)doubleValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + if (typeConverter.InnerType == typeof(float)) + { + var value = (float)doubleValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(double)}' to '{typeConverter.StrongType.FullName}'"); + } + + private IStronglyTyped ConvertFromBigInt(BigInteger bigIntValue, ICustomTypeConverter typeConverter) + { + if (typeConverter.InnerType == typeof(decimal)) + { + var value = (decimal)bigIntValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + // NOTE: It's not clear when `BigInteger` is being read from JSON `ushort` is here just in case + if (typeConverter.InnerType == typeof(ushort)) + { + var value = (ushort)bigIntValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + // NOTE: It's not clear when `BigInteger` is being read from JSON `int` is here just in case + if (typeConverter.InnerType == typeof(int)) + { + var value = (int)bigIntValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + // NOTE: It's not clear when `BigInteger` is being read from JSON `long` is here just in case + if (typeConverter.InnerType == typeof(long)) + { + var value = (long)bigIntValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; + } + + throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(BigInteger)}' to '{typeConverter.StrongType.FullName}'"); + } + + private IStronglyTyped ConvertToTimeSpan(object value, TypeConverter typeConverter) + { + var timeSpanValue = XmlConvert.ToTimeSpan(value.ToString()); + var result = typeConverter.ConvertFrom(timeSpanValue); + return (IStronglyTyped)result; + } + } +} \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj new file mode 100644 index 0000000..0349b47 --- /dev/null +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -0,0 +1,50 @@ + + + + netstandard2.1 + 9 + enable + + + + Xtz.StronglyTyped.NewtonsoftJson + 0.19.0 + true + snupkg + Vlad DX + Extenza + 'Newtonsoft.Json' support for `Xtz.StronglyTyped`, library to strongly-type your interfaces (properties, methods, APIs, DB entities, appsettings). + +Eliminate stringly-typed interfaces. + +Make compiler your friend, introduce semantics to your code. + strongly-typed, strong type, type converter, Newtonsoft.Json, json serialization, appsettings, compiler + Copyright (c) Vlad DX, 2021 + git + https://github.com/dev-experience/Xtz.StronglyTyped + true + true + MIT + true + + + + true + true + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + From 47fc2f3e50a72f57f5a1f93f2f8dae85cd2b16c6 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:17:48 +0200 Subject: [PATCH 09/37] Fix `Xtz.StronglyTyped.Api_3_1.IntegrationTests` - Use `JsonSerializerSettings` with `StronglyTypedNewtonsoftConverter` provided - Add another strongly-typed property which is never set for tests with `null` --- .../WebApi/StronglyTypedWeatherForecast.cs | 2 ++ .../WebApiTests.cs | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs index 4c16a59..193f089 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs @@ -7,6 +7,8 @@ public class StronglyTypedWeatherForecast { public City City { get; set; } + public Country NullValue { get; set; } + public DateTime Date { get; set; } public DegreesCelsius TemperatureC { get; set; } diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs index b30d14a..8c6dc91 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs @@ -5,11 +5,15 @@ using Newtonsoft.Json; using NUnit.Framework; using Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApi; +using Xtz.StronglyTyped.BuiltinTypes.Address; +using Xtz.StronglyTyped.NewtonsoftJson; namespace Xtz.StronglyTyped.Api_3_1.IntegrationTests { public class WebApiTests : IDisposable { + private readonly JsonSerializerSettings _jsonSerializerSettings; + private readonly WebApiFactory _factory = new(); private readonly HttpClient _client; @@ -17,6 +21,10 @@ public class WebApiTests : IDisposable public WebApiTests() { _client = _factory.CreateClient(); + + var jsonSerializerSettings = new JsonSerializerSettings(); + jsonSerializerSettings.Converters.Add(new StronglyTypedNewtonsoftConverter()); + _jsonSerializerSettings = jsonSerializerSettings; } [Test] @@ -40,8 +48,11 @@ public async Task ShouldDeserialize_ForStandardEndpoint_WhenNewtonsoftUsed() response.EnsureSuccessStatusCode(); Assert.NotNull(response.Content); + Country country = null; + var x = JsonConvert.SerializeObject(country); + var responseStr = await response.Content.ReadAsStringAsync(); - var responseObject = JsonConvert.DeserializeObject>(responseStr); + var responseObject = JsonConvert.DeserializeObject>(responseStr, _jsonSerializerSettings); } [Test] @@ -80,7 +91,7 @@ public async Task ShouldDeserialize_ForStronglyTypedEndpoint_WhenNewtonsoftUsed( Assert.NotNull(response.Content); var responseStr = await response.Content.ReadAsStringAsync(); - var responseObject = JsonConvert.DeserializeObject>(responseStr); + var responseObject = JsonConvert.DeserializeObject>(responseStr, _jsonSerializerSettings); } [Test] From 912dc63b0ad12fbaaaf11576035f8cb19a41fa61 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:19:19 +0200 Subject: [PATCH 10/37] Remove redundant test models --- .../TypeConverters/IntTests.cs | 6 ++++-- .../Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs | 4 ++-- .../Xtz.StronglyTyped.UnitTests/_TestModels/City.cs | 9 --------- .../_TestModels/CountryAllowingEmpty.cs | 9 --------- 4 files changed, 6 insertions(+), 22 deletions(-) delete mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/City.cs delete mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryAllowingEmpty.cs diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/IntTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/IntTests.cs index 4d399a8..c86bee1 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/IntTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/IntTests.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using NUnit.Framework; using Xtz.StronglyTyped.TypeConverters; @@ -41,11 +42,12 @@ public void TypeConverter_ShouldThrow_GivenInvalidInt32(int value) //// Act - TestDelegate action = () => typeConverter.ConvertFrom(value); + [ExcludeFromCodeCoverage] + void Action() => typeConverter.ConvertFrom(value); //// Assert - Assert.Throws(action); + Assert.Throws(Action); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs index 419dded..d29a77d 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs @@ -75,7 +75,7 @@ public void ShouldThrow_GivenNull_ForAllowEmpty() //// Act [ExcludeFromCodeCoverage] - void Action() => new CountryAllowingEmpty(value); + void Action() => new StronglyTypedStringAllowEmpty(value); //// Assert @@ -91,7 +91,7 @@ public void ShouldCreate_GivenEmpty_ForAllowEmpty() //// Act - var result = new CountryAllowingEmpty(value); + var result = new StronglyTypedStringAllowEmpty(value); //// Assert diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/City.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/City.cs deleted file mode 100644 index eae3bb8..0000000 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/City.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Xtz.StronglyTyped.SourceGenerator; - -namespace Xtz.StronglyTyped.UnitTests -{ - [StrongType] - public sealed partial class City - { - } -} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryAllowingEmpty.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryAllowingEmpty.cs deleted file mode 100644 index ba0ea1f..0000000 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryAllowingEmpty.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Xtz.StronglyTyped.SourceGenerator; - -namespace Xtz.StronglyTyped.UnitTests -{ - [StrongType(Allow.Empty)] - public sealed partial class CountryAllowingEmpty - { - } -} From 9df75fedb8d02bec819927ba20bd1b257a8d5c79 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:21:33 +0200 Subject: [PATCH 11/37] Update `Xtz.StronglyTyped.UnitTests` - Add strongly-typed models with all supported inner types - Add `BoolTests` - Add `SystemTextJsonTests` to test JSON serialization based on `System.Text.Json` --- .../TypeConverters/BoolTests.cs | 30 + .../Values/SystemTextJsonTests.cs | 667 ++++++++++++++++++ .../Xtz.StronglyTyped.UnitTests.csproj | 3 +- .../_TestModels/StronglyTypedBool.cs | 10 + .../_TestModels/StronglyTypedBoolStruct.cs | 10 + .../_TestModels/StronglyTypedByte.cs | 9 + .../_TestModels/StronglyTypedChar.cs | 9 + .../_TestModels/StronglyTypedDateTime.cs | 10 + .../_TestModels/StronglyTypedDecimal.cs | 9 + .../_TestModels/StronglyTypedDouble.cs | 9 + .../_TestModels/StronglyTypedFloat.cs | 9 + .../_TestModels/StronglyTypedGuid.cs | 10 + .../StronglyTypedGuidAllowEmpty.cs | 10 + .../_TestModels/StronglyTypedGuidId.cs | 11 + .../_TestModels/StronglyTypedGuidStruct.cs | 10 + .../_TestModels/StronglyTypedInt.cs | 9 + .../_TestModels/StronglyTypedIntId.cs | 10 + .../_TestModels/StronglyTypedIntStruct.cs | 9 + .../_TestModels/StronglyTypedLong.cs | 9 + .../_TestModels/StronglyTypedSbyte.cs | 9 + .../_TestModels/StronglyTypedShort.cs | 9 + .../_TestModels/StronglyTypedString.cs | 9 + .../StronglyTypedStringAllowEmpty.cs | 9 + .../StronglyTypedStringAllowEmptyStruct.cs | 9 + .../_TestModels/StronglyTypedStringStruct.cs | 9 + .../_TestModels/StronglyTypedTimeSpan.cs | 10 + .../_TestModels/StronglyTypedUint.cs | 9 + .../_TestModels/StronglyTypedUlong.cs | 9 + .../_TestModels/StronglyTypedUshort.cs | 9 + 29 files changed, 943 insertions(+), 1 deletion(-) create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/Values/SystemTextJsonTests.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedByte.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedChar.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDateTime.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDecimal.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDouble.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedFloat.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuid.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidAllowEmpty.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidId.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidStruct.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedInt.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntId.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntStruct.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedLong.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedSbyte.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedShort.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedString.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmpty.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmptyStruct.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringStruct.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedTimeSpan.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUint.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUlong.cs create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUshort.cs diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs new file mode 100644 index 0000000..118235f --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs @@ -0,0 +1,30 @@ +using System.ComponentModel; +using NUnit.Framework; + +namespace Xtz.StronglyTyped.UnitTests.TypeConverters +{ + public class BoolTests + { + [Test] + public void TypeConverter_ShouldParseStronglyTyped_GivenBool() + { + //// Arrange + + var value = true; + var strongType = typeof(StronglyTypedBool); + var typeConverter = TypeDescriptor.GetConverter(strongType); + + var expected = new StronglyTypedBool(value); + + //// Act + + var result = typeConverter.ConvertFrom(value) as StronglyTypedBool; + + //// Assert + + Assert.IsNotNull(result); + Assert.AreEqual(expected, result); + Assert.AreEqual(expected.Value, result.Value); + } + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/SystemTextJsonTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/SystemTextJsonTests.cs new file mode 100644 index 0000000..94d6b80 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/SystemTextJsonTests.cs @@ -0,0 +1,667 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using System.Xml; +using AutoFixture; +using NUnit.Framework; +using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; +using Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders; +using Xtz.StronglyTyped.BuiltinTypes.Internet; + +namespace Xtz.StronglyTyped.UnitTests.Values +{ + public class SystemTextJsonTests + { + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldSerialize_GivenStronglyTypedStringClass(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedString(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldSerialize_GivenStronglyTypedStringStruct(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedStringStruct(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldSerialize_GivenStronglyTypedStringClassAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedStringAllowEmpty(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldSerialize_GivenStronglyTypedStringStructAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedStringAllowEmptyStruct(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) + { + // Arrange + + var stronglyTyped = new StronglyTypedGuid(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] + public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) + { + // Arrange + + var stronglyTyped = new StronglyTypedGuidStruct(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldSerialize_GivenStronglyTypedGuidIdClass(Guid value) + { + // Arrange + + var stronglyTyped = new StronglyTypedGuidId(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedIntClass(int value) + { + // Arrange + + var stronglyTyped = new StronglyTypedInt(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedIntStruct(int value) + { + // Arrange + + var stronglyTyped = new StronglyTypedIntStruct(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(1)] + [TestCase(546)] + [TestCase(6474)] + [TestCase(int.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedIntIdClass(int value) + { + // Arrange + + var stronglyTyped = new StronglyTypedIntId(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(false)] + [TestCase(true)] + public void ShouldSerialize_GivenStronglyTypedBoolClass(bool value) + { + // Arrange + + var stronglyTyped = new StronglyTypedBool(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(byte.MinValue)] + [TestCase(0)] + [TestCase(1)] + [TestCase(233)] + [TestCase(byte.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedByteClass(byte value) + { + // Arrange + + var stronglyTyped = new StronglyTypedByte(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(char.MinValue)] + [TestCase('I')] + [TestCase('x')] + [TestCase('\xF0')] + [TestCase('Ž')] + [TestCase('£')] + [TestCase('¶')] + [TestCase('Ð')] + [TestCase('4')] + [TestCase('ߡ')] + [TestCase('ݓ')] + [TestCase('Ѝ')] + [TestCase(char.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedCharClass(char value) + { + // Arrange + + var stronglyTyped = new StronglyTypedChar(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + //[TestCase(-79228162514264337593543950335m)] + [TestCase(-5745)] + [TestCase(-968.2566)] + //[TestCase(-968.2566m)] + //[TestCase(-1m)] + [TestCase(0)] + //[TestCase(0m)] + [TestCase(1)] + //[TestCase(1m)] + [TestCase(34634)] + [TestCase(6534.353)] + //[TestCase(79228162514264337593543950335m)] + public void ShouldSerialize_GivenStronglyTypedByteClass(decimal value) + { + // Arrange + + var stronglyTyped = new StronglyTypedDecimal(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(double.MinValue)] + [TestCase(double.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353)] + [TestCase(double.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedDoubleClass(double value) + { + // Arrange + + var stronglyTyped = new StronglyTypedDouble(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(float.MinValue)] + [TestCase(float.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566F)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353F)] + [TestCase(float.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedFloatClass(float value) + { + // Arrange + + var stronglyTyped = new StronglyTypedFloat(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(long.MinValue)] + [TestCase(-5745)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(41343252352352)] + [TestCase(long.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedLongClass(long value) + { + // Arrange + + var stronglyTyped = new StronglyTypedLong(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(sbyte.MinValue)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(sbyte.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedSbyteClass(sbyte value) + { + // Arrange + + var stronglyTyped = new StronglyTypedSbyte(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(short.MinValue)] + [TestCase(-24633)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(7374)] + [TestCase(short.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedShortClass(short value) + { + // Arrange + + var stronglyTyped = new StronglyTypedShort(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(uint.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(uint.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedUintClass(uint value) + { + // Arrange + + var stronglyTyped = new StronglyTypedUint(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(ulong.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(847847645745745U)] + [TestCase(ulong.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedUlongClass(ulong value) + { + // Arrange + + var stronglyTyped = new StronglyTypedUlong(value); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + public void ShouldSerialize_GivenStronglyTypedUshortClass() + { + // Arrange + + var values = new ushort[] { ushort.MinValue, 0, 1, 34, 7374, ushort.MaxValue }; + + var stronglyTyped = values.Select(x => (StronglyTypedUshort)x); + var json = JsonSerializer.Serialize(values); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("2020-08-14T14:35:57+0000")] + [TestCase("2020-08-14T14:35:57-1000")] + [TestCase("2020-08-14T14:35:57Z")] + [TestCase("2320-07-21T23:00:35Z")] + [TestCase("1539-05-06T09:20:45Z")] + public void ShouldSerialize_GivenStronglyTypedDateTimeClass(DateTime value) + { + // Arrange + + var stronglyTyped = new StronglyTypedDateTime(value); + var json = JsonSerializer.Serialize(value.ToUniversalTime()); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(TimeSpan.TicksPerMillisecond)] + [TestCase(TimeSpan.TicksPerSecond)] + [TestCase(TimeSpan.TicksPerMinute)] + [TestCase(TimeSpan.TicksPerHour)] + [TestCase(TimeSpan.TicksPerDay)] + public void ShouldSerialize_GivenStronglyTypedTimeSpanClass_FromTicks(long value) + { + // Arrange + + var timeSpan = TimeSpan.FromTicks(value); + var stronglyTyped = new StronglyTypedTimeSpan(timeSpan); + var expected = $"\"{XmlConvert.ToString(stronglyTyped)}\""; + var expectedString = $"\"{stronglyTyped}\""; + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(expected, result); + Assert.AreEqual(expectedString, result); + } + + [Test] + [TestCase("6")] + [TestCase("6:12")] + [TestCase("6:12:14")] + [TestCase("6:12:14:45")] + [TestCase("6.12:14:45")] + [TestCase("6:12:14:45.3448")] + public void ShouldSerialize_GivenStronglyTypedTimeSpanClass(TimeSpan value) + { + // Arrange + + var stronglyTyped = new StronglyTypedTimeSpan(value); + var expected = $"\"{XmlConvert.ToString(value)}\""; + var expectedString = $"\"{stronglyTyped}\""; + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(expected, result); + Assert.AreEqual(expectedString, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedEmailClass(Email stronglyTyped) + { + // Arrange + + var json = JsonSerializer.Serialize(stronglyTyped.Value.Address); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedEmailsClass(IReadOnlyCollection stronglyTyped) + { + // Arrange + + var value = stronglyTyped.Select(x => x.Value.Address); + var json = JsonSerializer.Serialize(value); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedIpV4AddressClass(IpV4Address stronglyTyped) + { + // Arrange + + var expected = $"\"{stronglyTyped.Value}\""; + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(expected, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedIpV6AddressClass(IpV6Address stronglyTyped) + { + // Arrange + + var expected = $"\"{stronglyTyped.Value}\""; + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(expected, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedMacAddressClass(MacAddress stronglyTyped) + { + // Arrange + + var expectedToString = $"\"{stronglyTyped.Value}\""; + var expected = $"\"{stronglyTyped.ToString(MacAddress.Separator.Hyphen)}\""; + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.AreEqual(expected, result); + Assert.AreEqual(expectedToString, result.Replace("-", string.Empty)); + } + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj index 3a9d704..fd6d700 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj @@ -2,7 +2,7 @@ net5 - 9 + preview @@ -22,6 +22,7 @@ + diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs new file mode 100644 index 0000000..a31bc8f --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs @@ -0,0 +1,10 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + // TODO: Remove `Allow.Empty` when it's supported by generator + [StrongType(typeof(bool))] + public sealed partial class StronglyTypedBool + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs new file mode 100644 index 0000000..f6607af --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs @@ -0,0 +1,10 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + // TODO: Remove `Allow.Empty` when it's supported by generator + [StrongType(typeof(bool), Allow.Empty)] + public partial struct StronglyTypedBoolStruct + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedByte.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedByte.cs new file mode 100644 index 0000000..06acf8c --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedByte.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(byte))] + public sealed partial class StronglyTypedByte + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedChar.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedChar.cs new file mode 100644 index 0000000..622c60a --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedChar.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(char))] + public sealed partial class StronglyTypedChar + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDateTime.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDateTime.cs new file mode 100644 index 0000000..8f30bf5 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDateTime.cs @@ -0,0 +1,10 @@ +using System; +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(DateTime))] + public sealed partial class StronglyTypedDateTime + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDecimal.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDecimal.cs new file mode 100644 index 0000000..dfc12ac --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDecimal.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(decimal))] + public sealed partial class StronglyTypedDecimal + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDouble.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDouble.cs new file mode 100644 index 0000000..00611a6 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedDouble.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(double))] + public sealed partial class StronglyTypedDouble + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedFloat.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedFloat.cs new file mode 100644 index 0000000..fc26576 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedFloat.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(float))] + public sealed partial class StronglyTypedFloat + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuid.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuid.cs new file mode 100644 index 0000000..d9d0f23 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuid.cs @@ -0,0 +1,10 @@ +using System; +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(Guid))] + public sealed partial class StronglyTypedGuid + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidAllowEmpty.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidAllowEmpty.cs new file mode 100644 index 0000000..dcadd0d --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidAllowEmpty.cs @@ -0,0 +1,10 @@ +using System; +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(Guid), Allow.Empty)] + public sealed partial class StronglyTypedGuidAllowEmpty + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidId.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidId.cs new file mode 100644 index 0000000..e785b80 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidId.cs @@ -0,0 +1,11 @@ +using System; +using Xtz.StronglyTyped.BuiltinTypes.Ids; +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(Guid))] + public partial class StronglyTypedGuidId : GuidId + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidStruct.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidStruct.cs new file mode 100644 index 0000000..8565e1d --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedGuidStruct.cs @@ -0,0 +1,10 @@ +using System; +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(Guid))] + public partial struct StronglyTypedGuidStruct + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedInt.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedInt.cs new file mode 100644 index 0000000..e4b711c --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedInt.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(int))] + public sealed partial class StronglyTypedInt + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntId.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntId.cs new file mode 100644 index 0000000..bfba099 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntId.cs @@ -0,0 +1,10 @@ +using Xtz.StronglyTyped.BuiltinTypes.Ids; +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(int))] + public partial class StronglyTypedIntId : IntId + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntStruct.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntStruct.cs new file mode 100644 index 0000000..f53037b --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedIntStruct.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(int))] + public partial struct StronglyTypedIntStruct + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedLong.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedLong.cs new file mode 100644 index 0000000..b511286 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedLong.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(long))] + public sealed partial class StronglyTypedLong + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedSbyte.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedSbyte.cs new file mode 100644 index 0000000..7ce9648 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedSbyte.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(sbyte))] + public sealed partial class StronglyTypedSbyte + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedShort.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedShort.cs new file mode 100644 index 0000000..3a8cc75 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedShort.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(short))] + public sealed partial class StronglyTypedShort + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedString.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedString.cs new file mode 100644 index 0000000..9384ffa --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedString.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType] + public sealed partial class StronglyTypedString + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmpty.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmpty.cs new file mode 100644 index 0000000..024989e --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmpty.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(Allow.Empty)] + public sealed partial class StronglyTypedStringAllowEmpty + { + } +} diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmptyStruct.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmptyStruct.cs new file mode 100644 index 0000000..1f72ce9 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringAllowEmptyStruct.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(Allow.Empty)] + public partial struct StronglyTypedStringAllowEmptyStruct + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringStruct.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringStruct.cs new file mode 100644 index 0000000..62b5839 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedStringStruct.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType] + public partial struct StronglyTypedStringStruct + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedTimeSpan.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedTimeSpan.cs new file mode 100644 index 0000000..e8848d8 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedTimeSpan.cs @@ -0,0 +1,10 @@ +using System; +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(TimeSpan))] + public sealed partial class StronglyTypedTimeSpan + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUint.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUint.cs new file mode 100644 index 0000000..746d20f --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUint.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(uint))] + public sealed partial class StronglyTypedUint + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUlong.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUlong.cs new file mode 100644 index 0000000..52820fa --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUlong.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(ulong))] + public sealed partial class StronglyTypedUlong + { + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUshort.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUshort.cs new file mode 100644 index 0000000..d3139ab --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedUshort.cs @@ -0,0 +1,9 @@ +using Xtz.StronglyTyped.SourceGenerator; + +namespace Xtz.StronglyTyped.UnitTests +{ + [StrongType(typeof(ushort))] + public sealed partial class StronglyTypedUshort + { + } +} \ No newline at end of file From dc131f521d685baac0f01623f71de13268b97f7c Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:23:13 +0200 Subject: [PATCH 12/37] Add `Xtz.StronglyTyped.NewtonsoftJson.UnitTests` - Add tests for JSON serialization with `Newtonsoft.Json` based on `StronglyTypedNewtonsoftConverter` - Add tests for JSON deserialization with `Newtonsoft.Json` based on `StronglyTypedNewtonsoftConverter` --- .../DeserializationTests.cs | 771 ++++++++++++++++++ .../SerializationTests.cs | 689 ++++++++++++++++ ...onglyTyped.NewtonsoftJson.UnitTests.csproj | 34 + ...ewtonsoftJson.UnitTests.csproj.DotSettings | 3 + .../_TestModels/SerializationDto.cs | 26 + src/Xtz.StronglyTyped.sln | 13 + 6 files changed, 1536 insertions(+) create mode 100644 src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs create mode 100644 src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs create mode 100644 src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj create mode 100644 src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj.DotSettings create mode 100644 src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/_TestModels/SerializationDto.cs diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs new file mode 100644 index 0000000..1786b23 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs @@ -0,0 +1,771 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml; +using Newtonsoft.Json; +using NUnit.Framework; +using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; +using Xtz.StronglyTyped.BuiltinTypes.Internet; +using Xtz.StronglyTyped.UnitTests; + +namespace Xtz.StronglyTyped.NewtonsoftJson.UnitTests +{ + public class DeserializationTests + { + private static readonly JsonSerializerSettings JSON_SERIALIZER_SETTINGS; + + static DeserializationTests() + { + var jsonSerializerSettings = new JsonSerializerSettings(); + jsonSerializerSettings.Converters.Add(new StronglyTypedNewtonsoftConverter()); + JSON_SERIALIZER_SETTINGS = jsonSerializerSettings; + } + + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldDeserialize_ToStronglyTypedStringClass(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldDeserialize_ToStronglyTypedStringStruct(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldDeserialize_ToStronglyTypedStringClassAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldDeserialize_ToStronglyTypedStringStructAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] + public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldDeserialize_ToStronglyTypedGuidIdClass(Guid value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedIntClass(int value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedIntStruct(int value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(1)] + [TestCase(546)] + [TestCase(6474)] + [TestCase(int.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedIntIdClass(int value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(false)] + [TestCase(true)] + public void ShouldDeserialize_ToStronglyTypedBoolClass(bool value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(byte.MinValue)] + [TestCase(0)] + [TestCase(1)] + [TestCase(233)] + [TestCase(byte.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedByteClass(byte value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(char.MinValue)] + [TestCase('I')] + [TestCase('x')] + [TestCase('\xF0')] + [TestCase('Ž')] + [TestCase('£')] + [TestCase('¶')] + [TestCase('Ð')] + [TestCase('4')] + [TestCase('ߡ')] + [TestCase('ݓ')] + [TestCase('Ѝ')] + [TestCase(char.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedCharClass(char value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(-5745)] + [TestCase(-968.2566)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353)] + public void ShouldDeserialize_ToStronglyTypedDecimalClass(decimal value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + public void ShouldDeserialize_ToStronglyTypedDecimalClass() + { + // Arrange + + var values = new[] { decimal.MinValue, -968.2566m, decimal.MinusOne, decimal.Zero, decimal.One, decimal.MaxValue }; + + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = BuildNonStringArray(stronglyTyped.Select(x => x.TestValue.Value).Cast()); + + // Act + + var result = JsonConvert.DeserializeObject>>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + } + + [Test] + [TestCase(double.MinValue)] + [TestCase(double.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353)] + [TestCase(double.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedDoubleClass(double value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(float.MinValue)] + [TestCase(float.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566F)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353F)] + [TestCase(float.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedFloatClass(float value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(long.MinValue)] + [TestCase(-5745)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(41343252352352)] + [TestCase(long.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedLongClass(long value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(sbyte.MinValue)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(sbyte.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedSbyteClass(sbyte value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(short.MinValue)] + [TestCase(-24633)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(7374)] + [TestCase(short.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedShortClass(short value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(uint.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(uint.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedUintClass(uint value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(ulong.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(847847645745745U)] + [TestCase(ulong.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedUlongClass(ulong value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + public void ShouldDeserialize_ToStronglyTypedUshortClass() + { + var values = new[] { ushort.MinValue, (ushort)0, (ushort)1, (ushort)34, (ushort)7374, ushort.MaxValue }; + // Arrange + + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = BuildNonStringArray(stronglyTyped.Select(x => x.TestValue.Value).Cast()); + + // Act + + var result = JsonConvert.DeserializeObject>>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + } + + [Test] + [TestCase("2020-08-14T14:35:57+0000")] + [TestCase("2020-08-14T14:35:57-1000")] + [TestCase("2020-08-14T14:35:57Z")] + [TestCase("2320-07-21T23:00:35Z")] + [TestCase("1539-05-06T09:20:45Z")] + public void ShouldDeserialize_ToStronglyTypedDateTimeClass(DateTime value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(TimeSpan.TicksPerMillisecond)] + [TestCase(TimeSpan.TicksPerSecond)] + [TestCase(TimeSpan.TicksPerMinute)] + [TestCase(TimeSpan.TicksPerHour)] + [TestCase(TimeSpan.TicksPerDay)] + public void ShouldDeserialize_ToStronglyTypedTimeSpanClass_FromTicks(long value) + { + var timeSpan = TimeSpan.FromTicks(value); + var expected = XmlConvert.ToString(timeSpan); + + // Arrange + + var stronglyTyped = new SerializationDto(timeSpan); + var json = BuildString(expected); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("6")] + [TestCase("6:12")] + [TestCase("6:12:14")] + [TestCase("6:12:14:45")] + [TestCase("6.12:14:45")] + [TestCase("6:12:14:45.3448")] + public void ShouldDeserialize_ToStronglyTypedTimeSpanClass(TimeSpan timeSpan) + { + var expected = XmlConvert.ToString(timeSpan); + + // Arrange + + var stronglyTyped = new SerializationDto(timeSpan); + var json = BuildString(expected); + + // Act + + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + //[Test] + //[StrongAutoData] + //public void ShouldDeserialize_ToStronglyTypedEmailClass(Email value) + //{ + // // Arrange + + // var stronglyTyped = new SerializationDto(value); + // var json = BuildString(stronglyTyped.TestValue.Value.Address); + + // // Act + + // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // // Assert + + // Assert.NotNull(result); + // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + //} + + //[Test] + //[StrongAutoData] + //public void ShouldDeserialize_ToStronglyTypedEmailsClass(IReadOnlyCollection values) + //{ + // // Arrange + + // var stronglyTyped = values + // .Select(x => new SerializationDto(x)) + // .ToArray(); + // var json = BuildStringArray(stronglyTyped.Select(x => x.TestValue.Value.Address)); + + // // Act + + // var result = JsonConvert.DeserializeObject>>(json, JSON_SERIALIZER_SETTINGS); + + // // Assert + + // Assert.NotNull(result); + // Assert.IsNotEmpty(result); + // Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + // Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + //} + + //[Test] + //[StrongAutoData] + //public void ShouldDeserialize_ToStronglyTypedIpV4AddressClass(IpV4Address value) + //{ + // // Arrange + + // var stronglyTyped = new SerializationDto(value); + // var json = BuildString(value); + + // // Act + + // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // // Assert + + // Assert.NotNull(result); + // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + //} + + //[Test] + //[StrongAutoData] + //public void ShouldDeserialize_ToStronglyTypedIpV6AddressClass(IpV6Address value) + //{ + // // Arrange + + // var stronglyTyped = new SerializationDto(value); + // var json = BuildString(value); + + // // Act + + // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // // Assert + + // Assert.NotNull(result); + // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + //} + + //[Test] + //[StrongAutoData] + //public void ShouldDeserialize_ToStronglyTypedMacAddressClass(MacAddress value) + //{ + // // Arrange + + // var stronglyTyped = new SerializationDto(value); + // var json = BuildString(value); + + // // Act + + // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + + // // Assert + + // Assert.NotNull(result); + // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + //} + + private static string BuildStringArray(IEnumerable values) + { + var stringBuilder = new StringBuilder("[ \n"); + + foreach (var value in values) + { + stringBuilder.AppendFormat(" {0},\n", BuildString(value)); + } + + stringBuilder.Append("]"); + var result = stringBuilder.ToString(); + return result; + } + + private static string BuildNonStringArray(IEnumerable values) + { + var stringBuilder = new StringBuilder("[ \n"); + + foreach (var value in values) + { + stringBuilder.AppendFormat("{0},\n", BuildNonString(value)); + } + + stringBuilder.Append("]"); + var result = stringBuilder.ToString(); + return result; + } + + private static string BuildString(object value) + { + return $"{{ \"testValue\": \"{value?.ToString()?.Replace("\"", "\\\"")}\" }}"; + } + + private static string BuildNonString(object value) + { + return $"{{ \"testValue\": {value} }}"; + } + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs new file mode 100644 index 0000000..5139021 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs @@ -0,0 +1,689 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml; +using Newtonsoft.Json; +using NUnit.Framework; +using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; +using Xtz.StronglyTyped.BuiltinTypes.Internet; +using Xtz.StronglyTyped.UnitTests; + +namespace Xtz.StronglyTyped.NewtonsoftJson.UnitTests +{ + public class SerializationTests + { + private static readonly JsonSerializerSettings JSON_SERIALIZER_SETTINGS; + + static SerializationTests() + { + var jsonSerializerSettings = new JsonSerializerSettings(); + jsonSerializerSettings.Converters.Add(new StronglyTypedNewtonsoftConverter()); + JSON_SERIALIZER_SETTINGS = jsonSerializerSettings; + } + + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldSerialize_GivenStronglyTypedStringClass(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedString(value); + var json = JsonConvert.SerializeObject(value, JSON_SERIALIZER_SETTINGS); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldSerialize_GivenStronglyTypedStringStruct(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedStringStruct(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldSerialize_GivenStronglyTypedStringClassAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedStringAllowEmpty(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(value, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldSerialize_GivenStronglyTypedStringStructAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new StronglyTypedStringAllowEmptyStruct(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) + { + // Arrange + + var stronglyTyped = new StronglyTypedGuid(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] + public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) + { + // Arrange + + var stronglyTyped = new StronglyTypedGuidStruct(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldSerialize_GivenStronglyTypedGuidIdClass(Guid value) + { + // Arrange + + var stronglyTyped = new StronglyTypedGuidId(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedIntClass(int value) + { + // Arrange + + var stronglyTyped = new StronglyTypedInt(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedIntStruct(int value) + { + // Arrange + + var stronglyTyped = new StronglyTypedIntStruct(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(1)] + [TestCase(546)] + [TestCase(6474)] + [TestCase(int.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedIntIdClass(int value) + { + // Arrange + + var stronglyTyped = new StronglyTypedIntId(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(false)] + [TestCase(true)] + public void ShouldSerialize_GivenStronglyTypedBoolClass(bool value) + { + // Arrange + + var stronglyTyped = new StronglyTypedBool(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(byte.MinValue)] + [TestCase(0)] + [TestCase(1)] + [TestCase(233)] + [TestCase(byte.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedByteClass(byte value) + { + // Arrange + + var stronglyTyped = new StronglyTypedByte(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(char.MinValue)] + [TestCase('I')] + [TestCase('x')] + [TestCase('\xF0')] + [TestCase('Ž')] + [TestCase('£')] + [TestCase('¶')] + [TestCase('Ð')] + [TestCase('4')] + [TestCase('ߡ')] + [TestCase('ݓ')] + [TestCase('Ѝ')] + [TestCase(char.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedCharClass(char value) + { + // Arrange + + var stronglyTyped = new StronglyTypedChar(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(-5745)] + [TestCase(-968.2566)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353)] + public void ShouldSerialize_GivenStronglyTypedDecimalClass(decimal value) + { + // Arrange + + var stronglyTyped = new StronglyTypedDecimal(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + public void ShouldSerialize_GivenStronglyTypedDecimalClass() + { + // Arrange + + var values = new [] { decimal.MinValue, -968.2566m, decimal.MinusOne, decimal.Zero, decimal.One, decimal.MaxValue }; + + var stronglyTyped = values.Select(x => (StronglyTypedDecimal)x); + var json = JsonConvert.SerializeObject(values); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(double.MinValue)] + [TestCase(double.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353)] + [TestCase(double.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedDoubleClass(double value) + { + // Arrange + + var stronglyTyped = new StronglyTypedDouble(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(float.MinValue)] + [TestCase(float.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566F)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353F)] + [TestCase(float.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedFloatClass(float value) + { + // Arrange + + var stronglyTyped = new StronglyTypedFloat(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(long.MinValue)] + [TestCase(-5745)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(41343252352352)] + [TestCase(long.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedLongClass(long value) + { + // Arrange + + var stronglyTyped = new StronglyTypedLong(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(sbyte.MinValue)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(sbyte.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedSbyteClass(sbyte value) + { + // Arrange + + var stronglyTyped = new StronglyTypedSbyte(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(short.MinValue)] + [TestCase(-24633)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(7374)] + [TestCase(short.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedShortClass(short value) + { + // Arrange + + var stronglyTyped = new StronglyTypedShort(value); + var json = JsonConvert.SerializeObject(value, JSON_SERIALIZER_SETTINGS); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(uint.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(uint.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedUintClass(uint value) + { + // Arrange + + var stronglyTyped = new StronglyTypedUint(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(ulong.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(847847645745745U)] + [TestCase(ulong.MaxValue)] + public void ShouldSerialize_GivenStronglyTypedUlongClass(ulong value) + { + // Arrange + + var stronglyTyped = new StronglyTypedUlong(value); + var json = JsonConvert.SerializeObject(value); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + public void ShouldSerialize_GivenStronglyTypedUshortClass() + { + // Arrange + + var values = new[] { ushort.MinValue, (ushort)0, (ushort)1, (ushort)34, (ushort)7374, ushort.MaxValue }; + + var stronglyTyped = values.Select(x => (StronglyTypedUshort)x); + var json = JsonConvert.SerializeObject(values); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase("2020-08-14T14:35:57+0000")] + [TestCase("2020-08-14T14:35:57-1000")] + [TestCase("2020-08-14T14:35:57Z")] + [TestCase("2320-07-21T23:00:35Z")] + [TestCase("1539-05-06T09:20:45Z")] + public void ShouldSerialize_GivenStronglyTypedDateTimeClass(DateTime value) + { + // Arrange + + var stronglyTyped = new StronglyTypedDateTime(value); + var json = JsonConvert.SerializeObject(value.ToUniversalTime()); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [TestCase(TimeSpan.TicksPerMillisecond)] + [TestCase(TimeSpan.TicksPerSecond)] + [TestCase(TimeSpan.TicksPerMinute)] + [TestCase(TimeSpan.TicksPerHour)] + [TestCase(TimeSpan.TicksPerDay)] + public void ShouldSerialize_GivenStronglyTypedTimeSpanClass_FromTicks(long value) + { + // Arrange + + var timeSpan = TimeSpan.FromTicks(value); + var stronglyTyped = new StronglyTypedTimeSpan(timeSpan); + var expected = $"\"{XmlConvert.ToString(stronglyTyped)}\""; + var expectedString = $"\"{stronglyTyped}\""; + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(expected, result); + Assert.AreEqual(expectedString, result); + } + + [Test] + [TestCase("6")] + [TestCase("6:12")] + [TestCase("6:12:14")] + [TestCase("6:12:14:45")] + [TestCase("6.12:14:45")] + [TestCase("6:12:14:45.3448")] + public void ShouldSerialize_GivenStronglyTypedTimeSpanClass(TimeSpan value) + { + // Arrange + + var stronglyTyped = new StronglyTypedTimeSpan(value); + var expected = $"\"{XmlConvert.ToString(value)}\""; + var expectedString = $"\"{stronglyTyped}\""; + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(expected, result); + Assert.AreEqual(expectedString, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedEmailClass(Email stronglyTyped) + { + // Arrange + + var json = JsonConvert.SerializeObject(stronglyTyped.Value.Address); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(json, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedEmailsClass(IReadOnlyCollection stronglyTyped) + { + // Arrange + + var collection = stronglyTyped.Select(x => x.Value.Address).ToArray(); + var json = JsonConvert.SerializeObject(collection); + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.IsNotEmpty(collection); + Assert.AreEqual(json, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedIpV4AddressClass(IpV4Address stronglyTyped) + { + // Arrange + + var expected = $"\"{stronglyTyped.Value}\""; + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(expected, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedIpV6AddressClass(IpV6Address stronglyTyped) + { + // Arrange + + var expected = $"\"{stronglyTyped.Value}\""; + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(expected, result); + } + + [Test] + [StrongAutoData] + public void ShouldSerialize_GivenStronglyTypedMacAddressClass(MacAddress stronglyTyped) + { + // Arrange + + var expectedToString = $"\"{stronglyTyped.Value}\""; + var expected = $"\"{stronglyTyped.ToString(MacAddress.Separator.Hyphen)}\""; + + // Act + + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.AreEqual(expected, result); + Assert.AreEqual(expectedToString, result.Replace("-", string.Empty)); + } + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj new file mode 100644 index 0000000..cd7928d --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj @@ -0,0 +1,34 @@ + + + + net5 + preview + + + + + + + + + True + $(BaseIntermediateOutputPath)\GeneratedFiles + + + + + + + + + + + + + + + + + + + diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj.DotSettings b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj.DotSettings new file mode 100644 index 0000000..eaf4b15 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj.DotSettings @@ -0,0 +1,3 @@ + + True + True \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/_TestModels/SerializationDto.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/_TestModels/SerializationDto.cs new file mode 100644 index 0000000..23599cd --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/_TestModels/SerializationDto.cs @@ -0,0 +1,26 @@ +using System; +using System.Diagnostics; + +namespace Xtz.StronglyTyped.NewtonsoftJson.UnitTests +{ + [DebuggerDisplay("{TestValue,nq}")] + public class SerializationDto + where TStronglyTyped : IStronglyTyped + { + public SerializationDto() + { + } + + public SerializationDto(TStronglyTyped stronglyTyped) + { + TestValue = stronglyTyped; + } + + public SerializationDto(object value) + { + TestValue = (TStronglyTyped)Activator.CreateInstance(typeof(TStronglyTyped), value); + } + + public TStronglyTyped TestValue { get; set; } + } +} \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.sln b/src/Xtz.StronglyTyped.sln index 54e54a6..e53f3a9 100644 --- a/src/Xtz.StronglyTyped.sln +++ b/src/Xtz.StronglyTyped.sln @@ -37,6 +37,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.EntityFra EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.Api_3_1.IntegrationTests", "Xtz.StronglyTyped.Api_3_1.IntegrationTests\Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj", "{1AAE34E7-203E-41D3-A054-4767C5A99FAC}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.NewtonsoftJson", "Xtz.StronglyTyped.NewtonsoftJson\Xtz.StronglyTyped.NewtonsoftJson.csproj", "{D7EB61AD-95C7-418C-B1B5-60D0B73905EA}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.NewtonsoftJson.UnitTests", "Tests\Xtz.StronglyTyped.NewtonsoftJson.UnitTests\Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj", "{97EDCF82-8DA3-4DB1-96C4-FB2481A75666}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -103,6 +107,14 @@ Global {1AAE34E7-203E-41D3-A054-4767C5A99FAC}.Debug|Any CPU.Build.0 = Debug|Any CPU {1AAE34E7-203E-41D3-A054-4767C5A99FAC}.Release|Any CPU.ActiveCfg = Release|Any CPU {1AAE34E7-203E-41D3-A054-4767C5A99FAC}.Release|Any CPU.Build.0 = Release|Any CPU + {D7EB61AD-95C7-418C-B1B5-60D0B73905EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7EB61AD-95C7-418C-B1B5-60D0B73905EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7EB61AD-95C7-418C-B1B5-60D0B73905EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7EB61AD-95C7-418C-B1B5-60D0B73905EA}.Release|Any CPU.Build.0 = Release|Any CPU + {97EDCF82-8DA3-4DB1-96C4-FB2481A75666}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {97EDCF82-8DA3-4DB1-96C4-FB2481A75666}.Debug|Any CPU.Build.0 = Debug|Any CPU + {97EDCF82-8DA3-4DB1-96C4-FB2481A75666}.Release|Any CPU.ActiveCfg = Release|Any CPU + {97EDCF82-8DA3-4DB1-96C4-FB2481A75666}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -116,6 +128,7 @@ Global {F5E5B3F6-F0D7-4B11-A99C-87E3ADCEEFFE} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} {857370EC-A6FA-47FE-9005-8CC4D752630F} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} {1AAE34E7-203E-41D3-A054-4767C5A99FAC} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} + {97EDCF82-8DA3-4DB1-96C4-FB2481A75666} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F4FB6146-7992-4E75-B5F9-22B2ADA3C82F} From 25deb9e53c70a949e3b6f88e29358bdd9b8e5336 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 14:23:33 +0200 Subject: [PATCH 13/37] Bump up the versions --- .../Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.csproj | 2 +- .../Xtz.StronglyTyped.EntityFramework.csproj | 2 +- .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 2 +- .../Xtz.StronglyTyped.SourceGenerator.csproj | 2 +- .../Xtz.StronglyTyped.Swashbuckle.csproj | 2 +- src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index b5cf811..6b6d73d 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.AutoFixture - 0.19.0 + 0.20.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index 3994eb2..fcbb3bf 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.Bogus - 0.19.0 + 0.20.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index ef14370..2414ecf 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes - 0.19.0 + 0.20.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index 1e706ab..f7f3806 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.EntityFramework - 0.19.0 + 0.20.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index 0349b47..4b675ea 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.NewtonsoftJson - 0.19.0 + 0.20.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index aebdbe7..02d8fd6 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.SourceGenerator - 0.19.0 + 0.20.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index 72e38fe..a51556b 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.Swashbuckle - 0.19.0 + 0.20.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index 5fdbcaa..7027c20 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped - 0.19.0 + 0.20.0 true snupkg Vlad DX From 087a66ef31ecbfa86663c9eff5ede648d2e7053d Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 18:14:09 +0200 Subject: [PATCH 14/37] Upgrade NuGet dependencies and fix `nullable` warnings --- .../Xtz.StronglyTyped.Benchmark.csproj | 4 ++-- .../Xtz.StronglyTyped.Playground.csproj | 2 +- .../AddressAutoDataTests.cs | 2 +- .../CommerceAutoDataTests.cs | 2 +- .../CompanyAutoDataTests.cs | 2 +- .../FinanceAutoDataTests.cs | 2 +- .../InternetAutoDataTests.cs | 2 +- .../NameAutoDataTests.cs | 2 +- .../NumberAutoDataTests.cs | 2 +- .../PhoneAutoDataTests.cs | 2 +- .../VehicleAutoDataTests.cs | 2 +- ...tz.StronglyTyped.EntityFramework.IntegrationTests.csproj | 4 ++-- .../Xtz.StronglyTyped.IntegrationTests.csproj | 2 +- .../Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj | 2 +- ...tz.StronglyTyped.SourceGenerator.IntegrationTests.csproj | 4 ++-- .../Xtz.StronglyTyped.UnitTests.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.csproj | 2 +- .../Xtz.StronglyTyped.EntityFramework.csproj | 4 ++-- .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 4 ++-- src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs | 6 +++--- .../Xtz.StronglyTyped.SourceGenerator.csproj | 6 +++--- .../Xtz.StronglyTyped.Swashbuckle.csproj | 4 ++-- src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj | 2 +- 25 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj index 6557977..17b7f34 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj @@ -20,11 +20,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all diff --git a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj index 65cfcba..6c92284 100644 --- a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj +++ b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj @@ -45,7 +45,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs index d4d041d..f4dadf5 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs @@ -45,7 +45,7 @@ public void ShouldGenerateStronglyTypedValues( postalCode, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs index b910e33..9fdcdc8 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs @@ -35,7 +35,7 @@ public void ShouldGenerateStronglyTypedValues( subDepartment, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs index 6e076c8..e7b2854 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs @@ -39,7 +39,7 @@ public void ShouldGenerateStronglyTypedValues( siteCode }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs index 224c3e6..f3894d3 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs @@ -33,7 +33,7 @@ public void ShouldGenerateStronglyTypedValues( transactionType, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs index 7d6f09a..16e2a3c 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs @@ -46,7 +46,7 @@ public void ShouldGenerateStronglyTypedValues( websiteProtocol, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs index 9d65d20..409e129 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs @@ -27,7 +27,7 @@ public void ShouldGenerateStronglyTypedValues( nameSuffix, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs index b5f5aba..378563d 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs @@ -41,7 +41,7 @@ public void ShouldGenerateStronglyTypedValues( positiveInt64, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs index 206d8fa..d3efa26 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs @@ -19,7 +19,7 @@ public void ShouldGenerateStronglyTypedValues( phoneNumber, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs index 354e272..4c2d662 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs @@ -27,7 +27,7 @@ public void ShouldGenerateStronglyTypedValues( vin, }; - Assert.That(values, Is.All.Matches(x => !x.ToString().IsBogusGeneratedValue())); + Assert.That(values, Is.All.Matches(x => !x.ToString()!.IsBogusGeneratedValue())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj index 24fc097..6154438 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj index afa53be..00f1f38 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj @@ -29,7 +29,7 @@ - + diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj index cd7928d..c13445b 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj index 5592dca..fb25a89 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj @@ -16,8 +16,8 @@ - - + + diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj index fd6d700..82f6d65 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index 6b6d73d..6fc0fcd 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.AutoFixture - 0.20.0 + 0.20.1 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index fcbb3bf..912201a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.Bogus - 0.20.0 + 0.20.1 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index 2414ecf..44cf58d 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes - 0.20.0 + 0.20.1 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index f7f3806..12075e3 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.EntityFramework - 0.20.0 + 0.20.1 true snupkg Vlad DX @@ -34,7 +34,7 @@ Make compiler your friend, introduce semantics to your code. - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index 4b675ea..00e489a 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.NewtonsoftJson - 0.20.0 + 0.20.1 true snupkg Vlad DX @@ -34,7 +34,7 @@ Make compiler your friend, introduce semantics to your code. - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs index b0a2329..fe53a99 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs @@ -133,12 +133,12 @@ public WorkItemKind ExtractItemKind(SyntaxNode syntaxNode) { if (string.IsNullOrEmpty(typeName)) return null; - if (KNOWN_TYPES.TryGetValue(typeName, out var knownType)) + if (KNOWN_TYPES.TryGetValue(typeName!, out var knownType)) { return knownType; } - var systemType = SYSTEM_PRIVATE_CORE_LIB_ASSEMBLY.GetType(typeName); + var systemType = SYSTEM_PRIVATE_CORE_LIB_ASSEMBLY.GetType(typeName!); if (systemType != null) { return systemType; @@ -305,7 +305,7 @@ private bool HasEnumArgument( // [StrongType(..., allow: Allow.Null | Allow.Empty)] // ^ var binaryOperation = semanticModel.GetOperation(binaryExpressionSyntax); - if (binaryOperation?.Type.ToDisplayString() == typeof(TEnum).FullName) + if (binaryOperation?.Type?.ToDisplayString() == typeof(TEnum).FullName) { // [StrongType(..., allow: Allow.Null | Allow.Empty)] // ^ diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index 02d8fd6..3ab7386 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.SourceGenerator - 0.20.0 + 0.20.1 true snupkg Vlad DX @@ -46,11 +46,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index a51556b..0018b02 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.Swashbuckle - 0.20.0 + 0.20.1 true snupkg Vlad DX @@ -38,7 +38,7 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index 7027c20..3807a6c 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped - 0.20.0 + 0.20.1 true snupkg Vlad DX From 988d42eea881d66b6761b9540c1d108fdeea864c Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 18:15:14 +0200 Subject: [PATCH 15/37] Fix tests for non-string values - Fix tests (many are failing now) - Uncomment accidentially commented out tests --- .../DeserializationTests.cs | 184 +++++++++--------- 1 file changed, 94 insertions(+), 90 deletions(-) diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs index 1786b23..340faed 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs @@ -173,7 +173,7 @@ public void ShouldDeserialize_ToStronglyTypedIntClass(int value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -197,7 +197,7 @@ public void ShouldDeserialize_ToStronglyTypedIntStruct(int value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -219,7 +219,7 @@ public void ShouldDeserialize_ToStronglyTypedIntIdClass(int value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -232,23 +232,27 @@ public void ShouldDeserialize_ToStronglyTypedIntIdClass(int value) } [Test] - [TestCase(false)] - [TestCase(true)] - public void ShouldDeserialize_ToStronglyTypedBoolClass(bool value) + public void ShouldDeserialize_ToStronglyTypedBoolClass() { // Arrange - var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var values = new[] { false, true }; + + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = "[{\"testValue\": false }, {\"testValue\": true }]"; // Act - var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + var result = JsonConvert.DeserializeObject>>(json, JSON_SERIALIZER_SETTINGS); // Assert Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } [Test] @@ -262,7 +266,7 @@ public void ShouldDeserialize_ToStronglyTypedByteClass(byte value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -317,7 +321,7 @@ public void ShouldDeserialize_ToStronglyTypedDecimalClass(decimal value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -368,7 +372,7 @@ public void ShouldDeserialize_ToStronglyTypedDoubleClass(double value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -395,7 +399,7 @@ public void ShouldDeserialize_ToStronglyTypedFloatClass(float value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -420,7 +424,7 @@ public void ShouldDeserialize_ToStronglyTypedLongClass(long value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -444,7 +448,7 @@ public void ShouldDeserialize_ToStronglyTypedSbyteClass(sbyte value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -470,7 +474,7 @@ public void ShouldDeserialize_ToStronglyTypedShortClass(short value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -495,7 +499,7 @@ public void ShouldDeserialize_ToStronglyTypedUintClass(uint value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -521,7 +525,7 @@ public void ShouldDeserialize_ToStronglyTypedUlongClass(ulong value) // Arrange var stronglyTyped = new SerializationDto(value); - var json = BuildString(value); + var json = BuildNonString(value); // Act @@ -631,104 +635,104 @@ public void ShouldDeserialize_ToStronglyTypedTimeSpanClass(TimeSpan timeSpan) Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); } - //[Test] - //[StrongAutoData] - //public void ShouldDeserialize_ToStronglyTypedEmailClass(Email value) - //{ - // // Arrange + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedEmailClass(Email value) + { + // Arrange - // var stronglyTyped = new SerializationDto(value); - // var json = BuildString(stronglyTyped.TestValue.Value.Address); + var stronglyTyped = new SerializationDto(value); + var json = BuildString(stronglyTyped.TestValue.Value.Address); - // // Act + // Act - // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); - // // Assert + // Assert - // Assert.NotNull(result); - // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); - //} + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } - //[Test] - //[StrongAutoData] - //public void ShouldDeserialize_ToStronglyTypedEmailsClass(IReadOnlyCollection values) - //{ - // // Arrange + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedEmailsClass(IReadOnlyCollection values) + { + // Arrange - // var stronglyTyped = values - // .Select(x => new SerializationDto(x)) - // .ToArray(); - // var json = BuildStringArray(stronglyTyped.Select(x => x.TestValue.Value.Address)); + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = BuildStringArray(stronglyTyped.Select(x => x.TestValue.Value.Address)); - // // Act + // Act - // var result = JsonConvert.DeserializeObject>>(json, JSON_SERIALIZER_SETTINGS); + var result = JsonConvert.DeserializeObject>>(json, JSON_SERIALIZER_SETTINGS); - // // Assert + // Assert - // Assert.NotNull(result); - // Assert.IsNotEmpty(result); - // Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); - // Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); - //} + Assert.NotNull(result); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + } - //[Test] - //[StrongAutoData] - //public void ShouldDeserialize_ToStronglyTypedIpV4AddressClass(IpV4Address value) - //{ - // // Arrange + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedIpV4AddressClass(IpV4Address value) + { + // Arrange - // var stronglyTyped = new SerializationDto(value); - // var json = BuildString(value); + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); - // // Act + // Act - // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); - // // Assert + // Assert - // Assert.NotNull(result); - // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); - //} + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } - //[Test] - //[StrongAutoData] - //public void ShouldDeserialize_ToStronglyTypedIpV6AddressClass(IpV6Address value) - //{ - // // Arrange + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedIpV6AddressClass(IpV6Address value) + { + // Arrange - // var stronglyTyped = new SerializationDto(value); - // var json = BuildString(value); + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); - // // Act + // Act - // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); - // // Assert + // Assert - // Assert.NotNull(result); - // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); - //} + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } - //[Test] - //[StrongAutoData] - //public void ShouldDeserialize_ToStronglyTypedMacAddressClass(MacAddress value) - //{ - // // Arrange + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedMacAddressClass(MacAddress value) + { + // Arrange - // var stronglyTyped = new SerializationDto(value); - // var json = BuildString(value); + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); - // // Act + // Act - // var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); + var result = JsonConvert.DeserializeObject>(json, JSON_SERIALIZER_SETTINGS); - // // Assert + // Assert - // Assert.NotNull(result); - // Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); - //} + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } private static string BuildStringArray(IEnumerable values) { @@ -736,7 +740,7 @@ private static string BuildStringArray(IEnumerable values) foreach (var value in values) { - stringBuilder.AppendFormat(" {0},\n", BuildString(value)); + stringBuilder.AppendFormat(" {0},\n", BuildString(value)); } stringBuilder.Append("]"); @@ -750,7 +754,7 @@ private static string BuildNonStringArray(IEnumerable values) foreach (var value in values) { - stringBuilder.AppendFormat("{0},\n", BuildNonString(value)); + stringBuilder.AppendFormat(" {0},\n", BuildNonString(value)); } stringBuilder.Append("]"); From 183caf2a4b764ca2a25bb143bdb622b800ad7318 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 18:21:50 +0200 Subject: [PATCH 16/37] Fix JSON converters - Fix `StronglyTypedNewtonsoftConverter` (`Newtonsoft.Json` converter) - Fix `StronglyTypedJsonConverter` (`System.Text.Json` converter) - Fix tests --- .../SystemTextJson/DeserializationTests.cs | 790 ++++++++++++++++++ .../SerializationTests.cs} | 15 +- .../_TestModels/SerializationDto.cs | 26 + .../_TestModels/StronglyTypedBool.cs | 1 - .../_TestModels/StronglyTypedBoolStruct.cs | 1 - .../StronglyTypedNewtonsoftConverter.cs | 212 +++-- .../NewtonsoftJsonConverterException.cs | 14 + .../StronglyTypedJsonConverter.cs | 110 ++- 8 files changed, 1073 insertions(+), 96 deletions(-) create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs rename src/Tests/Xtz.StronglyTyped.UnitTests/{Values/SystemTextJsonTests.cs => SystemTextJson/SerializationTests.cs} (98%) create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/SerializationDto.cs create mode 100644 src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs new file mode 100644 index 0000000..ee263dd --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs @@ -0,0 +1,790 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Xml; +using NUnit.Framework; +using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; +using Xtz.StronglyTyped.BuiltinTypes.Internet; + +namespace Xtz.StronglyTyped.UnitTests.SystemTextJson +{ + public class DeserializationTests + { + private static readonly JsonSerializerOptions JSON_SERIALIZER_SETTINGS = new() + { + PropertyNameCaseInsensitive = true, + AllowTrailingCommas = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + }; + + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldDeserialize_ToStronglyTypedStringClass(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("New York")] + [TestCase("New \"The Big Apple\" York")] + public void ShouldDeserialize_ToStronglyTypedStringStruct(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldDeserialize_ToStronglyTypedStringClassAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("Brazil")] + [TestCase("Trinidad and Tobago")] + [TestCase("")] + public void ShouldDeserialize_ToStronglyTypedStringStructAllowEmpty(string value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] + public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] + public void ShouldDeserialize_ToStronglyTypedGuidIdClass(Guid value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedIntClass(int value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(int.MinValue)] + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + [TestCase(6372)] + [TestCase(int.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedIntStruct(int value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(1)] + [TestCase(546)] + [TestCase(6474)] + [TestCase(int.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedIntIdClass(int value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + public void ShouldDeserialize_ToStronglyTypedBoolClass() + { + // Arrange + + var values = new[] { false, true }; + + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = "[{\"testValue\": false }, {\"testValue\": true }]"; + + // Act + + var result = JsonSerializer.Deserialize>>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + } + + [Test] + [TestCase(byte.MinValue)] + [TestCase(0)] + [TestCase(1)] + [TestCase(233)] + [TestCase(byte.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedByteClass(byte value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase('I')] + [TestCase('x')] + [TestCase('\xF0')] + [TestCase('Ž')] + [TestCase('£')] + [TestCase('¶')] + [TestCase('Ð')] + [TestCase('4')] + [TestCase('ߡ')] + [TestCase('ݓ')] + [TestCase('Ѝ')] + [TestCase(char.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedCharClass(char value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(char.MinValue, "\\u0000")] + public void ShouldDeserialize_ToStronglyTypedCharClass(char value, string serializedValue) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(serializedValue); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(-5745)] + [TestCase(-968.2566)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353)] + public void ShouldDeserialize_ToStronglyTypedDecimalClass(decimal value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + public void ShouldDeserialize_ToStronglyTypedDecimalClass() + { + // Arrange + + var values = new[] { decimal.MinValue, -968.2566m, decimal.MinusOne, decimal.Zero, decimal.One, decimal.MaxValue }; + + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = BuildNonStringArray(stronglyTyped.Select(x => x.TestValue.Value).Cast()); + + // Act + + var result = JsonSerializer.Deserialize>>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + } + + [Test] + [TestCase(double.MinValue)] + [TestCase(double.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353)] + [TestCase(double.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedDoubleClass(double value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(float.MinValue)] + [TestCase(float.Epsilon)] + [TestCase(-5745)] + [TestCase(-968.2566F)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(6534.353F)] + [TestCase(float.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedFloatClass(float value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(long.MinValue)] + [TestCase(-5745)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34634)] + [TestCase(41343252352352)] + [TestCase(long.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedLongClass(long value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(sbyte.MinValue)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(sbyte.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedSbyteClass(sbyte value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(short.MinValue)] + [TestCase(-24633)] + [TestCase(-55)] + [TestCase(0)] + [TestCase(1)] + [TestCase(34)] + [TestCase(7374)] + [TestCase(short.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedShortClass(short value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(uint.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(uint.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedUintClass(uint value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(ulong.MinValue)] + [TestCase(0U)] + [TestCase(1U)] + [TestCase(34U)] + [TestCase(7374U)] + [TestCase(45745745U)] + [TestCase(847847645745745U)] + [TestCase(ulong.MaxValue)] + public void ShouldDeserialize_ToStronglyTypedUlongClass(ulong value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildNonString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + public void ShouldDeserialize_ToStronglyTypedUshortClass() + { + var values = new[] { ushort.MinValue, (ushort)0, (ushort)1, (ushort)34, (ushort)7374, ushort.MaxValue }; + // Arrange + + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = BuildNonStringArray(stronglyTyped.Select(x => x.TestValue.Value).Cast()); + + // Act + + var result = JsonSerializer.Deserialize>>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + } + + [Test] + [TestCase("2020-08-14T14:35:57+0000")] + [TestCase("2020-08-14T14:35:57-1000")] + [TestCase("2020-08-14T14:35:57Z")] + [TestCase("2320-07-21T23:00:35Z")] + [TestCase("1539-05-06T09:20:45Z")] + public void ShouldDeserialize_ToStronglyTypedDateTimeClass(DateTime value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase(TimeSpan.TicksPerMillisecond)] + [TestCase(TimeSpan.TicksPerSecond)] + [TestCase(TimeSpan.TicksPerMinute)] + [TestCase(TimeSpan.TicksPerHour)] + [TestCase(TimeSpan.TicksPerDay)] + public void ShouldDeserialize_ToStronglyTypedTimeSpanClass_FromTicks(long value) + { + var timeSpan = TimeSpan.FromTicks(value); + var expected = XmlConvert.ToString(timeSpan); + + // Arrange + + var stronglyTyped = new SerializationDto(timeSpan); + var json = BuildString(expected); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [TestCase("6")] + [TestCase("6:12")] + [TestCase("6:12:14")] + [TestCase("6:12:14:45")] + [TestCase("6.12:14:45")] + [TestCase("6:12:14:45.3448")] + public void ShouldDeserialize_ToStronglyTypedTimeSpanClass(TimeSpan timeSpan) + { + var expected = XmlConvert.ToString(timeSpan); + + // Arrange + + var stronglyTyped = new SerializationDto(timeSpan); + var json = BuildString(expected); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedEmailClass(Email value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(stronglyTyped.TestValue.Value.Address); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedEmailsClass(IReadOnlyCollection values) + { + // Arrange + + var stronglyTyped = values + .Select(x => new SerializationDto(x)) + .ToArray(); + var json = BuildStringArray(stronglyTyped.Select(x => x.TestValue.Value.Address)); + + // Act + + var result = JsonSerializer.Deserialize>>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.IsNotEmpty(result); + Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); + Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); + } + + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedIpV4AddressClass(IpV4Address value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedIpV6AddressClass(IpV6Address value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + [Test] + [StrongAutoData] + public void ShouldDeserialize_ToStronglyTypedMacAddressClass(MacAddress value) + { + // Arrange + + var stronglyTyped = new SerializationDto(value); + var json = BuildString(value); + + // Act + + var result = JsonSerializer.Deserialize>(json, JSON_SERIALIZER_SETTINGS); + + // Assert + + Assert.NotNull(result); + Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + } + + private static string BuildStringArray(IEnumerable values) + { + var stringBuilder = new StringBuilder("[ \n"); + + foreach (var value in values) + { + stringBuilder.AppendFormat(" {0},\n", BuildString(value)); + } + + stringBuilder.Append("]"); + var result = stringBuilder.ToString(); + return result; + } + + private static string BuildNonStringArray(IEnumerable values) + { + var stringBuilder = new StringBuilder("[ \n"); + + foreach (var value in values) + { + stringBuilder.AppendFormat("{0},\n", BuildNonString(value)); + } + + stringBuilder.Append("]"); + var result = stringBuilder.ToString(); + return result; + } + + private static string BuildString(object value) + { + return $"{{ \"testValue\": \"{value?.ToString()?.Replace("\"", "\\\"")}\" }}"; + } + + private static string BuildNonString(object value) + { + return $"{{ \"testValue\": {value} }}"; + } + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/SystemTextJsonTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs similarity index 98% rename from src/Tests/Xtz.StronglyTyped.UnitTests/Values/SystemTextJsonTests.cs rename to src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs index 94d6b80..a573eee 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/SystemTextJsonTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs @@ -3,15 +3,13 @@ using System.Linq; using System.Text.Json; using System.Xml; -using AutoFixture; using NUnit.Framework; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; -using Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders; using Xtz.StronglyTyped.BuiltinTypes.Internet; -namespace Xtz.StronglyTyped.UnitTests.Values +namespace Xtz.StronglyTyped.UnitTests.SystemTextJson { - public class SystemTextJsonTests + public class SerializationTests { [Test] [TestCase("New York")] @@ -213,14 +211,13 @@ public void ShouldSerialize_GivenStronglyTypedIntIdClass(int value) } [Test] - [TestCase(false)] - [TestCase(true)] - public void ShouldSerialize_GivenStronglyTypedBoolClass(bool value) + [TestCase(false, "false")] + [TestCase(true, "true")] + public void ShouldSerialize_GivenStronglyTypedBoolClass(bool value, string expectedValue) { // Arrange var stronglyTyped = new StronglyTypedBool(value); - var json = JsonSerializer.Serialize(value); // Act @@ -228,7 +225,7 @@ public void ShouldSerialize_GivenStronglyTypedBoolClass(bool value) // Assert - Assert.AreEqual(json, result); + Assert.AreEqual(expectedValue, result); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/SerializationDto.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/SerializationDto.cs new file mode 100644 index 0000000..d0f1a97 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/SerializationDto.cs @@ -0,0 +1,26 @@ +using System; +using System.Diagnostics; + +namespace Xtz.StronglyTyped.UnitTests +{ + [DebuggerDisplay("{TestValue,nq}")] + public class SerializationDto + where TStronglyTyped : IStronglyTyped + { + public SerializationDto() + { + } + + public SerializationDto(TStronglyTyped stronglyTyped) + { + TestValue = stronglyTyped; + } + + public SerializationDto(object value) + { + TestValue = (TStronglyTyped)Activator.CreateInstance(typeof(TStronglyTyped), value); + } + + public TStronglyTyped TestValue { get; set; } + } +} \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs index a31bc8f..657d93a 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBool.cs @@ -2,7 +2,6 @@ namespace Xtz.StronglyTyped.UnitTests { - // TODO: Remove `Allow.Empty` when it's supported by generator [StrongType(typeof(bool))] public sealed partial class StronglyTypedBool { diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs index f6607af..a920ea9 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/StronglyTypedBoolStruct.cs @@ -2,7 +2,6 @@ namespace Xtz.StronglyTyped.UnitTests { - // TODO: Remove `Allow.Empty` when it's supported by generator [StrongType(typeof(bool), Allow.Empty)] public partial struct StronglyTypedBoolStruct { diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs index ece0e62..96a1017 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs @@ -69,107 +69,80 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer writer.WriteValue(stringValue); } - private bool TryWriteNumber(object value, Type innerType, JsonWriter writer) + private IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter typeConverter) { - if (innerType == typeof(decimal)) - { - var decimalValue = (value as IStronglyTyped)?.Value ?? default(decimal); - writer.WriteValue(decimalValue); - return true; - } - - if (innerType == typeof(double)) - { - var doubleValue = (value as IStronglyTyped)?.Value ?? default(double); - writer.WriteValue(doubleValue); - return true; - } - - if (innerType == typeof(float)) - { - var floatValue = (value as IStronglyTyped)?.Value ?? default(float); - writer.WriteValue(floatValue); - return true; - } - - if (innerType == typeof(int)) + if (typeConverter.InnerType == typeof(byte)) { - var intValue = (value as IStronglyTyped)?.Value ?? default(int); - writer.WriteValue(intValue); - return true; + var value = unchecked((byte)longValue); + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; } - if (innerType == typeof(long)) + if (typeConverter.InnerType == typeof(decimal)) { - var longValue = (value as IStronglyTyped)?.Value ?? default(long); - writer.WriteValue(longValue); - return true; + var value = (decimal)longValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; } - if (innerType == typeof(uint)) + if (typeConverter.InnerType == typeof(double)) { - var uintValue = (value as IStronglyTyped)?.Value ?? default(uint); - writer.WriteValue(uintValue); - return true; + var value = (double)longValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; } - if (innerType == typeof(ulong)) + if (typeConverter.InnerType == typeof(float)) { - var ulongValue = (value as IStronglyTyped)?.Value ?? default(ulong); - writer.WriteValue(ulongValue); - return true; + var value = (float)longValue; + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; } - if (innerType == typeof(byte)) + if (typeConverter.InnerType == typeof(int)) { - var byteValue = (value as IStronglyTyped)?.Value ?? default(byte); - writer.WriteValue(byteValue); - return true; + var value = unchecked((int)longValue); + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; } - if (innerType == typeof(sbyte)) + if (typeConverter.InnerType == typeof(long)) { - var sbyteValue = (value as IStronglyTyped)?.Value ?? default(sbyte); - writer.WriteValue(sbyteValue); - return true; + var result = (typeConverter as TypeConverter)!.ConvertFrom(longValue); + return (IStronglyTyped)result; } - if (innerType == typeof(short)) + if (typeConverter.InnerType == typeof(sbyte)) { - var shortValue = (value as IStronglyTyped)?.Value ?? default(short); - writer.WriteValue(shortValue); - return true; + var value = unchecked((sbyte)longValue); + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; } - if (innerType == typeof(ushort)) + if (typeConverter.InnerType == typeof(short)) { - var ushortValue = (value as IStronglyTyped)?.Value ?? default(ushort); - writer.WriteValue(ushortValue); - return true; + var value = unchecked((short)longValue); + var result = (typeConverter as TypeConverter)!.ConvertFrom(value); + return (IStronglyTyped)result; } - return false; - } - - private IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter typeConverter) - { - if (typeConverter.InnerType == typeof(ushort)) + if (typeConverter.InnerType == typeof(uint)) { - var value = unchecked((ushort)longValue); + var value = unchecked((uint)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); return (IStronglyTyped)result; } - if (typeConverter.InnerType == typeof(int)) + if (typeConverter.InnerType == typeof(ulong)) { - var value = unchecked((int)longValue); + var value = unchecked((ulong)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); return (IStronglyTyped)result; } - if (typeConverter.InnerType == typeof(decimal)) + if (typeConverter.InnerType == typeof(ushort)) { - var value = (decimal)longValue; + var value = unchecked((ushort)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); return (IStronglyTyped)result; } @@ -179,6 +152,12 @@ private IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter type private IStronglyTyped ConvertFromDouble(double doubleValue, ICustomTypeConverter typeConverter) { + if (typeConverter.InnerType == typeof(double)) + { + var result = (typeConverter as TypeConverter)!.ConvertFrom(doubleValue); + return (IStronglyTyped)result; + } + if (typeConverter.InnerType == typeof(decimal)) { var value = (decimal)doubleValue; @@ -205,26 +184,9 @@ private IStronglyTyped ConvertFromBigInt(BigInteger bigIntValue, ICustomTypeConv return (IStronglyTyped)result; } - // NOTE: It's not clear when `BigInteger` is being read from JSON `ushort` is here just in case - if (typeConverter.InnerType == typeof(ushort)) - { - var value = (ushort)bigIntValue; - var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; - } - - // NOTE: It's not clear when `BigInteger` is being read from JSON `int` is here just in case - if (typeConverter.InnerType == typeof(int)) - { - var value = (int)bigIntValue; - var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; - } - - // NOTE: It's not clear when `BigInteger` is being read from JSON `long` is here just in case - if (typeConverter.InnerType == typeof(long)) + if (typeConverter.InnerType == typeof(ulong)) { - var value = (long)bigIntValue; + var value = (ulong)bigIntValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); return (IStronglyTyped)result; } @@ -238,5 +200,87 @@ private IStronglyTyped ConvertToTimeSpan(object value, TypeConverter typeConvert var result = typeConverter.ConvertFrom(timeSpanValue); return (IStronglyTyped)result; } + + private bool TryWriteNumber(object value, Type innerType, JsonWriter writer) + { + if (innerType == typeof(decimal)) + { + var decimalValue = (value as IStronglyTyped)?.Value ?? default(decimal); + writer.WriteValue(decimalValue); + return true; + } + + if (innerType == typeof(double)) + { + var doubleValue = (value as IStronglyTyped)?.Value ?? default(double); + writer.WriteValue(doubleValue); + return true; + } + + if (innerType == typeof(float)) + { + var floatValue = (value as IStronglyTyped)?.Value ?? default(float); + writer.WriteValue(floatValue); + return true; + } + + if (innerType == typeof(int)) + { + var intValue = (value as IStronglyTyped)?.Value ?? default(int); + writer.WriteValue(intValue); + return true; + } + + if (innerType == typeof(long)) + { + var longValue = (value as IStronglyTyped)?.Value ?? default(long); + writer.WriteValue(longValue); + return true; + } + + if (innerType == typeof(uint)) + { + var uintValue = (value as IStronglyTyped)?.Value ?? default(uint); + writer.WriteValue(uintValue); + return true; + } + + if (innerType == typeof(ulong)) + { + var ulongValue = (value as IStronglyTyped)?.Value ?? default(ulong); + writer.WriteValue(ulongValue); + return true; + } + + if (innerType == typeof(byte)) + { + var byteValue = (value as IStronglyTyped)?.Value ?? default(byte); + writer.WriteValue(byteValue); + return true; + } + + if (innerType == typeof(sbyte)) + { + var sbyteValue = (value as IStronglyTyped)?.Value ?? default(sbyte); + writer.WriteValue(sbyteValue); + return true; + } + + if (innerType == typeof(short)) + { + var shortValue = (value as IStronglyTyped)?.Value ?? default(short); + writer.WriteValue(shortValue); + return true; + } + + if (innerType == typeof(ushort)) + { + var ushortValue = (value as IStronglyTyped)?.Value ?? default(ushort); + writer.WriteValue(ushortValue); + return true; + } + + return false; + } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs b/src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs new file mode 100644 index 0000000..f89a62b --- /dev/null +++ b/src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs @@ -0,0 +1,14 @@ +using System; +using System.Diagnostics.CodeAnalysis; + +namespace Xtz.StronglyTyped.TypeConverters +{ + [ExcludeFromCodeCoverage] + public class JsonConverterException : StronglyTypedException + { + public JsonConverterException(Type type, string errorMessage) + : base(type, errorMessage) + { + } + } +} \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs index 20835b5..8d63879 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Text.Json; using System.Text.Json.Serialization; +using System.Xml; namespace Xtz.StronglyTyped.TypeConverters { @@ -15,9 +16,32 @@ public override bool CanConvert(Type typeToConvert) public override TStronglyTyped? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - var stringValue = reader.GetString(); var typeConverter = TypeDescriptor.GetConverter(typeToConvert); + if (typeConverter is ICustomTypeConverter customTypeConverter) + { + if (customTypeConverter.InnerType == typeof(TimeSpan)) + { + return (TStronglyTyped)typeConverter.ConvertFrom(XmlConvert.ToTimeSpan(reader.GetString())); + } + + if (customTypeConverter.InnerType == typeof(DateTime)) + { + return ReadDateTime(reader, customTypeConverter.StrongType, typeConverter); + } + + if (reader.TokenType is JsonTokenType.True or JsonTokenType.False) + { + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetBoolean()); + } + + if (reader.TokenType == JsonTokenType.Number) + { + return ReadNumber(reader, customTypeConverter.StrongType, customTypeConverter.InnerType, typeConverter); + } + } + + var stringValue = reader.GetString(); return (TStronglyTyped)typeConverter.ConvertFrom(stringValue); } @@ -49,6 +73,90 @@ public override void Write(Utf8JsonWriter writer, TStronglyTyped? value, JsonSer writer.WriteStringValue(stringValue); } + private TStronglyTyped? ReadNumber(Utf8JsonReader reader, Type strongType, Type innerType, TypeConverter typeConverter) + { + + if (innerType == typeof(int)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetInt32()); + } + + if (innerType == typeof(float)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetSingle()); + } + + if (innerType == typeof(decimal)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetDecimal()); + } + + if (innerType == typeof(long)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetInt64()); + } + + if (innerType == typeof(double)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetDouble()); + } + + if (innerType == typeof(byte)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetByte()); + } + + if (innerType == typeof(sbyte)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetSByte()); + } + + if (innerType == typeof(short)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetInt16()); + } + + if (innerType == typeof(uint)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetUInt32()); + } + + if (innerType == typeof(ulong)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetUInt64()); + } + + if (innerType == typeof(ushort)) + { + return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetUInt16()); + } + + throw new JsonConverterException(strongType, $"Can't convert value to '{strongType.FullName}'"); + } + + private TStronglyTyped? ReadDateTime(Utf8JsonReader reader, Type strongType, TypeConverter typeConverter) + { + if (reader.TryGetDateTime(out var dateTimeValue)) + { + return (TStronglyTyped)typeConverter.ConvertFrom(dateTimeValue); + } + + var stringValue = reader.GetString(); + if (DateTime.TryParse(stringValue, out var dateTimeValue2)) + { + return (TStronglyTyped)typeConverter.ConvertFrom(dateTimeValue2); + } + + throw new JsonConverterException(strongType, $"Can't convert from '{stringValue}' to '{strongType.FullName}'"); + } + + private IStronglyTyped ConvertToTimeSpan(string value, TypeConverter typeConverter) + { + var timeSpanValue = XmlConvert.ToTimeSpan(value.ToString()); + var result = typeConverter.ConvertFrom(timeSpanValue); + return (IStronglyTyped)result; + } + private bool TryWriteNumber(IStronglyTyped value, Type innerType, Utf8JsonWriter writer) { if (innerType == typeof(decimal)) From 943342be0197feda7643028763e6f572c118fe10 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Fri, 16 Jul 2021 18:26:46 +0200 Subject: [PATCH 17/37] Bump up the versions --- .../Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.csproj | 2 +- .../Xtz.StronglyTyped.EntityFramework.csproj | 2 +- .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 2 +- .../Xtz.StronglyTyped.SourceGenerator.csproj | 2 +- .../Xtz.StronglyTyped.Swashbuckle.csproj | 2 +- src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index 6fc0fcd..76d15f0 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.AutoFixture - 0.20.1 + 0.21.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index 912201a..d54c7e3 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.Bogus - 0.20.1 + 0.21.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index 44cf58d..73f0c9f 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes - 0.20.1 + 0.21.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index 12075e3..cb69366 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.EntityFramework - 0.20.1 + 0.21.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index 00e489a..cc7f98f 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.NewtonsoftJson - 0.20.1 + 0.21.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index 3ab7386..343acfc 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.SourceGenerator - 0.20.1 + 0.21.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index 0018b02..f0eb2ef 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.Swashbuckle - 0.20.1 + 0.21.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index 3807a6c..fd16958 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped - 0.20.1 + 0.21.0 true snupkg Vlad DX From 50d05fa90f3432156567ed972d575f634ddf0a5d Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 14:12:41 +0200 Subject: [PATCH 18/37] Remove redundant NuGet references --- .../Xtz.StronglyTyped.Benchmark.csproj | 3 --- .../Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj | 1 - .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 1 - 3 files changed, 5 deletions(-) diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj index 17b7f34..7343926 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj @@ -24,9 +24,6 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - all - diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index d54c7e3..69bc7b6 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -44,7 +44,6 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index cc7f98f..eef0478 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -34,7 +34,6 @@ Make compiler your friend, introduce semantics to your code. - all runtime; build; native; contentfiles; analyzers; buildtransitive From fd401c65a918a2a1a14362544154babf4845f2f2 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 14:13:27 +0200 Subject: [PATCH 19/37] Revert NuGet upgrades as they break `Xtz.StronglyTyped.SourceGenerator` --- .../Xtz.StronglyTyped.Benchmark.csproj | 2 +- .../Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj | 4 ++-- .../Xtz.StronglyTyped.SourceGenerator.csproj | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj index 7343926..3f3f8c3 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj @@ -20,7 +20,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj index fb25a89..5592dca 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj @@ -16,8 +16,8 @@ - - + + diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index 343acfc..c789309 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -46,11 +46,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + From d9325ccdd8d9b67512391aee6f926ff2c9983b09 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 14:14:22 +0200 Subject: [PATCH 20/37] Improve code coverage results by marking generated code with attribute `[GeneratedCode]` --- .../Builders/IdFakerSpecimenBuilder.cs | 1 - .../StronglyTypedGenerator.cs | 11 ++++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs index 431ff6e..83904e9 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using AutoFixture.Kernel; diff --git a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs index 397391f..5327fba 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs @@ -123,7 +123,10 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times { var writer = new CodeWriter(); - WriteBanner(writer, workItem, timestamp); + var version = GetType().Assembly.GetName().Version; + var assemblyVersion = $"{version.Major}.{version.Minor}.{version.Revision}.{version.Build}"; + + WriteBanner(writer, workItem, assemblyVersion, timestamp); writer.AppendLine(); using (writer.BeginScope($"namespace {workItem.Namespace}")) @@ -132,6 +135,7 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times writer.AppendLine("using System.Text.Json.Serialization;"); writer.AppendLine(); + writer.AppendLine($"[System.CodeDom.Compiler.GeneratedCode(\"{GetType().FullName}\", \"{assemblyVersion}\")]"); WriteTypeConverter(writer, workItem); writer.AppendLine($"[JsonConverter(typeof(Xtz.StronglyTyped.TypeConverters.StronglyTypedJsonConverter<{workItem.Namespace}.{workItem.TypeName}>))]"); @@ -153,11 +157,8 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times return generatedSourceCode; } - private void WriteBanner(CodeWriter writer, StronglyTypedWorkItem workItem, DateTime timestamp) + private void WriteBanner(CodeWriter writer, StronglyTypedWorkItem workItem, string assemblyVersion, DateTime timestamp) { - var version = GetType().Assembly.GetName().Version; - var assemblyVersion = $"{version.Major}.{version.Minor}.{version.Revision}.{version.Build}"; - writer.AppendLine( $@"//------------------------------------ // From 2fc2e28744d1fbddda1f70e3b68ef9c3ec8ae2ea Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 14:16:30 +0200 Subject: [PATCH 21/37] Bump up the versions --- .../Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.csproj | 2 +- .../Xtz.StronglyTyped.EntityFramework.csproj | 2 +- .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 2 +- .../Xtz.StronglyTyped.SourceGenerator.csproj | 2 +- .../Xtz.StronglyTyped.Swashbuckle.csproj | 2 +- src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index 76d15f0..32a09be 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.AutoFixture - 0.21.0 + 0.22.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index 69bc7b6..ad354d3 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.Bogus - 0.21.0 + 0.22.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index 73f0c9f..a75564a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes - 0.21.0 + 0.22.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index cb69366..ce62666 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.EntityFramework - 0.21.0 + 0.22.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index eef0478..ae8efb6 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.NewtonsoftJson - 0.21.0 + 0.22.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index c789309..f1fea4c 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.SourceGenerator - 0.21.0 + 0.22.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index f0eb2ef..14efde5 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.Swashbuckle - 0.21.0 + 0.22.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index fd16958..bf843bc 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped - 0.21.0 + 0.22.0 true snupkg Vlad DX From 2d44407e2111ee440b61b430cc95ac4bf1d1982a Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 16:55:39 +0200 Subject: [PATCH 22/37] Eliminate compiler and analyzer warnings by applying suggested actions or explicitly suppressing them --- .../Xtz.StronglyTyped.Benchmark/Program.cs | 3 +- .../SystemTextJsonSerializationEmails.cs | 2 +- .../SystemTextJsonSerializationGuidIds.cs | 8 +- .../SystemTextJsonSerializationIntIds.cs | 8 +- .../SystemTextJsonSerializationMacAddress.cs | 13 +-- .../Xtz.StronglyTyped.Playground/Program.cs | 4 +- .../AddressAutoDataTests.cs | 2 - .../CommerceAutoDataTests.cs | 2 - .../CompanyAutoDataTests.cs | 2 - .../FinanceAutoDataTests.cs | 2 - .../IdAutoDataTests.cs | 8 +- .../InternetAutoDataTests.cs | 2 - .../NameAutoDataTests.cs | 4 +- .../NumberAutoDataTests.cs | 2 - .../PhoneAutoDataTests.cs | 2 - .../VehicleAutoDataTests.cs | 2 - .../DbContextTests.cs | 8 +- .../Settings/Country.cs | 3 +- .../DeserializationTests.cs | 7 +- .../SerializationTests.cs | 5 +- .../BasicGeneratorTests.cs | 13 ++- .../CompileAndRunGeneratorTests.cs | 4 +- .../CustomizedGeneratorTests.cs | 89 +++++-------------- .../Basic/CountryIdTests.cs | 4 + .../Basic/StringEqualityOperatorsTests.cs | 1 + .../Basic/UserIdTests.cs | 1 + .../GlobalSuppressions.cs | 11 +++ .../SystemTextJson/DeserializationTests.cs | 7 +- .../SystemTextJson/SerializationTests.cs | 3 + .../TypeConverters/BoolTests.cs | 2 + .../TypeConverters/StringTests.cs | 1 + .../Values/CreationTests.cs | 6 +- .../WebApi/WeatherForecastController.cs | 14 +-- .../WebApiTests.cs | 11 +-- .../Builders/AddressFakerSpecimenBuilder.cs | 2 +- .../Builders/CommerceFakerSpecimenBuilder.cs | 2 +- .../Builders/CompanyFakerSpecimenBuilder.cs | 2 +- .../Builders/FinanceFakerSpecimenBuilder.cs | 2 +- .../Builders/IdFakerSpecimenBuilder.cs | 2 +- .../Builders/InternetFakerSpecimenBuilder.cs | 2 +- .../Builders/NameFakerSpecimenBuilder.cs | 2 +- .../Builders/PhoneFakerSpecimenBuilder.cs | 2 +- .../Builders/VehicleFakerSpecimenBuilder.cs | 2 +- .../Abstract/BaseFakerBuilder.cs | 8 +- .../CommerceFakerBuilder.cs | 2 +- .../IdFakerBuilder.cs | 1 + .../InternetFakerBuilder.cs | 8 ++ .../NameFakerBuilder.cs | 2 +- .../Internet/Email.cs | 6 +- .../Internet/ExampleEmail.cs | 2 +- .../Internet/WebsiteProtocol.cs | 2 + .../Strings/UpperCased.cs | 1 + .../ModelBuilderExtensions.cs | 2 + .../StronglyTypedNewtonsoftConverter.cs | 34 +++---- .../StronglyTypedGenerator.cs | 3 + .../StronglyTypedSchemaFilter.cs | 2 +- src/Xtz.StronglyTyped.sln.DotSettings | 4 + src/Xtz.StronglyTyped/IStronglyTyped.cs | 4 +- src/Xtz.StronglyTyped/StronglyTyped.cs | 8 +- .../StronglyTypedJsonConverter.cs | 2 + 60 files changed, 170 insertions(+), 195 deletions(-) create mode 100644 src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs create mode 100644 src/Xtz.StronglyTyped.sln.DotSettings diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs index 7074e4f..d53975f 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs @@ -3,12 +3,13 @@ namespace Xtz.StronglyTyped.Benchmark { - public class Program + public static class Program { public const int VALUE_COUNT = 1000; public static void Main(string[] args) { + // ReSharper disable once RedundantAssignment var config = DefaultConfig.Instance; #if DEBUG diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationEmails.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationEmails.cs index 933ed48..07c8aba 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationEmails.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationEmails.cs @@ -24,7 +24,7 @@ public SystemTextJsonSerializationEmails() { ////System.Diagnostics.Debugger.Launch(); - var fakerBuilder = new InternetFakerBuilder(true); + var fakerBuilder = new InternetFakerBuilder(); var faker = fakerBuilder.BuildEmailFaker(); _emails = faker.Generate(Program.VALUE_COUNT).ToArray(); diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs index 3ac1dd7..fbacebb 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationGuidIds.cs @@ -10,8 +10,6 @@ namespace Xtz.StronglyTyped.Benchmark [MemoryDiagnoser] public class SystemTextJsonSerializationGuidIds { - private readonly Faker _faker; - private readonly EmployeeGuidId[] _employeeGuidIds; private readonly EmployeeGuidId[] _otherEmployeeGuidIds; @@ -22,10 +20,10 @@ public class SystemTextJsonSerializationGuidIds public SystemTextJsonSerializationGuidIds() { - _faker = new Faker(); + var faker = new Faker(); - _employeeGuidIds = _faker.Generate(Program.VALUE_COUNT).ToArray(); - _otherEmployeeGuidIds = _faker.Generate(Program.VALUE_COUNT).ToArray(); + _employeeGuidIds = faker.Generate(Program.VALUE_COUNT).ToArray(); + _otherEmployeeGuidIds = faker.Generate(Program.VALUE_COUNT).ToArray(); _guids = _employeeGuidIds.Select(x => x.Value).ToArray(); _stronglyTypedGuidStructs = _employeeGuidIds.Select(x => (GuidStructId)x.Value).ToArray(); } diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs index d46a690..303b605 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationIntIds.cs @@ -10,8 +10,6 @@ namespace Xtz.StronglyTyped.Benchmark [MemoryDiagnoser] public class SystemTextJsonSerializationIntIds { - private readonly Faker _faker; - private readonly CompanyIntId[] _companyIntIds; private readonly CompanyIntId[] _otherCompanyIntIds; @@ -22,11 +20,11 @@ public class SystemTextJsonSerializationIntIds public SystemTextJsonSerializationIntIds() { - _faker = new Faker() + var faker = new Faker() .CustomInstantiator(f => new CompanyIntId(Math.Abs(f.Random.Int()))); - _companyIntIds = _faker.Generate(Program.VALUE_COUNT).ToArray(); - _otherCompanyIntIds = _faker.Generate(Program.VALUE_COUNT).ToArray(); + _companyIntIds = faker.Generate(Program.VALUE_COUNT).ToArray(); + _otherCompanyIntIds = faker.Generate(Program.VALUE_COUNT).ToArray(); _ints = _companyIntIds.Select(x => x.Value).ToArray(); _stronglyTypedIntStructs = _companyIntIds.Select(x => (IntStructId)x.Value).ToArray(); } diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs index 7582cd3..3885dd1 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/SystemTextJsonSerializationMacAddress.cs @@ -1,7 +1,6 @@ using System.Linq; using System.Text.Json; using BenchmarkDotNet.Attributes; -using Bogus; using Xtz.StronglyTyped.Benchmark.Models; using Xtz.StronglyTyped.BuiltinTypes.Bogus; using Xtz.StronglyTyped.BuiltinTypes.Internet; @@ -11,10 +10,6 @@ namespace Xtz.StronglyTyped.Benchmark [MemoryDiagnoser] public class SystemTextJsonSerializationMacAddress { - private readonly InternetFakerBuilder _fakerBuilder; - - private readonly Faker _faker; - private readonly MacAddress[] _macAddresses; private readonly MacAddress[] _otherMacAddresses; @@ -27,11 +22,11 @@ public class SystemTextJsonSerializationMacAddress public SystemTextJsonSerializationMacAddress() { - _fakerBuilder = new InternetFakerBuilder(true); - _faker = _fakerBuilder.BuildMacAddressFaker(); + var fakerBuilder = new InternetFakerBuilder(); + var faker = fakerBuilder.BuildMacAddressFaker(); - _macAddresses = _faker.Generate(Program.VALUE_COUNT).ToArray(); - _otherMacAddresses = _faker.Generate(Program.VALUE_COUNT).ToArray(); + _macAddresses = faker.Generate(Program.VALUE_COUNT).ToArray(); + _otherMacAddresses = faker.Generate(Program.VALUE_COUNT).ToArray(); _strings = _macAddresses.Select(x => x.ToString()).ToArray(); _stronglyTypedStrings = _strings.Select(x => (StronglyTypedString)x).ToArray(); _stronglyTypedStructs = _strings.Select(x => (StronglyTypedStringStruct)x).ToArray(); diff --git a/src/Misc/Xtz.StronglyTyped.Playground/Program.cs b/src/Misc/Xtz.StronglyTyped.Playground/Program.cs index 0309950..523cd50 100644 --- a/src/Misc/Xtz.StronglyTyped.Playground/Program.cs +++ b/src/Misc/Xtz.StronglyTyped.Playground/Program.cs @@ -25,7 +25,7 @@ public static void Main() private static void RunAddressFakers() { - var fakerBuilder = new AddressFakerBuilder(true); + var fakerBuilder = new AddressFakerBuilder(); var city = fakerBuilder.BuildCityFaker().Generate(); Console.WriteLine(city); @@ -72,7 +72,7 @@ private static void RunAddressFakers() private static void RunInternetFakers() { - var fakerBuilder = new InternetFakerBuilder(true); + var fakerBuilder = new InternetFakerBuilder(); var absoluteUri = fakerBuilder.BuildAbsoluteUriFaker().Generate(); Console.WriteLine(absoluteUri); diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs index f4dadf5..5a7fb0a 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/AddressAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.Address; diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs index 9fdcdc8..3962f35 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs index e7b2854..7c00182 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CompanyAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs index f3894d3..1432370 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/FinanceAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs index c228714..5097f08 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs @@ -14,8 +14,8 @@ public void ShouldGenerateStronglyTypedValues( EmployeeGuidId employeeGuidId, EmployeeIntId employeeIntId) { - Assert.AreNotEqual(default(Guid), employeeGuidId?.Value); - Assert.AreNotEqual(default(int), employeeIntId?.Value); + Assert.AreNotEqual(default(Guid), employeeGuidId.Value); + Assert.AreNotEqual(default(int), employeeIntId.Value); } [Test] @@ -24,8 +24,8 @@ public void ShouldGenerateStronglyTypedValues_WithCustomSpecimen( EmployeeGuidId employeeGuidId, EmployeeIntId employeeIntId) { - Assert.AreNotEqual(default(Guid), employeeGuidId?.Value); - Assert.AreNotEqual(default(int), employeeIntId?.Value); + Assert.AreNotEqual(default(Guid), employeeGuidId.Value); + Assert.AreNotEqual(default(int), employeeIntId.Value); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs index 16e2a3c..85ad6d2 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs @@ -1,6 +1,4 @@ -using System; using System.Collections.Generic; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs index 409e129..85418cc 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; @@ -34,7 +32,7 @@ public void ShouldGenerateStronglyTypedValues( [StrongAutoData] public void ShouldGenerateStronglyTypedFullName(FullName value) { - Assert.IsFalse(value.FirstName.ToString().IsBogusGeneratedValue()); + Assert.IsFalse(value.FirstName.ToString()!.IsBogusGeneratedValue()); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs index 378563d..49baca9 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NumberAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs index d3efa26..4da26eb 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/PhoneAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs index 4c2d662..f231d9f 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/VehicleAutoDataTests.cs @@ -1,5 +1,3 @@ -using System; -using System.Linq; using NUnit.Framework; using Xtz.StronglyTyped.BogusAutoFixture.UnitTests.Extensions; using Xtz.StronglyTyped.BuiltinTypes.AutoFixture; diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs index 6229250..1acbc70 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs @@ -1,10 +1,8 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using Bogus; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; using Xtz.StronglyTyped.BuiltinTypes.Address; @@ -18,8 +16,6 @@ namespace Xtz.StronglyTyped.EntityFramework.IntegrationTests { public class DbContextTests { - private readonly ServiceProvider _serviceProvider; - private readonly AppDbContext _dbContext; public DbContextTests() @@ -27,8 +23,8 @@ public DbContextTests() var services = new ServiceCollection(); services.AddDbContext(options => options.UseInMemoryDatabase(GetType().Namespace!)); - _serviceProvider = services.BuildServiceProvider(); - _dbContext = _serviceProvider.GetRequiredService(); + var serviceProvider = services.BuildServiceProvider(); + _dbContext = serviceProvider.GetRequiredService(); } [SetUp] diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Settings/Country.cs b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Settings/Country.cs index d20b1e6..0086421 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Settings/Country.cs +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Settings/Country.cs @@ -1,5 +1,4 @@ -using System.Diagnostics.CodeAnalysis; -using Xtz.StronglyTyped.SourceGenerator; +using Xtz.StronglyTyped.SourceGenerator; namespace Xtz.StronglyTyped.IntegrationTests.Settings { diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs index 340faed..e219a95 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs @@ -104,6 +104,7 @@ public void ShouldDeserialize_ToStronglyTypedStringStructAllowEmpty(string value Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) @@ -123,6 +124,7 @@ public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) @@ -142,6 +144,7 @@ public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldDeserialize_ToStronglyTypedGuidIdClass(Guid value) @@ -743,7 +746,7 @@ private static string BuildStringArray(IEnumerable values) stringBuilder.AppendFormat(" {0},\n", BuildString(value)); } - stringBuilder.Append("]"); + stringBuilder.Append(']'); var result = stringBuilder.ToString(); return result; } @@ -757,7 +760,7 @@ private static string BuildNonStringArray(IEnumerable values) stringBuilder.AppendFormat(" {0},\n", BuildNonString(value)); } - stringBuilder.Append("]"); + stringBuilder.Append(']'); var result = stringBuilder.ToString(); return result; } diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs index 5139021..1620a6d 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs @@ -72,7 +72,7 @@ public void ShouldSerialize_GivenStronglyTypedStringClassAllowEmpty(string value // Act - var result = JsonConvert.SerializeObject(value, JSON_SERIALIZER_SETTINGS); + var result = JsonConvert.SerializeObject(stronglyTyped, JSON_SERIALIZER_SETTINGS); // Assert @@ -99,6 +99,7 @@ public void ShouldSerialize_GivenStronglyTypedStringStructAllowEmpty(string valu Assert.AreEqual(json, result); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) @@ -117,6 +118,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) Assert.AreEqual(json, result); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) @@ -135,6 +137,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) Assert.AreEqual(json, result); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldSerialize_GivenStronglyTypedGuidIdClass(Guid value) diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs index 6093552..0739de9 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs @@ -1,4 +1,3 @@ -using System.Diagnostics; using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -101,7 +100,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); //// Assert @@ -147,7 +146,7 @@ public class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); //// Assert @@ -183,7 +182,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); //// Assert @@ -216,7 +215,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); //// Assert @@ -252,7 +251,7 @@ public partial record City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); //// Assert @@ -292,7 +291,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(new StronglyTypedGenerator()); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics); //// Assert diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs index 68477c6..00c03a5 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs @@ -64,7 +64,7 @@ public partial class DegreesCelsius3 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); var compiledBytes = CompileBytes(outputCompilation); @@ -115,7 +115,7 @@ public class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs index 73f484b..f9144c6 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs @@ -45,10 +45,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -102,10 +99,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -159,10 +153,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -216,10 +207,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -330,10 +318,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -388,10 +373,7 @@ public partial class CityEmail2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -446,10 +428,7 @@ public partial class CityIpAddress2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -504,10 +483,7 @@ public partial class CityMacAddress2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -561,10 +537,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -618,10 +591,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -675,10 +645,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -732,10 +699,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -789,10 +753,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -846,10 +807,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -903,10 +861,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -960,10 +915,7 @@ public partial class CityUri2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - - - var compiledBytes = CompileBytes(outputCompilation); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); [ExcludeFromCodeCoverage] void Action() @@ -1217,8 +1169,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); var compiledBytes = CompileBytes(outputCompilation); @@ -1267,7 +1218,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); var compiledBytes = CompileBytes(outputCompilation); @@ -1317,7 +1268,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); var compiledBytes = CompileBytes(outputCompilation); @@ -1367,7 +1318,7 @@ public partial class City2 GeneratorDriver driver = CSharpGeneratorDriver.Create(generator); // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls - driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); + driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); var compiledBytes = CompileBytes(outputCompilation); diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs index c973c77..fb1e211 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs @@ -1,5 +1,6 @@ using NUnit.Framework; using System; +using System.Diagnostics.CodeAnalysis; namespace Xtz.StronglyTyped.UnitTests.Basic { @@ -32,6 +33,7 @@ public void CountryId_ShouldThrow_GivenInvalidValue(int value) //// Act + [ExcludeFromCodeCoverage] // ReSharper disable once ObjectCreationAsStatement void Action() => new CountryId(value); @@ -50,6 +52,8 @@ public void CountryIdImplicitOperator_ShouldThrow_GivenDifferentType(object valu //// Act + [ExcludeFromCodeCoverage] + // ReSharper disable once UnusedLocalFunctionReturnValue CountryId Action() => (CountryId)value; //// Assert diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs index 6ba281c..4888390 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs @@ -16,6 +16,7 @@ public void EqualsOperator_ShouldBeTrue_ForSameStronglyTypedValues() //// Assert + // ReSharper disable once EqualExpressionComparison #pragma warning disable CS1718 // Comparison made to same variable Assert.IsTrue(value == value); #pragma warning restore CS1718 // Comparison made to same variable diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs index 1d863e2..49134e9 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs @@ -32,6 +32,7 @@ public void UserId_ShouldThrow_GivenInvalidValue() //// Act [ExcludeFromCodeCoverage] + // ReSharper disable once ObjectCreationAsStatement void Action() => new UserId(value); //// Assert diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs new file mode 100644 index 0000000..96317f4 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs @@ -0,0 +1,11 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.CountryIdTests.CountryId_ShouldThrow_GivenInvalidValue(System.Int32)")] +[assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Values.CreationTests.ShouldThrow_GivenNullOrEmpty(System.String)")] +[assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.UserIdTests.UserId_ShouldThrow_GivenInvalidValue")] +[assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Values.CreationTests.ShouldThrow_GivenNull_ForAllowEmpty")] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs index ee263dd..e017589 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs @@ -101,6 +101,7 @@ public void ShouldDeserialize_ToStronglyTypedStringStructAllowEmpty(string value Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) @@ -120,6 +121,7 @@ public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) @@ -139,6 +141,7 @@ public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldDeserialize_ToStronglyTypedGuidIdClass(Guid value) @@ -758,7 +761,7 @@ private static string BuildStringArray(IEnumerable values) stringBuilder.AppendFormat(" {0},\n", BuildString(value)); } - stringBuilder.Append("]"); + stringBuilder.Append(']'); var result = stringBuilder.ToString(); return result; } @@ -772,7 +775,7 @@ private static string BuildNonStringArray(IEnumerable values) stringBuilder.AppendFormat("{0},\n", BuildNonString(value)); } - stringBuilder.Append("]"); + stringBuilder.Append(']'); var result = stringBuilder.ToString(); return result; } diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs index a573eee..a1e2cce 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs @@ -89,6 +89,7 @@ public void ShouldSerialize_GivenStronglyTypedStringStructAllowEmpty(string valu Assert.AreEqual(json, result); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) @@ -107,6 +108,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) Assert.AreEqual(json, result); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("57c2a2d3-99cc-4468-998e-f3a3abe089ca")] public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) @@ -125,6 +127,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) Assert.AreEqual(json, result); } + // ReSharper disable once NUnit.IncorrectArgumentType [Test] [TestCase("61f6e72c-8db3-4a70-89b6-c3d07dbcce11")] public void ShouldSerialize_GivenStronglyTypedGuidIdClass(Guid value) diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs index 118235f..f7bc18c 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/BoolTests.cs @@ -14,10 +14,12 @@ public void TypeConverter_ShouldParseStronglyTyped_GivenBool() var strongType = typeof(StronglyTypedBool); var typeConverter = TypeDescriptor.GetConverter(strongType); + // ReSharper disable once ConditionIsAlwaysTrueOrFalse var expected = new StronglyTypedBool(value); //// Act + // ReSharper disable once ConditionIsAlwaysTrueOrFalse var result = typeConverter.ConvertFrom(value) as StronglyTypedBool; //// Assert diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/StringTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/StringTests.cs index ed3f409..a76b024 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/StringTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/StringTests.cs @@ -37,6 +37,7 @@ public void TypeConverter_ShouldReturnNull_GivenNull() //// Act + // ReSharper disable once ExpressionIsAlwaysNull var result = typeConverter.ConvertFrom(value); //// Assert diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs index d29a77d..1b4fa18 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/CreationTests.cs @@ -57,6 +57,7 @@ public void ShouldThrow_GivenNullOrEmpty(string country) //// Act + // ReSharper disable once ObjectCreationAsStatement [ExcludeFromCodeCoverage] void Action() => new Country(country); @@ -74,6 +75,9 @@ public void ShouldThrow_GivenNull_ForAllowEmpty() //// Act + // ReSharper disable once ObjectCreationAsStatement + // ReSharper disable once ExpressionIsAlwaysNull + // ReSharper disable once AssignNullToNotNullAttribute [ExcludeFromCodeCoverage] void Action() => new StronglyTypedStringAllowEmpty(value); @@ -97,7 +101,7 @@ public void ShouldCreate_GivenEmpty_ForAllowEmpty() Assert.NotNull(result); Assert.IsEmpty(result.Value); - Assert.IsEmpty(value.ToString()); + Assert.IsEmpty(value); } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs index 0f91c35..3060c00 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using Xtz.StronglyTyped.BuiltinTypes.Address; namespace Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApi @@ -11,18 +10,11 @@ namespace Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApi [Route("[controller]")] public class WeatherForecastController : ControllerBase { - private static readonly string[] Summaries = new[] + private static readonly string[] SUMMARIES = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - [HttpGet] public IEnumerable Get() { @@ -31,7 +23,7 @@ public IEnumerable Get() { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] + Summary = SUMMARIES[rng.Next(SUMMARIES.Length)] }) .ToArray(); } @@ -46,7 +38,7 @@ public IEnumerable StronglyTypedGet() City = new City("Amsterdam"), Date = DateTime.Now.AddDays(index), TemperatureC = (DegreesCelsius)rng.Next(-20, 55), - Summary = Summaries[rng.Next(Summaries.Length)] + Summary = SUMMARIES[rng.Next(SUMMARIES.Length)] }) .ToArray(); } diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs index 8c6dc91..8fa5423 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs @@ -49,10 +49,11 @@ public async Task ShouldDeserialize_ForStandardEndpoint_WhenNewtonsoftUsed() Assert.NotNull(response.Content); Country country = null; - var x = JsonConvert.SerializeObject(country); + // ReSharper disable once ExpressionIsAlwaysNull + var _ = JsonConvert.SerializeObject(country); var responseStr = await response.Content.ReadAsStringAsync(); - var responseObject = JsonConvert.DeserializeObject>(responseStr, _jsonSerializerSettings); + var __ = JsonConvert.DeserializeObject>(responseStr, _jsonSerializerSettings); } [Test] @@ -66,7 +67,7 @@ public async Task ShouldDeserialize_ForStandardEndpoint_WhenSystemTextJsonUsed() Assert.NotNull(response.Content); var responseStr = await response.Content.ReadAsStringAsync(); - var responseObject = System.Text.Json.JsonSerializer.Deserialize>(responseStr); + var _ = System.Text.Json.JsonSerializer.Deserialize>(responseStr); } [Test] @@ -91,7 +92,7 @@ public async Task ShouldDeserialize_ForStronglyTypedEndpoint_WhenNewtonsoftUsed( Assert.NotNull(response.Content); var responseStr = await response.Content.ReadAsStringAsync(); - var responseObject = JsonConvert.DeserializeObject>(responseStr, _jsonSerializerSettings); + var _ = JsonConvert.DeserializeObject>(responseStr, _jsonSerializerSettings); } [Test] @@ -105,7 +106,7 @@ public async Task ShouldDeserialize_ForStronglyTypedEndpoint_WhenSystemTextJsonU Assert.NotNull(response.Content); var responseStr = await response.Content.ReadAsStringAsync(); - var responseObject = System.Text.Json.JsonSerializer.Deserialize>(responseStr); + var _ = System.Text.Json.JsonSerializer.Deserialize>(responseStr); } public void Dispose() diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/AddressFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/AddressFakerSpecimenBuilder.cs index d4ff81c..1ee859a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/AddressFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/AddressFakerSpecimenBuilder.cs @@ -9,7 +9,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders // TODO: Add integration tests to check whether all types are included to the builders public class AddressFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly AddressFakerBuilder _builder = new(true); + private readonly AddressFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CommerceFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CommerceFakerSpecimenBuilder.cs index f2b16c9..6950be9 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CommerceFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CommerceFakerSpecimenBuilder.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders { public class CommerceFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly CommerceFakerBuilder _builder = new(true); + private readonly CommerceFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CompanyFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CompanyFakerSpecimenBuilder.cs index 7abe672..505a42c 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CompanyFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/CompanyFakerSpecimenBuilder.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders { public class CompanyFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly CompanyFakerBuilder _builder = new(true); + private readonly CompanyFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/FinanceFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/FinanceFakerSpecimenBuilder.cs index fcad81f..d2cc764 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/FinanceFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/FinanceFakerSpecimenBuilder.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders { public class FinanceFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly FinanceFakerBuilder _builder = new(true); + private readonly FinanceFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs index 83904e9..0571abc 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/IdFakerSpecimenBuilder.cs @@ -12,7 +12,7 @@ public class IdFakerSpecimenBuilder : ISpecimenBuilder { private NoSpecimen NoSpecimen { get; } = new(); - private readonly IdFakerBuilder _builder = new(true); + private readonly IdFakerBuilder _builder = new(); private readonly MethodInfo _buildGuidIdFakerMethod; diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/InternetFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/InternetFakerSpecimenBuilder.cs index 6bdd8c3..d8ef3d2 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/InternetFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/InternetFakerSpecimenBuilder.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders { public class InternetFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly InternetFakerBuilder _builder = new(true); + private readonly InternetFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/NameFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/NameFakerSpecimenBuilder.cs index e3c6e18..3d0294a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/NameFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/NameFakerSpecimenBuilder.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders { public class NameFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly NameFakerBuilder _builder = new(true); + private readonly NameFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/PhoneFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/PhoneFakerSpecimenBuilder.cs index 522f3b6..2743aa6 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/PhoneFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/PhoneFakerSpecimenBuilder.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders { public class PhoneFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly PhoneFakerBuilder _builder = new(true); + private readonly PhoneFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/VehicleFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/VehicleFakerSpecimenBuilder.cs index d2fb57c..fd87aa8 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/VehicleFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Builders/VehicleFakerSpecimenBuilder.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.AutoFixture.Builders { public class VehicleFakerSpecimenBuilder : BaseFakerSpecimenBuilder { - private readonly VehicleFakerBuilder _builder = new(true); + private readonly VehicleFakerBuilder _builder = new(); protected override Dictionary> FakerFactories => new() { diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Abstract/BaseFakerBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Abstract/BaseFakerBuilder.cs index 59818b0..574e8ca 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Abstract/BaseFakerBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Abstract/BaseFakerBuilder.cs @@ -6,9 +6,9 @@ namespace Xtz.StronglyTyped.BuiltinTypes.Bogus { public abstract class BaseFakerBuilder { - private static readonly MemoryCacheEntryOptions MemoryCacheEntryOptions = new() { Size = 1 }; + private static readonly MemoryCacheEntryOptions MEMORY_CACHE_ENTRY_OPTIONS = new() { Size = 1 }; - private static readonly MemoryCache Cache = new(new MemoryCacheOptions { SizeLimit = 500 }); + private static readonly MemoryCache CACHE = new(new MemoryCacheOptions { SizeLimit = 500 }); private readonly bool _useFakerCache; @@ -30,14 +30,14 @@ public Faker GetFaker(Func> fakerFactory, string? ? $"{typeof(TValue).FullName}_{cacheKey}" : typeof(TValue).FullName; - var cachedValue = Cache.Get(key); + var cachedValue = CACHE.Get(key); if (cachedValue != null) { return (cachedValue as Faker)!; } var result = fakerFactory(); - Cache.Set(key, result, MemoryCacheEntryOptions); + CACHE.Set(key, result, MEMORY_CACHE_ENTRY_OPTIONS); return result; } } diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CommerceFakerBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CommerceFakerBuilder.cs index b1fb556..0b38f21 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CommerceFakerBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CommerceFakerBuilder.cs @@ -98,7 +98,7 @@ public Faker BuildProductMaterialFaker() public Faker BuildProductFullNameFaker() { var result = GetFaker(() => new Faker() - .CustomInstantiator(f => + .CustomInstantiator(_ => { var productAdjective = BuildProductAdjectiveFaker().Generate(); var productMaterial = BuildProductMaterialFaker().Generate(); diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/IdFakerBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/IdFakerBuilder.cs index 6237972..88bc889 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/IdFakerBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/IdFakerBuilder.cs @@ -29,6 +29,7 @@ public Faker BuildIntIdFaker() where TIntId : IntId { var result = GetFaker(() => new Faker() + // ReSharper disable once RedundantArgumentDefaultValue .CustomInstantiator(f => (TIntId)Activator.CreateInstance(typeof(TIntId), f.Random.Int(1, int.MaxValue)))); return result; } diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/InternetFakerBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/InternetFakerBuilder.cs index 198f0f3..919253e 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/InternetFakerBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/InternetFakerBuilder.cs @@ -22,7 +22,9 @@ public Faker BuildAbsoluteUriFaker(WebsiteProtocol? protocol = null var cacheKey = $"{protocol}|{domain}|{fileExt}"; var result = GetFaker(() => new Faker() +#pragma warning disable 8604 .CustomInstantiator(f => new AbsoluteUri(f.Internet.UrlWithPath(protocol, domain, fileExt))), cacheKey); +#pragma warning restore 8604 return result; } @@ -64,7 +66,9 @@ public Faker BuildEmailFaker(FirstName? firstName = null, LastName? lastN var cacheKey = $"{firstName}|{lastName}|{provider}|{uniqueSuffix}"; var result = GetFaker(() => new Faker() +#pragma warning disable CS8604 // Possible null reference argument. .CustomInstantiator(f => new Email(f.Internet.Email(firstName, lastName, provider, uniqueSuffix))), cacheKey); +#pragma warning restore CS8604 // Possible null reference argument. return result; } @@ -78,7 +82,9 @@ public Faker BuildExampleEmailFaker(FirstName? firstName = null, L var cacheKey = $"{firstName}|{lastName}"; var result = GetFaker(() => new Faker() +#pragma warning disable CS8604 // Possible null reference argument. .CustomInstantiator(f => new ExampleEmail(f.Internet.ExampleEmail(firstName, lastName))), cacheKey); +#pragma warning restore CS8604 // Possible null reference argument. return result; } @@ -165,7 +171,9 @@ public Faker BuildUserNameFaker(FirstName? firstName = null, LastName? var cacheKey = $"{firstName}|{lastName}"; var result = GetFaker(() => new Faker() +#pragma warning disable CS8604 // Possible null reference argument. .CustomInstantiator(f => new UserName(f.Internet.UserName(firstName, lastName))), cacheKey); +#pragma warning restore CS8604 // Possible null reference argument. return result; } diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/NameFakerBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/NameFakerBuilder.cs index 2c6ce12..6c65e1c 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/NameFakerBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/NameFakerBuilder.cs @@ -20,7 +20,7 @@ public Faker BuildDisplayNameFaker(string locale = "en", Gender? ge var cacheKey = $"{locale}|{gender}"; var result = GetFaker(() => new Faker() - .CustomInstantiator(f => + .CustomInstantiator(_ => { var fullName = BuildFullNameFaker(locale, gender).Generate(); return new DisplayName($"{fullName.FirstName} {fullName.LastName}"); diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/Email.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/Email.cs index 851aba4..03d8faf 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/Email.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/Email.cs @@ -11,14 +11,14 @@ namespace Xtz.StronglyTyped.BuiltinTypes.Internet [StrongType(typeof(MailAddress))] public partial class Email : IHasMailAddress { - public static readonly IReadOnlyCollection IllegalChars = new[] { ' ', '<', '>' }; + public static readonly IReadOnlyCollection ILLEGAL_CHARS = new[] { ' ', '<', '>' }; - internal static readonly string IllegalCharsString = $"'{string.Join("', '", IllegalChars)}"; + internal static readonly string ILLEGAL_CHARS_STRING = $"'{string.Join("', '", ILLEGAL_CHARS)}"; public Email(string value) : this(new MailAddress(value)) { - if (value.Any(c => IllegalChars.Contains(c))) Throw($"Characters {IllegalCharsString} are not allowed in email"); + if (value.Any(c => ILLEGAL_CHARS.Contains(c))) Throw($"Characters {ILLEGAL_CHARS_STRING} are not allowed in email"); } } } diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/ExampleEmail.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/ExampleEmail.cs index ff94dda..d2869f0 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/ExampleEmail.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/ExampleEmail.cs @@ -13,7 +13,7 @@ public partial class ExampleEmail : IHasMailAddress public ExampleEmail(string value) : base(new MailAddress(value)) { - if (value.Any(c => Email.IllegalChars.Contains(c))) Throw($"Characters {Email.IllegalCharsString} are not allowed in email"); + if (value.Any(c => Email.ILLEGAL_CHARS.Contains(c))) Throw($"Characters {Email.ILLEGAL_CHARS_STRING} are not allowed in email"); } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/WebsiteProtocol.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/WebsiteProtocol.cs index f044998..149bda3 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/WebsiteProtocol.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/WebsiteProtocol.cs @@ -13,11 +13,13 @@ public partial class WebsiteProtocol /// /// HTTP protocol. /// + // ReSharper disable once UnusedMember.Global public static readonly WebsiteProtocol HTTP = new WebsiteProtocol("http"); /// /// HTTPS protocol. /// + // ReSharper disable once UnusedMember.Global public static readonly WebsiteProtocol HTTPS = new WebsiteProtocol("https"); protected override bool IsValid(string value) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Strings/UpperCased.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Strings/UpperCased.cs index 6b14f6d..49c0d85 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Strings/UpperCased.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Strings/UpperCased.cs @@ -6,6 +6,7 @@ public class UpperCased : StronglyTyped { public UpperCased(string value) + // ReSharper disable once ConstantConditionalAccessQualifier #pragma warning disable 8604 : base(value?.ToUpperInvariant()) #pragma warning restore 8604 diff --git a/src/Xtz.StronglyTyped.EntityFramework/ModelBuilderExtensions.cs b/src/Xtz.StronglyTyped.EntityFramework/ModelBuilderExtensions.cs index 5cba977..7fedbf8 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/ModelBuilderExtensions.cs +++ b/src/Xtz.StronglyTyped.EntityFramework/ModelBuilderExtensions.cs @@ -56,6 +56,8 @@ public static void RegisterStronglyTypedConverters(this ModelBuilder modelBuilde .Entity(entityType.Name) .Property(propertyInfo.Name) .HasConversion(valueConverter); + + // ReSharper disable once RedundantJumpStatement continue; } diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs index 96a1017..f510641 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Xml; using Newtonsoft.Json; @@ -7,6 +8,7 @@ namespace Xtz.StronglyTyped.NewtonsoftJson { + [SuppressMessage("Style", "IDE0034:Simplify 'default' expression", Justification = "Vlad DX: Reviewed (more readable code)")] public class StronglyTypedNewtonsoftConverter : JsonConverter { public override bool CanConvert(Type objectType) @@ -75,76 +77,76 @@ private IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter type { var value = unchecked((byte)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(decimal)) { var value = (decimal)longValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(double)) { var value = (double)longValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(float)) { var value = (float)longValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(int)) { var value = unchecked((int)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(long)) { var result = (typeConverter as TypeConverter)!.ConvertFrom(longValue); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(sbyte)) { var value = unchecked((sbyte)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(short)) { var value = unchecked((short)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(uint)) { var value = unchecked((uint)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(ulong)) { var value = unchecked((ulong)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(ushort)) { var value = unchecked((ushort)longValue); var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(long)}' to '{typeConverter.StrongType.FullName}'"); @@ -155,21 +157,21 @@ private IStronglyTyped ConvertFromDouble(double doubleValue, ICustomTypeConverte if (typeConverter.InnerType == typeof(double)) { var result = (typeConverter as TypeConverter)!.ConvertFrom(doubleValue); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(decimal)) { var value = (decimal)doubleValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(float)) { var value = (float)doubleValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(double)}' to '{typeConverter.StrongType.FullName}'"); @@ -181,14 +183,14 @@ private IStronglyTyped ConvertFromBigInt(BigInteger bigIntValue, ICustomTypeConv { var value = (decimal)bigIntValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } if (typeConverter.InnerType == typeof(ulong)) { var value = (ulong)bigIntValue; var result = (typeConverter as TypeConverter)!.ConvertFrom(value); - return (IStronglyTyped)result; + return (IStronglyTyped)result!; } throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(BigInteger)}' to '{typeConverter.StrongType.FullName}'"); diff --git a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs index 5327fba..0df39b6 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs @@ -128,6 +128,8 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times WriteBanner(writer, workItem, assemblyVersion, timestamp); + writer.AppendLine("//// ReSharper disable All"); + writer.AppendLine("#pragma warning disable"); writer.AppendLine(); using (writer.BeginScope($"namespace {workItem.Namespace}")) { @@ -463,6 +465,7 @@ private static void TryWriteToString(CodeWriter writer, StronglyTypedWorkItem wo writer.AppendLine("return $\"{Value}\";"); } writer.AppendLine(); + // ReSharper disable once RedundantJumpStatement return; } } diff --git a/src/Xtz.StronglyTyped.Swashbuckle/StronglyTypedSchemaFilter.cs b/src/Xtz.StronglyTyped.Swashbuckle/StronglyTypedSchemaFilter.cs index 4789bb0..bebbb3e 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/StronglyTypedSchemaFilter.cs +++ b/src/Xtz.StronglyTyped.Swashbuckle/StronglyTypedSchemaFilter.cs @@ -16,7 +16,7 @@ namespace Xtz.StronglyTyped.Swashbuckle public class StronglyTypedSchemaFilter : ISchemaFilter { // TODO: Replace strings by strong types - private readonly Dictionary TYPE_MAPPING = new() + private static readonly Dictionary TYPE_MAPPING = new() { { typeof(string), ("string", "string") }, { typeof(bool), ("boolean", null) }, diff --git a/src/Xtz.StronglyTyped.sln.DotSettings b/src/Xtz.StronglyTyped.sln.DotSettings new file mode 100644 index 0000000..4b661fa --- /dev/null +++ b/src/Xtz.StronglyTyped.sln.DotSettings @@ -0,0 +1,4 @@ + + <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> + <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/IStronglyTyped.cs b/src/Xtz.StronglyTyped/IStronglyTyped.cs index bd1c452..9459e11 100644 --- a/src/Xtz.StronglyTyped/IStronglyTyped.cs +++ b/src/Xtz.StronglyTyped/IStronglyTyped.cs @@ -1,6 +1,4 @@ -using System; - -namespace Xtz.StronglyTyped +namespace Xtz.StronglyTyped { /// /// Base interface for strongly-typed classes. diff --git a/src/Xtz.StronglyTyped/StronglyTyped.cs b/src/Xtz.StronglyTyped/StronglyTyped.cs index 4644645..6dbf181 100644 --- a/src/Xtz.StronglyTyped/StronglyTyped.cs +++ b/src/Xtz.StronglyTyped/StronglyTyped.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; namespace Xtz.StronglyTyped { @@ -31,6 +30,7 @@ private void ThrowIfInvalid(TInnerType value) if (ShouldThrowIfEmpty() && !(typeof(TInnerType).IsPrimitive || typeof(TInnerType) == typeof(decimal))) { + // ReSharper disable once RedundantNameQualifier if (Equals(value, string.Empty) || object.Equals(value, default(TInnerType))) { Throw($"'{value}' value is invalid for type {GetType()}"); @@ -44,6 +44,7 @@ private void ThrowIfInvalid(TInnerType value) } // Bypass. Can be overriden + // ReSharper disable once VirtualMemberNeverOverridden.Global protected virtual bool ShouldThrowIfEmpty() => true; protected void Throw(string errorMessage) => throw new InvalidValueException(GetType(), errorMessage); @@ -53,6 +54,7 @@ private void ThrowIfInvalid(TInnerType value) public override bool Equals(object? obj) { + // ReSharper disable once RedundantNameQualifier if (object.ReferenceEquals(this, obj)) { return true; @@ -78,11 +80,13 @@ public override bool Equals(object? obj) public override int GetHashCode() { + // ReSharper disable once ConstantConditionalAccessQualifier return Value?.GetHashCode() ?? default; } public override string ToString() { + // ReSharper disable once ConstantConditionalAccessQualifier return Value?.ToString() ?? string.Empty; } diff --git a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs index 8d63879..950b09d 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs @@ -1,11 +1,13 @@ using System; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; using System.Xml; namespace Xtz.StronglyTyped.TypeConverters { + [SuppressMessage("Style", "IDE0034:Simplify 'default' expression", Justification = "Vlad DX: Reviewed (more readable code)")] public class StronglyTypedJsonConverter : JsonConverter where TStronglyTyped : IStronglyTyped { From d2f4e3f7db2105f12074fb94554b8dec271a4220 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 16:56:15 +0200 Subject: [PATCH 23/37] Fix assembly conflict warnings --- .../Xtz.StronglyTyped.Benchmark.csproj | 10 ++-------- .../Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj | 1 + 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj index 3f3f8c3..4dd08cd 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj @@ -2,7 +2,7 @@ Exe - net5 + netstandard2.1 @@ -12,6 +12,7 @@ + @@ -19,11 +20,4 @@ - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj index 9ee43d8..a96bb5d 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj @@ -11,6 +11,7 @@ + From bac56741c6a8f8487df700d3fbc62f1e7aa69eb0 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 16:56:42 +0200 Subject: [PATCH 24/37] Minor project files cleanup --- .../Xtz.StronglyTyped.Playground.csproj | 18 ------------------ ...ped.EntityFramework.IntegrationTests.csproj | 5 ----- ...ronglyTyped.Api_3_1.IntegrationTests.csproj | 3 --- .../Xtz.StronglyTyped.SourceGenerator.csproj | 3 --- 4 files changed, 29 deletions(-) diff --git a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj index 6c92284..6186589 100644 --- a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj +++ b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj @@ -16,22 +16,11 @@ - - false - - True $(BaseIntermediateOutputPath)\GeneratedFiles - - @@ -44,11 +33,4 @@ - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj index 6154438..11c78a0 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj @@ -2,8 +2,6 @@ net5.0 - - false @@ -20,9 +18,6 @@ - - - diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj index a96bb5d..096ee2a 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj +++ b/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj @@ -17,9 +17,6 @@ - - - diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index f1fea4c..8b42788 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -51,9 +51,6 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - - From f2c99f478bbb61c4217ff848a8b1bba3b5e8d505 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 16:59:55 +0200 Subject: [PATCH 25/37] Refactor generator - Add usings - Remove redundant namespaces usage - Split `IEquatable.Equals()` implementation into two (struct, class) - Handle `nullable` --- .../StronglyTypedGenerator.cs | 80 ++++++++++++------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs index 0df39b6..b1d3034 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs @@ -28,9 +28,9 @@ public class StronglyTypedGenerator : IStronglyTypedGenerator { typeof(uint), new("uint", "uint.Parse(value)") }, { typeof(ulong), new("ulong", "ulong.Parse(value)") }, { typeof(ushort), new("ushort", "ushort.Parse(value)") }, - { typeof(DateTime), new(typeof(DateTime).FullName, "System.DateTime.Parse(value)") }, - { typeof(TimeSpan), new(typeof(TimeSpan).FullName, "System.TimeSpan.Parse(value)") }, - { typeof(Guid), new(typeof(Guid).FullName, "System.Guid.Parse(value)") }, + { typeof(DateTime), new(typeof(DateTime).FullName, "DateTime.Parse(value)") }, + { typeof(TimeSpan), new(typeof(TimeSpan).FullName, "TimeSpan.Parse(value)") }, + { typeof(Guid), new(typeof(Guid).FullName, "Guid.Parse(value)") }, // Skipping `MailAddress` as it has `(string value)` constructor { typeof(IPAddress), new(typeof(IPAddress).FullName, "System.Net.IPAddress.Parse(value)") }, { typeof(PhysicalAddress), new(typeof(PhysicalAddress).FullName, "System.Net.NetworkInformation.PhysicalAddress.Parse(value)") }, @@ -130,16 +130,22 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times writer.AppendLine("//// ReSharper disable All"); writer.AppendLine("#pragma warning disable"); + writer.AppendLine("#nullable enable"); + writer.AppendLine("#nullable disable warnings"); writer.AppendLine(); + using (writer.BeginScope($"namespace {workItem.Namespace}")) { + writer.AppendLine("using System;"); writer.AppendLine("using System.ComponentModel;"); writer.AppendLine("using System.Text.Json.Serialization;"); + writer.AppendLine("using Xtz.StronglyTyped;"); + writer.AppendLine("using Xtz.StronglyTyped.TypeConverters;"); writer.AppendLine(); writer.AppendLine($"[System.CodeDom.Compiler.GeneratedCode(\"{GetType().FullName}\", \"{assemblyVersion}\")]"); WriteTypeConverter(writer, workItem); - writer.AppendLine($"[JsonConverter(typeof(Xtz.StronglyTyped.TypeConverters.StronglyTypedJsonConverter<{workItem.Namespace}.{workItem.TypeName}>))]"); + writer.AppendLine($"[JsonConverter(typeof(StronglyTypedJsonConverter<{workItem.TypeName}>))]"); switch (workItem.Kind) { @@ -149,10 +155,12 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times case WorkItemKind.Struct: WriteStruct(writer, workItem); break; + // ReSharper disable once RedundantCaseLabel case WorkItemKind.Unknown: default: throw new CodeWriterException($"Not supported work item type '{workItem.Kind}'"); } + } var generatedSourceCode = writer.ToString(); @@ -162,15 +170,16 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times private void WriteBanner(CodeWriter writer, StronglyTypedWorkItem workItem, string assemblyVersion, DateTime timestamp) { writer.AppendLine( - $@"//------------------------------------ -// -// Type `{workItem.Namespace}.{workItem.TypeName}` -// -// This code was generated by generator '{GetType().FullName}' -// Assembly Version: {assemblyVersion} -// Generation timestamp: {timestamp:s}Z -// -//------------------------------------"); + $@"/* + + Type `{workItem.Namespace}.{workItem.TypeName}` + + This code was generated by generator '{GetType().FullName}' + Assembly Version: {assemblyVersion} + Generation timestamp: {timestamp:s}Z + +*/"); + writer.AppendLine(); } private void WriteTypeConverter(CodeWriter writer, StronglyTypedWorkItem workItem) @@ -178,8 +187,8 @@ private void WriteTypeConverter(CodeWriter writer, StronglyTypedWorkItem workIte var valueType = workItem.InnerType; var typeConverter = valueType switch { - var t when t == typeof(string) => $"[TypeConverter(typeof(Xtz.StronglyTyped.TypeConverters.StringTypeConverter<{workItem.TypeName}>))]", - _ => $"[TypeConverter(typeof(Xtz.StronglyTyped.TypeConverters.TypeConverter<{workItem.TypeName}, {workItem.InnerType.FullName}>))]", + var t when t == typeof(string) => $"[TypeConverter(typeof(StringTypeConverter<{workItem.TypeName}>))]", + _ => $"[TypeConverter(typeof(TypeConverter<{workItem.TypeName}, {workItem.InnerType.FullName}>))]", }; writer.AppendLine(typeConverter); @@ -188,14 +197,14 @@ private void WriteTypeConverter(CodeWriter writer, StronglyTypedWorkItem workIte private void WriteClass(CodeWriter writer, StronglyTypedWorkItem workItem) { var baseType = !workItem.ExtraFeatures.HasBaseClass - ? $" Xtz.StronglyTyped.StronglyTyped<{workItem.InnerType.FullName}>," + ? $" StronglyTyped<{workItem.InnerType.FullName}>," : String.Empty; var sealedStr = workItem.ExtraFeatures.IsAbstract ? string.Empty : " sealed"; - using (writer.BeginScope($"public{sealedStr} partial class {workItem.TypeName} :{baseType} System.IEquatable<{workItem.TypeName}>")) + using (writer.BeginScope($"public{sealedStr} partial class {workItem.TypeName} :{baseType} IEquatable<{workItem.TypeName}>")) { WriteXmlSummary(writer, $"Initializes a new instance of the class."); WriteXmlParam(writer, "value", "Inner value"); @@ -212,7 +221,7 @@ private void WriteClass(CodeWriter writer, StronglyTypedWorkItem workItem) TryWriteToString(writer, workItem); - WriteEquatableEquals(writer, workItem); + WriteClassEquatableEquals(writer, workItem); writer.AppendLine(); WriteExplicitOperatorToStrongType(writer, workItem); @@ -235,7 +244,7 @@ private void TryWriteAllowEmpty(CodeWriter writer, StronglyTypedWorkItem workIte private void WriteStruct(CodeWriter writer, StronglyTypedWorkItem workItem) { writer.AppendLine("[System.Diagnostics.DebuggerDisplay(\"[struct {GetType().Name,nq}] {Value}\")]"); - using (writer.BeginScope($"public readonly partial struct {workItem.TypeName} : Xtz.StronglyTyped.IStronglyTyped<{workItem.InnerType.FullName}>, System.IEquatable<{workItem.TypeName}>")) + using (writer.BeginScope($"public readonly partial struct {workItem.TypeName} : IStronglyTyped<{workItem.InnerType.FullName}>, IEquatable<{workItem.TypeName}>")) { WriteXmlSummary(writer, "Default instance."); writer.AppendLine($"public static readonly {workItem.TypeName} Default;"); @@ -261,7 +270,7 @@ private void WriteStruct(CodeWriter writer, StronglyTypedWorkItem workItem) TryWriteToString(writer, workItem); - WriteEquatableEquals(writer, workItem); + WriteStructEquatableEquals(writer, workItem); writer.AppendLine(); WriteStructEqualityMethods(writer, workItem); @@ -302,7 +311,7 @@ private void WriteGuidStringConstructor(CodeWriter writer, StronglyTypedWorkItem WriteXmlSummary(writer, $"Initializes a new instance of the {typeKindStr}."); writer.AppendLine($"public {workItem.TypeName}()"); - writer.AppendLine(" : this(System.Guid.NewGuid())"); + writer.AppendLine(" : this(Guid.NewGuid())"); using (writer.BeginScope()) { @@ -371,7 +380,7 @@ private void WriteStructThrowIfInvalid(CodeWriter writer, StronglyTypedWorkItem } writer.AppendLine(); - writer.AppendLine("private void Throw(string errorMessage) => throw new Xtz.StronglyTyped.StronglyTypedException(GetType(), errorMessage);"); + writer.AppendLine("private void Throw(string errorMessage) => throw new StronglyTypedException(GetType(), errorMessage);"); writer.AppendLine(); } @@ -401,12 +410,12 @@ private void WriteStructEqualityMethods(CodeWriter writer, StronglyTypedWorkItem } } - private static void WriteEquatableEquals(CodeWriter writer, StronglyTypedWorkItem workItem) + private static void WriteClassEquatableEquals(CodeWriter writer, StronglyTypedWorkItem workItem) { WriteXmlSummary(writer, "Determines whether the specified object is equal to the current instance."); WriteXmlParam(writer, "other", "The object to compare with the current instance."); WriteXmlReturns(writer, " if the specified object is equal to the current instance; otherwise, ."); - using (writer.BeginScope($"public bool Equals({workItem.TypeName} other)")) + using (writer.BeginScope($"public bool Equals({workItem.TypeName}? other)")) { writer.AppendLine("if (ReferenceEquals(null, other)) return false;"); writer.AppendLine("if (ReferenceEquals(this, other)) return true;"); @@ -414,6 +423,17 @@ private static void WriteEquatableEquals(CodeWriter writer, StronglyTypedWorkIte } } + private static void WriteStructEquatableEquals(CodeWriter writer, StronglyTypedWorkItem workItem) + { + WriteXmlSummary(writer, "Determines whether the specified object is equal to the current instance."); + WriteXmlParam(writer, "other", "The object to compare with the current instance."); + WriteXmlReturns(writer, " if the specified object is equal to the current instance; otherwise, ."); + using (writer.BeginScope($"public bool Equals({workItem.TypeName} other)")) + { + writer.AppendLine("return Equals(Value, other.Value);"); + } + } + private static void TryWriteToString(CodeWriter writer, StronglyTypedWorkItem workItem) { if (!workItem.ExtraFeatures.HasToString) @@ -422,8 +442,8 @@ private static void TryWriteToString(CodeWriter writer, StronglyTypedWorkItem wo if (workItem.InnerType == typeof(Guid)) { - WriteXmlSummary(writer, "Returns a string that represents inner ."); - WriteXmlReturns(writer, "A string that represents inner ."); + WriteXmlSummary(writer, "Returns a string that represents inner ."); + WriteXmlReturns(writer, "A string that represents inner ."); using (writer.BeginScope("public override string ToString()")) { writer.AppendLine("return $\"{Value:D}\";"); @@ -434,8 +454,8 @@ private static void TryWriteToString(CodeWriter writer, StronglyTypedWorkItem wo if (workItem.InnerType == typeof(DateTime)) { - WriteXmlSummary(writer, "Returns an ISO-8601 string that represents inner ."); - WriteXmlReturns(writer, "An ISO-8601 string that represents inner ."); + WriteXmlSummary(writer, "Returns an ISO-8601 string that represents inner ."); + WriteXmlReturns(writer, "An ISO-8601 string that represents inner ."); using (writer.BeginScope("public override string ToString()")) { writer.AppendLine("return $\"{Value.ToUniversalTime():s}Z\";"); @@ -446,8 +466,8 @@ private static void TryWriteToString(CodeWriter writer, StronglyTypedWorkItem wo if (workItem.InnerType == typeof(TimeSpan)) { - WriteXmlSummary(writer, "Returns an ISO-8601 string that represents inner ."); - WriteXmlReturns(writer, "An ISO-8601 string that represents inner ."); + WriteXmlSummary(writer, "Returns an ISO-8601 string that represents inner ."); + WriteXmlReturns(writer, "An ISO-8601 string that represents inner ."); using (writer.BeginScope("public override string ToString()")) { writer.AppendLine("return $\"{System.Xml.XmlConvert.ToString(Value)}\";"); From d18e07268bde256d3fa1a345e5c09caa839ff91d Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:01:22 +0200 Subject: [PATCH 26/37] Refactor JSON converters --- .../StronglyTypedNewtonsoftConverter.cs | 50 ++++---- .../TypeConverters/ICustomTypeConverter.cs | 10 ++ .../StronglyTypedJsonConverter.cs | 107 ++++++++---------- 3 files changed, 78 insertions(+), 89 deletions(-) diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs index f510641..d89c826 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs @@ -18,29 +18,24 @@ public override bool CanConvert(Type objectType) public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { - var typeConverter = TypeDescriptor.GetConverter(objectType); + var typeConverter = TypeDescriptor.GetConverter(objectType) as ICustomTypeConverter; - if (reader.Value is long longValue) + switch (reader.Value) { - return ConvertFromLong(longValue, typeConverter as ICustomTypeConverter); + case long longValue: + return ConvertFromLong(longValue, typeConverter!); + case double doubleValue: + return ConvertFromDouble(doubleValue, typeConverter!); + case BigInteger bigIntValue: + return ConvertFromBigInt(bigIntValue, typeConverter!); } - if (reader.Value is double doubleValue) + if (typeConverter!.InnerType == typeof(TimeSpan)) { - return ConvertFromDouble(doubleValue, typeConverter as ICustomTypeConverter); + return ConvertToTimeSpan(reader.Value!, typeConverter); } - if (reader.Value is BigInteger bigIntValue) - { - return ConvertFromBigInt(bigIntValue, typeConverter as ICustomTypeConverter); - } - - if (typeConverter is ICustomTypeConverter customTypeConverter && customTypeConverter.InnerType == typeof(TimeSpan)) - { - return ConvertToTimeSpan(reader.Value, typeConverter); - } - - return (IStronglyTyped)typeConverter.ConvertFrom(reader.Value); + return (IStronglyTyped)typeConverter.ConvertFrom(reader.Value!); } public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) @@ -51,20 +46,17 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer return; } - var typeConverter = TypeDescriptor.GetConverter(value.GetType()); - if (typeConverter is ICustomTypeConverter customTypeConverter) + var typeConverter = TypeDescriptor.GetConverter(value.GetType()) as ICustomTypeConverter; + if (typeConverter!.InnerType == typeof(bool)) { - if (customTypeConverter.InnerType == typeof(bool)) - { - var boolValue = (value as IStronglyTyped)?.Value ?? default(bool); - writer.WriteValue(boolValue); - return; - } + var boolValue = (value as IStronglyTyped)?.Value ?? default(bool); + writer.WriteValue(boolValue); + return; + } - if (TryWriteNumber(value, customTypeConverter.InnerType, writer)) - { - return; - } + if (TryWriteNumber(value, typeConverter!.InnerType, writer)) + { + return; } var stringValue = typeConverter.ConvertTo(value, typeof(string)) as string; @@ -196,7 +188,7 @@ private IStronglyTyped ConvertFromBigInt(BigInteger bigIntValue, ICustomTypeConv throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(BigInteger)}' to '{typeConverter.StrongType.FullName}'"); } - private IStronglyTyped ConvertToTimeSpan(object value, TypeConverter typeConverter) + private IStronglyTyped ConvertToTimeSpan(object value, ICustomTypeConverter typeConverter) { var timeSpanValue = XmlConvert.ToTimeSpan(value.ToString()); var result = typeConverter.ConvertFrom(timeSpanValue); diff --git a/src/Xtz.StronglyTyped/TypeConverters/ICustomTypeConverter.cs b/src/Xtz.StronglyTyped/TypeConverters/ICustomTypeConverter.cs index c086f4c..73c843e 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/ICustomTypeConverter.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/ICustomTypeConverter.cs @@ -7,5 +7,15 @@ public interface ICustomTypeConverter Type StrongType { get; } Type InnerType { get; } + + /// + /// Converts the given value to the converter's native type. + /// + object ConvertFrom(object value); + + /// + /// Converts the given value object to the specified destination type using the arguments. + /// + object ConvertTo(object value, Type destinationType); } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs index 950b09d..76b529c 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs @@ -16,35 +16,32 @@ public override bool CanConvert(Type typeToConvert) return typeof(IStronglyTyped).IsAssignableFrom(typeToConvert); } - public override TStronglyTyped? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + public override TStronglyTyped Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - var typeConverter = TypeDescriptor.GetConverter(typeToConvert); + var typeConverter = TypeDescriptor.GetConverter(typeToConvert) as ICustomTypeConverter; - if (typeConverter is ICustomTypeConverter customTypeConverter) + if (typeConverter!.InnerType == typeof(TimeSpan)) { - if (customTypeConverter.InnerType == typeof(TimeSpan)) - { - return (TStronglyTyped)typeConverter.ConvertFrom(XmlConvert.ToTimeSpan(reader.GetString())); - } + return (TStronglyTyped)typeConverter.ConvertFrom(XmlConvert.ToTimeSpan(reader.GetString()!))!; + } - if (customTypeConverter.InnerType == typeof(DateTime)) - { - return ReadDateTime(reader, customTypeConverter.StrongType, typeConverter); - } + if (typeConverter.InnerType == typeof(DateTime)) + { + return ReadDateTime(reader, typeConverter); + } - if (reader.TokenType is JsonTokenType.True or JsonTokenType.False) - { - return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetBoolean()); - } + if (reader.TokenType is JsonTokenType.True or JsonTokenType.False) + { + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetBoolean()!)!; + } - if (reader.TokenType == JsonTokenType.Number) - { - return ReadNumber(reader, customTypeConverter.StrongType, customTypeConverter.InnerType, typeConverter); - } + if (reader.TokenType == JsonTokenType.Number) + { + return ReadNumber(reader, typeConverter); } var stringValue = reader.GetString(); - return (TStronglyTyped)typeConverter.ConvertFrom(stringValue); + return (TStronglyTyped)typeConverter.ConvertFrom(stringValue!)!; } public override void Write(Utf8JsonWriter writer, TStronglyTyped? value, JsonSerializerOptions options) @@ -65,98 +62,88 @@ public override void Write(Utf8JsonWriter writer, TStronglyTyped? value, JsonSer return; } - if (TryWriteNumber(value, customTypeConverter.InnerType, writer)) - { - return; - } + if (TryWriteNumber(value, customTypeConverter.InnerType, writer)) return; } var stringValue = typeConverter.ConvertTo(value, typeof(string)) as string; writer.WriteStringValue(stringValue); } - private TStronglyTyped? ReadNumber(Utf8JsonReader reader, Type strongType, Type innerType, TypeConverter typeConverter) + private TStronglyTyped ReadNumber(Utf8JsonReader reader, ICustomTypeConverter typeConverter) { - if (innerType == typeof(int)) + if (typeConverter.InnerType == typeof(int)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetInt32()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetInt32())!; } - if (innerType == typeof(float)) + if (typeConverter.InnerType == typeof(float)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetSingle()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetSingle())!; } - if (innerType == typeof(decimal)) + if (typeConverter.InnerType == typeof(decimal)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetDecimal()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetDecimal())!; } - if (innerType == typeof(long)) + if (typeConverter.InnerType == typeof(long)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetInt64()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetInt64())!; } - if (innerType == typeof(double)) + if (typeConverter.InnerType == typeof(double)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetDouble()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetDouble())!; } - if (innerType == typeof(byte)) + if (typeConverter.InnerType == typeof(byte)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetByte()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetByte())!; } - if (innerType == typeof(sbyte)) + if (typeConverter.InnerType == typeof(sbyte)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetSByte()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetSByte())!; } - if (innerType == typeof(short)) + if (typeConverter.InnerType == typeof(short)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetInt16()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetInt16())!; } - if (innerType == typeof(uint)) + if (typeConverter.InnerType == typeof(uint)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetUInt32()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetUInt32())!; } - if (innerType == typeof(ulong)) + if (typeConverter.InnerType == typeof(ulong)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetUInt64()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetUInt64())!; } - if (innerType == typeof(ushort)) + if (typeConverter.InnerType == typeof(ushort)) { - return (TStronglyTyped) typeConverter.ConvertFrom(reader.GetUInt16()); + return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetUInt16())!; } - throw new JsonConverterException(strongType, $"Can't convert value to '{strongType.FullName}'"); + throw new JsonConverterException(typeConverter.StrongType, $"Can't convert value to '{typeConverter.StrongType.FullName}'"); } - private TStronglyTyped? ReadDateTime(Utf8JsonReader reader, Type strongType, TypeConverter typeConverter) + private TStronglyTyped ReadDateTime(Utf8JsonReader reader, ICustomTypeConverter typeConverter) { if (reader.TryGetDateTime(out var dateTimeValue)) { - return (TStronglyTyped)typeConverter.ConvertFrom(dateTimeValue); + return (TStronglyTyped)typeConverter.ConvertFrom(dateTimeValue)!; } var stringValue = reader.GetString(); if (DateTime.TryParse(stringValue, out var dateTimeValue2)) { - return (TStronglyTyped)typeConverter.ConvertFrom(dateTimeValue2); + return (TStronglyTyped)typeConverter.ConvertFrom(dateTimeValue2)!; } - throw new JsonConverterException(strongType, $"Can't convert from '{stringValue}' to '{strongType.FullName}'"); - } - - private IStronglyTyped ConvertToTimeSpan(string value, TypeConverter typeConverter) - { - var timeSpanValue = XmlConvert.ToTimeSpan(value.ToString()); - var result = typeConverter.ConvertFrom(timeSpanValue); - return (IStronglyTyped)result; + throw new JsonConverterException(typeConverter.StrongType, $"Can't convert from '{stringValue}' to '{typeConverter.StrongType.FullName}'"); } private bool TryWriteNumber(IStronglyTyped value, Type innerType, Utf8JsonWriter writer) @@ -241,4 +228,4 @@ private bool TryWriteNumber(IStronglyTyped value, Type innerType, Utf8JsonWriter return false; } } -} \ No newline at end of file +} From 2581ac893094f7181d13753c7eac87b617b73c44 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:09:35 +0200 Subject: [PATCH 27/37] Add `[DebuggerDisplay]` and `.ToString()` to records in `Xtz.StronglyTyped.BuiltinTypes` --- src/Xtz.StronglyTyped.BuiltinTypes/Commerce/Price.cs | 9 +++++++-- .../Commerce/ProductFullName.cs | 11 ++++++++--- .../Finance/Currency/Currency.cs | 11 ++++++++--- src/Xtz.StronglyTyped.BuiltinTypes/Name/FullName.cs | 10 ++++++++-- .../StrongTypes/ConstructorDescriptor.cs | 5 ++++- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/Price.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/Price.cs index 85e79b6..83313d3 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/Price.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/Price.cs @@ -1,9 +1,14 @@ -using Xtz.StronglyTyped.BuiltinTypes.Finance; +using System.Diagnostics; +using Xtz.StronglyTyped.BuiltinTypes.Finance; namespace Xtz.StronglyTyped.BuiltinTypes.Commerce { /// /// Price. /// - public record Price(Amount Amount, Currency Currency); + [DebuggerDisplay("{ToString(),nq}")] + public record Price(Amount Amount, Currency Currency) + { + public override string ToString() => $"{Amount} {Currency.Code}"; + } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/ProductFullName.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/ProductFullName.cs index 3695210..047a834 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/ProductFullName.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Commerce/ProductFullName.cs @@ -1,9 +1,14 @@ -namespace Xtz.StronglyTyped.BuiltinTypes.Commerce +using System.Diagnostics; + +namespace Xtz.StronglyTyped.BuiltinTypes.Commerce { - // TODO: Add `[DebuggerDisplay]` and `.ToString()` to all records /// /// Product full name. /// /// {Adjective} {Material} {Name} - public record ProductFullName(ProductAdjective ProductAdjective, ProductMaterial ProductMaterial, ProductShortName ProductShortName); + [DebuggerDisplay("{ToString(),nq}")] + public record ProductFullName(ProductAdjective ProductAdjective, ProductMaterial ProductMaterial, ProductShortName ProductShortName) + { + public override string ToString() => $"{ProductAdjective} {ProductMaterial} {ProductShortName}"; + } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Finance/Currency/Currency.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Finance/Currency/Currency.cs index 32e2d64..a4db0c0 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Finance/Currency/Currency.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Finance/Currency/Currency.cs @@ -1,8 +1,13 @@ -namespace Xtz.StronglyTyped.BuiltinTypes.Finance +using System.Diagnostics; + +namespace Xtz.StronglyTyped.BuiltinTypes.Finance { /// /// Represents a currency. /// - public record Currency(CurrencyName Name, CurrencyCode Code, CurrencySymbol? Symbol); - + [DebuggerDisplay("{ToString(),nq}")] + public record Currency(CurrencyName Name, CurrencyCode Code, CurrencySymbol? Symbol) + { + public override string ToString() => $"{Name} ({Code}, symbol: {Symbol ?? ""})"; + } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Name/FullName.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Name/FullName.cs index 9dbc163..0bbb309 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Name/FullName.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Name/FullName.cs @@ -1,7 +1,13 @@ -namespace Xtz.StronglyTyped.BuiltinTypes.Name +using System.Diagnostics; + +namespace Xtz.StronglyTyped.BuiltinTypes.Name { /// /// Full name. /// - public record FullName(FirstName FirstName, LastName LastName); + [DebuggerDisplay("{ToString(),nq}")] + public record FullName(FirstName FirstName, LastName LastName) + { + public override string ToString() => $"{FirstName} {LastName})"; + } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.SourceGenerator/StrongTypes/ConstructorDescriptor.cs b/src/Xtz.StronglyTyped.SourceGenerator/StrongTypes/ConstructorDescriptor.cs index 0ac29e3..1eb7e79 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/StrongTypes/ConstructorDescriptor.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/StrongTypes/ConstructorDescriptor.cs @@ -1,4 +1,7 @@ -namespace Xtz.StronglyTyped.SourceGenerator +using System.Diagnostics.CodeAnalysis; + +namespace Xtz.StronglyTyped.SourceGenerator { + [ExcludeFromCodeCoverage] public record ConstructorDescriptor(string TypeName, string ParsingExpression); } \ No newline at end of file From 02c52be7d7bf37424b6bfce2df4320f1dc1e55e8 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:39:11 +0200 Subject: [PATCH 28/37] Resovle some `// TODO:` - Replace customly-implemented types by `[StrongType(...)]` - Drop `IConvertible` support --- .../BasicGeneratorTests.cs | 2 + .../CustomizedGeneratorTests.cs | 2 + .../Values/ConversionTests.cs | 2 +- .../_TestModels/CountryId.cs | 99 +------------------ .../_TestModels/EmployeeIntId.cs | 29 +----- .../_TestModels/UserId.cs | 17 +--- .../Internet/AvatarUri.cs | 2 +- 7 files changed, 15 insertions(+), 138 deletions(-) diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs index 0739de9..dcc7659 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs @@ -5,6 +5,8 @@ namespace Xtz.StronglyTyped.SourceGenerator.IntegrationTests { + // TODO: Write unit tests for `DataExtractor` + public class BasicGeneratorTests : GeneratorTestsBase { [Test] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs index f9144c6..50b2d67 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs @@ -5,6 +5,8 @@ namespace Xtz.StronglyTyped.SourceGenerator.IntegrationTests { + // TODO: Write tests for incomplete code (e.g. `[StrongType(]`) to test that generator ignores such cases + public class CustomizedGeneratorTests : GeneratorTestsBase { [Test] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs index 0f4f771..1caf5e5 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs @@ -70,7 +70,7 @@ public void Convert_ShouldConvertToInt_GivenStronglyTyped_WhenConvertingFromObje { //// Arrange - var stronglyTyped = new EmployeeIntId(value); + object stronglyTyped = new EmployeeIntId(value); //// Act diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryId.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryId.cs index 3145f5c..5f62946 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryId.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/CountryId.cs @@ -1,103 +1,10 @@ -using System; -using System.ComponentModel; -using Xtz.StronglyTyped.TypeConverters; +using Xtz.StronglyTyped.SourceGenerator; namespace Xtz.StronglyTyped.UnitTests { - // TODO: Replace by auto-generated class with [StrongType(typeof(int))] - [TypeConverter(typeof(TypeConverter))] - public class CountryId : StronglyTyped, IConvertible + [StrongType(typeof(int))] + public partial class CountryId { - public CountryId(int value) - : base(value) - { - } - - public TypeCode GetTypeCode() - { - return Value.GetTypeCode(); - } - - public bool ToBoolean(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(Boolean)}'"); - } - - public byte ToByte(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(Byte)}'"); - } - - public char ToChar(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(Char)}'"); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(DateTime)}'"); - } - - public decimal ToDecimal(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(Decimal)}'"); - } - - public double ToDouble(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(Double)}'"); - } - - public short ToInt16(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(Int16)}'"); - } - - public int ToInt32(IFormatProvider provider) - { - return Value; - } - - public long ToInt64(IFormatProvider provider) - { - return Value; - } - - public sbyte ToSByte(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(SByte)}'"); - } - - public float ToSingle(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(Single)}'"); - } - - public string ToString(IFormatProvider provider) - { - return Value.ToString(provider); - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{conversionType}'"); - } - - public ushort ToUInt16(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(UInt16)}'"); - } - - public uint ToUInt32(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(UInt32)}'"); - } - - public ulong ToUInt64(IFormatProvider provider) - { - throw new InvalidCastException($"'{nameof(CountryId)}' is not convertible to '{nameof(UInt64)}'"); - } - protected override bool IsValid(int value) { // ID must be greater than 0 diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/EmployeeIntId.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/EmployeeIntId.cs index ced73e7..f2fd2ae 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/EmployeeIntId.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/EmployeeIntId.cs @@ -1,32 +1,9 @@ -using System.ComponentModel; -using Xtz.StronglyTyped.TypeConverters; +using Xtz.StronglyTyped.SourceGenerator; namespace Xtz.StronglyTyped.UnitTests { - // TODO: Replace by auto-generated struct with [StrongType(typeof(int))] - [TypeConverter(typeof(TypeConverter))] - public struct EmployeeIntId : IStronglyTyped + [StrongType(typeof(int))] + public partial struct EmployeeIntId { - public int Value { get; } - - public EmployeeIntId(int value) - { - Value = value; - } - - public override string ToString() - { - return Value.ToString(); - } - - public static explicit operator EmployeeIntId(int value) - { - return new EmployeeIntId(value); - } - - public static implicit operator int(EmployeeIntId stronglyTyped) - { - return stronglyTyped.Value; - } } } diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/UserId.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/UserId.cs index a6840e5..8ebb75e 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/UserId.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/_TestModels/UserId.cs @@ -1,21 +1,10 @@ using System; -using System.ComponentModel; -using Xtz.StronglyTyped.TypeConverters; +using Xtz.StronglyTyped.SourceGenerator; namespace Xtz.StronglyTyped.UnitTests { - // TODO: Replace by auto-generated class with [StrongType(typeof(int))] - [TypeConverter(typeof(TypeConverter))] - public class UserId : StronglyTyped + [StrongType(typeof(Guid))] + public partial class UserId { - public UserId(Guid value) - : base(value) - { - } - - protected override bool IsValid(Guid value) - { - return value != Guid.Empty; - } } } diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/AvatarUri.cs b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/AvatarUri.cs index 293af59..bdb7d49 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Internet/AvatarUri.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Internet/AvatarUri.cs @@ -3,7 +3,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.Internet { - // TODO: Use `AbsoluteUri` as inner type + // TODO: Vlad DX: Think about using `AbsoluteUri` as inner type (could be not the best idea to use strong type in strong type) /// /// Absolute avatar URL. /// From 73a6c8b97aaff1062fd1934ff807b4fc02687783 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:40:16 +0200 Subject: [PATCH 29/37] Fix a bug of failing to analyze incomplete code --- .../DataExtractor.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs index fe53a99..94ea10f 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs @@ -224,8 +224,9 @@ private DoesAllowEmpty ExtractAllowEmpty(SemanticModel semanticModel, AttributeS // ^ Constructor method var attributeSymbol = semanticModel.GetSymbolInfo(strongTypeAttributeSyntax); var methodSymbol = attributeSymbol.Symbol as IMethodSymbol; + if (methodSymbol == null) return (DoesAllowEmpty)false; - var result = HasEnumArgument(semanticModel, methodSymbol!, attributeArgumentSyntaxes, Allow.Empty); + var result = HasEnumArgument(semanticModel, methodSymbol, attributeArgumentSyntaxes, Allow.Empty); return (DoesAllowEmpty)result; } @@ -287,7 +288,7 @@ private bool HasEnumArgument( // [StrongType(..., allow: Allow.Null)] // ^ - var hasParameterOfType = methodSymbol!.Parameters.Any(x => x.Type.ToDisplayString() == typeof(TEnum).FullName); + var hasParameterOfType = methodSymbol.Parameters.Any(x => x.Type.ToDisplayString() == typeof(TEnum).FullName); if (!hasParameterOfType) return false; // [StrongType(..., allow: Allow.Null)] @@ -360,9 +361,11 @@ private bool IsEnumValue( private static bool HasSingleParameter( SemanticModel semanticModel, - BaseMethodDeclarationSyntax methodDeclarationSyntax, + BaseMethodDeclarationSyntax? methodDeclarationSyntax, Type expectedType) { + if (methodDeclarationSyntax is null) return false; + // constructor(...) // ^ if (methodDeclarationSyntax.ParameterList.Parameters.Count != 1) return false; @@ -382,12 +385,11 @@ private static bool HasSingleParameter( } private static bool HasNameAndReturnType( - MethodDeclarationSyntax methodDeclarationSyntax, + MethodDeclarationSyntax? methodDeclarationSyntax, string expectedName, string expectedReturnType) { - var result = methodDeclarationSyntax.Identifier.Text == expectedName - && methodDeclarationSyntax.ReturnType.ToString() == expectedReturnType; + var result = methodDeclarationSyntax?.Identifier.Text == expectedName && methodDeclarationSyntax?.ReturnType.ToString() == expectedReturnType; return result; } } From a35e9a6b8db160b2925362ddfa9c1af0f8326d1f Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:40:26 +0200 Subject: [PATCH 30/37] Minor refactoring --- .../Abstract/BaseFakerSpecimenBuilder.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs index fc32df2..fac223e 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Abstract/BaseFakerSpecimenBuilder.cs @@ -20,15 +20,15 @@ public object Create(object request, ISpecimenContext context) { if (request is not ParameterInfo parameterInfo) { - switch (request) + return request switch { - case MultipleRequest {Request: SeededRequest { Request: Type multipleType }}: - return CreateArray(multipleType, context); - case SeededRequest { Request: Type type } when type.TryGetSingleGenericTypeArgument(typeof(IEnumerable<>), out Type? enumerableType): - return CreateEnumerable(enumerableType, context); - default: - return NoSpecimen; - } + MultipleRequest {Request: SeededRequest {Request: Type multipleType}} => + CreateArray(multipleType, context), + SeededRequest {Request: Type type} + when type.TryGetSingleGenericTypeArgument(typeof(IEnumerable<>), out var enumerableType) => + CreateEnumerable(enumerableType, context), + _ => NoSpecimen + }; } var parameterType = parameterInfo.ParameterType; From 493a05e6f11ceb9ae8c74acded03986dd9283d9b Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:42:26 +0200 Subject: [PATCH 31/37] Remove `IConvertible` tests --- .../Basic/ConversionTests.cs | 17 ----------------- .../Values/ConversionTests.cs | 19 ------------------- 2 files changed, 36 deletions(-) diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs index a77264a..9a50c73 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs @@ -91,22 +91,5 @@ public void Convert_ShouldConvertToInt_GivenCountryId(int countryId) Assert.AreEqual(countryId, result); } - - [TestCase(27)] - [Test] - public void Convert_ShouldConvertToInt_GivenCountryIdAsObject(int countryId) - { - //// Arrange - - object stronglyTyped = new CountryId(countryId); - - //// Act - - var result = Convert.ToInt32(stronglyTyped, CultureInfo.InvariantCulture); - - //// Assert - - Assert.AreEqual(countryId, result); - } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs index 1caf5e5..f23db59 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/ConversionTests.cs @@ -61,24 +61,5 @@ public void ShouldConvertToInt_GivenEmployeeIntId_WhenImplicitlyConverted(int va Assert.AreEqual(value, result); } - - [TestCase(-123)] - [TestCase(0)] - [TestCase(3697)] - [Test] - public void Convert_ShouldConvertToInt_GivenStronglyTyped_WhenConvertingFromObject(int value) - { - //// Arrange - - object stronglyTyped = new EmployeeIntId(value); - - //// Act - - var result = Convert.ToInt32(stronglyTyped); - - //// Assert - - Assert.AreEqual(value, result); - } } } \ No newline at end of file From 5f0894c8721dab9cc939537cda95067fe002fad2 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:42:34 +0200 Subject: [PATCH 32/37] Minor refactoring --- src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs index 94ea10f..424740b 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs @@ -223,8 +223,7 @@ private DoesAllowEmpty ExtractAllowEmpty(SemanticModel semanticModel, AttributeS // [StrongType(..., allow: Allow.Empty)] // ^ Constructor method var attributeSymbol = semanticModel.GetSymbolInfo(strongTypeAttributeSyntax); - var methodSymbol = attributeSymbol.Symbol as IMethodSymbol; - if (methodSymbol == null) return (DoesAllowEmpty)false; + if (attributeSymbol.Symbol is not IMethodSymbol methodSymbol) return (DoesAllowEmpty)false; var result = HasEnumArgument(semanticModel, methodSymbol, attributeArgumentSyntaxes, Allow.Empty); return (DoesAllowEmpty)result; From a9aa9a9dd56b420de96a295f9666668cbb8028f5 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 17:50:35 +0200 Subject: [PATCH 33/37] Bump up versions --- README.md | 1 + .../Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj | 2 +- .../Xtz.StronglyTyped.BuiltinTypes.csproj | 2 +- .../Xtz.StronglyTyped.EntityFramework.csproj | 2 +- .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 2 +- .../Xtz.StronglyTyped.SourceGenerator.csproj | 2 +- .../Xtz.StronglyTyped.Swashbuckle.csproj | 2 +- src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj | 2 +- 9 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f44a3c2..f560ce4 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ | **Xtz.StornglyTyped.BuiltinTypes.AutoFixture** | [![NuGet](http://img.shields.io/nuget/v/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.svg)](https://www.nuget.org/packages/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/) | | **Xtz.StornglyTyped.BuiltinTypes.Bogus** | [![NuGet](http://img.shields.io/nuget/v/Xtz.StronglyTyped.BuiltinTypes.Bogus.svg)](https://www.nuget.org/packages/Xtz.StronglyTyped.BuiltinTypes.Bogus/) | | **Xtz.StornglyTyped.EntityFramework** | [![NuGet](http://img.shields.io/nuget/v/Xtz.StronglyTyped.EntityFramework.svg)](https://www.nuget.org/packages/Xtz.StronglyTyped.EntityFramework/) | +| **Xtz.StronglyTyped.NewtonsoftJson** | [![NuGet](http://img.shields.io/nuget/v/Xtz.StronglyTyped.NewtonsoftJson.svg)](https://www.nuget.org/packages/Xtz.StronglyTyped.NewtonsoftJson/) | | **Xtz.StornglyTyped.SourceGenerator** | [![NuGet](http://img.shields.io/nuget/v/Xtz.StronglyTyped.SourceGenerator.svg)](https://www.nuget.org/packages/Xtz.StronglyTyped.SourceGenerator/) | | **Xtz.StornglyTyped.Swashbuckle** | [![NuGet](http://img.shields.io/nuget/v/Xtz.StronglyTyped.Swashbuckle.svg)](https://www.nuget.org/packages/Xtz.StronglyTyped.Swashbuckle/) | diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index 32a09be..327ed49 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.AutoFixture - 0.22.0 + 0.23.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index ad354d3..219478c 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes.Bogus - 0.22.0 + 0.23.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index a75564a..2e6df9e 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.BuiltinTypes - 0.22.0 + 0.23.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index ce62666..7e8de44 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.EntityFramework - 0.22.0 + 0.23.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index ae8efb6..6db06f7 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.NewtonsoftJson - 0.22.0 + 0.23.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index 8b42788..f1e965d 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.SourceGenerator - 0.22.0 + 0.23.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index 14efde5..12bde8c 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped.Swashbuckle - 0.22.0 + 0.23.0 true snupkg Vlad DX diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index bf843bc..e7ccbd7 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -8,7 +8,7 @@ Xtz.StronglyTyped - 0.22.0 + 0.23.0 true snupkg Vlad DX From 549a160899f03bdf791aa1edf1105f098ad5ab41 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 18:03:25 +0200 Subject: [PATCH 34/37] Add `NUnit.Analyzers` and apply suggestions --- .../WebApi/DegreesCelsius.cs | 0 .../WebApi/Startup.cs | 0 .../WebApi/StronglyTypedWeatherForecast.cs | 0 .../WebApi/WeatherForecast.cs | 0 .../WebApi/WeatherForecastController.cs | 0 .../WebApi/WebApiFactory.cs | 0 .../WebApiTests.cs | 12 +- ...onglyTyped.Api_3_1.IntegrationTests.csproj | 10 +- .../_RecordHack.cs | 0 .../IdAutoDataTests.cs | 8 +- .../InternetAutoDataTests.cs | 2 +- ...glyTyped.BogusAutoFixture.UnitTests.csproj | 1 + .../DbContextTests.cs | 8 +- ...ed.EntityFramework.IntegrationTests.csproj | 2 +- .../ConfigurationTests.cs | 4 +- .../SystemTextJsonTests.cs | 6 +- .../Xtz.StronglyTyped.IntegrationTests.csproj | 3 +- .../DeserializationTests.cs | 124 ++++++++--------- .../SerializationTests.cs | 70 +++++----- ...onglyTyped.NewtonsoftJson.UnitTests.csproj | 5 +- .../BasicGeneratorTests.cs | 30 ++-- .../CompileAndRunGeneratorTests.cs | 2 +- .../CustomizedGeneratorTests.cs | 72 +++++----- .../Misc/GeneratorTestsBase.cs | 18 +-- ...ed.SourceGenerator.IntegrationTests.csproj | 5 +- .../Basic/ConversionTests.cs | 10 +- .../Basic/CountryIdTests.cs | 2 +- .../Basic/StringCreationTests.cs | 6 +- .../Basic/StringEqualityOperatorsTests.cs | 22 +-- .../Basic/StringEqualityTests.cs | 22 +-- .../Basic/UpperCasedTests.cs | 2 +- .../Basic/UserIdTests.cs | 2 +- .../GlobalSuppressions.cs | 1 + .../SystemTextJson/DeserializationTests.cs | 128 +++++++++--------- .../SystemTextJson/SerializationTests.cs | 66 ++++----- .../TypeConverters/AbsoluteUriTests.cs | 8 +- .../TypeConverters/BoolTests.cs | 4 +- .../TypeConverters/EmailTests.cs | 4 +- .../TypeConverters/IntTests.cs | 4 +- .../TypeConverters/MacAddressTests.cs | 8 +- .../TypeConverters/StringTests.cs | 6 +- .../Values/ConversionTests.cs | 9 +- .../Values/CreationTests.cs | 6 +- .../Values/EqualityOperatorsTests.cs | 22 +-- .../Values/EqualityTests.cs | 16 +-- .../Values/TypeConverterTests.cs | 4 +- .../Xtz.StronglyTyped.UnitTests.csproj | 5 +- .../DataExtractor.cs | 2 +- src/Xtz.StronglyTyped.sln | 16 ++- 49 files changed, 383 insertions(+), 374 deletions(-) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/DegreesCelsius.cs (100%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs (100%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs (100%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecast.cs (100%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs (100%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WebApiFactory.cs (100%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs (91%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj (54%) rename src/{ => Tests}/Xtz.StronglyTyped.Api_3_1.IntegrationTests/_RecordHack.cs (100%) diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/DegreesCelsius.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/DegreesCelsius.cs similarity index 100% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/DegreesCelsius.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/DegreesCelsius.cs diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs similarity index 100% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/Startup.cs diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs similarity index 100% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/StronglyTypedWeatherForecast.cs diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecast.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecast.cs similarity index 100% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecast.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecast.cs diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs similarity index 100% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WeatherForecastController.cs diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WebApiFactory.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WebApiFactory.cs similarity index 100% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WebApiFactory.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApi/WebApiFactory.cs diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs similarity index 91% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs index 8fa5423..1d45c2c 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs +++ b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs @@ -35,7 +35,7 @@ public async Task ShouldReturn200_ForStandardEndpoint() // Assert response.EnsureSuccessStatusCode(); - Assert.NotNull(response.Content); + Assert.That(response.Content, Is.Not.Null); } [Test] @@ -46,7 +46,7 @@ public async Task ShouldDeserialize_ForStandardEndpoint_WhenNewtonsoftUsed() // Assert response.EnsureSuccessStatusCode(); - Assert.NotNull(response.Content); + Assert.That(response.Content, Is.Not.Null); Country country = null; // ReSharper disable once ExpressionIsAlwaysNull @@ -64,7 +64,7 @@ public async Task ShouldDeserialize_ForStandardEndpoint_WhenSystemTextJsonUsed() // Assert response.EnsureSuccessStatusCode(); - Assert.NotNull(response.Content); + Assert.That(response.Content, Is.Not.Null); var responseStr = await response.Content.ReadAsStringAsync(); var _ = System.Text.Json.JsonSerializer.Deserialize>(responseStr); @@ -78,7 +78,7 @@ public async Task ShouldReturn200_ForStronglyTypedEndpoint() // Assert response.EnsureSuccessStatusCode(); - Assert.NotNull(response.Content); + Assert.That(response.Content, Is.Not.Null); } [Test] @@ -89,7 +89,7 @@ public async Task ShouldDeserialize_ForStronglyTypedEndpoint_WhenNewtonsoftUsed( // Assert response.EnsureSuccessStatusCode(); - Assert.NotNull(response.Content); + Assert.That(response.Content, Is.Not.Null); var responseStr = await response.Content.ReadAsStringAsync(); var _ = JsonConvert.DeserializeObject>(responseStr, _jsonSerializerSettings); @@ -103,7 +103,7 @@ public async Task ShouldDeserialize_ForStronglyTypedEndpoint_WhenSystemTextJsonU // Assert response.EnsureSuccessStatusCode(); - Assert.NotNull(response.Content); + Assert.That(response.Content, Is.Not.Null); var responseStr = await response.Content.ReadAsStringAsync(); var _ = System.Text.Json.JsonSerializer.Deserialize>(responseStr); diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj similarity index 54% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj index 096ee2a..12fadde 100644 --- a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj @@ -8,16 +8,18 @@ + + - - - - + + + + diff --git a/src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/_RecordHack.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/_RecordHack.cs similarity index 100% rename from src/Xtz.StronglyTyped.Api_3_1.IntegrationTests/_RecordHack.cs rename to src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/_RecordHack.cs diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs index 5097f08..fe98c5a 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/IdAutoDataTests.cs @@ -14,8 +14,8 @@ public void ShouldGenerateStronglyTypedValues( EmployeeGuidId employeeGuidId, EmployeeIntId employeeIntId) { - Assert.AreNotEqual(default(Guid), employeeGuidId.Value); - Assert.AreNotEqual(default(int), employeeIntId.Value); + Assert.That(employeeGuidId.Value, Is.Not.EqualTo(default(Guid))); + Assert.That(employeeIntId.Value, Is.Not.EqualTo(default(int))); } [Test] @@ -24,8 +24,8 @@ public void ShouldGenerateStronglyTypedValues_WithCustomSpecimen( EmployeeGuidId employeeGuidId, EmployeeIntId employeeIntId) { - Assert.AreNotEqual(default(Guid), employeeGuidId.Value); - Assert.AreNotEqual(default(int), employeeIntId.Value); + Assert.That(employeeGuidId.Value, Is.Not.EqualTo(default(Guid))); + Assert.That(employeeIntId.Value, Is.Not.EqualTo(default(int))); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs index 85ad6d2..8fd7b75 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/InternetAutoDataTests.cs @@ -51,7 +51,7 @@ public void ShouldGenerateStronglyTypedValues( [StrongAutoData] public void ShouldGenerateStronglyTypedCollection(IEnumerable values) { - Assert.IsNotEmpty(values); + Assert.That(values, Is.Not.Empty); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj index 4d317cb..ccdd6e3 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj @@ -17,6 +17,7 @@ + diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs index 1acbc70..2a6de3f 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/DbContextTests.cs @@ -59,7 +59,7 @@ public void ShouldSucceed_SeedingData_ForGuidId(IReadOnlyCollection cityNa // Assert - Assert.AreEqual(cityNames.Count, cities.Length); + Assert.That(cities.Length, Is.EqualTo(cityNames.Count)); } [Test] @@ -100,8 +100,8 @@ public void ShouldSucceed_SeedingData_ForRelatedData(IReadOnlyCollection c // Assert - Assert.AreEqual(cityNames.Count, cities.Length); - Assert.AreEqual(weatherForecastEntities.Length, weatherForecasts.Length); + Assert.That(cities.Length, Is.EqualTo(cityNames.Count)); + Assert.That(weatherForecasts.Length, Is.EqualTo(weatherForecastEntities.Length)); } [Test] @@ -135,7 +135,7 @@ public void ShouldSucceed_SeedingData_ForStructId() // Assert - Assert.AreEqual(entities.Length, employees.Length); + Assert.That(employees.Length, Is.EqualTo(entities.Length)); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj index 11c78a0..3dd5ffe 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj @@ -9,8 +9,8 @@ + - diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs b/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs index e445475..980a5af 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs @@ -44,8 +44,8 @@ public void ShouldBindSection_GivenAppSettingsJson() // Assert - Assert.NotNull(settings); - Assert.AreEqual(expectedCountry, settings.Country); + Assert.That(settings, Is.Not.Null); + Assert.That(settings.Country, Is.EqualTo(expectedCountry)); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/SystemTextJsonTests.cs b/src/Tests/Xtz.StronglyTyped.IntegrationTests/SystemTextJsonTests.cs index c145a5f..4d51914 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/SystemTextJsonTests.cs +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/SystemTextJsonTests.cs @@ -48,7 +48,7 @@ public void ShouldSerialize_GivenNestedSettings() // Assert Assert.That(() => result.Contains(country)); - Assert.AreEqual(expected, result); + Assert.That(result, Is.EqualTo(expected)); } [Test] @@ -67,7 +67,7 @@ public void ShouldDeserializeSettings_GivenString() // Assert Assert.IsNotNull(result); - Assert.AreEqual(expected, result.Country); + Assert.That(result.Country, Is.EqualTo(expected)); } [Test] @@ -86,7 +86,7 @@ public void ShouldDeserializeNestedSettings_GivenString() // Assert Assert.IsNotNull(result); - Assert.AreEqual(expected, result.Inner.Country); + Assert.That(result.Inner.Country, Is.EqualTo(expected)); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj index 00f1f38..634bd4d 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj @@ -29,10 +29,11 @@ + + - diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs index e219a95..f25d132 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs @@ -38,8 +38,8 @@ public void ShouldDeserialize_ToStronglyTypedStringClass(string value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -58,8 +58,8 @@ public void ShouldDeserialize_ToStronglyTypedStringStruct(string value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -79,8 +79,8 @@ public void ShouldDeserialize_ToStronglyTypedStringClassAllowEmpty(string value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -100,8 +100,8 @@ public void ShouldDeserialize_ToStronglyTypedStringStructAllowEmpty(string value // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -120,8 +120,8 @@ public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -140,8 +140,8 @@ public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -160,8 +160,8 @@ public void ShouldDeserialize_ToStronglyTypedGuidIdClass(Guid value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -184,8 +184,8 @@ public void ShouldDeserialize_ToStronglyTypedIntClass(int value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -208,8 +208,8 @@ public void ShouldDeserialize_ToStronglyTypedIntStruct(int value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -230,8 +230,8 @@ public void ShouldDeserialize_ToStronglyTypedIntIdClass(int value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -252,8 +252,8 @@ public void ShouldDeserialize_ToStronglyTypedBoolClass() // Assert - Assert.NotNull(result); - Assert.IsNotEmpty(result); + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.Not.Empty); Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -277,8 +277,8 @@ public void ShouldDeserialize_ToStronglyTypedByteClass(byte value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -308,8 +308,8 @@ public void ShouldDeserialize_ToStronglyTypedCharClass(char value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -332,8 +332,8 @@ public void ShouldDeserialize_ToStronglyTypedDecimalClass(decimal value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -354,8 +354,8 @@ public void ShouldDeserialize_ToStronglyTypedDecimalClass() // Assert - Assert.NotNull(result); - Assert.IsNotEmpty(result); + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.Not.Empty); Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -383,8 +383,8 @@ public void ShouldDeserialize_ToStronglyTypedDoubleClass(double value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -410,8 +410,8 @@ public void ShouldDeserialize_ToStronglyTypedFloatClass(float value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -435,8 +435,8 @@ public void ShouldDeserialize_ToStronglyTypedLongClass(long value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -459,8 +459,8 @@ public void ShouldDeserialize_ToStronglyTypedSbyteClass(sbyte value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -485,8 +485,8 @@ public void ShouldDeserialize_ToStronglyTypedShortClass(short value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -510,8 +510,8 @@ public void ShouldDeserialize_ToStronglyTypedUintClass(uint value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -536,8 +536,8 @@ public void ShouldDeserialize_ToStronglyTypedUlongClass(ulong value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -557,8 +557,8 @@ public void ShouldDeserialize_ToStronglyTypedUshortClass() // Assert - Assert.NotNull(result); - Assert.IsNotEmpty(result); + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.Not.Empty); Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -582,8 +582,8 @@ public void ShouldDeserialize_ToStronglyTypedDateTimeClass(DateTime value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -608,8 +608,8 @@ public void ShouldDeserialize_ToStronglyTypedTimeSpanClass_FromTicks(long value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -634,8 +634,8 @@ public void ShouldDeserialize_ToStronglyTypedTimeSpanClass(TimeSpan timeSpan) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -653,8 +653,8 @@ public void ShouldDeserialize_ToStronglyTypedEmailClass(Email value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -674,8 +674,8 @@ public void ShouldDeserialize_ToStronglyTypedEmailsClass(IReadOnlyCollection>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -695,8 +695,8 @@ public void ShouldDeserialize_ToStronglyTypedIpV4AddressClass(IpV4Address value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -714,8 +714,8 @@ public void ShouldDeserialize_ToStronglyTypedIpV6AddressClass(IpV6Address value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -733,8 +733,8 @@ public void ShouldDeserialize_ToStronglyTypedMacAddressClass(MacAddress value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } private static string BuildStringArray(IEnumerable values) diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs index 1620a6d..3d71c91 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/SerializationTests.cs @@ -37,7 +37,7 @@ public void ShouldSerialize_GivenStronglyTypedStringClass(string value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -56,7 +56,7 @@ public void ShouldSerialize_GivenStronglyTypedStringStruct(string value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -76,7 +76,7 @@ public void ShouldSerialize_GivenStronglyTypedStringClassAllowEmpty(string value // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -96,7 +96,7 @@ public void ShouldSerialize_GivenStronglyTypedStringStructAllowEmpty(string valu // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -115,7 +115,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -134,7 +134,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -153,7 +153,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidIdClass(Guid value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -176,7 +176,7 @@ public void ShouldSerialize_GivenStronglyTypedIntClass(int value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -199,7 +199,7 @@ public void ShouldSerialize_GivenStronglyTypedIntStruct(int value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -220,7 +220,7 @@ public void ShouldSerialize_GivenStronglyTypedIntIdClass(int value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -239,7 +239,7 @@ public void ShouldSerialize_GivenStronglyTypedBoolClass(bool value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -261,7 +261,7 @@ public void ShouldSerialize_GivenStronglyTypedByteClass(byte value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -291,7 +291,7 @@ public void ShouldSerialize_GivenStronglyTypedCharClass(char value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -314,7 +314,7 @@ public void ShouldSerialize_GivenStronglyTypedDecimalClass(decimal value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -333,7 +333,7 @@ public void ShouldSerialize_GivenStronglyTypedDecimalClass() // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -359,7 +359,7 @@ public void ShouldSerialize_GivenStronglyTypedDoubleClass(double value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -385,7 +385,7 @@ public void ShouldSerialize_GivenStronglyTypedFloatClass(float value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -409,7 +409,7 @@ public void ShouldSerialize_GivenStronglyTypedLongClass(long value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -432,7 +432,7 @@ public void ShouldSerialize_GivenStronglyTypedSbyteClass(sbyte value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -457,7 +457,7 @@ public void ShouldSerialize_GivenStronglyTypedShortClass(short value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -481,7 +481,7 @@ public void ShouldSerialize_GivenStronglyTypedUintClass(uint value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -506,7 +506,7 @@ public void ShouldSerialize_GivenStronglyTypedUlongClass(ulong value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -525,7 +525,7 @@ public void ShouldSerialize_GivenStronglyTypedUshortClass() // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -547,7 +547,7 @@ public void ShouldSerialize_GivenStronglyTypedDateTimeClass(DateTime value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -571,8 +571,8 @@ public void ShouldSerialize_GivenStronglyTypedTimeSpanClass_FromTicks(long value // Assert - Assert.AreEqual(expected, result); - Assert.AreEqual(expectedString, result); + Assert.That(result, Is.EqualTo(expected)); + Assert.That(result, Is.EqualTo(expectedString)); } [Test] @@ -596,8 +596,8 @@ public void ShouldSerialize_GivenStronglyTypedTimeSpanClass(TimeSpan value) // Assert - Assert.AreEqual(expected, result); - Assert.AreEqual(expectedString, result); + Assert.That(result, Is.EqualTo(expected)); + Assert.That(result, Is.EqualTo(expectedString)); } [Test] @@ -614,7 +614,7 @@ public void ShouldSerialize_GivenStronglyTypedEmailClass(Email stronglyTyped) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -632,8 +632,8 @@ public void ShouldSerialize_GivenStronglyTypedEmailsClass(IReadOnlyCollection - - + + + diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs index dcc7659..e7e1629 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs @@ -106,9 +106,9 @@ public partial class City2 //// Assert - Assert.IsTrue(diagnostics.IsEmpty); + Assert.That(diagnostics.IsEmpty, Is.True); // Input syntax tree, a generated one, and logs - Assert.AreEqual(4, outputCompilation.SyntaxTrees.Count()); + Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(4)); PrintDiagnosticsToDebug(outputCompilation); Assert.IsFalse(outputCompilation.GetDiagnostics().IsEmpty); @@ -152,9 +152,9 @@ public class City2 //// Assert - Assert.IsTrue(diagnostics.IsEmpty); + Assert.That(diagnostics.IsEmpty, Is.True); // Input syntax tree, a generated one, and logs - Assert.AreEqual(5, outputCompilation.SyntaxTrees.Count()); + Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(5)); PrintDiagnosticsToDebug(outputCompilation); Assert.IsFalse(outputCompilation.GetDiagnostics().IsEmpty); @@ -188,9 +188,9 @@ public partial class City2 //// Assert - Assert.IsTrue(diagnostics.IsEmpty); + Assert.That(diagnostics.IsEmpty, Is.True); // Input syntax tree, a generated one, and logs - Assert.AreEqual(5, outputCompilation.SyntaxTrees.Count()); + Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(5)); PrintDiagnosticsToDebug(outputCompilation); Assert.IsFalse(outputCompilation.GetDiagnostics().IsEmpty); @@ -221,12 +221,12 @@ public partial class City2 //// Assert - Assert.IsTrue(diagnostics.IsEmpty); + Assert.That(diagnostics.IsEmpty, Is.True); // Input syntax tree, a generated one, and logs - Assert.AreEqual(4, outputCompilation.SyntaxTrees.Count()); + Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(4)); PrintDiagnosticsToDebug(outputCompilation); - Assert.IsTrue(outputCompilation.GetDiagnostics().IsEmpty); + Assert.That(outputCompilation.GetDiagnostics().IsEmpty, Is.True); } [Test] @@ -257,12 +257,12 @@ public partial record City2 //// Assert - Assert.IsTrue(diagnostics.IsEmpty); + Assert.That(diagnostics.IsEmpty, Is.True); // Input syntax tree, a generated one, and logs - Assert.AreEqual(4, outputCompilation.SyntaxTrees.Count()); + Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(4)); PrintDiagnosticsToDebug(outputCompilation); - Assert.IsTrue(outputCompilation.GetDiagnostics().IsEmpty); + Assert.That(outputCompilation.GetDiagnostics().IsEmpty, Is.True); } [Test] @@ -297,12 +297,12 @@ public partial class City2 //// Assert - Assert.IsTrue(diagnostics.IsEmpty); + Assert.That(diagnostics.IsEmpty, Is.True); // Input syntax tree, a generated one, and logs - Assert.AreEqual(4, outputCompilation.SyntaxTrees.Count()); + Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(4)); PrintDiagnosticsToDebug(outputCompilation); - Assert.IsTrue(outputCompilation.GetDiagnostics().IsEmpty); + Assert.That(outputCompilation.GetDiagnostics().IsEmpty, Is.True); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs index 00c03a5..bfa1fb9 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CompileAndRunGeneratorTests.cs @@ -73,7 +73,7 @@ public partial class DegreesCelsius3 // Assert - Assert.AreEqual(0, result); + Assert.That(result, Is.EqualTo(0)); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs index 50b2d67..19b2e29 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs @@ -59,8 +59,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -113,8 +113,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("''")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains("''"), Is.True); } [Test] @@ -167,8 +167,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -221,8 +221,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("''")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains("''"), Is.True); } //// [Test] @@ -332,8 +332,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -387,8 +387,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -442,8 +442,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -497,8 +497,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -551,8 +551,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -605,8 +605,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("''")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains("''"), Is.True); } [Test] @@ -659,8 +659,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -713,8 +713,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("''")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains("''"), Is.True); } [Test] @@ -767,8 +767,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -821,8 +821,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("''")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains("''"), Is.True); } [Test] @@ -875,8 +875,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains(""), Is.True); } [Test] @@ -929,8 +929,8 @@ void Action() //// Assert var exception = Assert.Throws(Action); - Assert.IsInstanceOf(exception?.InnerException); - Assert.IsTrue(exception?.InnerException?.Message.Contains("''")); + Assert.That(exception?.InnerException, Is.InstanceOf()); + Assert.That(exception?.InnerException?.Message.Contains("''"), Is.True); } // [Test] @@ -1179,7 +1179,7 @@ public partial class City2 // Assert - Assert.AreEqual(0, result); + Assert.That(result, Is.EqualTo(0)); } [Test] @@ -1229,7 +1229,7 @@ public partial class City2 // Assert - Assert.AreEqual(0, result); + Assert.That(result, Is.EqualTo(0)); } [Test] @@ -1279,7 +1279,7 @@ public partial class City2 // Assert - Assert.AreEqual(0, result); + Assert.That(result, Is.EqualTo(0)); } [Test] @@ -1329,7 +1329,7 @@ public partial class City2 // Assert - Assert.AreEqual(0, result); + Assert.That(result, Is.EqualTo(0)); } // [Test] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/GeneratorTestsBase.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/GeneratorTestsBase.cs index ec36797..8714e28 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/GeneratorTestsBase.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/GeneratorTestsBase.cs @@ -110,23 +110,23 @@ protected static object LoadAndExecute(byte[] compiledAssembly, params string[] protected static void AssertGenerationSuccess(int treeNumber, ImmutableArray diagnostics, Compilation outputCompilation, GeneratorDriverRunResult runResult) { - Assert.IsTrue(diagnostics.IsEmpty); + Assert.That(diagnostics.IsEmpty, Is.True); // Input syntax tree, a generated one, and logs - Assert.AreEqual(treeNumber + 1, outputCompilation.SyntaxTrees.Count()); + Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(treeNumber + 1)); PrintDiagnosticsToDebug(outputCompilation); - Assert.IsTrue(outputCompilation.GetDiagnostics().IsEmpty); + Assert.That(outputCompilation.GetDiagnostics().IsEmpty, Is.True); // A generated syntax tree and logs - Assert.AreEqual(treeNumber, runResult.GeneratedTrees.Length); - Assert.IsTrue(runResult.Diagnostics.IsEmpty); + Assert.That(runResult.GeneratedTrees.Length, Is.EqualTo(treeNumber)); + Assert.That(runResult.Diagnostics.IsEmpty, Is.True); // Asserting the individual results on a by-generator basis var generatorResult = runResult.Results[0]; - Assert.AreEqual(typeof(StronglyTypedGenerator), generatorResult.Generator.GetType()); - Assert.IsTrue(generatorResult.Diagnostics.IsEmpty); - Assert.AreEqual(treeNumber, generatorResult.GeneratedSources.Length); - Assert.IsNull(generatorResult.Exception); + Assert.That(generatorResult.Generator.GetType(), Is.EqualTo(typeof(StronglyTypedGenerator))); + Assert.That(generatorResult.Diagnostics.IsEmpty, Is.True); + Assert.That(generatorResult.GeneratedSources.Length, Is.EqualTo(treeNumber)); + Assert.That(generatorResult.Exception, Is.Null); } [ExcludeFromCodeCoverage] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj index 5592dca..ef3ea32 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj @@ -18,10 +18,11 @@ - + + + - diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs index 9a50c73..86d5b69 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/ConversionTests.cs @@ -21,7 +21,7 @@ public void ShouldParseStronglyTyped_GivenString_WhenExplicitlyConverted(string //// Assert - Assert.AreEqual(expected, result); + Assert.That(result, Is.EqualTo(expected)); } [TestCase("Norway")] @@ -38,7 +38,7 @@ public void ShouldConvertToString_GivenStronglyTyped_WhenExplicitlyConverted(str //// Assert - Assert.AreEqual(country, result); + Assert.That(result, Is.EqualTo(country)); } [TestCase("Norway")] @@ -55,7 +55,7 @@ public void ShouldConvertToString_GivenStronglyTyped_WhenImplicitlyConverted(str //// Assert - Assert.AreEqual(country, result); + Assert.That(result, Is.EqualTo(country)); } [TestCase("Norway")] @@ -72,7 +72,7 @@ public void Convert_ShouldConvertToString_GivenCountry(string country) //// Assert - Assert.AreEqual(country, result); + Assert.That(result, Is.EqualTo(country)); } [TestCase(27)] @@ -89,7 +89,7 @@ public void Convert_ShouldConvertToInt_GivenCountryId(int countryId) //// Assert - Assert.AreEqual(countryId, result); + Assert.That(result, Is.EqualTo(countryId)); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs index fb1e211..c9bd1b3 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/CountryIdTests.cs @@ -20,7 +20,7 @@ public void CountryId_ShouldSucceed_GivenValidValue(int value) //// Assert - Assert.AreEqual(value, result.Value); + Assert.That(result.Value, Is.EqualTo(value)); } [TestCase(Int32.MinValue)] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringCreationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringCreationTests.cs index 80ef7da..9124236 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringCreationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringCreationTests.cs @@ -17,8 +17,8 @@ public void ShouldInstantiate_GivenString(string country) //// Assert - Assert.NotNull(result); - Assert.AreEqual(country, result.Value); + Assert.That(result, Is.Not.Null); + Assert.That(result.Value, Is.EqualTo(country)); } [TestCase("Norway")] @@ -35,7 +35,7 @@ public void ShouldReturnInnerValue_WhenToString(string country) //// Assert - Assert.AreEqual(country, result); + Assert.That(result, Is.EqualTo(country)); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs index 4888390..80c207e 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs @@ -18,7 +18,7 @@ public void EqualsOperator_ShouldBeTrue_ForSameStronglyTypedValues() // ReSharper disable once EqualExpressionComparison #pragma warning disable CS1718 // Comparison made to same variable - Assert.IsTrue(value == value); + Assert.That(value == value, Is.True); #pragma warning restore CS1718 // Comparison made to same variable } @@ -34,8 +34,8 @@ public void EqualsOperator_ShouldBeTrue_ForSameValuesStronglyTypedAndObject() //// Assert - Assert.IsTrue(value == objValue); - Assert.IsTrue(objValue == value); + Assert.That(value == objValue, Is.True); + Assert.That(objValue == value, Is.True); } [Test] @@ -50,8 +50,8 @@ public void EqualsOperator_ShouldBeTrue_ForStronglyTypedValues_GivenSameStrings( //// Assert - Assert.IsTrue(value1 == value2); - Assert.IsTrue(value2 == value1); + Assert.That(value1 == value2, Is.True); + Assert.That(value2 == value1, Is.True); } [Test] @@ -66,8 +66,8 @@ public void EqualsOperator_ShouldBeTrue_ForStronglyTypedAndObject_GivenSameStrin //// Assert - Assert.IsTrue(value == objValue); - Assert.IsTrue(objValue == value); + Assert.That(value == objValue, Is.True); + Assert.That(objValue == value, Is.True); } [Test] @@ -146,8 +146,8 @@ public void NotEqualsOperator_ShouldBeTrue_ForStronglyTypedValues_GivenDifferent //// Assert - Assert.IsTrue(value1 != value2); - Assert.IsTrue(value2 != value1); + Assert.That(value1 != value2, Is.True); + Assert.That(value2 != value1, Is.True); } [Test] @@ -162,8 +162,8 @@ public void NotEqualsOperator_ShouldBeTrue_ForStronglyTypedAndObject_GivenDiffer //// Assert - Assert.IsTrue(value != objValue); - Assert.IsTrue(objValue != value); + Assert.That(value != objValue, Is.True); + Assert.That(objValue != value, Is.True); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityTests.cs index 4a4f0b8..f3c4257 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityTests.cs @@ -18,8 +18,8 @@ public void StronglyTypedValues_ShouldBeEqual_GivenSameStrings(string country) //// Assert - Assert.AreEqual(result1, result2); - Assert.AreEqual(result1.Value, result2.Value); + Assert.That(result2, Is.EqualTo(result1)); + Assert.That(result2.Value, Is.EqualTo(result1.Value)); } [TestCase("Norway", "Denmark")] @@ -35,8 +35,8 @@ public void StronglyTypedValues_ShouldBeNotEqual_GivenDifferentStrings(string co //// Assert - Assert.AreNotEqual(result1, result2); - Assert.AreNotEqual(result1.Value, result2.Value); + Assert.That(result2, Is.Not.EqualTo(result1)); + Assert.That(result2.Value, Is.Not.EqualTo(result1.Value)); } [TestCase("Norway")] @@ -52,8 +52,8 @@ public void ShouldBeEqual_GivenTheSameCalculatedStrings(string country) //// Assert - Assert.AreEqual(result1, result2); - Assert.AreEqual(result1.Value, result2.Value); + Assert.That(result2, Is.EqualTo(result1)); + Assert.That(result2.Value, Is.EqualTo(result1.Value)); } [TestCase("Norway")] @@ -69,8 +69,8 @@ public void ShouldBeNotEqual_GivenTheDifferentlyCasedValues(string country) //// Assert - Assert.AreNotEqual(result1, result2); - Assert.AreEqual(result1.Value, result2.Value.ToUpper()); + Assert.That(result2, Is.Not.EqualTo(result1)); + Assert.That(result2.Value.ToUpper(), Is.EqualTo(result1.Value)); } [TestCase("Norway")] @@ -86,8 +86,8 @@ public void GetHashCode_ShouldBeEqual_GivenSameStrings(string country) //// Assert - Assert.AreEqual(result1.GetHashCode(), result2.GetHashCode()); - Assert.AreEqual(result1.GetHashCode(), result2.GetHashCode()); + Assert.That(result2.GetHashCode(), Is.EqualTo(result1.GetHashCode())); + Assert.That(result2.GetHashCode(), Is.EqualTo(result1.GetHashCode())); } [TestCase("Norway", "Denmark")] @@ -103,7 +103,7 @@ public void GetHashCode_ShouldBeNotEqual_GivenDifferentStrings(string country1, //// Assert - Assert.AreNotEqual(result1.GetHashCode(), result2.GetHashCode()); + Assert.That(result2.GetHashCode(), Is.Not.EqualTo(result1.GetHashCode())); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UpperCasedTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UpperCasedTests.cs index f56dbed..47418ff 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UpperCasedTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UpperCasedTests.cs @@ -21,7 +21,7 @@ public void StronglyTyped_ShouldBeUpperCased_GivenCasedString(string value) //// Assert - Assert.AreEqual(expected, result.Value); + Assert.That(result.Value, Is.EqualTo(expected)); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs index 49134e9..53f5639 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/UserIdTests.cs @@ -19,7 +19,7 @@ public void UserId_ShouldInstantiate_GivenGuid() //// Assert - Assert.AreNotEqual(Guid.Empty, result); + Assert.That(result, Is.Not.EqualTo(Guid.Empty)); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs index 96317f4..76c9ad8 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs @@ -9,3 +9,4 @@ [assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Values.CreationTests.ShouldThrow_GivenNullOrEmpty(System.String)")] [assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.UserIdTests.UserId_ShouldThrow_GivenInvalidValue")] [assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Values.CreationTests.ShouldThrow_GivenNull_ForAllowEmpty")] +[assembly: SuppressMessage("Assertion", "NUnit2021:Incompatible types for EqualTo constraint", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.UserIdTests.UserId_ShouldInstantiate_GivenGuid")] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs index e017589..ab07073 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs @@ -35,8 +35,8 @@ public void ShouldDeserialize_ToStronglyTypedStringClass(string value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -55,8 +55,8 @@ public void ShouldDeserialize_ToStronglyTypedStringStruct(string value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -76,8 +76,8 @@ public void ShouldDeserialize_ToStronglyTypedStringClassAllowEmpty(string value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -97,8 +97,8 @@ public void ShouldDeserialize_ToStronglyTypedStringStructAllowEmpty(string value // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -117,8 +117,8 @@ public void ShouldDeserialize_ToStronglyTypedGuidClass(Guid value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -137,8 +137,8 @@ public void ShouldDeserialize_ToStronglyTypedGuidStruct(Guid value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -157,8 +157,8 @@ public void ShouldDeserialize_ToStronglyTypedGuidIdClass(Guid value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -181,8 +181,8 @@ public void ShouldDeserialize_ToStronglyTypedIntClass(int value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -205,8 +205,8 @@ public void ShouldDeserialize_ToStronglyTypedIntStruct(int value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -227,8 +227,8 @@ public void ShouldDeserialize_ToStronglyTypedIntIdClass(int value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -249,8 +249,8 @@ public void ShouldDeserialize_ToStronglyTypedBoolClass() // Assert - Assert.NotNull(result); - Assert.IsNotEmpty(result); + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.Not.Empty); Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -274,8 +274,8 @@ public void ShouldDeserialize_ToStronglyTypedByteClass(byte value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -304,8 +304,8 @@ public void ShouldDeserialize_ToStronglyTypedCharClass(char value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -323,8 +323,8 @@ public void ShouldDeserialize_ToStronglyTypedCharClass(char value, string serial // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -347,8 +347,8 @@ public void ShouldDeserialize_ToStronglyTypedDecimalClass(decimal value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -369,8 +369,8 @@ public void ShouldDeserialize_ToStronglyTypedDecimalClass() // Assert - Assert.NotNull(result); - Assert.IsNotEmpty(result); + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.Not.Empty); Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -398,8 +398,8 @@ public void ShouldDeserialize_ToStronglyTypedDoubleClass(double value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -425,8 +425,8 @@ public void ShouldDeserialize_ToStronglyTypedFloatClass(float value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -450,8 +450,8 @@ public void ShouldDeserialize_ToStronglyTypedLongClass(long value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -474,8 +474,8 @@ public void ShouldDeserialize_ToStronglyTypedSbyteClass(sbyte value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -500,8 +500,8 @@ public void ShouldDeserialize_ToStronglyTypedShortClass(short value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -525,8 +525,8 @@ public void ShouldDeserialize_ToStronglyTypedUintClass(uint value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -551,8 +551,8 @@ public void ShouldDeserialize_ToStronglyTypedUlongClass(ulong value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -572,8 +572,8 @@ public void ShouldDeserialize_ToStronglyTypedUshortClass() // Assert - Assert.NotNull(result); - Assert.IsNotEmpty(result); + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.Not.Empty); Assert.That(result, Has.Exactly(stronglyTyped.Length).Items); Assert.That(result, Is.All.Matches>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -597,8 +597,8 @@ public void ShouldDeserialize_ToStronglyTypedDateTimeClass(DateTime value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -623,8 +623,8 @@ public void ShouldDeserialize_ToStronglyTypedTimeSpanClass_FromTicks(long value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -649,8 +649,8 @@ public void ShouldDeserialize_ToStronglyTypedTimeSpanClass(TimeSpan timeSpan) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -668,8 +668,8 @@ public void ShouldDeserialize_ToStronglyTypedEmailClass(Email value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -689,8 +689,8 @@ public void ShouldDeserialize_ToStronglyTypedEmailsClass(IReadOnlyCollection>(x => stronglyTyped.Any(s => s.TestValue == x.TestValue))); } @@ -710,8 +710,8 @@ public void ShouldDeserialize_ToStronglyTypedIpV4AddressClass(IpV4Address value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -729,8 +729,8 @@ public void ShouldDeserialize_ToStronglyTypedIpV6AddressClass(IpV6Address value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } [Test] @@ -748,8 +748,8 @@ public void ShouldDeserialize_ToStronglyTypedMacAddressClass(MacAddress value) // Assert - Assert.NotNull(result); - Assert.AreEqual(stronglyTyped.TestValue, result.TestValue); + Assert.That(result, Is.Not.Null); + Assert.That(result.TestValue, Is.EqualTo(stronglyTyped.TestValue)); } private static string BuildStringArray(IEnumerable values) diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs index a1e2cce..46a852a 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs @@ -27,7 +27,7 @@ public void ShouldSerialize_GivenStronglyTypedStringClass(string value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -46,7 +46,7 @@ public void ShouldSerialize_GivenStronglyTypedStringStruct(string value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -66,7 +66,7 @@ public void ShouldSerialize_GivenStronglyTypedStringClassAllowEmpty(string value // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -86,7 +86,7 @@ public void ShouldSerialize_GivenStronglyTypedStringStructAllowEmpty(string valu // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -105,7 +105,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidClass(Guid value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -124,7 +124,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidStruct(Guid value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } // ReSharper disable once NUnit.IncorrectArgumentType @@ -143,7 +143,7 @@ public void ShouldSerialize_GivenStronglyTypedGuidIdClass(Guid value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -166,7 +166,7 @@ public void ShouldSerialize_GivenStronglyTypedIntClass(int value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -189,7 +189,7 @@ public void ShouldSerialize_GivenStronglyTypedIntStruct(int value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -210,7 +210,7 @@ public void ShouldSerialize_GivenStronglyTypedIntIdClass(int value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -228,7 +228,7 @@ public void ShouldSerialize_GivenStronglyTypedBoolClass(bool value, string expec // Assert - Assert.AreEqual(expectedValue, result); + Assert.That(result, Is.EqualTo(expectedValue)); } [Test] @@ -250,7 +250,7 @@ public void ShouldSerialize_GivenStronglyTypedByteClass(byte value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -280,7 +280,7 @@ public void ShouldSerialize_GivenStronglyTypedCharClass(char value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -309,7 +309,7 @@ public void ShouldSerialize_GivenStronglyTypedByteClass(decimal value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -335,7 +335,7 @@ public void ShouldSerialize_GivenStronglyTypedDoubleClass(double value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -361,7 +361,7 @@ public void ShouldSerialize_GivenStronglyTypedFloatClass(float value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -385,7 +385,7 @@ public void ShouldSerialize_GivenStronglyTypedLongClass(long value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -408,7 +408,7 @@ public void ShouldSerialize_GivenStronglyTypedSbyteClass(sbyte value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -433,7 +433,7 @@ public void ShouldSerialize_GivenStronglyTypedShortClass(short value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -457,7 +457,7 @@ public void ShouldSerialize_GivenStronglyTypedUintClass(uint value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -482,7 +482,7 @@ public void ShouldSerialize_GivenStronglyTypedUlongClass(ulong value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -501,7 +501,7 @@ public void ShouldSerialize_GivenStronglyTypedUshortClass() // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -523,7 +523,7 @@ public void ShouldSerialize_GivenStronglyTypedDateTimeClass(DateTime value) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -547,8 +547,8 @@ public void ShouldSerialize_GivenStronglyTypedTimeSpanClass_FromTicks(long value // Assert - Assert.AreEqual(expected, result); - Assert.AreEqual(expectedString, result); + Assert.That(result, Is.EqualTo(expected)); + Assert.That(result, Is.EqualTo(expectedString)); } [Test] @@ -572,8 +572,8 @@ public void ShouldSerialize_GivenStronglyTypedTimeSpanClass(TimeSpan value) // Assert - Assert.AreEqual(expected, result); - Assert.AreEqual(expectedString, result); + Assert.That(result, Is.EqualTo(expected)); + Assert.That(result, Is.EqualTo(expectedString)); } [Test] @@ -590,7 +590,7 @@ public void ShouldSerialize_GivenStronglyTypedEmailClass(Email stronglyTyped) // Assert - Assert.AreEqual(json, result); + Assert.That(result, Is.EqualTo(json)); } [Test] @@ -608,7 +608,7 @@ public void ShouldSerialize_GivenStronglyTypedEmailsClass(IReadOnlyCollection - - + + + diff --git a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs index 424740b..0b3d853 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs @@ -388,7 +388,7 @@ private static bool HasNameAndReturnType( string expectedName, string expectedReturnType) { - var result = methodDeclarationSyntax?.Identifier.Text == expectedName && methodDeclarationSyntax?.ReturnType.ToString() == expectedReturnType; + var result = methodDeclarationSyntax?.Identifier.Text == expectedName && methodDeclarationSyntax.ReturnType.ToString() == expectedReturnType; return result; } } diff --git a/src/Xtz.StronglyTyped.sln b/src/Xtz.StronglyTyped.sln index e53f3a9..57e343d 100644 --- a/src/Xtz.StronglyTyped.sln +++ b/src/Xtz.StronglyTyped.sln @@ -35,12 +35,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.SourceGen EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.EntityFramework.IntegrationTests", "Tests\Xtz.StronglyTyped.EntityFramework.IntegrationTests\Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj", "{857370EC-A6FA-47FE-9005-8CC4D752630F}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.Api_3_1.IntegrationTests", "Xtz.StronglyTyped.Api_3_1.IntegrationTests\Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj", "{1AAE34E7-203E-41D3-A054-4767C5A99FAC}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.NewtonsoftJson", "Xtz.StronglyTyped.NewtonsoftJson\Xtz.StronglyTyped.NewtonsoftJson.csproj", "{D7EB61AD-95C7-418C-B1B5-60D0B73905EA}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.NewtonsoftJson.UnitTests", "Tests\Xtz.StronglyTyped.NewtonsoftJson.UnitTests\Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj", "{97EDCF82-8DA3-4DB1-96C4-FB2481A75666}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Solution Items", "_Solution Items", "{C6D98E0C-09A4-4152-914D-411DB65B1379}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.Api_3_1.IntegrationTests", "Tests\Xtz.StronglyTyped.Api_3_1.IntegrationTests\Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj", "{975017C6-0E2B-4F10-BE65-B412755BEDF7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -103,10 +105,6 @@ Global {857370EC-A6FA-47FE-9005-8CC4D752630F}.Debug|Any CPU.Build.0 = Debug|Any CPU {857370EC-A6FA-47FE-9005-8CC4D752630F}.Release|Any CPU.ActiveCfg = Release|Any CPU {857370EC-A6FA-47FE-9005-8CC4D752630F}.Release|Any CPU.Build.0 = Release|Any CPU - {1AAE34E7-203E-41D3-A054-4767C5A99FAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1AAE34E7-203E-41D3-A054-4767C5A99FAC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1AAE34E7-203E-41D3-A054-4767C5A99FAC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1AAE34E7-203E-41D3-A054-4767C5A99FAC}.Release|Any CPU.Build.0 = Release|Any CPU {D7EB61AD-95C7-418C-B1B5-60D0B73905EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D7EB61AD-95C7-418C-B1B5-60D0B73905EA}.Debug|Any CPU.Build.0 = Debug|Any CPU {D7EB61AD-95C7-418C-B1B5-60D0B73905EA}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -115,6 +113,10 @@ Global {97EDCF82-8DA3-4DB1-96C4-FB2481A75666}.Debug|Any CPU.Build.0 = Debug|Any CPU {97EDCF82-8DA3-4DB1-96C4-FB2481A75666}.Release|Any CPU.ActiveCfg = Release|Any CPU {97EDCF82-8DA3-4DB1-96C4-FB2481A75666}.Release|Any CPU.Build.0 = Release|Any CPU + {975017C6-0E2B-4F10-BE65-B412755BEDF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {975017C6-0E2B-4F10-BE65-B412755BEDF7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {975017C6-0E2B-4F10-BE65-B412755BEDF7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {975017C6-0E2B-4F10-BE65-B412755BEDF7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -127,8 +129,8 @@ Global {0002B673-EEEC-42DE-BBD7-B434C01E4F3E} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} {F5E5B3F6-F0D7-4B11-A99C-87E3ADCEEFFE} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} {857370EC-A6FA-47FE-9005-8CC4D752630F} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} - {1AAE34E7-203E-41D3-A054-4767C5A99FAC} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} {97EDCF82-8DA3-4DB1-96C4-FB2481A75666} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} + {975017C6-0E2B-4F10-BE65-B412755BEDF7} = {364CF428-8597-468D-9B5E-8D9A7BC6CC29} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F4FB6146-7992-4E75-B5F9-22B2ADA3C82F} From a108124a3416ad838d627f383deb71a0faf81062 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 18:37:35 +0200 Subject: [PATCH 35/37] Add more static analyzers and fix/suppress warnings - Add `Microsoft.CodeAnalysis.BannedApiAnalyzers` - Add `Microsoft.CodeAnalysis.CSharp.CodeStyle` - Add `Microsoft.CodeAnalysis.NetAnalyzers` - Add `Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers` - Add `Roslynator.Analyzers` - Add `Roslynator.CodeAnalysis.Analyzers` --- .../Xtz.StronglyTyped.Benchmark.csproj | 8 +++++ .../Xtz.StronglyTyped.Playground/Program.cs | 2 +- .../Xtz.StronglyTyped.Playground.csproj | 8 +++++ ...onglyTyped.Api_3_1.IntegrationTests.csproj | 16 +++++++++ ...glyTyped.BogusAutoFixture.UnitTests.csproj | 8 +++++ ...ed.EntityFramework.IntegrationTests.csproj | 8 +++++ .../Xtz.StronglyTyped.IntegrationTests.csproj | 8 +++++ ...onglyTyped.NewtonsoftJson.UnitTests.csproj | 8 +++++ .../Properties/GlobalSuppressions.cs | 9 +++++ ...ed.SourceGenerator.IntegrationTests.csproj | 8 +++++ ...erator.IntegrationTests.csproj.DotSettings | 3 +- .../Xtz.StronglyTyped.UnitTests.csproj | 8 +++++ ...onglyTyped.BuiltinTypes.AutoFixture.csproj | 24 ++++++++++++++ .../Properties/GlobalSuppressions.cs | 8 +++++ ...tz.StronglyTyped.BuiltinTypes.Bogus.csproj | 24 ++++++++++++++ ...yped.BuiltinTypes.Bogus.csproj.DotSettings | 3 +- .../Xtz.StronglyTyped.BuiltinTypes.csproj | 24 ++++++++++++++ .../Xtz.StronglyTyped.EntityFramework.csproj | 24 ++++++++++++++ .../Properties/GlobalSuppressions.cs | 8 +++++ .../StronglyTypedNewtonsoftConverter.cs | 10 +++--- .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 28 ++++++++++++++++ ...glyTyped.NewtonsoftJson.csproj.DotSettings | 2 ++ .../GlobalSuppressions.cs | 9 +++++ .../Xtz.StronglyTyped.SourceGenerator.csproj | 33 ++++++++++++++++++- .../Xtz.StronglyTyped.Swashbuckle.csproj | 24 ++++++++++++++ src/Xtz.StronglyTyped/IStronglyTyped.cs | 2 ++ .../InvalidValueException.cs | 3 +- .../Properties/GlobalSuppressions.cs | 11 +++++++ .../Xtz.StronglyTyped.csproj | 28 ++++++++++++++++ .../Xtz.StronglyTyped.csproj.DotSettings | 2 ++ 30 files changed, 351 insertions(+), 10 deletions(-) create mode 100644 src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs create mode 100644 src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Properties/GlobalSuppressions.cs create mode 100644 src/Xtz.StronglyTyped.NewtonsoftJson/Properties/GlobalSuppressions.cs create mode 100644 src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj.DotSettings create mode 100644 src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs create mode 100644 src/Xtz.StronglyTyped/Properties/GlobalSuppressions.cs create mode 100644 src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj.DotSettings diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj index 4dd08cd..d2a9b57 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj @@ -13,6 +13,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Misc/Xtz.StronglyTyped.Playground/Program.cs b/src/Misc/Xtz.StronglyTyped.Playground/Program.cs index 523cd50..f26bf19 100644 --- a/src/Misc/Xtz.StronglyTyped.Playground/Program.cs +++ b/src/Misc/Xtz.StronglyTyped.Playground/Program.cs @@ -5,7 +5,7 @@ namespace Xtz.StronglyTyped.Playground { // NOTE: Just a simple console app for demo purposes - public class Program + public static class Program { public static void Main() { diff --git a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj index 6186589..df5d73a 100644 --- a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj +++ b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj @@ -22,9 +22,17 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj index 12fadde..f32e949 100644 --- a/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj @@ -5,13 +5,29 @@ 9 + + true + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj index ccdd6e3..14bb899 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj @@ -15,10 +15,18 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj index 3dd5ffe..d2a88b7 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj @@ -5,12 +5,20 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj index 634bd4d..6950dda 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj @@ -25,6 +25,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -34,6 +38,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj index 575b267..85e9132 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj @@ -16,11 +16,19 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs new file mode 100644 index 0000000..5748ce2 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs @@ -0,0 +1,9 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.GeneratorTestsException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.TestsExecutionException")] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj index ef3ea32..9d643ce 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj @@ -16,6 +16,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -23,6 +27,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj.DotSettings b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj.DotSettings index e492290..9fec72c 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj.DotSettings +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj.DotSettings @@ -1,2 +1,3 @@  - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj index 00af5d2..e9a2900 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj @@ -15,11 +15,19 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index 327ed49..bdd519b 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -33,6 +33,10 @@ Make compiler your friend, introduce semantics to your code. true + + true + + @@ -40,12 +44,32 @@ Make compiler your friend, introduce semantics to your code. + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Properties/GlobalSuppressions.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Properties/GlobalSuppressions.cs new file mode 100644 index 0000000..0feb075 --- /dev/null +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Properties/GlobalSuppressions.cs @@ -0,0 +1,8 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.BuiltinTypes.Bogus.FakerBuilderException")] diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index 219478c..b336e5a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -33,17 +33,41 @@ Make compiler your friend, introduce semantics to your code. true + + true + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj.DotSettings b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj.DotSettings index cd122a4..da8409b 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj.DotSettings +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj.DotSettings @@ -1,2 +1,3 @@  - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index 2e6df9e..7cd337a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -33,6 +33,10 @@ Make compiler your friend, introduce semantics to your code. true + + true + + True $(BaseIntermediateOutputPath)\GeneratedFiles @@ -43,10 +47,30 @@ Make compiler your friend, introduce semantics to your code. + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index 7e8de44..faec0f1 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -33,12 +33,36 @@ Make compiler your friend, introduce semantics to your code. true + + true + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Properties/GlobalSuppressions.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/Properties/GlobalSuppressions.cs new file mode 100644 index 0000000..897b1dd --- /dev/null +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Properties/GlobalSuppressions.cs @@ -0,0 +1,8 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.NewtonsoftJson.NewtonsoftJsonConverterException")] diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs index d89c826..56b1606 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/StronglyTypedNewtonsoftConverter.cs @@ -63,7 +63,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer writer.WriteValue(stringValue); } - private IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter typeConverter) + private static IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter typeConverter) { if (typeConverter.InnerType == typeof(byte)) { @@ -144,7 +144,7 @@ private IStronglyTyped ConvertFromLong(long longValue, ICustomTypeConverter type throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(long)}' to '{typeConverter.StrongType.FullName}'"); } - private IStronglyTyped ConvertFromDouble(double doubleValue, ICustomTypeConverter typeConverter) + private static IStronglyTyped ConvertFromDouble(double doubleValue, ICustomTypeConverter typeConverter) { if (typeConverter.InnerType == typeof(double)) { @@ -169,7 +169,7 @@ private IStronglyTyped ConvertFromDouble(double doubleValue, ICustomTypeConverte throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(double)}' to '{typeConverter.StrongType.FullName}'"); } - private IStronglyTyped ConvertFromBigInt(BigInteger bigIntValue, ICustomTypeConverter typeConverter) + private static IStronglyTyped ConvertFromBigInt(BigInteger bigIntValue, ICustomTypeConverter typeConverter) { if (typeConverter.InnerType == typeof(decimal)) { @@ -188,14 +188,14 @@ private IStronglyTyped ConvertFromBigInt(BigInteger bigIntValue, ICustomTypeConv throw new NewtonsoftJsonConverterException(typeConverter.StrongType, $"Can't convert from '{typeof(BigInteger)}' to '{typeConverter.StrongType.FullName}'"); } - private IStronglyTyped ConvertToTimeSpan(object value, ICustomTypeConverter typeConverter) + private static IStronglyTyped ConvertToTimeSpan(object value, ICustomTypeConverter typeConverter) { var timeSpanValue = XmlConvert.ToTimeSpan(value.ToString()); var result = typeConverter.ConvertFrom(timeSpanValue); return (IStronglyTyped)result; } - private bool TryWriteNumber(object value, Type innerType, JsonWriter writer) + private static bool TryWriteNumber(object value, Type innerType, JsonWriter writer) { if (innerType == typeof(decimal)) { diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index 6db06f7..1519582 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -33,12 +33,40 @@ Make compiler your friend, introduce semantics to your code. true + + true + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj.DotSettings b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj.DotSettings new file mode 100644 index 0000000..30e1370 --- /dev/null +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs b/src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs new file mode 100644 index 0000000..9da0837 --- /dev/null +++ b/src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs @@ -0,0 +1,9 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.CodeWriterException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.SyntaxReceiverException")] diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index f1e965d..2c037dd 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -29,6 +29,10 @@ true + + true + + false @@ -45,12 +49,39 @@ - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index 12bde8c..54c1214 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -33,11 +33,35 @@ Make compiler your friend, introduce semantics to your code. true + + true + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped/IStronglyTyped.cs b/src/Xtz.StronglyTyped/IStronglyTyped.cs index 9459e11..777ce87 100644 --- a/src/Xtz.StronglyTyped/IStronglyTyped.cs +++ b/src/Xtz.StronglyTyped/IStronglyTyped.cs @@ -1,5 +1,7 @@ namespace Xtz.StronglyTyped { + // TODO: Check out `Microsoft.CodeAnalysis.PublicApiAnalyzers` analyzer + /// /// Base interface for strongly-typed classes. /// diff --git a/src/Xtz.StronglyTyped/InvalidValueException.cs b/src/Xtz.StronglyTyped/InvalidValueException.cs index d9dd5fb..dd1d089 100644 --- a/src/Xtz.StronglyTyped/InvalidValueException.cs +++ b/src/Xtz.StronglyTyped/InvalidValueException.cs @@ -6,7 +6,8 @@ namespace Xtz.StronglyTyped [ExcludeFromCodeCoverage] public class InvalidValueException : StronglyTypedException { - public InvalidValueException(Type type, string errorMessage) : base(type, errorMessage) + public InvalidValueException(Type type, string errorMessage) + : base(type, errorMessage) { } } diff --git a/src/Xtz.StronglyTyped/Properties/GlobalSuppressions.cs b/src/Xtz.StronglyTyped/Properties/GlobalSuppressions.cs new file mode 100644 index 0000000..e24c699 --- /dev/null +++ b/src/Xtz.StronglyTyped/Properties/GlobalSuppressions.cs @@ -0,0 +1,11 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.InvalidValueException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.StronglyTypedException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.TypeConverters.JsonConverterException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.TypeConverters.StringTypeConverterException")] diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index e7ccbd7..2acfebf 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -33,11 +33,39 @@ Make compiler your friend, introduce semantics to your code. true + + true + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj.DotSettings b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj.DotSettings new file mode 100644 index 0000000..30e1370 --- /dev/null +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file From a9d43c223da9040bb66a705e7d5c11f9116f4c9d Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 18:42:17 +0200 Subject: [PATCH 36/37] Set analysis level for .NET analyzer --- .../Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj | 1 + .../Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj | 1 + .../Xtz.StronglyTyped.BuiltinTypes.csproj | 1 + .../Xtz.StronglyTyped.EntityFramework.csproj | 1 + .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 1 + .../Xtz.StronglyTyped.SourceGenerator.csproj | 1 + .../Xtz.StronglyTyped.Swashbuckle.csproj | 1 + src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj | 1 + 8 files changed, 8 insertions(+) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index bdd519b..17e4992 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -35,6 +35,7 @@ Make compiler your friend, introduce semantics to your code. true + latest diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index b336e5a..57365fc 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -35,6 +35,7 @@ Make compiler your friend, introduce semantics to your code. true + latest diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index 7cd337a..b72367a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -35,6 +35,7 @@ Make compiler your friend, introduce semantics to your code. true + latest diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index faec0f1..f25db64 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -35,6 +35,7 @@ Make compiler your friend, introduce semantics to your code. true + latest diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index 1519582..e4bb51e 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -35,6 +35,7 @@ Make compiler your friend, introduce semantics to your code. true + latest diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index 2c037dd..25ff173 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -31,6 +31,7 @@ true + latest diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index 54c1214..589a66c 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -35,6 +35,7 @@ Make compiler your friend, introduce semantics to your code. true + latest diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index 2acfebf..fcb266b 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -35,6 +35,7 @@ Make compiler your friend, introduce semantics to your code. true + latest From ed7ed3344bbe9648da5b764ca1022358042993f4 Mon Sep 17 00:00:00 2001 From: Vlad DX Date: Sat, 17 Jul 2021 21:30:44 +0200 Subject: [PATCH 37/37] Apply code analysis suggestions or explicitly suppress them --- src/.editorconfig | 4 ++ .../GlobalSuppressions.cs | 8 +++ .../Xtz.StronglyTyped.Benchmark/Program.cs | 2 +- .../Xtz.StronglyTyped.Benchmark.csproj | 4 ++ .../Xtz.StronglyTyped.Playground.csproj | 4 ++ .../GlobalSuppressions.cs | 12 ++++ .../WebApiTests.cs | 4 +- ...onglyTyped.Api_3_1.IntegrationTests.csproj | 4 ++ .../CommerceAutoDataTests.cs | 12 ++-- .../GlobalSuppressions.cs | 8 +++ .../NameAutoDataTests.cs | 2 +- ...glyTyped.BogusAutoFixture.UnitTests.csproj | 4 ++ ...ed.EntityFramework.IntegrationTests.csproj | 4 ++ .../ConfigurationTests.cs | 3 +- .../Xtz.StronglyTyped.IntegrationTests.csproj | 4 ++ .../DeserializationTests.cs | 2 +- .../GlobalSuppressions.cs | 9 +++ ...onglyTyped.NewtonsoftJson.UnitTests.csproj | 4 ++ .../BasicGeneratorTests.cs | 6 +- .../CustomizedGeneratorTests.cs | 62 +++++++++---------- .../GlobalSuppressions.cs | 13 ++++ .../SimpleUnloadableAssemblyLoadContext.cs | 2 +- .../Properties/GlobalSuppressions.cs | 9 --- .../StructGeneratorTests.cs | 6 +- ...ed.SourceGenerator.IntegrationTests.csproj | 4 ++ .../Basic/StringEqualityOperatorsTests.cs | 18 +++--- .../GlobalSuppressions.cs | 5 ++ .../SystemTextJson/DeserializationTests.cs | 2 +- .../SystemTextJson/SerializationTests.cs | 21 ++++++- .../TypeConverters/MacAddressTests.cs | 27 -------- .../Values/EqualityOperatorsTests.cs | 25 ++++---- .../Xtz.StronglyTyped.UnitTests.csproj | 4 ++ ...onglyTyped.BuiltinTypes.AutoFixture.csproj | 4 ++ .../CompanyFakerBuilder.cs | 2 +- .../FakerBuilderException.cs | 16 ++++- .../{Properties => }/GlobalSuppressions.cs | 0 ...tz.StronglyTyped.BuiltinTypes.Bogus.csproj | 8 +++ .../Xtz.StronglyTyped.BuiltinTypes.csproj | 4 ++ .../Xtz.StronglyTyped.EntityFramework.csproj | 4 ++ .../{Properties => }/GlobalSuppressions.cs | 0 .../NewtonsoftJsonConverterException.cs | 10 +++ .../Xtz.StronglyTyped.NewtonsoftJson.csproj | 4 ++ .../Attributes/Allow.cs | 2 +- .../Attributes/StrongTypeAttribute.cs | 4 +- .../CodeWriter.cs | 8 +-- .../DataExtractor.cs | 2 +- .../Exceptions/CodeWriterException.cs | 10 +++ .../Exceptions/SyntaxReceiverException.cs | 10 +++ .../GlobalSuppressions.cs | 9 ++- .../StronglyTypedGenerator.cs | 17 ++--- .../Xtz.StronglyTyped.SourceGenerator.csproj | 4 ++ .../Xtz.StronglyTyped.Swashbuckle.csproj | 4 ++ src/Xtz.StronglyTyped.sln | 3 + src/Xtz.StronglyTyped.sln.DotSettings | 3 +- .../{Properties => }/GlobalSuppressions.cs | 4 ++ .../InvalidValueException.cs | 10 +++ .../StronglyTyped.Operators.cs | 15 ++--- .../StronglyTypedException.cs | 19 ++++++ .../NewtonsoftJsonConverterException.cs | 10 +++ .../StringTypeConverterException.cs | 10 +++ .../StronglyTypedJsonConverter.cs | 15 ++--- .../TypeConverters/TypeConverterException.cs | 16 ++++- .../Xtz.StronglyTyped.csproj | 4 ++ 63 files changed, 375 insertions(+), 154 deletions(-) create mode 100644 src/.editorconfig create mode 100644 src/Misc/Xtz.StronglyTyped.Benchmark/GlobalSuppressions.cs create mode 100644 src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/GlobalSuppressions.cs create mode 100644 src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/GlobalSuppressions.cs create mode 100644 src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/GlobalSuppressions.cs create mode 100644 src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/GlobalSuppressions.cs delete mode 100644 src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs rename src/Xtz.StronglyTyped.BuiltinTypes.Bogus/{Properties => }/GlobalSuppressions.cs (100%) rename src/Xtz.StronglyTyped.NewtonsoftJson/{Properties => }/GlobalSuppressions.cs (100%) rename src/Xtz.StronglyTyped/{Properties => }/GlobalSuppressions.cs (56%) diff --git a/src/.editorconfig b/src/.editorconfig new file mode 100644 index 0000000..451d6da --- /dev/null +++ b/src/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# RCS1124: Inline local variable. +dotnet_diagnostic.RCS1124.severity = none diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/GlobalSuppressions.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/GlobalSuppressions.cs new file mode 100644 index 0000000..24d3acb --- /dev/null +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/GlobalSuppressions.cs @@ -0,0 +1,8 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Major Code Smell", "S1854:Unused assignments should be removed", Justification = "Vlad DX: Reviewed. It's intentional", Scope = "member", Target = "~M:Xtz.StronglyTyped.Benchmark.Program.Main()")] diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs b/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs index d53975f..62dea7b 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Program.cs @@ -7,7 +7,7 @@ public static class Program { public const int VALUE_COUNT = 1000; - public static void Main(string[] args) + public static void Main() { // ReSharper disable once RedundantAssignment var config = DefaultConfig.Instance; diff --git a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj index d2a9b57..f46585b 100644 --- a/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj +++ b/src/Misc/Xtz.StronglyTyped.Benchmark/Xtz.StronglyTyped.Benchmark.csproj @@ -21,6 +21,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj index df5d73a..2582ab7 100644 --- a/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj +++ b/src/Misc/Xtz.StronglyTyped.Playground/Xtz.StronglyTyped.Playground.csproj @@ -33,6 +33,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/GlobalSuppressions.cs new file mode 100644 index 0000000..b7c13d2 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/GlobalSuppressions.cs @@ -0,0 +1,12 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Minor Code Smell", "S1481:Unused local variables should be removed", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApiTests.ShouldDeserialize_ForStandardEndpoint_WhenNewtonsoftUsed~System.Threading.Tasks.Task")] +[assembly: SuppressMessage("Minor Code Smell", "S1481:Unused local variables should be removed", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApiTests.ShouldDeserialize_ForStandardEndpoint_WhenSystemTextJsonUsed~System.Threading.Tasks.Task")] +[assembly: SuppressMessage("Minor Code Smell", "S1481:Unused local variables should be removed", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApiTests.ShouldDeserialize_ForStronglyTypedEndpoint_WhenNewtonsoftUsed~System.Threading.Tasks.Task")] +[assembly: SuppressMessage("Minor Code Smell", "S1481:Unused local variables should be removed", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApiTests.ShouldDeserialize_ForStronglyTypedEndpoint_WhenSystemTextJsonUsed~System.Threading.Tasks.Task")] +[assembly: SuppressMessage("Style", "IDE0059:Unnecessary assignment of a value", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.Api_3_1.IntegrationTests.WebApiTests.ShouldDeserialize_ForStandardEndpoint_WhenNewtonsoftUsed~System.Threading.Tasks.Task")] diff --git a/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs index 1d45c2c..617c704 100644 --- a/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs +++ b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/WebApiTests.cs @@ -10,12 +10,12 @@ namespace Xtz.StronglyTyped.Api_3_1.IntegrationTests { - public class WebApiTests : IDisposable + public sealed class WebApiTests : IDisposable { private readonly JsonSerializerSettings _jsonSerializerSettings; private readonly WebApiFactory _factory = new(); - + private readonly HttpClient _client; public WebApiTests() diff --git a/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj index f32e949..0958b98 100644 --- a/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.Api_3_1.IntegrationTests/Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj @@ -28,6 +28,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs index 3962f35..2f20f4f 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/CommerceAutoDataTests.cs @@ -40,18 +40,18 @@ public void ShouldGenerateStronglyTypedValues( [StrongAutoData] public void ShouldGenerateStronglyTypedPrice(Price value) { - Assert.IsFalse(value.Currency.Code.ToString().IsBogusGeneratedValue()); - Assert.IsFalse(value.Currency.Name.ToString().IsBogusGeneratedValue()); - Assert.IsFalse(value.Currency.Symbol?.ToString().IsBogusGeneratedValue()); + Assert.That(value.Currency.Code.ToString().IsBogusGeneratedValue(), Is.False); + Assert.That(value.Currency.Name.ToString().IsBogusGeneratedValue(), Is.False); + Assert.That(value.Currency.Symbol?.ToString().IsBogusGeneratedValue(), Is.False); } [Test] [StrongAutoData] public void ShouldGenerateStronglyTypedProductFullName(ProductFullName value) { - Assert.IsFalse(value.ProductAdjective.ToString().IsBogusGeneratedValue()); - Assert.IsFalse(value.ProductMaterial.ToString().IsBogusGeneratedValue()); - Assert.IsFalse(value.ProductShortName.ToString().IsBogusGeneratedValue()); + Assert.That(value.ProductAdjective.ToString().IsBogusGeneratedValue(), Is.False); + Assert.That(value.ProductMaterial.ToString().IsBogusGeneratedValue(), Is.False); + Assert.That(value.ProductShortName.ToString().IsBogusGeneratedValue(), Is.False); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/GlobalSuppressions.cs new file mode 100644 index 0000000..d69ff69 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/GlobalSuppressions.cs @@ -0,0 +1,8 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Major Code Smell", "S4144:Methods should not have identical implementations", Justification = "Vlad DX: Reviewed. It's intentional", Scope = "member", Target = "~M:Xtz.StronglyTyped.BogusAutoFixture.UnitTests.IdAutoDataTests.ShouldGenerateStronglyTypedValues_WithCustomSpecimen(UnitTests.EmployeeGuidId,UnitTests.EmployeeIntId)")] diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs index 85418cc..786506e 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/NameAutoDataTests.cs @@ -32,7 +32,7 @@ public void ShouldGenerateStronglyTypedValues( [StrongAutoData] public void ShouldGenerateStronglyTypedFullName(FullName value) { - Assert.IsFalse(value.FirstName.ToString()!.IsBogusGeneratedValue()); + Assert.That(value.FirstName.ToString()!.IsBogusGeneratedValue(), Is.False); } } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj index 14bb899..8d85df5 100644 --- a/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests/Xtz.StronglyTyped.BogusAutoFixture.UnitTests.csproj @@ -27,6 +27,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj index d2a88b7..c0ab62c 100644 --- a/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.EntityFramework.IntegrationTests/Xtz.StronglyTyped.EntityFramework.IntegrationTests.csproj @@ -19,6 +19,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs b/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs index 980a5af..7f737ce 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/ConfigurationTests.cs @@ -23,8 +23,9 @@ public void ShouldNotFail_WhenBindingFromAppSettingsJson() configuration.Bind("Xtz.Test", settings); // Assert - + // Shouldn't fail + Assert.That(settings.Country, Is.Not.Null); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj index 6950dda..62b5c22 100644 --- a/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.IntegrationTests/Xtz.StronglyTyped.IntegrationTests.csproj @@ -42,6 +42,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs index f25d132..4b3477e 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/DeserializationTests.cs @@ -244,7 +244,7 @@ public void ShouldDeserialize_ToStronglyTypedBoolClass() var stronglyTyped = values .Select(x => new SerializationDto(x)) .ToArray(); - var json = "[{\"testValue\": false }, {\"testValue\": true }]"; + const string json = "[{\"testValue\": false }, {\"testValue\": true }]"; // Act diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/GlobalSuppressions.cs new file mode 100644 index 0000000..23b42d3 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/GlobalSuppressions.cs @@ -0,0 +1,9 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Vlad DX: Reviewed. Not possible", Scope = "member", Target = "~M:Xtz.StronglyTyped.NewtonsoftJson.UnitTests.DeserializationTests.#cctor")] +[assembly: SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Vlad DX: Reviewed. Not possible", Scope = "member", Target = "~M:Xtz.StronglyTyped.NewtonsoftJson.UnitTests.SerializationTests.#cctor")] diff --git a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj index 85e9132..9761e6a 100644 --- a/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests/Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj @@ -29,6 +29,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs index e7e1629..d346430 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/BasicGeneratorTests.cs @@ -111,7 +111,7 @@ public partial class City2 Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(4)); PrintDiagnosticsToDebug(outputCompilation); - Assert.IsFalse(outputCompilation.GetDiagnostics().IsEmpty); + Assert.That(outputCompilation.GetDiagnostics().IsEmpty, Is.False); } [Test] @@ -157,7 +157,7 @@ public class City2 Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(5)); PrintDiagnosticsToDebug(outputCompilation); - Assert.IsFalse(outputCompilation.GetDiagnostics().IsEmpty); + Assert.That(outputCompilation.GetDiagnostics().IsEmpty, Is.False); } [Test] @@ -193,7 +193,7 @@ public partial class City2 Assert.That(outputCompilation.SyntaxTrees.Count(), Is.EqualTo(5)); PrintDiagnosticsToDebug(outputCompilation); - Assert.IsFalse(outputCompilation.GetDiagnostics().IsEmpty); + Assert.That(outputCompilation.GetDiagnostics().IsEmpty, Is.False); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs index 19b2e29..d01bb63 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/CustomizedGeneratorTests.cs @@ -14,7 +14,7 @@ public void ShouldGenerate_NoNull_ByDefault() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -68,7 +68,7 @@ public void ShouldGenerate_NoEmpty_ByDefault() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -122,7 +122,7 @@ public void ShouldGenerate_NoNull_WhenStringTypeProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -176,7 +176,7 @@ public void ShouldGenerate_NoEmpty_WhenStringTypeProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -265,7 +265,6 @@ void Action() //// // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls //// driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - //// var compiledBytes = CompileBytes(outputCompilation); //// [ExcludeFromCodeCoverage] @@ -287,7 +286,7 @@ public void ShouldGenerate_NoNull_WhenUriTypeProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -341,7 +340,7 @@ public void ShouldGenerate_NoNull_WhenMailAddressTypeProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -396,7 +395,7 @@ public void ShouldGenerate_NoNull_WhenIPAddressTypeProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -451,7 +450,7 @@ public void ShouldGenerate_NoNull_WhenPhysicalAddressTypeProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -506,7 +505,7 @@ public void ShouldGenerate_NoNull_WhenAllowUnknownProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -520,7 +519,7 @@ public static int Main(string[] args) } } - [StrongType(Allow.Unknown)] + [StrongType(Allow.None)] public partial class City2 { } @@ -560,7 +559,7 @@ public void ShouldGenerate_NoEmpty_WhenAllowUnknownProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -574,7 +573,7 @@ public static int Main(string[] args) } } - [StrongType(Allow.Unknown)] + [StrongType(Allow.None)] public partial class City2 { } @@ -614,7 +613,7 @@ public void ShouldGenerate_NoNull_WhenStringTypeAllowUnknownProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -628,7 +627,7 @@ public static int Main(string[] args) } } - [StrongType(typeof(string), Allow.Unknown)] + [StrongType(typeof(string), Allow.None)] public partial class CityUri2 { } @@ -668,7 +667,7 @@ public void ShouldGenerate_NoEmpty_WhenStringTypeAllowUnknownProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -682,7 +681,7 @@ public static int Main(string[] args) } } - [StrongType(typeof(string), Allow.Unknown)] + [StrongType(typeof(string), Allow.None)] public partial class CityUri2 { } @@ -722,7 +721,7 @@ public void ShouldGenerate_NoNull_WhenInvalidAllowProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -776,7 +775,7 @@ public void ShouldGenerate_NoEmpty_WhenInvalidAllowProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -830,7 +829,7 @@ public void ShouldGenerate_NoNull_WhenStringTypeInvalidAllowProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -884,7 +883,7 @@ public void ShouldGenerate_NoEmpty_WhenStringTypeInvalidAllowProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -933,6 +932,7 @@ void Action() Assert.That(exception?.InnerException?.Message.Contains("''"), Is.True); } +#pragma warning disable S125 // [Test] // public void ShouldGenerate_AllowNull_WhenAllowNullProvided() // { @@ -973,7 +973,6 @@ void Action() // // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls // driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - // var compiledBytes = CompileBytes(outputCompilation); // var result = LoadAndExecute(compiledBytes); @@ -1023,7 +1022,6 @@ void Action() // // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls // driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - // var compiledBytes = CompileBytes(outputCompilation); // var result = LoadAndExecute(compiledBytes); @@ -1073,7 +1071,6 @@ void Action() // // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls // driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - // var compiledBytes = CompileBytes(outputCompilation); // var result = LoadAndExecute(compiledBytes); @@ -1123,7 +1120,6 @@ void Action() // // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls // driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - // var compiledBytes = CompileBytes(outputCompilation); // var result = LoadAndExecute(compiledBytes); @@ -1132,13 +1128,14 @@ void Action() // Assert.AreEqual(0, result); // } +#pragma warning restore S125 [Test] public void ShouldGenerate_AllowEmpty_WhenAllowEmptyProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -1187,7 +1184,7 @@ public void ShouldGenerate_AllowEmpty_WhenDoubleAllowEmptyProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -1222,7 +1219,6 @@ public partial class City2 // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - var compiledBytes = CompileBytes(outputCompilation); var result = LoadAndExecute(compiledBytes); @@ -1237,7 +1233,7 @@ public void ShouldGenerate_AllowEmpty_WhenTripleAllowEmptyProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -1272,7 +1268,6 @@ public partial class City2 // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - var compiledBytes = CompileBytes(outputCompilation); var result = LoadAndExecute(compiledBytes); @@ -1287,7 +1282,7 @@ public void ShouldGenerate_AllowEmpty_WhenNamedEmptyProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -1322,7 +1317,6 @@ public partial class City2 // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - var compiledBytes = CompileBytes(outputCompilation); var result = LoadAndExecute(compiledBytes); @@ -1332,6 +1326,7 @@ public partial class City2 Assert.That(result, Is.EqualTo(0)); } +#pragma warning disable S125 // [Test] // public void ShouldGenerate_AllowEmpty_WhenTypeAndAllowNullEmptyProvided() // { @@ -1372,7 +1367,6 @@ public partial class City2 // // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls // driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - // var compiledBytes = CompileBytes(outputCompilation); // var result = LoadAndExecute(compiledBytes); @@ -1422,7 +1416,6 @@ public partial class City2 // // NOTE: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls // driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out _); - // var compiledBytes = CompileBytes(outputCompilation); // var result = LoadAndExecute(compiledBytes); @@ -1431,5 +1424,6 @@ public partial class City2 // Assert.AreEqual(0, result); // } +#pragma warning restore S125 } } \ No newline at end of file diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/GlobalSuppressions.cs new file mode 100644 index 0000000..bbb53d1 --- /dev/null +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/GlobalSuppressions.cs @@ -0,0 +1,13 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.GeneratorTestsException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.TestsExecutionException")] +[assembly: SuppressMessage("Major Code Smell", "S1118:Utility classes should not have public constructors", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.GeneratorTestsBase")] +[assembly: SuppressMessage("Performance", "RCS1197:Optimize StringBuilder.Append/AppendLine call.", Justification = "Vlad DX: Reviewed. More readable", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.GeneratorTestsBase.CompileBytes(Microsoft.CodeAnalysis.Compilation)~System.Byte[]")] +[assembly: SuppressMessage("Major Code Smell", "S4144:Methods should not have identical implementations", Justification = "Vlad DX: Reviewed. It's intentional", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.InnerTypesGeneratorTests.ShouldGenerateClass_ForKnownTypes(System.Type)")] +[assembly: SuppressMessage("Major Code Smell", "S4144:Methods should not have identical implementations", Justification = "Vlad DX: Reviewed. It's intentional", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.InnerTypesGeneratorTests.ShouldGenerateStruct_ForKnownTypes(System.Type)")] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/SimpleUnloadableAssemblyLoadContext.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/SimpleUnloadableAssemblyLoadContext.cs index 5eba32e..d6d55bb 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/SimpleUnloadableAssemblyLoadContext.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Misc/SimpleUnloadableAssemblyLoadContext.cs @@ -4,7 +4,7 @@ namespace Xtz.StronglyTyped.SourceGenerator.IntegrationTests { - internal class SimpleUnloadableAssemblyLoadContext : AssemblyLoadContext, IDisposable + internal sealed class SimpleUnloadableAssemblyLoadContext : AssemblyLoadContext, IDisposable { public SimpleUnloadableAssemblyLoadContext() : base(true) diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs deleted file mode 100644 index 5748ce2..0000000 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Properties/GlobalSuppressions.cs +++ /dev/null @@ -1,9 +0,0 @@ -// This file is used by Code Analysis to maintain SuppressMessage -// attributes that are applied to this project. -// Project-level suppressions either have no target or are given -// a specific target and scoped to a namespace, type, member, etc. - -using System.Diagnostics.CodeAnalysis; - -[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.GeneratorTestsException")] -[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.IntegrationTests.TestsExecutionException")] diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/StructGeneratorTests.cs b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/StructGeneratorTests.cs index f12c14d..b580ea0 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/StructGeneratorTests.cs +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/StructGeneratorTests.cs @@ -11,7 +11,7 @@ public void ShouldGeneratePartialStruct_WhenAttributeProvidedAndIsValidExists() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped; @@ -56,7 +56,7 @@ public void ShouldGeneratePartialStruct_WhenAttributeProvidedAndToStringExists() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; @@ -88,7 +88,7 @@ public void ShouldGenerateThrowIfInvalid_WhenIsValidMethodProvided() { //// Arrange - var sourceCode = @" + const string sourceCode = @" namespace IntegrationTests.Generated { using Xtz.StronglyTyped.SourceGenerator; diff --git a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj index 9d643ce..81137d4 100644 --- a/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests/Xtz.StronglyTyped.SourceGenerator.IntegrationTests.csproj @@ -31,6 +31,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs index 80c207e..cef41cf 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Basic/StringEqualityOperatorsTests.cs @@ -16,8 +16,8 @@ public void EqualsOperator_ShouldBeTrue_ForSameStronglyTypedValues() //// Assert - // ReSharper disable once EqualExpressionComparison #pragma warning disable CS1718 // Comparison made to same variable + // ReSharper disable once EqualExpressionComparison Assert.That(value == value, Is.True); #pragma warning restore CS1718 // Comparison made to same variable } @@ -82,8 +82,8 @@ public void EqualsOperator_ShouldBeFalse_ForStronglyTypedValues_GivenDifferentSt //// Assert - Assert.IsFalse(value1 == value2); - Assert.IsFalse(value2 == value1); + Assert.That(value1 == value2, Is.False); + Assert.That(value2 == value1, Is.False); } [Test] @@ -98,8 +98,8 @@ public void EqualsOperator_ShouldBeFalse_ForStronglyTypedAndObject_GivenDifferen //// Assert - Assert.IsFalse(value == objValue); - Assert.IsFalse(objValue == value); + Assert.That(value == objValue, Is.False); + Assert.That(objValue == value, Is.False); } [Test] @@ -114,8 +114,8 @@ public void NotEqualsOperator_ShouldBeFalse_ForStronglyTypedValues_GivenSameStri //// Assert - Assert.IsFalse(value1 != value2); - Assert.IsFalse(value2 != value1); + Assert.That(value1 != value2, Is.False); + Assert.That(value2 != value1, Is.False); } [Test] @@ -130,8 +130,8 @@ public void NotEqualsOperator_ShouldBeFalse_ForStronglyTypedAndObject_GivenSameS //// Assert - Assert.IsFalse(value != objValue); - Assert.IsFalse(objValue != value); + Assert.That(value != objValue, Is.False); + Assert.That(objValue != value, Is.False); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs index 76c9ad8..3a7ab8a 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/GlobalSuppressions.cs @@ -10,3 +10,8 @@ [assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.UserIdTests.UserId_ShouldThrow_GivenInvalidValue")] [assembly: SuppressMessage("Performance", "CA1806:Do not ignore method results", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Values.CreationTests.ShouldThrow_GivenNull_ForAllowEmpty")] [assembly: SuppressMessage("Assertion", "NUnit2021:Incompatible types for EqualTo constraint", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.UserIdTests.UserId_ShouldInstantiate_GivenGuid")] +[assembly: SuppressMessage("Minor Code Smell", "S3241:Methods should not return values that are never used", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.CountryIdTests.CountryIdImplicitOperator_ShouldThrow_GivenDifferentType(System.Object)")] +[assembly: SuppressMessage("Assertion", "NUnit2010:Use EqualConstraint for better assertion messages in case of failure", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.StringEqualityOperatorsTests.EqualsOperator_ShouldBeTrue_ForSameStronglyTypedValues")] +[assembly: SuppressMessage("Major Bug", "S1764:Identical expressions should not be used on both sides of a binary operator", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "member", Target = "~M:Xtz.StronglyTyped.UnitTests.Basic.StringEqualityOperatorsTests.EqualsOperator_ShouldBeTrue_ForSameStronglyTypedValues")] +[assembly: SuppressMessage("Assertion", "NUnit2010:Use EqualConstraint for better assertion messages in case of failure", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "type", Target = "~T:Xtz.StronglyTyped.UnitTests.Values.EqualityOperatorsTests")] +[assembly: SuppressMessage("Assertion", "NUnit2010:Use EqualConstraint for better assertion messages in case of failure", Justification = "Vlad DX: Reviewed (unit tests)", Scope = "type", Target = "~T:Xtz.StronglyTyped.UnitTests.Basic.StringEqualityOperatorsTests")] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs index ab07073..ce3d4fb 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/DeserializationTests.cs @@ -241,7 +241,7 @@ public void ShouldDeserialize_ToStronglyTypedBoolClass() var stronglyTyped = values .Select(x => new SerializationDto(x)) .ToArray(); - var json = "[{\"testValue\": false }, {\"testValue\": true }]"; + const string json = "[{\"testValue\": false }, {\"testValue\": true }]"; // Act diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs index 46a852a..be89dd5 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/SystemTextJson/SerializationTests.cs @@ -296,7 +296,7 @@ public void ShouldSerialize_GivenStronglyTypedCharClass(char value) [TestCase(34634)] [TestCase(6534.353)] //[TestCase(79228162514264337593543950335m)] - public void ShouldSerialize_GivenStronglyTypedByteClass(decimal value) + public void ShouldSerialize_GivenStronglyTypedDecimalClass(decimal value) { // Arrange @@ -312,6 +312,25 @@ public void ShouldSerialize_GivenStronglyTypedByteClass(decimal value) Assert.That(result, Is.EqualTo(json)); } + [Test] + public void ShouldSerialize_GivenStronglyTypedDecimalClass() + { + // Arrange + + var values = new [] { decimal.MinValue, -968.2566m, decimal.MinusOne, decimal.Zero, decimal.One, decimal.MaxValue }; + + var stronglyTyped = values.Select(x => (StronglyTypedDecimal)x); + var json = JsonSerializer.Serialize(values); + + // Act + + var result = JsonSerializer.Serialize(stronglyTyped); + + // Assert + + Assert.That(result, Is.EqualTo(json)); + } + [Test] [TestCase(double.MinValue)] [TestCase(double.Epsilon)] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/MacAddressTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/MacAddressTests.cs index 33de6b5..6fee8b3 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/MacAddressTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/TypeConverters/MacAddressTests.cs @@ -88,33 +88,6 @@ public void TypeConverter_ShouldConvertToString_GivenStronglyTyped(string value) Assert.That(result, Is.EqualTo(expected)); } - [TestCase("00:11:22:33:44:55")] - [TestCase("00-11-22-33-44-55")] - [TestCase("A0:A1:A2:A3:A4:A5")] - [TestCase("A0-A1-A2-A3-A4-A5")] - [TestCase("554433221100")] - [TestCase("ffeeddbbccaa")] - [TestCase("FFEEDDBBCCAA")] - [Test] - public void TypeConverter_ShouldConvertToString_WhenImplicitlyCastedToString(string value) - { - //// Arrange - - var stronglyTypedValue = new MacAddress(PhysicalAddress.Parse(ValueToString(value))); - var strongType = typeof(MacAddress); - var typeConverter = TypeDescriptor.GetConverter(strongType); - - var expected = ValueToString(value); - - //// Act - - var result = typeConverter.ConvertToString(stronglyTypedValue); - - //// Assert - - Assert.That(result, Is.EqualTo(expected)); - } - private static string ValueToString(string value) { var normalized = value diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/EqualityOperatorsTests.cs b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/EqualityOperatorsTests.cs index 3176e50..ce28132 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Values/EqualityOperatorsTests.cs +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Values/EqualityOperatorsTests.cs @@ -16,10 +16,7 @@ public void EqualsOperator_ShouldBeTrue_ForSameStronglyTypedValues() //// Assert -#pragma warning disable CS1718 // Comparison made to same variable - // ReSharper disable once EqualExpressionComparison Assert.That(value == value, Is.True); -#pragma warning restore CS1718 // Comparison made to same variable } [Test] @@ -47,12 +44,12 @@ public void EqualsOperator_ShouldBeFalse_ForStronglyTypedValue_AndSameString() //// Act var value1 = new Country("Norway"); - var value2 = "Norway"; + const string value2 = "Norway"; //// Assert - Assert.IsFalse(value1 == value2); - Assert.IsFalse(value2 == value1); + Assert.That(value1 == value2, Is.False); + Assert.That(value2 == value1, Is.False); } [Test] @@ -99,8 +96,8 @@ public void EqualsOperator_ShouldBeFalse_ForStronglyTypedValues_GivenDifferentSt //// Assert - Assert.IsFalse(value1 == value2); - Assert.IsFalse(value2 == value1); + Assert.That(value1 == value2, Is.False); + Assert.That(value2 == value1, Is.False); } [Test] @@ -115,8 +112,8 @@ public void EqualsOperator_ShouldBeFalse_ForStronglyTypedAndObject_GivenDifferen //// Assert - Assert.IsFalse(value == objValue); - Assert.IsFalse(objValue == value); + Assert.That(value == objValue, Is.False); + Assert.That(objValue == value, Is.False); } [Test] @@ -131,8 +128,8 @@ public void NotEqualsOperator_ShouldBeFalse_ForStronglyTypedValues_GivenSameStri //// Assert - Assert.IsFalse(value1 != value2); - Assert.IsFalse(value2 != value1); + Assert.That(value1 != value2, Is.False); + Assert.That(value2 != value1, Is.False); } [Test] @@ -147,8 +144,8 @@ public void NotEqualsOperator_ShouldBeFalse_ForStronglyTypedAndObject_GivenSameS //// Assert - Assert.IsFalse(value != objValue); - Assert.IsFalse(objValue != value); + Assert.That(value != objValue, Is.False); + Assert.That(objValue != value, Is.False); } [Test] diff --git a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj index e9a2900..794c282 100644 --- a/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj +++ b/src/Tests/Xtz.StronglyTyped.UnitTests/Xtz.StronglyTyped.UnitTests.csproj @@ -28,6 +28,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj index 17e4992..8a19b8a 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.AutoFixture/Xtz.StronglyTyped.BuiltinTypes.AutoFixture.csproj @@ -71,6 +71,10 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CompanyFakerBuilder.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CompanyFakerBuilder.cs index 77aac2f..c9243f6 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CompanyFakerBuilder.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/CompanyFakerBuilder.cs @@ -5,7 +5,7 @@ namespace Xtz.StronglyTyped.BuiltinTypes.Bogus { public class CompanyFakerBuilder : BaseFakerBuilder { - private CommerceFakerBuilder _commerceFakerBuilder; + private readonly CommerceFakerBuilder _commerceFakerBuilder; public CompanyFakerBuilder(bool useFakerCache = true) : base(useFakerCache) diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/FakerBuilderException.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/FakerBuilderException.cs index 85692ef..41b1883 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/FakerBuilderException.cs +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/FakerBuilderException.cs @@ -1,16 +1,28 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped.BuiltinTypes.Bogus { [ExcludeFromCodeCoverage] + [Serializable] public class FakerBuilderException : ApplicationException { - public FakerBuilderException(string message) : base(message) + public FakerBuilderException(string message) + : base(message) { } - public FakerBuilderException(string message, Exception innerException) : base(message, innerException) + public FakerBuilderException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Constructor is used for deserialization. + /// + protected FakerBuilderException(SerializationInfo info, StreamingContext context) + : base(info, context) { } } diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Properties/GlobalSuppressions.cs b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/GlobalSuppressions.cs similarity index 100% rename from src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Properties/GlobalSuppressions.cs rename to src/Xtz.StronglyTyped.BuiltinTypes.Bogus/GlobalSuppressions.cs diff --git a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj index 57365fc..bf2f982 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes.Bogus/Xtz.StronglyTyped.BuiltinTypes.Bogus.csproj @@ -42,6 +42,10 @@ Make compiler your friend, introduce semantics to your code. + + + + @@ -69,6 +73,10 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj index b72367a..0628d6d 100644 --- a/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj +++ b/src/Xtz.StronglyTyped.BuiltinTypes/Xtz.StronglyTyped.BuiltinTypes.csproj @@ -72,6 +72,10 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj index f25db64..76cfcbe 100644 --- a/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj +++ b/src/Xtz.StronglyTyped.EntityFramework/Xtz.StronglyTyped.EntityFramework.csproj @@ -64,6 +64,10 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Properties/GlobalSuppressions.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/GlobalSuppressions.cs similarity index 100% rename from src/Xtz.StronglyTyped.NewtonsoftJson/Properties/GlobalSuppressions.cs rename to src/Xtz.StronglyTyped.NewtonsoftJson/GlobalSuppressions.cs diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs b/src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs index e44b0e0..f21d8e2 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/NewtonsoftJsonConverterException.cs @@ -1,14 +1,24 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped.NewtonsoftJson { [ExcludeFromCodeCoverage] + [Serializable] public class NewtonsoftJsonConverterException : StronglyTypedException { public NewtonsoftJsonConverterException(Type type, string errorMessage) : base(type, errorMessage) { } + + /// + /// Constructor is used for deserialization. + /// + protected NewtonsoftJsonConverterException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj index e4bb51e..facb6af 100644 --- a/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj +++ b/src/Xtz.StronglyTyped.NewtonsoftJson/Xtz.StronglyTyped.NewtonsoftJson.csproj @@ -68,6 +68,10 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Attributes/Allow.cs b/src/Xtz.StronglyTyped.SourceGenerator/Attributes/Allow.cs index b6f9cf3..b705707 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Attributes/Allow.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/Attributes/Allow.cs @@ -8,7 +8,7 @@ namespace Xtz.StronglyTyped.SourceGenerator [Flags] public enum Allow { - Unknown = 0, + None = 0, /// /// Allow empty strings or default struct values (but doesn't affect numbers). /// diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Attributes/StrongTypeAttribute.cs b/src/Xtz.StronglyTyped.SourceGenerator/Attributes/StrongTypeAttribute.cs index 166f315..fe7910a 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Attributes/StrongTypeAttribute.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/Attributes/StrongTypeAttribute.cs @@ -11,13 +11,13 @@ public class StrongTypeAttribute : Attribute public Allow Allow { get; } - public StrongTypeAttribute(Type innerType, Allow allow = Allow.Unknown) + public StrongTypeAttribute(Type innerType, Allow allow = Allow.None) { InnerType = innerType; Allow = allow; } - public StrongTypeAttribute(Allow allow = Allow.Unknown) + public StrongTypeAttribute(Allow allow = Allow.None) : this(typeof(string), allow) { } diff --git a/src/Xtz.StronglyTyped.SourceGenerator/CodeWriter.cs b/src/Xtz.StronglyTyped.SourceGenerator/CodeWriter.cs index 1ff429f..6c61d6b 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/CodeWriter.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/CodeWriter.cs @@ -33,13 +33,13 @@ public IDisposable BeginScope() { // TODO: Replace by pre-built array of spaces. Do substring Content.Append(new string(' ', IndentLevel * 4)).AppendLine("{"); - IndentLevel += 1; + IndentLevel++; return _scopeTracker; } public void EndScope() { - IndentLevel -= 1; + IndentLevel--; Content.Append(new string(' ', IndentLevel * 4)).AppendLine("}"); } @@ -49,10 +49,10 @@ public void EndScope() public override string ToString() => Content.ToString(); - public class ScopeTracker : IDisposable + public sealed class ScopeTracker : IDisposable { private readonly CodeWriter _parent; - + public ScopeTracker(CodeWriter parent) { _parent = parent; diff --git a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs index 0b3d853..424740b 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/DataExtractor.cs @@ -388,7 +388,7 @@ private static bool HasNameAndReturnType( string expectedName, string expectedReturnType) { - var result = methodDeclarationSyntax?.Identifier.Text == expectedName && methodDeclarationSyntax.ReturnType.ToString() == expectedReturnType; + var result = methodDeclarationSyntax?.Identifier.Text == expectedName && methodDeclarationSyntax?.ReturnType.ToString() == expectedReturnType; return result; } } diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/CodeWriterException.cs b/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/CodeWriterException.cs index 2f4c051..f681490 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/CodeWriterException.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/CodeWriterException.cs @@ -1,9 +1,11 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped.SourceGenerator { [ExcludeFromCodeCoverage] + [Serializable] public class CodeWriterException : ApplicationException { public CodeWriterException(string message) @@ -15,5 +17,13 @@ public CodeWriterException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Constructor is used for deserialization. + /// + protected CodeWriterException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/SyntaxReceiverException.cs b/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/SyntaxReceiverException.cs index 279de07..4c7162b 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/SyntaxReceiverException.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/Exceptions/SyntaxReceiverException.cs @@ -1,9 +1,11 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped.SourceGenerator { [ExcludeFromCodeCoverage] + [Serializable] public class SyntaxReceiverException : ApplicationException { public SyntaxReceiverException(string message) @@ -15,5 +17,13 @@ public SyntaxReceiverException(string message, Exception innerException) : base(message, innerException) { } + + /// + /// Constructor is used for deserialization. + /// + protected SyntaxReceiverException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs b/src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs index 9da0837..a1e46ab 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/GlobalSuppressions.cs @@ -5,5 +5,10 @@ using System.Diagnostics.CodeAnalysis; -[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.CodeWriterException")] -[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.SyntaxReceiverException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed. It's intended", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.CodeWriterException")] +[assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed. It's intended", Scope = "type", Target = "~T:Xtz.StronglyTyped.SourceGenerator.SyntaxReceiverException")] +[assembly: SuppressMessage("Major Code Smell", "S108:Nested blocks of code should not be left empty", Justification = "Vlad DX: Reviewed. Source generator", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.StronglyTypedGenerator.WriteClass(Xtz.StronglyTyped.SourceGenerator.CodeWriter,Xtz.StronglyTyped.SourceGenerator.StronglyTypedWorkItem)")] +[assembly: SuppressMessage("Major Code Smell", "S108:Nested blocks of code should not be left empty", Justification = "Vlad DX: Reviewed. Source generator", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.StronglyTypedGenerator.WriteGuidStringConstructor(Xtz.StronglyTyped.SourceGenerator.CodeWriter,Xtz.StronglyTyped.SourceGenerator.StronglyTypedWorkItem)")] +[assembly: SuppressMessage("Major Code Smell", "S108:Nested blocks of code should not be left empty", Justification = "Vlad DX: Reviewed. Source generator", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.StronglyTypedGenerator.WriteStringConstructor(Xtz.StronglyTyped.SourceGenerator.CodeWriter,Xtz.StronglyTyped.SourceGenerator.StronglyTypedWorkItem)")] +[assembly: SuppressMessage("Major Code Smell", "S108:Nested blocks of code should not be left empty", Justification = "Vlad DX: Reviewed. Source generator", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.StronglyTypedGenerator.TryWriteParsingStringConstructor(Xtz.StronglyTyped.SourceGenerator.CodeWriter,Xtz.StronglyTyped.SourceGenerator.StronglyTypedWorkItem)")] +[assembly: SuppressMessage("Minor Code Smell", "S3626:Jump statements should not be redundant", Justification = "Vlad DX: Reviewed", Scope = "member", Target = "~M:Xtz.StronglyTyped.SourceGenerator.StronglyTypedGenerator.TryWriteToString(Xtz.StronglyTyped.SourceGenerator.CodeWriter,Xtz.StronglyTyped.SourceGenerator.StronglyTypedWorkItem)")] diff --git a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs index b1d3034..e52c362 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs +++ b/src/Xtz.StronglyTyped.SourceGenerator/StronglyTypedGenerator.cs @@ -36,7 +36,7 @@ public class StronglyTypedGenerator : IStronglyTypedGenerator { typeof(PhysicalAddress), new(typeof(PhysicalAddress).FullName, "System.Net.NetworkInformation.PhysicalAddress.Parse(value)") }, // Skipping `Uri` as it has `(string value)` constructor }; - + private readonly IDataExtractor _dataExtractor = new DataExtractor(); private readonly List _log = new(); @@ -89,7 +89,7 @@ public void Execute(GeneratorExecutionContext context) #endif // IMPORTANT: Check if you use any types from `Xtz.StronglyTyped` library or any other library. Remove if any. Dependencies are not copied along with source generator (if they are not analyzers as well). // https://github.com/dotnet/roslyn/discussions/47517#discussioncomment-63842 - + _log.Add("\nIMPORTANT: Check if you use any types from `Xtz.StronglyTyped` library or any other library. Remove if any. Dependencies are not copied along with source generator (if they are not analyzers as well).\nhttps://github.com/dotnet/roslyn/discussions/47517#discussioncomment-63842\n"); _log.Add($"Method '{nameof(StronglyTypedGenerator)}.{nameof(Execute)}()' threw an exception '{e.Message}'.\n\nStack trace: {e.StackTrace}\n\nProcess ID: {processId}\n\nProcess name: {processName}\n\nFusion log: {e.FusionLog}"); } @@ -115,7 +115,7 @@ public void Execute(GeneratorExecutionContext context) { context.AddSource("_1-receiver-log", BuildLogText(receiver.Log, "SYNTAX RECEIVER LOG")); context.AddSource("_2-data-extractor-log", BuildLogText(_dataExtractor.Log, "DATA EXTRACTOR LOG")); - context.AddSource($"_3-generator-log", BuildLogText(_log, "GENERATOR LOG")); + context.AddSource("_3-generator-log", BuildLogText(_log, "GENERATOR LOG")); } } @@ -160,7 +160,6 @@ private string GenerateSourceCode(StronglyTypedWorkItem workItem, DateTime times default: throw new CodeWriterException($"Not supported work item type '{workItem.Kind}'"); } - } var generatedSourceCode = writer.ToString(); @@ -182,7 +181,7 @@ private void WriteBanner(CodeWriter writer, StronglyTypedWorkItem workItem, stri writer.AppendLine(); } - private void WriteTypeConverter(CodeWriter writer, StronglyTypedWorkItem workItem) + private static void WriteTypeConverter(CodeWriter writer, StronglyTypedWorkItem workItem) { var valueType = workItem.InnerType; var typeConverter = valueType switch @@ -250,7 +249,7 @@ private void WriteStruct(CodeWriter writer, StronglyTypedWorkItem workItem) writer.AppendLine($"public static readonly {workItem.TypeName} Default;"); writer.AppendLine(); - WriteXmlSummary(writer, $"Inner value."); + WriteXmlSummary(writer, "Inner value."); writer.AppendLine($"public {workItem.InnerType.FullName} Value {{ get; }}"); writer.AppendLine(); @@ -399,6 +398,7 @@ private void WriteStructEqualityMethods(CodeWriter writer, StronglyTypedWorkItem WriteXmlReturns(writer, "A 32-bit signed integer that is the hash code for this instance."); using (writer.BeginScope("public override int GetHashCode()")) { + // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression if (workItem.InnerType.IsValueType) { writer.AppendLine("return Value.GetHashCode();"); @@ -485,6 +485,7 @@ private static void TryWriteToString(CodeWriter writer, StronglyTypedWorkItem wo writer.AppendLine("return $\"{Value}\";"); } writer.AppendLine(); + // ReSharper disable once RedundantJumpStatement return; } @@ -580,9 +581,9 @@ private static SourceText BuildLogText(IReadOnlyCollection log, string t { var version = typeof(StronglyTypedGenerator).Assembly.GetName().Version; var assemblyVersion = $"{version.Major}.{version.Minor}.{version.Revision}.{version.Build}"; - + var result = SourceText.From( - string.Format(@"/*{0}{1}{0}{0}{2}{0}{0}{3}{0}{0}{4}{0}{0}*/", + string.Format("/*{0}{1}{0}{0}{2}{0}{0}{3}{0}{0}{4}{0}{0}*/", Environment.NewLine, title, $"This code was generated by generator '{typeof(StronglyTypedGenerator).FullName}'\nAssembly Version: {assemblyVersion}", diff --git a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj index 25ff173..8dfb617 100644 --- a/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj +++ b/src/Xtz.StronglyTyped.SourceGenerator/Xtz.StronglyTyped.SourceGenerator.csproj @@ -83,6 +83,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj index 589a66c..524dc0a 100644 --- a/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj +++ b/src/Xtz.StronglyTyped.Swashbuckle/Xtz.StronglyTyped.Swashbuckle.csproj @@ -63,6 +63,10 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/Xtz.StronglyTyped.sln b/src/Xtz.StronglyTyped.sln index 57e343d..085f7fa 100644 --- a/src/Xtz.StronglyTyped.sln +++ b/src/Xtz.StronglyTyped.sln @@ -40,6 +40,9 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.NewtonsoftJson.UnitTests", "Tests\Xtz.StronglyTyped.NewtonsoftJson.UnitTests\Xtz.StronglyTyped.NewtonsoftJson.UnitTests.csproj", "{97EDCF82-8DA3-4DB1-96C4-FB2481A75666}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Solution Items", "_Solution Items", "{C6D98E0C-09A4-4152-914D-411DB65B1379}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtz.StronglyTyped.Api_3_1.IntegrationTests", "Tests\Xtz.StronglyTyped.Api_3_1.IntegrationTests\Xtz.StronglyTyped.Api_3_1.IntegrationTests.csproj", "{975017C6-0E2B-4F10-BE65-B412755BEDF7}" EndProject diff --git a/src/Xtz.StronglyTyped.sln.DotSettings b/src/Xtz.StronglyTyped.sln.DotSettings index 4b661fa..cf9e2c7 100644 --- a/src/Xtz.StronglyTyped.sln.DotSettings +++ b/src/Xtz.StronglyTyped.sln.DotSettings @@ -1,4 +1,5 @@  <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> - <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> \ No newline at end of file + <Policy Inspect="True" Prefix="" Suffix="" Style="AA_BB" /> + True \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/Properties/GlobalSuppressions.cs b/src/Xtz.StronglyTyped/GlobalSuppressions.cs similarity index 56% rename from src/Xtz.StronglyTyped/Properties/GlobalSuppressions.cs rename to src/Xtz.StronglyTyped/GlobalSuppressions.cs index e24c699..735b43c 100644 --- a/src/Xtz.StronglyTyped/Properties/GlobalSuppressions.cs +++ b/src/Xtz.StronglyTyped/GlobalSuppressions.cs @@ -9,3 +9,7 @@ [assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.StronglyTypedException")] [assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.TypeConverters.JsonConverterException")] [assembly: SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.TypeConverters.StringTypeConverterException")] +[assembly: SuppressMessage("Blocker Code Smell", "S3875:\"operator==\" should not be overloaded on reference types", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.StronglyTyped`1")] +[assembly: SuppressMessage("Style", "IDE0002:Name can be simplified", Justification = "Vlad DX: Reviewed", Scope = "type", Target = "~T:Xtz.StronglyTyped.StronglyTyped`1")] +[assembly: SuppressMessage("Style", "IDE0034:Simplify 'default' expression", Justification = "Vlad DX: Reviewed. More readable", Scope = "type", Target = "~T:Xtz.StronglyTyped.TypeConverters.StronglyTypedJsonConverter`1")] +[assembly: SuppressMessage("Major Code Smell", "S1066:Collapsible \"if\" statements should be merged", Justification = "Vlad DX: Reviewed. More readable", Scope = "member", Target = "~M:Xtz.StronglyTyped.StronglyTyped`1.ThrowIfInvalid(`0)")] diff --git a/src/Xtz.StronglyTyped/InvalidValueException.cs b/src/Xtz.StronglyTyped/InvalidValueException.cs index dd1d089..1a31d8c 100644 --- a/src/Xtz.StronglyTyped/InvalidValueException.cs +++ b/src/Xtz.StronglyTyped/InvalidValueException.cs @@ -1,14 +1,24 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped { [ExcludeFromCodeCoverage] + [Serializable] public class InvalidValueException : StronglyTypedException { public InvalidValueException(Type type, string errorMessage) : base(type, errorMessage) { } + + /// + /// Constructor is used for deserialization. + /// + protected InvalidValueException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/StronglyTyped.Operators.cs b/src/Xtz.StronglyTyped/StronglyTyped.Operators.cs index 31ca97c..39a8fda 100644 --- a/src/Xtz.StronglyTyped/StronglyTyped.Operators.cs +++ b/src/Xtz.StronglyTyped/StronglyTyped.Operators.cs @@ -1,37 +1,38 @@ -using System; +using System.Diagnostics.CodeAnalysis; namespace Xtz.StronglyTyped { + [SuppressMessage("ReSharper", "RedundantNameQualifier")] public abstract partial class StronglyTyped { public static bool operator ==(StronglyTyped objA, StronglyTyped objB) { - return Object.Equals(objA, objB); + return object.Equals(objA, objB); } public static bool operator !=(StronglyTyped objA, StronglyTyped objB) { - return !Object.Equals(objA, objB); + return !object.Equals(objA, objB); } public static bool operator ==(object objA, StronglyTyped objB) { - return Object.Equals(objA, objB); + return object.Equals(objA, objB); } public static bool operator !=(object objA, StronglyTyped objB) { - return !Object.Equals(objA, objB); + return !object.Equals(objA, objB); } public static bool operator ==(StronglyTyped objA, object objB) { - return Object.Equals(objA, objB); + return object.Equals(objA, objB); } public static bool operator !=(StronglyTyped objA, object objB) { - return !Object.Equals(objA, objB); + return !object.Equals(objA, objB); } public static implicit operator TInnerType?(StronglyTyped? value) diff --git a/src/Xtz.StronglyTyped/StronglyTypedException.cs b/src/Xtz.StronglyTyped/StronglyTypedException.cs index f215ca7..63b6c6f 100644 --- a/src/Xtz.StronglyTyped/StronglyTypedException.cs +++ b/src/Xtz.StronglyTyped/StronglyTypedException.cs @@ -1,9 +1,12 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; +using System.Security.Permissions; namespace Xtz.StronglyTyped { [ExcludeFromCodeCoverage] + [Serializable] public class StronglyTypedException : Exception { public Type Type { get; } @@ -19,5 +22,21 @@ public StronglyTypedException(Type type, string errorMessage, Exception innerExc { Type = type; } + + /// + /// Constructor is used for deserialization. + /// + protected StronglyTypedException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + Type = (Type)info.GetValue(nameof(Type), typeof(Type)); + } + + [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue(nameof(Type), Type); + } } } diff --git a/src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs b/src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs index f89a62b..0966283 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/NewtonsoftJsonConverterException.cs @@ -1,14 +1,24 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped.TypeConverters { [ExcludeFromCodeCoverage] + [Serializable] public class JsonConverterException : StronglyTypedException { public JsonConverterException(Type type, string errorMessage) : base(type, errorMessage) { } + + /// + /// Constructor is used for deserialization. + /// + protected JsonConverterException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/TypeConverters/StringTypeConverterException.cs b/src/Xtz.StronglyTyped/TypeConverters/StringTypeConverterException.cs index 2a771ca..e619f78 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/StringTypeConverterException.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/StringTypeConverterException.cs @@ -1,13 +1,23 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped.TypeConverters { [ExcludeFromCodeCoverage] + [Serializable] public class StringTypeConverterException : StronglyTypedException { public StringTypeConverterException(Type type, string errorMessage) : base(type, errorMessage) { } + + /// + /// Constructor is used for deserialization. + /// + protected StringTypeConverterException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } } \ No newline at end of file diff --git a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs index 76b529c..df41d4d 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/StronglyTypedJsonConverter.cs @@ -1,13 +1,11 @@ using System; using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; using System.Xml; namespace Xtz.StronglyTyped.TypeConverters { - [SuppressMessage("Style", "IDE0034:Simplify 'default' expression", Justification = "Vlad DX: Reviewed (more readable code)")] public class StronglyTypedJsonConverter : JsonConverter where TStronglyTyped : IStronglyTyped { @@ -27,7 +25,7 @@ public override TStronglyTyped Read(ref Utf8JsonReader reader, Type typeToConver if (typeConverter.InnerType == typeof(DateTime)) { - return ReadDateTime(reader, typeConverter); + return StronglyTypedJsonConverter.ReadDateTime(reader, typeConverter); } if (reader.TokenType is JsonTokenType.True or JsonTokenType.False) @@ -37,7 +35,7 @@ public override TStronglyTyped Read(ref Utf8JsonReader reader, Type typeToConver if (reader.TokenType == JsonTokenType.Number) { - return ReadNumber(reader, typeConverter); + return StronglyTypedJsonConverter.ReadNumber(reader, typeConverter); } var stringValue = reader.GetString(); @@ -69,9 +67,8 @@ public override void Write(Utf8JsonWriter writer, TStronglyTyped? value, JsonSer writer.WriteStringValue(stringValue); } - private TStronglyTyped ReadNumber(Utf8JsonReader reader, ICustomTypeConverter typeConverter) + private static TStronglyTyped ReadNumber(Utf8JsonReader reader, ICustomTypeConverter typeConverter) { - if (typeConverter.InnerType == typeof(int)) { return (TStronglyTyped)typeConverter.ConvertFrom(reader.GetInt32())!; @@ -130,7 +127,7 @@ private TStronglyTyped ReadNumber(Utf8JsonReader reader, ICustomTypeConverter ty throw new JsonConverterException(typeConverter.StrongType, $"Can't convert value to '{typeConverter.StrongType.FullName}'"); } - private TStronglyTyped ReadDateTime(Utf8JsonReader reader, ICustomTypeConverter typeConverter) + private static TStronglyTyped ReadDateTime(Utf8JsonReader reader, ICustomTypeConverter typeConverter) { if (reader.TryGetDateTime(out var dateTimeValue)) { @@ -142,11 +139,11 @@ private TStronglyTyped ReadDateTime(Utf8JsonReader reader, ICustomTypeConverter { return (TStronglyTyped)typeConverter.ConvertFrom(dateTimeValue2)!; } - + throw new JsonConverterException(typeConverter.StrongType, $"Can't convert from '{stringValue}' to '{typeConverter.StrongType.FullName}'"); } - private bool TryWriteNumber(IStronglyTyped value, Type innerType, Utf8JsonWriter writer) + private static bool TryWriteNumber(IStronglyTyped value, Type innerType, Utf8JsonWriter writer) { if (innerType == typeof(decimal)) { diff --git a/src/Xtz.StronglyTyped/TypeConverters/TypeConverterException.cs b/src/Xtz.StronglyTyped/TypeConverters/TypeConverterException.cs index 891434b..a35911c 100644 --- a/src/Xtz.StronglyTyped/TypeConverters/TypeConverterException.cs +++ b/src/Xtz.StronglyTyped/TypeConverters/TypeConverterException.cs @@ -1,16 +1,28 @@ using System; using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; namespace Xtz.StronglyTyped.TypeConverters { [ExcludeFromCodeCoverage] + [Serializable] public class TypeConverterException : StronglyTypedException { - public TypeConverterException(Type type, string errorMessage) : base(type, errorMessage) + public TypeConverterException(Type type, string errorMessage) + : base(type, errorMessage) { } - public TypeConverterException(Type type, string errorMessage, Exception innerException) : base(type, errorMessage, innerException) + public TypeConverterException(Type type, string errorMessage, Exception innerException) + : base(type, errorMessage, innerException) + { + } + + /// + /// Constructor is used for deserialization. + /// + protected TypeConverterException(SerializationInfo info, StreamingContext context) + : base(info, context) { } } diff --git a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj index fcb266b..b977a13 100644 --- a/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj +++ b/src/Xtz.StronglyTyped/Xtz.StronglyTyped.csproj @@ -67,6 +67,10 @@ Make compiler your friend, introduce semantics to your code. all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +