From be8307f23953c3a325406f7f3c96bb76560987f1 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sun, 26 Mar 2023 07:20:08 +0100 Subject: [PATCH 1/8] Reinstated and updated Examples --- Consumers.sln | 2 + .../DapperExamples.cs | 1 + .../EfCoreExamples.cs | 2 +- .../LinqToDbExamples.cs | 2 +- .../SerializationAndConversion.cs | 8 +- .../SyntaxExamples/CanHaveOtherMethods.cs | 13 ---- .../SyntaxExamples/Nesting.cs | 9 ++- .../SyntaxExamples/NoDefaulting.cs | 17 ++-- .../NoUserDefinedConstructors.cs | 34 ++++---- .../SyntaxExamples/ReadOnlyStructs.cs | 15 ---- .../SyntaxExamples/Types.cs | 4 - .../Types/DateTimeOffsetVo.cs | 78 ++++++++++++++++--- samples/Intellenum.Examples/Types/FloatVo.cs | 78 ++++++++++++++++--- samples/Intellenum.Examples/Types/IntoVo.cs | 48 +++++++++--- samples/Intellenum.Examples/Types/StringVo.cs | 36 ++++++--- .../TypicalScenarios/BasicExamples.cs | 55 +++++-------- .../TypicalScenarios/EqualityExamples.cs | 4 +- .../TypicalScenarios/ExplicitCasting.cs | 10 ++- .../TypicalScenarios/Instances.cs | 64 +++------------ .../TypicalScenarios/NormalizationExample.cs | 55 ------------- .../TypicalScenarios/UsingInterfaces.cs | 50 ------------ .../TypicalScenarios/ValidationExample.cs | 45 ----------- src/Intellenum/AnalyzerReleases.Shipped.md | 1 - 23 files changed, 280 insertions(+), 351 deletions(-) delete mode 100644 samples/Intellenum.Examples/SyntaxExamples/CanHaveOtherMethods.cs delete mode 100644 samples/Intellenum.Examples/SyntaxExamples/ReadOnlyStructs.cs delete mode 100644 samples/Intellenum.Examples/SyntaxExamples/Types.cs delete mode 100644 samples/Intellenum.Examples/TypicalScenarios/NormalizationExample.cs delete mode 100644 samples/Intellenum.Examples/TypicalScenarios/UsingInterfaces.cs delete mode 100644 samples/Intellenum.Examples/TypicalScenarios/ValidationExample.cs diff --git a/Consumers.sln b/Consumers.sln index ce2825d7..5db902d6 100644 --- a/Consumers.sln +++ b/Consumers.sln @@ -29,6 +29,8 @@ Global GlobalSection(ProjectConfigurationPlatforms) = postSolution {E89977F3-1B28-498E-8D63-9E49EE4D84D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E89977F3-1B28-498E-8D63-9E49EE4D84D3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E89977F3-1B28-498E-8D63-9E49EE4D84D3}.Release|Any CPU.Build.0 = Release|Any CPU + {E89977F3-1B28-498E-8D63-9E49EE4D84D3}.Debug|Any CPU.Build.0 = Debug|Any CPU {B9FAC951-7F77-49B5-9DCD-C8278B45EE65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B9FAC951-7F77-49B5-9DCD-C8278B45EE65}.Release|Any CPU.ActiveCfg = Release|Any CPU {B9FAC951-7F77-49B5-9DCD-C8278B45EE65}.Release|Any CPU.Build.0 = Release|Any CPU diff --git a/samples/Intellenum.Examples/SerializationAndConversion/DapperExamples.cs b/samples/Intellenum.Examples/SerializationAndConversion/DapperExamples.cs index 44632563..6dc17ff6 100644 --- a/samples/Intellenum.Examples/SerializationAndConversion/DapperExamples.cs +++ b/samples/Intellenum.Examples/SerializationAndConversion/DapperExamples.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using Dapper; +using Intellenum.Examples.Types; using Microsoft.Data.Sqlite; namespace Intellenum.Examples.SerializationAndConversion diff --git a/samples/Intellenum.Examples/SerializationAndConversion/EfCoreExamples.cs b/samples/Intellenum.Examples/SerializationAndConversion/EfCoreExamples.cs index 13a29379..ef7d7ee8 100644 --- a/samples/Intellenum.Examples/SerializationAndConversion/EfCoreExamples.cs +++ b/samples/Intellenum.Examples/SerializationAndConversion/EfCoreExamples.cs @@ -24,7 +24,7 @@ private void EfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new TestEntity { Id = EfCoreStringVo.From("foo!") }; + var original = new TestEntity { Id = EfCoreStringVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); diff --git a/samples/Intellenum.Examples/SerializationAndConversion/LinqToDbExamples.cs b/samples/Intellenum.Examples/SerializationAndConversion/LinqToDbExamples.cs index 2e8d53fd..a7c1ff24 100644 --- a/samples/Intellenum.Examples/SerializationAndConversion/LinqToDbExamples.cs +++ b/samples/Intellenum.Examples/SerializationAndConversion/LinqToDbExamples.cs @@ -23,7 +23,7 @@ private void LinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new TestEntity { Id = LinqToDbStringVo.From("foo!") }; + var original = new TestEntity { Id = LinqToDbStringVo.Item1}; using (var context = new TestDbContext(connection)) { context.CreateTable(); diff --git a/samples/Intellenum.Examples/SerializationAndConversion/SerializationAndConversion.cs b/samples/Intellenum.Examples/SerializationAndConversion/SerializationAndConversion.cs index 442eabb4..474c88e2 100644 --- a/samples/Intellenum.Examples/SerializationAndConversion/SerializationAndConversion.cs +++ b/samples/Intellenum.Examples/SerializationAndConversion/SerializationAndConversion.cs @@ -1,7 +1,7 @@ // ReSharper disable UnusedVariable #pragma warning disable CS0219 -using System; using System.Threading.Tasks; +using Intellenum.Examples.Types; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; @@ -9,8 +9,6 @@ namespace Intellenum.Examples.SerializationAndConversion { public class SerializationAndConversionExamples : IScenario { - private static readonly DateTimeOffset _date1 = new DateTimeOffset(1970, 6, 10, 14, 01, 02, TimeSpan.Zero) + TimeSpan.FromTicks(12345678); - public Task Run() { SerializeWithNewtonsoftJson(); @@ -21,7 +19,7 @@ public Task Run() public void SerializeWithNewtonsoftJson() { - var g1 = NewtonsoftJsonDateTimeOffsetVo.From(_date1); + var g1 = NewtonsoftJsonDateTimeOffsetVo.Item1; string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); @@ -32,7 +30,7 @@ public void SerializeWithNewtonsoftJson() public void SerializeWithSystemTextJson() { - var foo = SystemTextJsonDateTimeOffsetVo.From(_date1); + var foo = SystemTextJsonDateTimeOffsetVo.Item1; string serializedFoo = SystemTextJsonSerializer.Serialize(foo); string serializedString = SystemTextJsonSerializer.Serialize(foo.Value); diff --git a/samples/Intellenum.Examples/SyntaxExamples/CanHaveOtherMethods.cs b/samples/Intellenum.Examples/SyntaxExamples/CanHaveOtherMethods.cs deleted file mode 100644 index f3bf7e33..00000000 --- a/samples/Intellenum.Examples/SyntaxExamples/CanHaveOtherMethods.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace Intellenum.Examples.SyntaxExamples -{ - [Intellenum] - public partial struct Duration - { - private static Validation Validate(TimeSpan timeSpan) => - timeSpan >= TimeSpan.Zero ? Validation.Ok : Validation.Invalid("Cannot be negative"); - - public Duration DecreaseBy(TimeSpan amount) => Duration.From(Value - amount); - } -} \ No newline at end of file diff --git a/samples/Intellenum.Examples/SyntaxExamples/Nesting.cs b/samples/Intellenum.Examples/SyntaxExamples/Nesting.cs index bb252f9c..b6b1abe0 100644 --- a/samples/Intellenum.Examples/SyntaxExamples/Nesting.cs +++ b/samples/Intellenum.Examples/SyntaxExamples/Nesting.cs @@ -5,7 +5,7 @@ namespace Intellenum.Examples.SyntaxExamples { /* - * Value Objects can be in nested namespaces, but cannot be in a nested class. + * Can be in nested namespaces, but cannot be in a nested class. * This example below is OK as it's just a nested namespace. */ namespace Namespace1 @@ -13,7 +13,9 @@ namespace Namespace1 namespace Namespace2 { [Intellenum] - public partial struct NestedType + [Instance("Standard", 1)] + [Instance("Gold", 2)] + public partial class CustomerType { } } @@ -29,7 +31,8 @@ internal class AndAnother { // uncomment to get error VOG001: Type 'NestedType' cannot be nested - remove it from inside AndAnother // [Intellenum(typeof(int))] - public partial struct NestedType + // [Instance("Item1", 1)] + public partial class NestedType { } } diff --git a/samples/Intellenum.Examples/SyntaxExamples/NoDefaulting.cs b/samples/Intellenum.Examples/SyntaxExamples/NoDefaulting.cs index cc4870b3..a4faace9 100644 --- a/samples/Intellenum.Examples/SyntaxExamples/NoDefaulting.cs +++ b/samples/Intellenum.Examples/SyntaxExamples/NoDefaulting.cs @@ -27,10 +27,6 @@ public Naughty() // CustomerId v5 = new(); // var _ = new CustomerId(); // new CustomerId(); - - // this is valid syntax as it can't easily be picked up at compile time, - // but it throws a ValueObjectValidationException at runtime. - CustomerId[] customerIds = new CustomerId[10]; } // a method can't accept a VO and default it @@ -49,8 +45,13 @@ public Naughty() } [Intellenum] - public partial struct CustomerId { } - - [Intellenum] - public partial class VendorId { } + [Instance("Standard", 1)] + [Instance("Gold", 2)] + public partial class CustomerType { } + + [Intellenum] + [Instance("Good", "Good")] + [Instance("Average", "Average")] + [Instance("Bad", "Bad")] + public partial class VendorRating { } } \ No newline at end of file diff --git a/samples/Intellenum.Examples/SyntaxExamples/NoUserDefinedConstructors.cs b/samples/Intellenum.Examples/SyntaxExamples/NoUserDefinedConstructors.cs index c5ae6859..8a1e39ef 100644 --- a/samples/Intellenum.Examples/SyntaxExamples/NoUserDefinedConstructors.cs +++ b/samples/Intellenum.Examples/SyntaxExamples/NoUserDefinedConstructors.cs @@ -6,34 +6,32 @@ any validation you might have added. */ [Intellenum] - public partial struct CustomerId + [Instance("Standard", 1)] + [Instance("Gold", 2)] + public partial class CustomerType { - private static Validation Validate(in int value) => - value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); - - // uncomment - error CS0111: Type 'CustomerId' already defines a member called 'CustomerId' with the same parameter type - // public CustomerId() { } + // uncomment - error CS0111: Type 'CustomerType' already defines a member called 'CustomerType' with the same parameter type + // public CustomerType() { } // uncomment - error VOG008: Cannot have user defined constructors, please use the From method for creation. - // public CustomerId(int value) { } + // public CustomerType(int value) { } // uncomment - error VOG008: Cannot have user defined constructors, please use the From method for creation. - // public CustomerId(int v1, int v2) : this(v1) { } + // public CustomerType(int v1, int v2) : this(v1) { } } [Intellenum] - public partial class VendorId + [Instance("Standard", 1)] + [Instance("Preferred", 2)] + public partial class VendorType { - private static Validation Validate(in int value) => - value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); - - // uncomment - error CS0111: Type 'VendorId' already defines a member called 'VendorId' with the same parameter type - // public VendorId() { } + // uncomment - error CS0111: Type 'VendorType' already defines a member called 'VendorType' with the same parameter type + // public VendorType() { } // uncomment - error VOG008: Cannot have user defined constructors, please use the From method for creation. - // public VendorId(int value) { } - // public VendorId(int v1, int v2) : this(v1) { } - // public VendorId(int v1, int v2, int v3) : this(v1) { } - // public VendorId(int v1, int v2, int v3, int v4) : this(v1) { } + // public VendorType(int value) { } + // public VendorType(int v1, int v2) : this(v1) { } + // public VendorType(int v1, int v2, int v3) : this(v1) { } + // public VendorType(int v1, int v2, int v3, int v4) : this(v1) { } } } \ No newline at end of file diff --git a/samples/Intellenum.Examples/SyntaxExamples/ReadOnlyStructs.cs b/samples/Intellenum.Examples/SyntaxExamples/ReadOnlyStructs.cs deleted file mode 100644 index 30909fd3..00000000 --- a/samples/Intellenum.Examples/SyntaxExamples/ReadOnlyStructs.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Intellenum.Examples.SyntaxExamples -{ - [Intellenum] - public readonly partial struct MyReadOnlyVo - { - } - - public class UseReadOnly - { - public UseReadOnly() - { - MyReadOnlyVo.From(123); - } - } -} diff --git a/samples/Intellenum.Examples/SyntaxExamples/Types.cs b/samples/Intellenum.Examples/SyntaxExamples/Types.cs deleted file mode 100644 index 8a42ed8b..00000000 --- a/samples/Intellenum.Examples/SyntaxExamples/Types.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace Intellenum.Examples.SyntaxExamples; - -#pragma warning disable CS0219 - diff --git a/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs b/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs index 8701456a..8747bd6f 100644 --- a/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs +++ b/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs @@ -3,29 +3,87 @@ namespace Intellenum.Examples.Types { [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial struct DateTimeOffsetVo { } + public partial class DateTimeOffsetVo + { + public static readonly DateTimeOffsetVo None = new DateTimeOffsetVo("None", DateTimeOffset.MinValue); + } [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial struct NoConverterDateTimeOffsetVo { } + public partial class NoConverterDateTimeOffsetVo + { + static NoConverterDateTimeOffsetVo() + { + Instance("Item1", DateTimeOffset.Parse("2020-01-01T00:00:00Z")); + Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + } + } [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTimeOffset))] - public partial struct NoJsonDateTimeOffsetVo { } + public partial class NoJsonDateTimeOffsetVo + { + static NoJsonDateTimeOffsetVo() + { + Instance("Item1", DateTimeOffset.Parse("2020-01-01T00:00:00Z")); + } + } [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTimeOffset))] - public partial struct NewtonsoftJsonDateTimeOffsetVo { } + public partial class NewtonsoftJsonDateTimeOffsetVo + { + static NewtonsoftJsonDateTimeOffsetVo() + { + Instance("Item1", new DateTimeOffset(2019, 12, 13, 14, 15, 16, TimeSpan.Zero)); + Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + } + } [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial struct SystemTextJsonDateTimeOffsetVo { } + public partial class SystemTextJsonDateTimeOffsetVo + { + static SystemTextJsonDateTimeOffsetVo() + { + Instance("Item1", new DateTimeOffset(2019, 12, 13, 14, 15, 16, TimeSpan.Zero)); + Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + } + } [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial struct BothJsonDateTimeOffsetVo { } + public partial class BothJsonDateTimeOffsetVo + { + static BothJsonDateTimeOffsetVo() + { + Instance("Item1", new DateTimeOffset(2019, 12, 13, 14, 15, 16, TimeSpan.Zero)); + Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + } + } [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial struct EfCoreDateTimeOffsetVo { } + public partial class EfCoreDateTimeOffsetVo + { + static EfCoreDateTimeOffsetVo() + { + Instance("Item1", new DateTimeOffset(2019, 12, 13, 14, 15, 16, TimeSpan.Zero)); + Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + } + } [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTimeOffset))] - public partial struct DapperDateTimeOffsetVo { } + public partial class DapperDateTimeOffsetVo + { + static DapperDateTimeOffsetVo() + { + Instance("Item1", new DateTimeOffset(2019, 12, 13, 14, 15, 16, TimeSpan.Zero)); + Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + } + } [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial struct LinqToDbDateTimeOffsetVo { } -} + public partial class LinqToDbDateTimeOffsetVo + { + static LinqToDbDateTimeOffsetVo() + { + Instance("Item1", new DateTimeOffset(2019, 12, 13, 14, 15, 16, TimeSpan.Zero)); + Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + } + } +} \ No newline at end of file diff --git a/samples/Intellenum.Examples/Types/FloatVo.cs b/samples/Intellenum.Examples/Types/FloatVo.cs index 66b8dcd0..9b169373 100644 --- a/samples/Intellenum.Examples/Types/FloatVo.cs +++ b/samples/Intellenum.Examples/Types/FloatVo.cs @@ -1,32 +1,88 @@ namespace Intellenum.Examples.Types { [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson)] - public readonly partial struct Celsius { } + public partial class Celsius + { + static Celsius() + { + Instance("AbsoluteZero", -273.15f); + Instance("FreezingPointOfWater", 0f); + Instance("BoilingPointOfWater", 100f); + } + } [Intellenum(conversions: Conversions.None)] - public partial struct FloatVo { } + public partial class FloatVo + { + static FloatVo() + { + Instance("Item1", 1.23f); + Instance("Item2", 3.21f); + } + } [Intellenum(conversions: Conversions.None)] - public partial struct NoConverterFloatVo { } + public partial class NoConverterFloatVo + { + static NoConverterFloatVo() + { + Instance("Item1", 1.23f); + Instance("Item2", 3.21f); + } + } [Intellenum(conversions: Conversions.TypeConverter)] - public partial struct NoJsonFloatVo { } + public partial class NoJsonFloatVo + { + static NoJsonFloatVo() + { + Instance("Item1", 1.23f); + Instance("Item2", 3.21f); + } + } [Intellenum(conversions: Conversions.NewtonsoftJson)] - public partial struct NewtonsoftJsonFloatVo { } + public partial class NewtonsoftJsonFloatVo + { + static NewtonsoftJsonFloatVo() + { + Instance("Item1", 1.23f); + Instance("Item2", 3.21f); + } + } [Intellenum(conversions: Conversions.SystemTextJson)] - public partial struct SystemTextJsonFloatVo { } + [Instance("Item1", 1f)] + [Instance("Item2", 2f)] + public partial class SystemTextJsonFloatVo + { + } [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson)] - public partial struct BothJsonFloatVo { } + [Instance("Item1", 1f)] + [Instance("Item2", 2f)] + public partial class BothJsonFloatVo + { + } [Intellenum(conversions: Conversions.EfCoreValueConverter)] - public partial struct EfCoreFloatVo { } + [Instance("Item1", 1f)] + [Instance("Item2", 2f)] + public partial class EfCoreFloatVo + { + } [Intellenum(conversions: Conversions.DapperTypeHandler)] - public partial struct DapperFloatVo { } + [Instance("Item1", 1f)] + [Instance("Item2", 2f)] + public partial class DapperFloatVo + { + } [Intellenum(conversions: Conversions.LinqToDbValueConverter)] - public partial struct LinqToDbFloatVo { } -} + [Instance("Item1", 1f)] + [Instance("Item2", 2f)] + public partial class LinqToDbFloatVo + { + } +} \ No newline at end of file diff --git a/samples/Intellenum.Examples/Types/IntoVo.cs b/samples/Intellenum.Examples/Types/IntoVo.cs index deebd4a2..5661a7bb 100644 --- a/samples/Intellenum.Examples/Types/IntoVo.cs +++ b/samples/Intellenum.Examples/Types/IntoVo.cs @@ -2,39 +2,63 @@ // the underlying type can be omitted and is defaulted to int [Intellenum] -public partial struct MyValueObject { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class MyValueObject { } // the underlying type can be specified [Intellenum(typeof(int))] -public partial struct MyValueObject2 { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class MyValueObject2 { } // conversions can be specified, but if not, it defaults to TypeConverter and SystemTextJson [Intellenum(conversions: Conversions.None, underlyingType: typeof(int))] -public partial struct IntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class IntVo { } [Intellenum(conversions: Conversions.None)] -public partial struct IntGenericVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class IntGenericVo { } [Intellenum(conversions: Conversions.None)] -public partial struct NoConverterIntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class NoConverterIntVo { } [Intellenum(conversions: Conversions.TypeConverter)] -public partial struct NoJsonIntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class NoJsonIntVo { } [Intellenum(conversions: Conversions.NewtonsoftJson)] -public partial struct NewtonsoftJsonIntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class NewtonsoftJsonIntVo { } [Intellenum(conversions: Conversions.SystemTextJson)] -public partial struct SystemTextJsonIntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class SystemTextJsonIntVo { } [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson)] -public partial struct BothJsonIntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class BothJsonIntVo { } [Intellenum(conversions: Conversions.EfCoreValueConverter)] -public partial struct EfCoreIntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class EfCoreIntVo { } [Intellenum(conversions: Conversions.DapperTypeHandler)] -public partial struct DapperIntVo { } +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class DapperIntVo { } [Intellenum(conversions: Conversions.LinqToDbValueConverter)] -public partial struct LinqToDbIntVo { } \ No newline at end of file +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class LinqToDbIntVo { } \ No newline at end of file diff --git a/samples/Intellenum.Examples/Types/StringVo.cs b/samples/Intellenum.Examples/Types/StringVo.cs index dd032e2f..c4fbaec1 100644 --- a/samples/Intellenum.Examples/Types/StringVo.cs +++ b/samples/Intellenum.Examples/Types/StringVo.cs @@ -1,29 +1,47 @@ namespace Intellenum.Examples.Types { [Intellenum(conversions: Conversions.None)] - public partial struct StringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class StringVo { } [Intellenum(conversions: Conversions.None)] - public partial struct NoConverterStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class NoConverterStringVo { } [Intellenum(conversions: Conversions.TypeConverter)] - public partial struct NoJsonStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class NoJsonStringVo { } [Intellenum(conversions: Conversions.NewtonsoftJson)] - public partial struct NewtonsoftJsonStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class NewtonsoftJsonStringVo { } [Intellenum(conversions: Conversions.SystemTextJson)] - public partial struct SystemTextJsonStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class SystemTextJsonStringVo { } [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson)] - public partial struct BothJsonStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class BothJsonStringVo { } [Intellenum(conversions: Conversions.EfCoreValueConverter)] - public partial struct EfCoreStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class EfCoreStringVo { } [Intellenum(conversions: Conversions.DapperTypeHandler)] - public partial struct DapperStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class DapperStringVo { } [Intellenum(conversions: Conversions.LinqToDbValueConverter)] - public partial struct LinqToDbStringVo { } + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class LinqToDbStringVo { } } diff --git a/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs b/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs index 54a078a2..70e493eb 100644 --- a/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs +++ b/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs @@ -11,7 +11,7 @@ public Task Run() // Argument 1: cannot convert from 'SupplierId' to 'CustomerId' // new CustomerProcessor().Process(SupplierId.From(123), SupplierId.From(321), Amount.From(123)); - new CustomerProcessor().Process(CustomerId.From(123), SupplierId.From(321), Amount.From(123)); + new CustomerProcessor().Process(CustomerType.FromName("Standard"), SupplierType.FromValue(1)); return Task.CompletedTask; } @@ -19,61 +19,48 @@ public Task Run() // defaults to int [Intellenum] - internal readonly partial struct Score + [Instance("Won", 2)] + [Instance("Drawn", 1)] + [Instance("Lost", 0)] + internal partial class Score { } - // can be internal structs + // can be internal class [Intellenum] - internal partial struct Centimeter - { - } - - // can be internal classes - [Intellenum] - internal partial class Meter - { - } - - // can be readonly internal - [Intellenum] - internal readonly partial struct Furlong + [Instance("Accepted", 0)] + [Instance("Shipped", 1)] + internal partial class OrderStatus { } // can be internal sealed [Intellenum] - internal sealed partial class Lumens + [Instance("Warm", 1)] + [Instance("Bright", 5)] + internal sealed partial class LumenType { } [Intellenum] - public partial class CustomerId + [Instance("Standard", 0)] + [Instance("Gold", 1)] + + public partial class CustomerType { } [Intellenum] - public partial class SupplierId - { - } + [Instance("Standard", 0)] + [Instance("Preferred", 1)] - // defaults to int, but configured to throw an AmountException - [Intellenum(throws: typeof(AmountException))] - public partial class Amount + public partial class SupplierType { - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("Must be > 0"); // throws an AmountException } - public class AmountException : Exception - { - public AmountException(string message) : base(message) - { - } - } - internal class CustomerProcessor { - internal void Process(CustomerId customerId, SupplierId supplierId, Amount amount) => - Console.WriteLine($"Processing customer {customerId}, supplier {supplierId}, with amount {amount}"); + internal void Process(CustomerType customerType, SupplierType supplierType) => + Console.WriteLine($"Processing customer {customerType}, supplier {supplierType}"); } } \ No newline at end of file diff --git a/samples/Intellenum.Examples/TypicalScenarios/EqualityExamples.cs b/samples/Intellenum.Examples/TypicalScenarios/EqualityExamples.cs index 581a5713..5f575823 100644 --- a/samples/Intellenum.Examples/TypicalScenarios/EqualityExamples.cs +++ b/samples/Intellenum.Examples/TypicalScenarios/EqualityExamples.cs @@ -11,7 +11,9 @@ public Task Run() { // error CS0019: Operator '==' cannot be applied to operands of type 'Age' and 'Centigrade' - // Console.WriteLine(Age.From(1) == Centigrade.From(1)); // true + Console.WriteLine(Centigrade.AbsoluteZero == Centigrade.FromName("AbsoluteZero")); // true + Console.WriteLine(Centigrade.AbsoluteZero == Centigrade.FromValue(-273.15m)); // true + Console.WriteLine(Centigrade.AbsoluteZero == -273.15m); // true return Task.CompletedTask; } diff --git a/samples/Intellenum.Examples/TypicalScenarios/ExplicitCasting.cs b/samples/Intellenum.Examples/TypicalScenarios/ExplicitCasting.cs index e3c68f9f..5f306f0b 100644 --- a/samples/Intellenum.Examples/TypicalScenarios/ExplicitCasting.cs +++ b/samples/Intellenum.Examples/TypicalScenarios/ExplicitCasting.cs @@ -8,10 +8,11 @@ internal class ExplicitCastingScenario : IScenario public Task Run() { // We can create an instance with an explicit cast. If there is validation, it is still run. - Score score1 = (Score) 20; - Score score2 = Score.From(20); + ScoreType score1 = (ScoreType)2; + ScoreType score2 = ScoreType.FromValue(2); Console.WriteLine(score1 == score2); // true + Console.WriteLine(score1 == ScoreType.Won); // true // We can cast an instance to the underlying type too int score3 = (int) score2; @@ -23,7 +24,10 @@ public Task Run() // defaults to int [Intellenum] - internal readonly partial struct Score + [Instance("Won", 2)] + [Instance("Drawn", 1)] + [Instance("Lost", 0)] + internal partial class ScoreType { } } \ No newline at end of file diff --git a/samples/Intellenum.Examples/TypicalScenarios/Instances.cs b/samples/Intellenum.Examples/TypicalScenarios/Instances.cs index 931400ac..fa464085 100644 --- a/samples/Intellenum.Examples/TypicalScenarios/Instances.cs +++ b/samples/Intellenum.Examples/TypicalScenarios/Instances.cs @@ -14,13 +14,13 @@ internal class InstanceExamples : IScenario public Task Run() { VendorInformation vi = new VendorInformation(); - Console.WriteLine((bool) (vi.VendorId == VendorId.Unspecified)); // true - Console.WriteLine((bool) (vi.VendorId != VendorId.Invalid)); // true + Console.WriteLine((bool) (vi.VendorId == VendorType.Unspecified)); // true + Console.WriteLine((bool) (vi.VendorId != VendorType.Invalid)); // true // from a text file that is screwed, we'll end up with: var invalidVi = VendorInformation.FromTextFile(); - Console.WriteLine((bool) (invalidVi.VendorId == VendorId.Invalid)); // true + Console.WriteLine((bool) (invalidVi.VendorId == VendorType.Invalid)); // true return Task.CompletedTask; } @@ -31,70 +31,30 @@ public Task Run() [Instance("Freezing", 0.0f)] [Instance("Boiling", 100.0f)] [Instance("AbsoluteZero", -273.15f)] - public readonly partial struct Centigrade + public partial class Centigrade { - public static Validation Validate(float value) => - value >= AbsoluteZero.Value ? Validation.Ok : Validation.Invalid("Cannot be colder than absolute zero"); - } - - /* - * Instances are the only way to avoid validation, so we can create instances - * that nobody else can. This is useful for creating special instances - * that represent concepts such as 'invalid' and 'unspecified'. - */ - [Intellenum] - [Instance("Unspecified", -1)] - [Instance("Invalid", -2)] - public readonly partial struct Age - { - private static Validation Validate(int value) => - value > 0 ? Validation.Ok : Validation.Invalid("Must be greater than zero."); - } - - [Intellenum] - [Instance("Unspecified", 0)] - [Instance("Invalid", -1)] - public partial class VendorId - { - private static Validation Validate(int value) => - value > 0 ? Validation.Ok : Validation.Invalid("Must be greater than zero."); } [Intellenum] - [Instance("Invalid", "[INVALID]")] - public partial class VendorName + [Instance("Unspecified", "[UNSPCFD]")] + [Instance("Invalid", "[INVLD]")] + [Instance("Standard", "[STD]")] + [Instance("Preferred", "[PRF]")] + public partial class VendorType { } public class VendorInformation { - public VendorId VendorId { get; private init; } = VendorId.Unspecified; + public VendorType VendorId { get; private init; } = VendorType.Standard; public static VendorInformation FromTextFile() { - // image the text file is screwed... + // imaging the text file is screwed... return new VendorInformation { - VendorId = VendorId.Invalid + VendorId = VendorType.Invalid }; } } - - public class VendorRelatedThings - { - public VendorName GetVendorName(VendorId id) - { - if (id == VendorId.Unspecified) - throw new InvalidOperationException("The vendor ID was unspecified"); - - // throw if invalid - if (id == VendorId.Invalid) - throw new InvalidOperationException("The vendor ID was invalid"); - - // or record it as invalid - if (id == VendorId.Invalid) return VendorName.Invalid; - - return VendorName.From("abc"); - } - } } \ No newline at end of file diff --git a/samples/Intellenum.Examples/TypicalScenarios/NormalizationExample.cs b/samples/Intellenum.Examples/TypicalScenarios/NormalizationExample.cs deleted file mode 100644 index da0bb56a..00000000 --- a/samples/Intellenum.Examples/TypicalScenarios/NormalizationExample.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace Intellenum.Examples.TypicalScenarios.Normalization -{ - // Represent a string scraped from some other text, e.g. a web-page, online article, etc. - // It cannot be empty, or start / end with whitespace. - // We have a normalization method that first normalizes the string, then the - // validation method that validates it. - [Intellenum] - public partial class ScrapedString - { - private static Validation Validate(string value) - { - return value.Length == 0 ? Validation.Invalid("Can't be empty") : Validation.Ok; - } - - private static string NormalizeInput(string input) => input.Trim(); - } - - internal class NormalizationExample : IScenario - { - public Task Run() - { - /* output: - Processing "Fred Flintstone" - Processing "Wilma Flintstone" - Processing "Barney Rubble" - Can't be empty - */ - string[] names = new[] { " Fred Flintstone", "Wilma Flintstone\t", " Barney Rubble \t", " \t \t" }; - - var processor = new Processor(); - - foreach (string name in names) - { - try - { - processor.Process(ScrapedString.From(name)); - } - catch (ValueObjectValidationException e) - { - Console.WriteLine(e.Message); - } - } - - return Task.CompletedTask; - } - - private class Processor - { - internal void Process(ScrapedString item) => Console.WriteLine($"Processing \"{item}\""); - } - } -} \ No newline at end of file diff --git a/samples/Intellenum.Examples/TypicalScenarios/UsingInterfaces.cs b/samples/Intellenum.Examples/TypicalScenarios/UsingInterfaces.cs deleted file mode 100644 index 99c5ba06..00000000 --- a/samples/Intellenum.Examples/TypicalScenarios/UsingInterfaces.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; - -namespace Intellenum.Examples.TypicalScenarios.UsingInterfaces -{ - /// - /// Scenario: I want my IDs to have a common interface so that I can pass them to a method that - /// just wants to know they are an ID. - /// - internal class UsingInterfaces : IScenario - { - public Task Run() - { - ProcessIds(CustomerId.From(123), AccountId.From(321), DerivedId1.From(666), DerivedId2.From(42)); - return Task.CompletedTask; - } - - void ProcessIds(params IHaveAnId[] ids) - { - Console.WriteLine("IDs are " + string.Join(", ", ids.Select(i => i.Value))); - } - } - - public interface IHaveAnId - { - public T Value { get; } - } - - // defaults to int - [Intellenum] - internal readonly partial struct CustomerId : IHaveAnId - { - } - - [Intellenum] - internal partial struct AccountId : IHaveAnId - { - } - - // You could derive from this, but if you the type you're wrapping is a reference type, - // then be aware that there could be severe overhead of wrapping a reference type - // as a value type. One of the goals of Vogen is to not add too much overhead - // (in terms of memory/speed) over using the primitive type itself. - [Intellenum] - internal partial class Id : IHaveAnId { } - - internal class DerivedId1 : Id { } - internal class DerivedId2 : Id { } -} \ No newline at end of file diff --git a/samples/Intellenum.Examples/TypicalScenarios/ValidationExample.cs b/samples/Intellenum.Examples/TypicalScenarios/ValidationExample.cs deleted file mode 100644 index a78e3368..00000000 --- a/samples/Intellenum.Examples/TypicalScenarios/ValidationExample.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace Intellenum.Examples.TypicalScenarios.ValidationExamples -{ - [Intellenum] - - public partial class Dave - { - private static Validation Validate(string value) => - value.StartsWith("dave ", StringComparison.OrdinalIgnoreCase) || - value.StartsWith("david ", StringComparison.OrdinalIgnoreCase) - ? Validation.Ok - : Validation.Invalid($"must be a dave or david - {value} is neither."); - } - - internal class ValidationExample : IScenario - { - public Task Run() - { - string[] names = new[] { "Dave Grohl", "David Beckham", "Fred Flintstone" }; - - var processor = new DaveProcessor(); - - foreach (string name in names) - { - try - { - processor.Process(Dave.From(name)); - } - catch (ValueObjectValidationException e) - { - Console.WriteLine(e.Message); - } - } - - return Task.CompletedTask; - } - - private class DaveProcessor - { - internal void Process(Dave dave) => Console.WriteLine($"Processing {dave}"); - } - } -} \ No newline at end of file diff --git a/src/Intellenum/AnalyzerReleases.Shipped.md b/src/Intellenum/AnalyzerReleases.Shipped.md index 960f7847..33c0f807 100644 --- a/src/Intellenum/AnalyzerReleases.Shipped.md +++ b/src/Intellenum/AnalyzerReleases.Shipped.md @@ -7,7 +7,6 @@ Rule ID | Category | Severity | Notes --------|----------|----------|------- -AddValidationMethod | Usage | Info | AddValidationAnalyzer VOG010 | Usage | Error | DoNotUseNewAnalyzer VOG009 | Usage | Error | DoNotUseDefaultAnalyzer VOG025 | Usage | Error | DoNotUseReflection \ No newline at end of file From 086ee0c25405c26bf8131405b6fd72f8f62b31b6 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 30 Mar 2023 23:08:17 +0100 Subject: [PATCH 2/8] Got more tests working. Removed 'ValueChecked' --- Consumers.sln | 1 + README.md | 6 +- Scratch/GeneralTests.cs | 20 -- Scratch/ImpliedFieldNameTests.cs | 37 ++ Scratch/ListTests.cs | 25 ++ .../Types/DateTimeOffsetVo.cs | 2 +- samples/Intellenum.Examples/Types/IntoVo.cs | 4 +- .../TypicalScenarios/BasicExamples.cs | 13 + .../TypicalScenarios/Instances.cs | 8 + .../IntellenumAttribute.cs | 4 +- .../IntellenumValidationException.cs | 89 ++++- .../BuildInstancePropertiesFromAttributes.cs | 1 + src/Intellenum/BuildWorkItems.cs | 49 ++- src/Intellenum/Generators/ClassGenerator.cs | 66 ++-- src/Intellenum/InstanceGeneration.cs | 31 +- src/Intellenum/InstanceProperties.cs | 15 +- src/Intellenum/TryParseGeneration.cs | 4 +- src/Intellenum/Util.cs | 34 +- tests/ConsumerTests/BugFixTests.cs | 43 ++- tests/ConsumerTests/ConsumerTests.csproj | 5 + tests/ConsumerTests/CreationTests.cs | 70 +--- .../DeserializationValidationTests.cs | 30 +- .../IntDeserializationValidationTests.cs | 35 +- .../SharedTypes.cs | 46 +-- .../StringDeserializationValidationTests.cs | 94 ++--- tests/ConsumerTests/EqualityTests.cs | 60 ++-- .../DeserializationValidationTests.cs | 107 ------ .../IntDeserializationValidationTests.cs | 28 +- .../SharedTypes.cs | 41 +-- .../StringDeserializationValidationTests.cs | 28 +- tests/ConsumerTests/HashCodeTests.cs | 193 +++-------- tests/ConsumerTests/IComparableTests.cs | 260 ++++---------- .../Instances/InstanceFieldTests.cs | 28 -- .../ConsumerTests/Instances/InstanceTests.cs | 39 ++- tests/ConsumerTests/Instances/Types.cs | 38 +-- tests/ConsumerTests/ModuleInitialization.cs | 82 +---- .../NormalizeInputTests_Class.cs | 69 ---- .../NormalizeInputTests_Struct.cs | 77 ----- .../ConsumerTests/RecordClassCreationTests.cs | 57 ---- tests/ConsumerTests/RecordClassTests.cs | 70 ---- .../RecordStructCreationTests.cs | 56 --- tests/ConsumerTests/RecordStructTests.cs | 69 ---- .../ClassVos/AnyOtherTypeVoTests.cs | 113 +++--- .../ClassVos/BoolVoTests.cs | 76 ++--- .../ClassVos/ByteVoTests.cs | 57 ++-- .../ClassVos/CharVoTests.cs | 68 ++-- .../ClassVos/DateOnlyVoTests.cs | 88 +++-- .../ClassVos/DateTimeOffsetVoTests.cs | 68 ++-- .../ClassVos/DateTimeVoTests.cs | 70 ++-- .../ClassVos/DecimalVoTests.cs | 76 +++-- .../ClassVos/DoubleVoTests.cs | 68 ++-- .../ClassVos/FloatVoTests.cs | 67 ++-- .../ClassVos/GuidVoTests.cs | 88 ++--- .../ClassVos/IntVoTests.cs | 62 ++-- .../ClassVos/LongVoTests.cs | 62 ++-- .../ClassVos/ShortVoTests.cs | 74 ++-- .../ClassVos/StringVoTests.cs | 70 ++-- .../ClassVos/TimeOnlyVoTests.cs | 81 +++-- .../ClassVos/Types/BoolVo.cs | 38 ++- .../ClassVos/Types/ByteVo.cs | 42 ++- .../ClassVos/Types/CharVo.cs | 38 ++- .../ClassVos/Types/DateOnlyVo.cs | 102 ++++-- .../ClassVos/Types/DateTimeOffsetVo.cs | 105 ++++-- .../ClassVos/Types/DateTimeVo.cs | 104 ++++-- .../ClassVos/Types/DecimalVo.cs | 113 ++++-- .../ClassVos/Types/DoubleVo.cs | 49 ++- .../ClassVos/Types/FloatVo.cs | 42 ++- .../ClassVos/Types/FooVo.cs | 125 +++++-- .../ClassVos/Types/GuidVo.cs | 103 ++++-- .../ClassVos/Types/IntoVo.cs | 42 ++- .../ClassVos/Types/LongVo.cs | 42 ++- .../ClassVos/Types/ShortVo.cs | 42 ++- .../ClassVos/Types/StringVo.cs | 38 ++- .../ClassVos/Types/TimeOnlyVo.cs | 107 ++++-- .../ComplexSerializationTests.cs | 131 ++----- .../CustomizationTests.cs | 99 ++++-- .../RecordClassVos/AnyOtherTypeVoTests.cs | 320 ----------------- .../RecordClassVos/BoolVoTests.cs | 280 --------------- .../RecordClassVos/ByteVoTests.cs | 264 -------------- .../RecordClassVos/CharVoTests.cs | 263 -------------- .../RecordClassVos/DateTimeOffsetVoTests.cs | 272 --------------- .../RecordClassVos/DateTimeVoTests.cs | 272 --------------- .../RecordClassVos/DecimalVoTests.cs | 294 ---------------- .../RecordClassVos/DoubleVoTests.cs | 286 ---------------- .../RecordClassVos/FloatVoTests.cs | 286 ---------------- .../RecordClassVos/GuidVoTests.cs | 262 -------------- .../RecordClassVos/IntVoTests.cs | 261 -------------- .../RecordClassVos/LongVoTests.cs | 261 -------------- .../RecordClassVos/ShortVoTests.cs | 261 -------------- .../RecordClassVos/StringVoTests.cs | 261 -------------- .../RecordClassVos/Types/BoolVo.cs | 29 -- .../RecordClassVos/Types/ByteVo.cs | 29 -- .../RecordClassVos/Types/CharVo.cs | 29 -- .../RecordClassVos/Types/DateTimeOffsetVo.cs | 31 -- .../RecordClassVos/Types/DateTimeVo.cs | 31 -- .../RecordClassVos/Types/DecimalVo.cs | 29 -- .../RecordClassVos/Types/DoubleVo.cs | 29 -- .../RecordClassVos/Types/FloatVo.cs | 29 -- .../RecordClassVos/Types/FooVo.cs | 40 --- .../RecordClassVos/Types/GuidVo.cs | 31 -- .../RecordClassVos/Types/IntoVo.cs | 29 -- .../RecordClassVos/Types/LongVo.cs | 29 -- .../RecordClassVos/Types/ShortVo.cs | 29 -- .../RecordClassVos/Types/StringVo.cs | 29 -- .../RecordStructVos/AnyOtherTypeVoTests.cs | 322 ------------------ .../RecordStructVos/BoolVoTests.cs | 280 --------------- .../RecordStructVos/ByteVoTests.cs | 264 -------------- .../RecordStructVos/CharVoTests.cs | 263 -------------- .../RecordStructVos/DateTimeOffsetVoTests.cs | 272 --------------- .../RecordStructVos/DateTimeVoTests.cs | 272 --------------- .../RecordStructVos/DecimalVoTests.cs | 294 ---------------- .../RecordStructVos/DoubleVoTests.cs | 286 ---------------- .../RecordStructVos/FloatVoTests.cs | 286 ---------------- .../RecordStructVos/GuidVoTests.cs | 262 -------------- .../RecordStructVos/IntVoTests.cs | 261 -------------- .../RecordStructVos/LongVoTests.cs | 261 -------------- .../RecordStructVos/ShortVoTests.cs | 261 -------------- .../RecordStructVos/StringVoTests.cs | 261 -------------- .../RecordStructVos/Types/BoolVo.cs | 29 -- .../RecordStructVos/Types/ByteVo.cs | 29 -- .../RecordStructVos/Types/CharVo.cs | 29 -- .../RecordStructVos/Types/DateTimeOffsetVo.cs | 31 -- .../RecordStructVos/Types/DateTimeVo.cs | 31 -- .../RecordStructVos/Types/DecimalVo.cs | 29 -- .../RecordStructVos/Types/DoubleVo.cs | 29 -- .../RecordStructVos/Types/FloatVo.cs | 29 -- .../RecordStructVos/Types/FooVo.cs | 40 --- .../RecordStructVos/Types/GuidVo.cs | 31 -- .../RecordStructVos/Types/IntoVo.cs | 29 -- .../RecordStructVos/Types/LongVo.cs | 29 -- .../RecordStructVos/Types/ShortVo.cs | 29 -- .../RecordStructVos/Types/StringVo.cs | 29 -- .../StructVos/AnyOtherTypeVoTests.cs | 320 ----------------- .../StructVos/BoolVoTests.cs | 280 --------------- .../StructVos/ByteVoTests.cs | 264 -------------- .../StructVos/CharVoTests.cs | 263 -------------- .../StructVos/DateTimeOffsetVoTests.cs | 272 --------------- .../StructVos/DateTimeVoTests.cs | 272 --------------- .../StructVos/DecimalVoTests.cs | 273 --------------- .../StructVos/DoubleVoTests.cs | 264 -------------- .../StructVos/FloatVoTests.cs | 264 -------------- .../StructVos/GuidVoTests.cs | 262 -------------- .../StructVos/IntVoTests.cs | 261 -------------- .../StructVos/LongVoTests.cs | 261 -------------- .../StructVos/ShortVoTests.cs | 261 -------------- .../StructVos/StringVoTests.cs | 261 -------------- .../StructVos/Types/BoolVo.cs | 29 -- .../StructVos/Types/ByteVo.cs | 29 -- .../StructVos/Types/CharVo.cs | 29 -- .../StructVos/Types/DateTimeOffsetVo.cs | 31 -- .../StructVos/Types/DateTimeVo.cs | 31 -- .../StructVos/Types/DecimalVo.cs | 29 -- .../StructVos/Types/DoubleVo.cs | 29 -- .../StructVos/Types/FloatVo.cs | 29 -- .../StructVos/Types/FooVo.cs | 40 --- .../StructVos/Types/GuidVo.cs | 31 -- .../StructVos/Types/IntoVo.cs | 29 -- .../StructVos/Types/LongVo.cs | 29 -- .../StructVos/Types/ShortVo.cs | 29 -- .../StructVos/Types/StringVo.cs | 29 -- .../ToStringTests/BasicFunctionality.cs | 16 +- .../ConsumerTests/ToStringTests/Derivation.cs | 37 +- tests/ConsumerTests/TryParseTests/Tests.cs | 48 +-- tests/ConsumerTests/TryParseTests/Types.cs | 49 +-- tests/ConsumerTests/Types/Age.cs | 7 +- tests/ConsumerTests/Types/Anything.cs | 6 +- tests/ConsumerTests/Types/CustomerId.cs | 4 - tests/ConsumerTests/Types/CustomerType.cs | 6 + tests/ConsumerTests/Types/Dave.cs | 12 - tests/ConsumerTests/Types/EightiesDate.cs | 11 - .../Types/EscapedTypesAndNamespaces.cs | 42 ++- .../ConsumerTests/Types/MinimalValidation.cs | 8 - tests/ConsumerTests/Types/MyInt.cs | 24 +- tests/ConsumerTests/Types/MyIntStruct.cs | 13 - .../Types/MyIntStructWithADefaultOf22.cs | 30 -- tests/ConsumerTests/Types/MyRecord.cs | 29 -- tests/ConsumerTests/Types/MyRecordClassInt.cs | 26 -- .../Types/MyRecordClassString.cs | 25 -- tests/ConsumerTests/Types/MyString.cs | 13 +- tests/ConsumerTests/Types/Name.cs | 14 - tests/ConsumerTests/Types/NameType.cs | 9 + tests/ConsumerTests/Types/Number.cs | 10 +- tests/ConsumerTests/Types/Score.cs | 7 - tests/ConsumerTests/Types/ScoreType.cs | 10 + tests/ConsumerTests/ValidationTests.cs | 102 ------ .../Explicit_instance_tests.cs | 2 +- tests/ScratchSnapshotTests/GeneralTests.cs | 316 +++++++++++++++++ tests/ScratchSnapshotTests/SnapshotRunner.cs | 2 +- ...ces_using_Instance_attributes.verified.txt | 198 ++++++++--- ...stances_using_Instance_method.verified.txt | 196 +++++++++-- ...using_a_mixture_of_mechanisms.verified.txt | 198 ++++++++--- ....Explicit_instances_using_new.verified.txt | 196 +++++++++-- ...tances_using_target_typed_new.verified.txt | 196 +++++++++-- ...ests.int_created_successfully.verified.txt | 198 ++++++++--- ...s.string_created_successfully.verified.txt | 76 ++--- ...es_can_have_reserved_keywords.verified.txt | 158 ++++++--- .../GeneralTests.No_namespace.verified.txt | 156 ++++++--- ...al_class_created_successfully.verified.txt | 156 ++++++--- ...neralTests.Produces_instances.verified.txt | 160 ++++++--- ...es_can_have_reserved_keywords.verified.txt | 200 ++++++++--- .../GeneralTests.No_namespace.verified.txt | 198 ++++++++--- ...al_class_created_successfully.verified.txt | 198 ++++++++--- ...neralTests.Produces_instances.verified.txt | 202 ++++++++--- ...es_can_have_reserved_keywords.verified.txt | 196 +++++++++-- ...es_can_have_reserved_keywords.verified.txt | 198 ++++++++--- ...icAttributeTests.No_namespace.verified.txt | 196 +++++++++-- ...ibuteTests.Produces_instances.verified.txt | 202 ++++++++--- ...tances_with_derived_attribute.verified.txt | 202 ++++++++--- 208 files changed, 5571 insertions(+), 16306 deletions(-) create mode 100644 Scratch/ImpliedFieldNameTests.cs create mode 100644 Scratch/ListTests.cs delete mode 100644 tests/ConsumerTests/GenericDeserializationValidationTests/DeserializationValidationTests.cs delete mode 100644 tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Class.cs delete mode 100644 tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Struct.cs delete mode 100644 tests/ConsumerTests/RecordClassCreationTests.cs delete mode 100644 tests/ConsumerTests/RecordClassTests.cs delete mode 100644 tests/ConsumerTests/RecordStructCreationTests.cs delete mode 100644 tests/ConsumerTests/RecordStructTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/AnyOtherTypeVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/BoolVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ByteVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/CharVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeOffsetVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DecimalVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DoubleVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/FloatVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/GuidVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/IntVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/LongVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ShortVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/StringVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/BoolVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ByteVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/CharVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeOffsetVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DecimalVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DoubleVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FloatVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FooVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/GuidVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/IntoVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/LongVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ShortVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/StringVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/AnyOtherTypeVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/BoolVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ByteVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/CharVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeOffsetVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DecimalVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DoubleVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/FloatVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/GuidVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/IntVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/LongVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ShortVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/StringVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/BoolVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ByteVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/CharVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeOffsetVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DecimalVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DoubleVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FloatVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FooVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/GuidVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/IntoVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/LongVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ShortVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/StringVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/AnyOtherTypeVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/BoolVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/ByteVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/CharVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeOffsetVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/DecimalVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/DoubleVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/FloatVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/GuidVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/IntVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/LongVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/ShortVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/StringVoTests.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/BoolVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ByteVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/CharVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeOffsetVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DecimalVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DoubleVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FloatVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FooVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/GuidVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/IntoVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/LongVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ShortVo.cs delete mode 100644 tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/StringVo.cs delete mode 100644 tests/ConsumerTests/Types/CustomerId.cs create mode 100644 tests/ConsumerTests/Types/CustomerType.cs delete mode 100644 tests/ConsumerTests/Types/Dave.cs delete mode 100644 tests/ConsumerTests/Types/EightiesDate.cs delete mode 100644 tests/ConsumerTests/Types/MinimalValidation.cs delete mode 100644 tests/ConsumerTests/Types/MyIntStruct.cs delete mode 100644 tests/ConsumerTests/Types/MyIntStructWithADefaultOf22.cs delete mode 100644 tests/ConsumerTests/Types/MyRecord.cs delete mode 100644 tests/ConsumerTests/Types/MyRecordClassInt.cs delete mode 100644 tests/ConsumerTests/Types/MyRecordClassString.cs delete mode 100644 tests/ConsumerTests/Types/Name.cs create mode 100644 tests/ConsumerTests/Types/NameType.cs delete mode 100644 tests/ConsumerTests/Types/Score.cs create mode 100644 tests/ConsumerTests/Types/ScoreType.cs delete mode 100644 tests/ConsumerTests/ValidationTests.cs diff --git a/Consumers.sln b/Consumers.sln index 5db902d6..1db478ed 100644 --- a/Consumers.sln +++ b/Consumers.sln @@ -34,6 +34,7 @@ Global {B9FAC951-7F77-49B5-9DCD-C8278B45EE65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B9FAC951-7F77-49B5-9DCD-C8278B45EE65}.Release|Any CPU.ActiveCfg = Release|Any CPU {B9FAC951-7F77-49B5-9DCD-C8278B45EE65}.Release|Any CPU.Build.0 = Release|Any CPU + {B9FAC951-7F77-49B5-9DCD-C8278B45EE65}.Debug|Any CPU.Build.0 = Debug|Any CPU {53C7CAAF-5A7F-4AF0-9C9E-216F13A054D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {53C7CAAF-5A7F-4AF0-9C9E-216F13A054D7}.Debug|Any CPU.Build.0 = Debug|Any CPU {53C7CAAF-5A7F-4AF0-9C9E-216F13A054D7}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/README.md b/README.md index 453f2dd1..461f3c2f 100644 --- a/README.md +++ b/README.md @@ -197,10 +197,8 @@ _note that EnumGenerators isn't here as we use the standard C# enum to get its v | SmartEnums | 0.3246 ns | 0.0082 ns | 0.0069 ns | - | | **Intellenums** | **0.3198 ns** | **0.0103 ns** | **0.0096 ns** | **-** | -Note that Intellenums also has a `ValueCheck` property which throws if the -value hasn't been initialised. This takes twice as long. This isn't usually a problem -but if you're in a very tight loop and you're sure everything is initialized, then use -`Value` instead +### What does `ToString` return? +It returns the name of the instance. > NOTE: Intellenum is in pre-release at the moment, so probably isn't production ready and the API might (and probably will) change. diff --git a/Scratch/GeneralTests.cs b/Scratch/GeneralTests.cs index 1a9dbd97..b407d572 100644 --- a/Scratch/GeneralTests.cs +++ b/Scratch/GeneralTests.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using Xunit.Abstractions; namespace Scratch; @@ -81,23 +80,4 @@ public void General() ((int) t1 == t1).Should().BeTrue(); ((int) t1 == 1).Should().BeTrue(); } -} -public class ListTests -{ - private readonly ITestOutputHelper _testOutputHelper; - - public ListTests(ITestOutputHelper testOutputHelper) => _testOutputHelper = testOutputHelper; - - [Fact] - public void General() - { - var l = CustomerType.List(); - - l.Count().Should().Be(2); - - foreach (var (name, value) in CustomerType.List()) - { - _testOutputHelper.WriteLine($"{name} - {value}"); - } - } } \ No newline at end of file diff --git a/Scratch/ImpliedFieldNameTests.cs b/Scratch/ImpliedFieldNameTests.cs new file mode 100644 index 00000000..323939be --- /dev/null +++ b/Scratch/ImpliedFieldNameTests.cs @@ -0,0 +1,37 @@ +using FluentAssertions; +using Intellenum; + +namespace Scratch +{ + public class ImpliedFieldNameTests + { + [Fact] + public void General() + { + ImpliedFieldName.Instance1.Value.Should().Be(1); + ImpliedFieldName.Instance2.Value.Should().Be(2); + ImpliedFieldName.Instance3.Value.Should().Be(3); + + ImpliedFieldName.IsDefined(1).Should().BeTrue(); + ImpliedFieldName.IsDefined(2).Should().BeTrue(); + ImpliedFieldName.IsDefined(3).Should().BeTrue(); + + ImpliedFieldName.TryFromName("Instance1", out var i1).Should().BeTrue(); + i1.Value.Should().Be(1); + + ImpliedFieldName.TryFromName("INSTANCE 3!!", out var i3).Should().BeTrue(); + i3.Value.Should().Be(3); + + ImpliedFieldName.IsDefined(3).Should().BeTrue(); + ImpliedFieldName.IsNamedDefined("INSTANCE 3!!").Should().BeTrue(); + } + } + + [Intellenum] + public partial class ImpliedFieldName + { + public static readonly ImpliedFieldName Instance1 = new(1); + public static readonly ImpliedFieldName Instance2 = new(2); + public static readonly ImpliedFieldName Instance3 = new("INSTANCE 3!!", 3); + } +} \ No newline at end of file diff --git a/Scratch/ListTests.cs b/Scratch/ListTests.cs new file mode 100644 index 00000000..4d4cc014 --- /dev/null +++ b/Scratch/ListTests.cs @@ -0,0 +1,25 @@ +using FluentAssertions; +using Xunit.Abstractions; + +namespace Scratch +{ + public class ListTests + { + private readonly ITestOutputHelper _testOutputHelper; + + public ListTests(ITestOutputHelper testOutputHelper) => _testOutputHelper = testOutputHelper; + + [Fact] + public void General() + { + var l = CustomerType.List(); + + l.Count().Should().Be(2); + + foreach (var (name, value) in CustomerType.List()) + { + _testOutputHelper.WriteLine($"{name} - {value}"); + } + } + } +} \ No newline at end of file diff --git a/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs b/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs index 8747bd6f..22b68228 100644 --- a/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs +++ b/samples/Intellenum.Examples/Types/DateTimeOffsetVo.cs @@ -14,7 +14,7 @@ public partial class NoConverterDateTimeOffsetVo static NoConverterDateTimeOffsetVo() { Instance("Item1", DateTimeOffset.Parse("2020-01-01T00:00:00Z")); - Instance("Item2", new DateTimeOffset(2020, 12, 13, 14, 15, 16, TimeSpan.Zero)); + Instance("Item2", DateTimeOffset.Parse("2020-01-02T00:00:00Z")); } } diff --git a/samples/Intellenum.Examples/Types/IntoVo.cs b/samples/Intellenum.Examples/Types/IntoVo.cs index 5661a7bb..c7f29ca5 100644 --- a/samples/Intellenum.Examples/Types/IntoVo.cs +++ b/samples/Intellenum.Examples/Types/IntoVo.cs @@ -4,13 +4,13 @@ [Intellenum] [Instance("Item1", 1)] [Instance("Item2", 2)] -public partial class MyValueObject { } +public partial class MyIntellenum { } // the underlying type can be specified [Intellenum(typeof(int))] [Instance("Item1", 1)] [Instance("Item2", 2)] -public partial class MyValueObject2 { } +public partial class MyIntellenum2 { } // conversions can be specified, but if not, it defaults to TypeConverter and SystemTextJson [Intellenum(conversions: Conversions.None, underlyingType: typeof(int))] diff --git a/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs b/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs index 70e493eb..5a5eceff 100644 --- a/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs +++ b/samples/Intellenum.Examples/TypicalScenarios/BasicExamples.cs @@ -57,6 +57,19 @@ public partial class CustomerType public partial class SupplierType { } + + [Intellenum] + public partial class FooEnum + { + public static readonly FooEnum Item1 = new("Item1", new Foo("a", 1)); + public static readonly FooEnum Item2= new("Item2", new Foo("b", 2)); + } + + public record class Foo(string Name, int Age) : IComparable + { + public int CompareTo(Foo other) => Age.CompareTo(other.Age); + } + internal class CustomerProcessor { diff --git a/samples/Intellenum.Examples/TypicalScenarios/Instances.cs b/samples/Intellenum.Examples/TypicalScenarios/Instances.cs index fa464085..cfe337db 100644 --- a/samples/Intellenum.Examples/TypicalScenarios/Instances.cs +++ b/samples/Intellenum.Examples/TypicalScenarios/Instances.cs @@ -21,11 +21,19 @@ public Task Run() var invalidVi = VendorInformation.FromTextFile(); Console.WriteLine((bool) (invalidVi.VendorId == VendorType.Invalid)); // true + + Console.WriteLine(ImpliedFieldName.Instance1); return Task.CompletedTask; } } + [Intellenum] + public partial class ImpliedFieldName + { + public static readonly ImpliedFieldName Instance1 = new(1); + } + [Intellenum] [Instance("Freezing", 0.0f)] diff --git a/src/Intellenum.SharedTypes/IntellenumAttribute.cs b/src/Intellenum.SharedTypes/IntellenumAttribute.cs index 49bd6bd7..c5eb2cd4 100644 --- a/src/Intellenum.SharedTypes/IntellenumAttribute.cs +++ b/src/Intellenum.SharedTypes/IntellenumAttribute.cs @@ -20,8 +20,9 @@ public class IntellenumAttribute : IntellenumAttribute // using parameter indexes (i.e. it expected param 0 to be the underlying type etc. public IntellenumAttribute( Conversions conversions = Conversions.Default, + Customizations customizations = Customizations.None, DebuggerAttributeGeneration debuggerAttributes = DebuggerAttributeGeneration.Default) - : base(typeof(T), conversions, debuggerAttributes) + : base(typeof(T), conversions, customizations, debuggerAttributes) { } } @@ -40,6 +41,7 @@ public class IntellenumAttribute : Attribute public IntellenumAttribute( Type? underlyingType = null!, Conversions conversions = Conversions.Default, + Customizations customizations = Customizations.None, DebuggerAttributeGeneration debuggerAttributes = DebuggerAttributeGeneration.Default) { } diff --git a/src/Intellenum.SharedTypes/IntellenumValidationException.cs b/src/Intellenum.SharedTypes/IntellenumValidationException.cs index 70726818..64ac9b7a 100644 --- a/src/Intellenum.SharedTypes/IntellenumValidationException.cs +++ b/src/Intellenum.SharedTypes/IntellenumValidationException.cs @@ -4,7 +4,29 @@ namespace Intellenum; [Serializable] -public class IntellenumValidationException : Exception +public class IntellenumException : Exception +{ + public IntellenumException() + { + } + + public IntellenumException(string message) : base(message) + { + } + + public IntellenumException(string message, Exception inner) : base(message, inner) + { + } + + protected IntellenumException( + SerializationInfo info, + StreamingContext context) : base(info, context) + { + } +} + +[Serializable] +public class IntellenumValidationException : IntellenumException { public IntellenumValidationException() { @@ -23,4 +45,69 @@ protected IntellenumValidationException( StreamingContext context) : base(info, context) { } +} + +[Serializable] +public class IntellenumUninitialisedException : IntellenumException +{ + public IntellenumUninitialisedException() + { + } + + public IntellenumUninitialisedException(string message) : base(message) + { + } + + public IntellenumUninitialisedException(string message, Exception inner) : base(message, inner) + { + } + + protected IntellenumUninitialisedException( + SerializationInfo info, + StreamingContext context) : base(info, context) + { + } +} +[Serializable] +public class IntellenumCreationFailedException : IntellenumException +{ + public IntellenumCreationFailedException() + { + } + + public IntellenumCreationFailedException(string message) : base(message) + { + } + + public IntellenumCreationFailedException(string message, Exception inner) : base(message, inner) + { + } + + protected IntellenumCreationFailedException( + SerializationInfo info, + StreamingContext context) : base(info, context) + { + } +} + +[Serializable] +public class IntellenumMatchFailedException : IntellenumException +{ + public IntellenumMatchFailedException() + { + } + + public IntellenumMatchFailedException(string message) : base(message) + { + } + + public IntellenumMatchFailedException(string message, Exception inner) : base(message, inner) + { + } + + protected IntellenumMatchFailedException( + SerializationInfo info, + StreamingContext context) : base(info, context) + { + } } \ No newline at end of file diff --git a/src/Intellenum/BuildInstancePropertiesFromAttributes.cs b/src/Intellenum/BuildInstancePropertiesFromAttributes.cs index 6f1704e4..cd4ab636 100644 --- a/src/Intellenum/BuildInstancePropertiesFromAttributes.cs +++ b/src/Intellenum/BuildInstancePropertiesFromAttributes.cs @@ -111,6 +111,7 @@ internal static class BuildInstancePropertiesFromAttributes return new InstanceProperties( InstanceSource.FromAttribute, (string)nameConstant.Value!, + (string)nameConstant.Value!, r.Value, valueConstant.Value!, (string) (commentConstant.Value ?? string.Empty)); diff --git a/src/Intellenum/BuildWorkItems.cs b/src/Intellenum/BuildWorkItems.cs index 73b9b512..e691d005 100644 --- a/src/Intellenum/BuildWorkItems.cs +++ b/src/Intellenum/BuildWorkItems.cs @@ -280,7 +280,7 @@ private static IEnumerable FindInstancesFromInstanceMethodIn ArgumentSyntax second = eachInvocation.ArgumentList.Arguments[1]; var secondAsString = second.ToString(); - yield return new InstanceProperties(InstanceSource.FromInstanceMethod, firstAsString, secondAsString, secondAsString); + yield return new InstanceProperties(InstanceSource.FromInstanceMethod, firstAsString, firstAsString, secondAsString, secondAsString); } } @@ -301,7 +301,7 @@ private static IEnumerable FindInstancesFromNewStatements(IN var allConstructors = syntax.DescendantNodes().OfType(); - var newExpressions = allConstructors.Where(c => c.ArgumentList?.Arguments.Count == 2).ToList(); + var newExpressions = allConstructors.Where(c => c.ArgumentList?.Arguments.Count >=1).ToList(); var implicitNews = newExpressions.OfType() .Where(x => IsMatch(x, voClass.Name)).ToList(); @@ -317,20 +317,49 @@ private static IEnumerable FindInstancesFromNewStatements(IN var args = newExpression.ArgumentList; if (args is null) continue; - if (args.Arguments.Count != 2) continue; - ArgumentSyntax first = args.Arguments[0]; - - var firstAsString = (first.Expression as LiteralExpressionSyntax)?.Token.Value as string; - if (firstAsString is null) + // we could have 1 or 2 arguments here. + // if it's two, then the first is the "enum name", and the second is the value (the "Field Name" is inferred from the syntax + // it it's just one, then the "Field Name" is the "enum name", and the value is inferred from the syntax. + + string fieldName = each.Name; // todo: get the field name from the syntax + string enumName = fieldName; + + bool explicitlyNamed = false; + if (args.Arguments.Count >2) continue; + + if (args.Arguments.Count == 2) { - throw new InvalidOperationException($"Expected string literal as name parameter to a parameter of the constructor for creating a type of '{voClass.Name}'"); + explicitlyNamed = true; + ArgumentSyntax first = args.Arguments[0]; + + var firstAsString = (first.Expression as LiteralExpressionSyntax)?.Token.Value as string; + + enumName = firstAsString ?? throw new InvalidOperationException( + $"Expected string literal as name parameter to a parameter of the constructor for creating a type of '{voClass.Name}'"); } + + int index = args.Arguments.Count == 2 ? 1 : 0; - ArgumentSyntax second = args.Arguments[1]; + // we don't need the expression - but would it come in handy? // todo: determine + ArgumentSyntax second = args.Arguments[index]; var secondAsString = second.ToString(); - yield return new InstanceProperties(InstanceSource.FromNewExpression, firstAsString, secondAsString, secondAsString); + // the name field makes no sense here because the name *must* be the name of the field being declared. + // the only thing that we could do is make it an alias if different, but users might as well declare the alias themselves as the field name. + // perhaps what we could do is look for a new express + // SmartEnum allows a different 'name' to be added, and it also allows the same 'value' to be added + // So we could treat this is an alias, e.g. "Item1" is the field name, and "Fred" is the name in the constructor, so 'FromName' + // could look up "Fred" and return the instance with the field name "Item1" + + + // in the static constructor, we can enumerate these and sets the values on the fields - it just means + // making FieldName and EnumName with private setters + + // actually, none of this will work - an explicit field *must* specify the field name, the enum name, and the value + // we can probably infer the enum name from the field name with a constructor overload, but that's it. + + yield return new InstanceProperties(InstanceSource.FromNewExpression, fieldName, enumName, secondAsString, secondAsString, "", explicitlyNamed); } bool isSameSymbol(ISymbol m) diff --git a/src/Intellenum/Generators/ClassGenerator.cs b/src/Intellenum/Generators/ClassGenerator.cs index cd6a4f63..7f570e09 100644 --- a/src/Intellenum/Generators/ClassGenerator.cs +++ b/src/Intellenum/Generators/ClassGenerator.cs @@ -32,20 +32,22 @@ public string BuildClass(VoWorkItem item, TypeDeclarationSyntax tds) #endif private readonly global::System.Boolean _isInitialized; private readonly {itemUnderlyingType} _value; - - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public {itemUnderlyingType} ValueChecked - {{ - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - {{ - if(!_isInitialized) Throw(); - return _value; - }} - }} + + {InstanceGeneration.GeneratePrivateConstructionInitialisationIfNeeded(item)} + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public {itemUnderlyingType} ValueChecked +// {{ +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// {{ +// if(!_isInitialized) Throw(); +// return _value; +// }} +// }} /// /// Gets the underlying value if set, otherwise default @@ -68,7 +70,7 @@ private void Throw() global::System.String message = ""Use of uninitialized Value Object.""; #endif - throw new {nameof(IntellenumValidationException)}(message); + throw new {nameof(IntellenumUninitialisedException)}(message); }} @@ -85,14 +87,22 @@ private void Throw() }} [global::System.Diagnostics.DebuggerStepThroughAttribute] - private {className}(string name, {itemUnderlyingType} value) + private {className}({itemUnderlyingType} value) {{ _value = value; - Name = name; + Name = ""[INFERRED-TO-BE-REPLACED!]""; + _isInitialized = true; + }} + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private {className}(string enumName, {itemUnderlyingType} value) + {{ + _value = value; + Name = enumName; _isInitialized = true; }} - public string Name {{ get; }} + public string Name {{ get; private set; }} /// /// Builds an instance from an enum value. @@ -254,22 +264,6 @@ private static void Instance(string name, {itemUnderlyingType} value) }} }} - private void EnsureInitialized() - {{ - if (!_isInitialized) - {{ -#if DEBUG - global::System.String message = ""Use of uninitialized Value Object at: "" + _stackTrace ?? """"; -#else - global::System.String message = ""Use of uninitialized Value Object.""; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - }} - }} - - {InstanceGeneration.GenerateAnyInstances(tds, item)} public static global::System.Collections.Generic.IEnumerable<{className}> List() @@ -280,6 +274,8 @@ private void EnsureInitialized() {Util.GenerateToString(item)} {Util.GenerateAnyConversionBodies(tds, item)} + + {TryParseGeneration.GenerateTryParseIfNeeded(item)} {Util.GenerateDebuggerProxyForClasses(tds, item)} }} @@ -290,7 +286,7 @@ private static string GenerateNullCheckIfNeeded(VoWorkItem voWorkItem) => voWorkItem.IsValueType ? string.Empty : $@" if (value is null) {{ - throw new global::System.InvalidOperationException(""Cannot create a value object with null.""); + throw new {nameof(IntellenumCreationFailedException)}(""Cannot create an Intellenum instance with a null.""); }} "; } \ No newline at end of file diff --git a/src/Intellenum/InstanceGeneration.cs b/src/Intellenum/InstanceGeneration.cs index 1a50684f..9a347f1a 100644 --- a/src/Intellenum/InstanceGeneration.cs +++ b/src/Intellenum/InstanceGeneration.cs @@ -21,7 +21,7 @@ public static string GenerateIEnumerableYields(VoWorkItem item) foreach (InstanceProperties each in item.InstanceProperties) { - sb.AppendLine($"yield return {each.Name};"); + sb.AppendLine($"yield return {each.FieldName};"); } return sb.ToString(); @@ -54,7 +54,7 @@ private static string GenerateInstance( return $@" // instance... -{BuildInstanceComment(classDeclarationSyntax.Identifier, instanceProperties.TripleSlashComments, itemFullNamespace)}public static readonly {classDeclarationSyntax.Identifier} {Util.EscapeIfRequired(instanceProperties.Name)} = new {classDeclarationSyntax.Identifier}(""{instanceProperties.Name}"",{instanceValue});"; +{BuildInstanceComment(classDeclarationSyntax.Identifier, instanceProperties.TripleSlashComments, itemFullNamespace)}public static readonly {classDeclarationSyntax.Identifier} {Util.EscapeIfRequired(instanceProperties.FieldName)} = new {classDeclarationSyntax.Identifier}(""{instanceProperties.FieldName}"",{instanceValue});"; } private static string BuildInstanceComment(SyntaxToken syntaxToken, string? commentText, string fullNamespace) @@ -198,4 +198,31 @@ public static BuildResult TryBuildInstanceValueAsText(string propertyName, objec $"Named instance '{propertyName}' has a value type '{propertyValue?.GetType()}' of '{propertyValue}' which cannot be converted to the underlying type of '{underlyingType}' - {e.Message}"); } } + + public static string GeneratePrivateConstructionInitialisationIfNeeded(VoWorkItem item) + { + var implicitlyNamedInstances = item.InstanceProperties.Where(i => !i.ExplicitlyNamed).ToList(); + if (implicitlyNamedInstances.Count == 0) + { + return ""; + } + + StringBuilder sb = new StringBuilder(); + sb.AppendLine( + $$""" + static {{item.VoTypeName}}() +{ +"""); + + foreach (var eachInstance in implicitlyNamedInstances) + { + sb.AppendLine($"{eachInstance.FieldName}.Name = \"{eachInstance.FieldName}\";"); + } + + sb.AppendLine( + $$""" +} +"""); + return sb.ToString(); + } } \ No newline at end of file diff --git a/src/Intellenum/InstanceProperties.cs b/src/Intellenum/InstanceProperties.cs index c0c7f0ad..5b4d86a4 100644 --- a/src/Intellenum/InstanceProperties.cs +++ b/src/Intellenum/InstanceProperties.cs @@ -3,25 +3,32 @@ public class InstanceProperties { public InstanceProperties(InstanceSource source, - string name, + string fieldName, + string enumFriendlyName, string valueAsText, object value, - string tripleSlashComments = "") + string tripleSlashComments = "", + bool explicitlyNamed = true) { Source = source; - Name = name; + FieldName = fieldName; + EnumEnumFriendlyName = enumFriendlyName; ValueAsText = valueAsText; Value = value; TripleSlashComments = tripleSlashComments; + ExplicitlyNamed = explicitlyNamed; } public InstanceSource Source { get; } - public string Name { get; } + public string FieldName { get; } + + public string EnumEnumFriendlyName { get; } public string ValueAsText { get; } public object Value { get; } public string TripleSlashComments { get; } + public bool ExplicitlyNamed { get; } } diff --git a/src/Intellenum/TryParseGeneration.cs b/src/Intellenum/TryParseGeneration.cs index 013ec186..500b4de6 100644 --- a/src/Intellenum/TryParseGeneration.cs +++ b/src/Intellenum/TryParseGeneration.cs @@ -51,10 +51,10 @@ private static void BuildMethod(IMethodSymbol methodSymbol, StringBuilder sb, Vo /// /// The value created via the method. /// - /// Thrown when the value can be parsed, but is not valid. + /// Thrown when the value can be parsed, but is not valid. public static global::System.Boolean TryParse({parameters}, {GenerateNotNullWhenAttribute()} out {item.VoTypeName} result) {{ if({item.UnderlyingTypeFullName}.TryParse({parameterNames}, out var r)) {{ - result = From(r); + result = FromValue(r); return true; }} diff --git a/src/Intellenum/Util.cs b/src/Intellenum/Util.cs index bf701126..c679ba6d 100644 --- a/src/Intellenum/Util.cs +++ b/src/Intellenum/Util.cs @@ -30,7 +30,7 @@ public static string GenerateCallToValidateForDeserializing(VoWorkItem workItem) foreach (var eachInstance in workItem.InstanceProperties) { - string escapedName = EscapeIfRequired(eachInstance.Name); + string escapedName = EscapeIfRequired(eachInstance.FieldName); sb.AppendLine($" if(value == {escapedName}.Value) return {escapedName};"); } @@ -221,23 +221,23 @@ string generateForConstant() public static string GenerateFromValueImplementation(VoWorkItem item) { if (item.IsConstant) - return """ + return $$""" bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new {{nameof(IntellenumMatchFailedException)}}($"{{item.VoTypeName}} has no matching instances with a value of '{value}'"); """; - return """ + return $$""" bool b = _valuesToEnums.Value.TryGetValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new {{nameof(IntellenumMatchFailedException)}}($"{{item.VoTypeName}} has no matching instances with a value of '{value}'"); """; } public static string GenerateFromNameImplementation(VoWorkItem item) => - """ + $$""" bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new {{nameof(IntellenumMatchFailedException)}}($"{{item.VoTypeName}} has no matching instances named '{name}'"); """; public static string GenerateTryFromValueImplementation(VoWorkItem item) @@ -260,7 +260,7 @@ string generateFromConstant() // if (!b.Success) throw new InvalidOperationException(b.ErrorMessage); - generate(each.ValueAsText, each.Name); + generate(each.ValueAsText, each.FieldName); } sb.AppendLine( @@ -299,7 +299,7 @@ public static string GenerateTryFromNameImplementation(VoWorkItem item) foreach (var eachInstance in item.InstanceProperties) { - generate(eachInstance.Name); + generate(eachInstance.FieldName, eachInstance.EnumEnumFriendlyName); } sb.AppendLine(""" @@ -311,12 +311,12 @@ public static string GenerateTryFromNameImplementation(VoWorkItem item) return sb.ToString(); - void generate(string name) + void generate(string fieldName, string name) { sb.AppendLine( $$""" - case nameof({{item.VoTypeName}}.{{name}}): - instance = {{item.VoTypeName}}.{{name}}; + case ("{{name}}"): + instance = {{item.VoTypeName}}.{{fieldName}}; return true; """); } @@ -335,27 +335,27 @@ public static string GenerateLazyLookupsIfNeeded(VoWorkItem item) private static readonly System.Lazy> _namesToValues = new( () => new() { - {{ GenerateLazyLookupEntries(item, prop => new($"\"{prop.Name}\"", $"{prop.Name}.Value")) }} + {{ GenerateLazyLookupEntries(item, prop => new($"\"{prop.FieldName}\"", $"{prop.FieldName}.Value")) }} }); private static readonly System.Lazy> _namesToEnums = new( () => new() { - {{ GenerateLazyLookupEntries(item, prop => new($"\"{prop.Name}\"", $"{prop.Name}")) }} + {{ GenerateLazyLookupEntries(item, prop => new($"\"{prop.FieldName}\"", $"{prop.FieldName}")) }} }); private static readonly System.Lazy> _enumsToValues = new( () => new() { - {{ GenerateLazyLookupEntries(item, prop => new($"{prop.Name}", $"{prop.Name}.Value")) }} + {{ GenerateLazyLookupEntries(item, prop => new($"{prop.FieldName}", $"{prop.FieldName}.Value")) }} }); private static readonly System.Lazy> _valuesToNames = new( () => new() { - {{ GenerateLazyLookupEntries(item, prop => new($"{prop.Name}.Value", $"\"{prop.Name}\"")) }} + {{ GenerateLazyLookupEntries(item, prop => new($"{prop.FieldName}.Value", $"\"{prop.FieldName}\"")) }} }); private static readonly System.Lazy> _valuesToEnums = new( () => new() { - {{ GenerateLazyLookupEntries(item, prop => new($"{prop.Name}.Value", $"{prop.Name}")) }} + {{ GenerateLazyLookupEntries(item, prop => new($"{prop.FieldName}.Value", $"{prop.FieldName}")) }} }); """; } diff --git a/tests/ConsumerTests/BugFixTests.cs b/tests/ConsumerTests/BugFixTests.cs index d7d905ee..42eaef3f 100644 --- a/tests/ConsumerTests/BugFixTests.cs +++ b/tests/ConsumerTests/BugFixTests.cs @@ -1,32 +1,27 @@ -using System; -using @double; -using @bool.@byte.@short.@float.@object; using FluentAssertions; using Newtonsoft.Json; -using Vogen; -using Vogen.Tests.Types; -using Xunit; +using Intellenum; namespace ConsumerTests.BugFixTests { public class BugFixTests { /// - /// Fixes bug https://github.com/SteveDunn/Vogen/issues/344 where a field that is a ValueObject and is null when + /// Fixes bug https://github.com/SteveDunn/Vogen/issues/344 where a field that is a Intellenum and is null when /// deserialized by Newtonsoft.Json, throws an exception instead of returning null. /// [Fact] - public void Bug344_Can_deserialze_a_null_field() + public void Bug344_Can_deserialize_a_null_field() { var p = new Person { - Age = Age.From(42) + AgeRange = AgeRange.Senior }; var serialized = JsonConvert.SerializeObject(p); var deserialized = JsonConvert.DeserializeObject(serialized)!; - deserialized.Age.Should().Be(Age.From(42)); + deserialized.AgeRange.Should().Be(AgeRange.Senior); deserialized.Name.Should().BeNull(); deserialized.Address.Should().BeNull(); } @@ -34,16 +29,30 @@ public void Bug344_Can_deserialze_a_null_field() public class Person { - public Age Age { get; init; } + public AgeRange AgeRange { get; init; } = AgeRange.Senior; - public Name? Name { get; set; } + public NameType? Name { get; set; } public Address? Address { get; set; } } + + [Intellenum(conversions: Conversions.NewtonsoftJson)] + [Instance("Junior", 1)] + [Instance("Adult", 2)] + [Instance("Senior", 3)] + public partial class AgeRange + { + } + + [Intellenum(typeof(string), conversions: Conversions.NewtonsoftJson)] + [Instance("FirstAndLast", 1)] + [Instance("Nickname", 2)] + public partial class NameType + { + } - [ValueObject(conversions: Conversions.NewtonsoftJson)] public partial struct Age { } - - [ValueObject(typeof(string), conversions: Conversions.NewtonsoftJson)] public partial class Name { } - - [ValueObject(typeof(string), conversions: Conversions.NewtonsoftJson)] public partial struct Address { } + [Intellenum(typeof(string), conversions: Conversions.NewtonsoftJson)] + [Instance("Full", 1)] + [Instance("JustThePostcode", 2)] + public partial class Address { } } \ No newline at end of file diff --git a/tests/ConsumerTests/ConsumerTests.csproj b/tests/ConsumerTests/ConsumerTests.csproj index 4a5711f6..0312c1b3 100644 --- a/tests/ConsumerTests/ConsumerTests.csproj +++ b/tests/ConsumerTests/ConsumerTests.csproj @@ -106,6 +106,11 @@ true Generated + ;CS1701;CS1702;CS1591;CA1014;CS9057;CA2255;CS1718 + + + + ;CS1701;CS1702;CS1591;CA1014;CS9057;CA2255;CS1718 diff --git a/tests/ConsumerTests/CreationTests.cs b/tests/ConsumerTests/CreationTests.cs index 9d8cecf6..a8386ac2 100644 --- a/tests/ConsumerTests/CreationTests.cs +++ b/tests/ConsumerTests/CreationTests.cs @@ -2,9 +2,8 @@ using @double; using @bool.@byte.@short.@float.@object; using FluentAssertions; -using Vogen; -using Vogen.Tests.Types; -using Xunit; +using Intellenum; +using Intellenum.Tests.Types; namespace NotSystem { @@ -22,8 +21,8 @@ public class CreationTests [Fact] public void Creation_Happy_Path_MyIntGeneric() { - MyIntGeneric vo1 = MyIntGeneric.From(123); - MyIntGeneric vo2 = MyIntGeneric.From(123); + MyIntGeneric vo1 = MyIntGeneric.Item1; + MyIntGeneric vo2 = MyIntGeneric.Item1; vo1.Should().Be(vo2); (vo1 == vo2).Should().BeTrue(); @@ -36,14 +35,14 @@ public void Creation_Happy_Path_MyIntGeneric() [Fact] public void Allows_using_Activate_CreateInstance_from_another_namespace() { - var x = NotSystem.Activator.CreateInstance(); + _ = NotSystem.Activator.CreateInstance(); } [Fact] public void Creation_Happy_Path_MyInt() { - MyInt vo1 = MyInt.From(123); - MyInt vo2 = MyInt.From(123); + MyInt vo1 = MyInt.Item1; + MyInt vo2 = MyInt.Item1; vo1.Should().Be(vo2); (vo1 == vo2).Should().BeTrue(); @@ -52,59 +51,24 @@ public void Creation_Happy_Path_MyInt() [Fact] public void Creation_Happy_Path_MyString() { - MyString vo1 = MyString.From("123"); - MyString vo2 = MyString.From("123"); + MyString vo1 = MyString.Item1; + MyString vo2 = MyString.Item1; vo1.Should().Be(vo2); (vo1 == vo2).Should().BeTrue(); } - [Fact] - public void Creation_Unhappy_Path_MyString() - { - Action action = () => MyString.From(null!); - - action.Should().Throw().WithMessage("Cannot create a value object with null."); - } - - [Fact] - public void Creation_Unhappy_Path_MyInt() - { - Action action = () => MyInt.From(-1); - - action.Should().Throw().WithMessage("must be greater than zero"); - } - - [Fact] - public void Default_vo_throws_at_runtime() - { - CustomerId[] ints = new CustomerId[10]; - Func action = () => ints[0].Value; - - action.Should().Throw().WithMessage("Use of uninitialized Value Object*"); - } - [Fact] public void Creation_can_create_a_VO_with_a_verbatim_identifier() { - @class c1 = @class.From(123); - @class c2 = @class.From(123); + @class c1 = @class.Item1; + @class c2 = @class.Item1; c1.Should().Be(c2); (c1 == c2).Should().BeTrue(); - @event e1 = @event.From(123); - @event e2 = @event.From(123); - - e1.Should().Be(e2); - (e1 == e2).Should().BeTrue(); - } - - [Fact] - public void Creation_can_create_a_VO_with_a_verbatim_name_and_verbatim_underlying_from_a_verbatim_namespace() - { - @event2 e1 = @event2.From(new @record.@struct.@float.@decimal()); - @event2 e2 = @event2.From(new @record.@struct.@float.@decimal()); + @event e1 = @event.Item1; + @event e2 = @event.Item1; e1.Should().Be(e2); (e1 == e2).Should().BeTrue(); @@ -113,8 +77,8 @@ public void Creation_can_create_a_VO_with_a_verbatim_name_and_verbatim_underlyin [Fact] public void Creation_can_create_a_VO_with_from_a_namespace_with_an_escaped_keyword() { - @classFromEscapedNamespace c1 = @classFromEscapedNamespace.From(123); - @classFromEscapedNamespace c2 = @classFromEscapedNamespace.From(123); + @classFromEscapedNamespace c1 = @classFromEscapedNamespace.Item1; + @classFromEscapedNamespace c2 = @classFromEscapedNamespace.Item1; c1.Should().Be(c2); (c1 == c2).Should().BeTrue(); @@ -123,8 +87,8 @@ public void Creation_can_create_a_VO_with_from_a_namespace_with_an_escaped_keywo [Fact] public void Underlying_types_can_have_escaped_keywords() { - @classFromEscapedNamespace c1 = @classFromEscapedNamespace.From(123); - @classFromEscapedNamespace c2 = @classFromEscapedNamespace.From(123); + @classFromEscapedNamespace c1 = @classFromEscapedNamespace.Item1; + @classFromEscapedNamespace c2 = @classFromEscapedNamespace.Item1; c1.Should().Be(c2); (c1 == c2).Should().BeTrue(); diff --git a/tests/ConsumerTests/DeserializationValidationTests/DeserializationValidationTests.cs b/tests/ConsumerTests/DeserializationValidationTests/DeserializationValidationTests.cs index a1ffb6bd..a42656f4 100644 --- a/tests/ConsumerTests/DeserializationValidationTests/DeserializationValidationTests.cs +++ b/tests/ConsumerTests/DeserializationValidationTests/DeserializationValidationTests.cs @@ -1,32 +1,10 @@ -using System; -using System.ComponentModel; -using FluentAssertions; -using Vogen; -using Xunit; +using Intellenum; namespace ConsumerTests.DeserializationValidationTests; -[ValueObject(typeof(int), Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowAnything)] -public partial class Vo_AllowAnything -{ - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -[ValueObject(typeof(int), Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowValidAndKnownInstances)] -[Instance("Invalid", 0)] -[Instance("Unknown", -1)] +[Intellenum(typeof(int), Conversions.TypeConverter)] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class Vo_AllowValidAndKnownInstances { - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -[ValueObject(typeof(int), Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowValidAndKnownInstances)] -[Instance("Invalid", 0)] -public partial class Vo_AllowValidAndKnownInstances_WithNormalizeMethod -{ - // the '-1' value was removed from the known instances (above), - // so this type demonstrates how we can normalize the input to cater for it. - private static int NormalizeInput(int input) => input == -1 ? 0 : input; - - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); } diff --git a/tests/ConsumerTests/DeserializationValidationTests/IntDeserializationValidationTests.cs b/tests/ConsumerTests/DeserializationValidationTests/IntDeserializationValidationTests.cs index 5a21ed08..2c2f5d31 100644 --- a/tests/ConsumerTests/DeserializationValidationTests/IntDeserializationValidationTests.cs +++ b/tests/ConsumerTests/DeserializationValidationTests/IntDeserializationValidationTests.cs @@ -3,8 +3,7 @@ using System.Threading.Tasks; using FluentAssertions; using Microsoft.Data.Sqlite; -using Xunit; -using Vogen; +using Intellenum; using Dapper; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; @@ -27,14 +26,14 @@ public async void Deserialization_dapper_should_not_bypass_validation_pass() } [Fact] - public async void Deserialization_dapper_should_not_bypass_validation_fail() + public async void Deserialization_dapper_should_throw_on_no_match() { using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); Func> vo = async () => (await connection.QueryAsync("SELECT 0")).AsList()[0].Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); + await vo.Should().ThrowExactlyAsync().WithMessage("MyVoInt_should_not_bypass_validation has no matching instances with a value of '0'"); } [Fact] @@ -55,7 +54,7 @@ public async void Deserialization_efcore_should_not_bypass_validation_pass() } [Fact] - public async void Deserialization_efcore_should_not_bypass_validation_fail() + public async void Deserialization_efcore_should_throw_on_no_match() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); @@ -67,7 +66,7 @@ public async void Deserialization_efcore_should_not_bypass_validation_fail() using (var context = new DeserializationValidationDbContext(options)) { Func> vo = async () => (await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.IntEntities!.FromSqlRaw("SELECT 0 As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); + await vo.Should().ThrowExactlyAsync().WithMessage("MyVoInt_should_not_bypass_validation has no matching instances with a value of '0'"); } } [Fact] @@ -84,7 +83,7 @@ public async void Deserialization_linqtodb_should_not_bypass_validation_pass() } [Fact] - public async void Deserialization_linqtodb_should_not_bypass_validation_fail() + public async void Deserialization_linqtodb_should_throw_on_no_match() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); @@ -93,7 +92,7 @@ public async void Deserialization_linqtodb_should_not_bypass_validation_fail() using (var context = new DeserializationValidationDataConnection(connection)) { Func> vo = async () => (await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 0 As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); + await vo.Should().ThrowExactlyAsync().WithMessage("MyVoInt_should_not_bypass_validation has no matching instances with a value of '0'"); } } @@ -109,20 +108,20 @@ public void TypeConversion_should_not_bypass_validation_pass() } [Fact] - public void TypeConversion_should_not_bypass_validation_fail() + public void TypeConversion_should_throw_on_no_match() { var converter = TypeDescriptor.GetConverter(typeof(MyVoInt_should_not_bypass_validation)); var invalidValue = 0; Action vo = () => converter.ConvertFrom(invalidValue); - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); + vo.Should().ThrowExactly().WithMessage("MyVoInt_should_not_bypass_validation has no matching instances with a value of '0'"); } [Fact] public void Deserialization_systemtextjson_should_not_bypass_validation_pass() { - var validValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.From(1)); + var validValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.Item1); var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; @@ -130,19 +129,19 @@ public void Deserialization_systemtextjson_should_not_bypass_validation_pass() } [Fact] - public void Deserialization_systemtextjson_should_not_bypass_validation_fail() + public void Deserialization_systemtextjson_should_throw_on_no_match() { - var invalidValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.From(1)).Replace("1", "0"); + var invalidValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.Item1).Replace("1", "0"); Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); + vo.Should().ThrowExactly().WithMessage("MyVoInt_should_not_bypass_validation has no matching instances with a value of '0'"); } [Fact] public void Deserialization_newtonsoft_should_not_bypass_validation_pass() { - var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.From(1)); + var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.Item1); var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; @@ -150,13 +149,13 @@ public void Deserialization_newtonsoft_should_not_bypass_validation_pass() } [Fact] - public void Deserialization_newtonsoft_should_not_bypass_validation_fail() + public void Deserialization_newtonsoft_should_throw_on_no_match() { - var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.From(1)).Replace("1", "0"); + var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.Item1).Replace("1", "0"); Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); + vo.Should().ThrowExactly().WithMessage("MyVoInt_should_not_bypass_validation has no matching instances with a value of '0'"); } } \ No newline at end of file diff --git a/tests/ConsumerTests/DeserializationValidationTests/SharedTypes.cs b/tests/ConsumerTests/DeserializationValidationTests/SharedTypes.cs index 89f7bdd8..5ca88762 100644 --- a/tests/ConsumerTests/DeserializationValidationTests/SharedTypes.cs +++ b/tests/ConsumerTests/DeserializationValidationTests/SharedTypes.cs @@ -4,45 +4,23 @@ using LinqToDB.Mapping; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Vogen; +using Intellenum; namespace ConsumerTests.DeserializationValidationTests; #region Value Objects -[ValueObject(typeof(int), Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Intellenum(typeof(int), Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class MyVoInt_should_not_bypass_validation { - private static Validation validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} - -[ValueObject(typeof(string), Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] -public partial class MyVoString_should_not_bypass_validation -{ - private static Validation validate(string value) - { - if (value.Length > 10) - return Validation.Ok; - - return Validation.Invalid("length must be greater than ten characters"); - } } -[ValueObject(typeof(string), Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowAnything)] -public partial class MyVoString_should_bypass_validation +[Intellenum(typeof(string), Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Instance("Item1", "Item1!")] +[Instance("Item2", "Item2!")] +public partial class MyVoString { - private static Validation validate(string value) - { - if (value.Length > 10) - return Validation.Ok; - - return Validation.Invalid("length must be greater than ten characters"); - } } #endregion @@ -72,7 +50,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { builder .Property(x => x.Id) - .HasConversion(new MyVoString_should_not_bypass_validation.EfCoreValueConverter()) + .HasConversion(new MyVoString.EfCoreValueConverter()) .ValueGeneratedNever(); }); } @@ -101,7 +79,7 @@ public class DeserializationValidationTestIntEntity public class DeserializationValidationTestStringEntity { - public MyVoString_should_not_bypass_validation? Id { get; set; } + public MyVoString? Id { get; set; } } #endregion @@ -116,8 +94,8 @@ public class DeserializationValidationTestLinqToDbTestIntEntity public class DeserializationValidationTestLinqToDbTestStringEntity { [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(MyVoString_should_not_bypass_validation.LinqToDbValueConverter))] - public MyVoString_should_not_bypass_validation? Id { get; set; } + [ValueConverter(ConverterType = typeof(MyVoString.LinqToDbValueConverter))] + public MyVoString? Id { get; set; } } #endregion #endregion \ No newline at end of file diff --git a/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs b/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs index d05e6ece..fe395b62 100644 --- a/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs +++ b/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs @@ -3,8 +3,7 @@ using System.Threading.Tasks; using FluentAssertions; using Microsoft.Data.Sqlite; -using Xunit; -using Vogen; +using Intellenum; using Dapper; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; @@ -16,36 +15,14 @@ namespace ConsumerTests.DeserializationValidationTests; public class StringDeserializationValidationTests { [Fact] - public async void Deserialization_dapper_should_not_bypass_validation_pass() + public async void Deserialization_dapper() { using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - var actual = (await connection.QueryAsync("SELECT 'abcdefghijk'")).AsList()[0].Value; + var actual = (await connection.QueryAsync("SELECT 'Item1!'")).AsList()[0].Value; - actual.Should().Be("abcdefghijk"); - } - - [Fact] - public async Task Deserialization_dapper_should_not_bypass_validation_fail() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - Func> vo = async () => (await connection.QueryAsync("SELECT 'abc'")).AsList()[0].Value; - - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); - } - - [Fact] - public async Task Deserialization_dapper_should_bypass_validation_fail() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - MyVoString_should_bypass_validation? vo = (await connection.QueryAsync("SELECT 'abc'")).AsList()[0]; - - vo.Value.Should().Be("abc"); + actual.Should().Be("Item11"); } [Fact] @@ -61,7 +38,7 @@ public async void Deserialization_efcore_should_not_bypass_validation_pass() using (var context = new DeserializationValidationDbContext(options)) { var actual = await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'abcdefghijk' As Id")); - actual.Id!.Value.Should().Be("abcdefghijk"); + actual.Id!.Value.Should().Be("Item1"); } } @@ -78,7 +55,7 @@ public async void Deserialization_efcore_should_not_bypass_validation_fail() using (var context = new DeserializationValidationDbContext(options)) { Func> vo = async () => (await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'abc' As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); + await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); } } [Fact] @@ -90,84 +67,59 @@ public async void Deserialization_linqtodb_should_not_bypass_validation_pass() using (var context = new DeserializationValidationDataConnection(connection)) { var actual = await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'abcdefghijk' As Id")); - actual.Id!.Value.Should().Be("abcdefghijk"); - } - } - - [Fact] - public async void Deserialization_linqtodb_should_not_bypass_validation_fail() - { - var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - //var original = new TestEntity { Id = LinqToDbStringVo.From("foo!") }; - using (var context = new DeserializationValidationDataConnection(connection)) - { - Func> vo = async () => (await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'abc' As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); + actual.Id!.Value.Should().Be("Item1"); } } [Fact] public void TypeConversion_should_not_bypass_validation_pass() { - var converter = TypeDescriptor.GetConverter(typeof(MyVoString_should_not_bypass_validation)); - var validValue = "abcdefghijk"; - - var actual = ((MyVoString_should_not_bypass_validation?) converter.ConvertFrom(validValue))!.Value; - - actual.Should().Be("abcdefghijk"); - } - - [Fact] - public void TypeConversion_should_not_bypass_validation_fail() - { - var converter = TypeDescriptor.GetConverter(typeof(MyVoString_should_not_bypass_validation)); - var invalidValue = "abc"; + var converter = TypeDescriptor.GetConverter(typeof(MyVoString)); + var validValue = "Item1"; - Action vo = () => converter.ConvertFrom(invalidValue); + var actual = ((MyVoString?) converter.ConvertFrom(validValue))!.Value; - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + actual.Should().Be("Item1"); } [Fact] public void Deserialization_systemtextjson_should_not_bypass_validation_pass() { - var validValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.From("abcdefghijk")); + var validValue = SystemTextJsonSerializer.Serialize(MyVoString.Item1); - var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; + var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; - actual.Should().Be("abcdefghijk"); + actual.Should().Be("Item1"); } [Fact] public void Deserialization_systemtextjson_should_not_bypass_validation_fail() { - var invalidValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.From("abcdefghijk")).Replace("abcdefghijk", "abc"); + var invalidValue = SystemTextJsonSerializer.Serialize(MyVoString.Item1).Replace("Item1", "abc"); - Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); + Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); } [Fact] public void Deserialization_newtonsoft_should_not_bypass_validation_pass() { - var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.From("abcdefghijk")); + var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString.Item1); - var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; + var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; - actual.Should().Be("abcdefghijk"); + actual.Should().Be("Item1"); } [Fact] public void Deserialization_newtonsoft_should_not_bypass_validation_fail() { - var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.From("abcdefghijk")).Replace("abcdefghijk", "abc"); + var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString.Item1).Replace("Item1", "abc"); - Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; + Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); } } \ No newline at end of file diff --git a/tests/ConsumerTests/EqualityTests.cs b/tests/ConsumerTests/EqualityTests.cs index 8ef3ffbf..e93da09c 100644 --- a/tests/ConsumerTests/EqualityTests.cs +++ b/tests/ConsumerTests/EqualityTests.cs @@ -7,10 +7,12 @@ // ReSharper disable PossibleNullReferenceException #pragma warning disable 252,253 +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + using System.Diagnostics; using FluentAssertions; -using Vogen.Tests.Types; -using Xunit; +using Intellenum.Tests.Types; namespace ConsumerTests { @@ -19,60 +21,60 @@ public class EqualityTests [Fact] public void equality_between_same_value_objects() { - Age.From(18).Equals(Age.From(18)).Should().BeTrue(); - (Age.From(18) == Age.From(18)).Should().BeTrue(); + Age.LegalVotingAge.Equals(Age.LegalVotingAge).Should().BeTrue(); + (Age.LegalVotingAge == Age.LegalVotingAge).Should().BeTrue(); // uses the generated IEquatable<> - (Age.From(18) == 18).Should().BeTrue(); - (18 == Age.From(18)).Should().BeTrue(); + (Age.LegalVotingAge == 18).Should().BeTrue(); + (18 == Age.LegalVotingAge).Should().BeTrue(); - (Age.From(18) != Age.From(19)).Should().BeTrue(); - (Age.From(18) == Age.From(19)).Should().BeFalse(); + (Age.LegalVotingAge != Age.LegalDrivingAge).Should().BeTrue(); + (Age.LegalVotingAge == Age.LegalDrivingAge).Should().BeFalse(); - Age.From(18).Equals(Age.From(18)).Should().BeTrue(); - (Age.From(18) == Age.From(18)).Should().BeTrue(); + Age.LegalVotingAge.Equals(Age.LegalVotingAge).Should().BeTrue(); + (Age.LegalVotingAge == Age.LegalVotingAge).Should().BeTrue(); } [Fact] public void equality_between_different_value_objects() { - Age.From(18).Equals(Score.From(18)).Should().BeFalse(); - (Age.From(18) == (object)Score.From(18)).Should().BeFalse(); + Age.LegalVotingAge.Equals(ScoreType.Points).Should().BeFalse(); + (Age.LegalVotingAge == (object)ScoreType.Points).Should().BeFalse(); } [Fact] public void equality_with_primitives() { - Age.From(18).Equals(-1).Should().BeFalse(); - (Age.From(18) == 18).Should().BeTrue(); - (18 == Age.From(18)).Should().BeTrue(); - Age.From(18).Equals(18).Should().BeTrue(); + Age.LegalVotingAge.Equals(-1).Should().BeFalse(); + (Age.LegalVotingAge == 18).Should().BeTrue(); + (18 == Age.LegalVotingAge).Should().BeTrue(); + Age.LegalVotingAge.Equals(18).Should().BeTrue(); - (Age.From(18) != Age.From(19)).Should().BeTrue(); - (Age.From(18) != 2).Should().BeTrue(); - (Age.From(18) == 2).Should().BeFalse(); - Age.From(18).Equals(Age.From(19)).Should().BeFalse(); + (Age.LegalVotingAge != Age.LegalDrivingAge).Should().BeTrue(); + (Age.LegalVotingAge != 2).Should().BeTrue(); + (Age.LegalVotingAge == 2).Should().BeFalse(); + Age.LegalVotingAge.Equals(Age.LegalDrivingAge).Should().BeFalse(); - Age.From(18).Equals(new StackFrame()).Should().BeFalse(); + Age.LegalVotingAge.Equals(new StackFrame()).Should().BeFalse(); - Age.From(18).Equals(Score.From(1)).Should().BeFalse(); + Age.LegalVotingAge.Equals(ScoreType.Points).Should().BeFalse(); } [Fact] public void reference_equality() { - var age1 = Age.From(18); - var age2 = Age.From(18); + var age1 = Age.LegalVotingAge; + var age2 = Age.LegalVotingAge; age1.Equals(age1).Should().BeTrue(); - object.ReferenceEquals(age1, age2).Should().BeFalse(); + object.ReferenceEquals(age1, age2).Should().BeTrue(); } [Fact] public void equality_with_object() { - var age = Age.From(18); + var age = Age.LegalVotingAge; age.Equals((object)age).Should().BeTrue(); age.Equals((object)"???").Should().BeFalse(); @@ -84,10 +86,10 @@ public void equality_with_object() [Fact] public void Nullness() { - (Age.From(50) == null!).Should().BeFalse(); - Age.From(50).Equals(null!).Should().BeFalse(); + (Age.LegalVotingAge == null!).Should().BeFalse(); + Age.LegalVotingAge!.Equals(null!).Should().BeFalse(); - (Age.From(50) != null!).Should().BeTrue(); + (Age.LegalDrivingAge != null!).Should().BeTrue(); } } } \ No newline at end of file diff --git a/tests/ConsumerTests/GenericDeserializationValidationTests/DeserializationValidationTests.cs b/tests/ConsumerTests/GenericDeserializationValidationTests/DeserializationValidationTests.cs deleted file mode 100644 index 2827c5cd..00000000 --- a/tests/ConsumerTests/GenericDeserializationValidationTests/DeserializationValidationTests.cs +++ /dev/null @@ -1,107 +0,0 @@ -using System; -using System.ComponentModel; -using FluentAssertions; -using Vogen; -using Xunit; - -namespace ConsumerTests.GenericDeserializationValidationTests; - -#if NET7_0_OR_GREATER -[ValueObject(Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowAnything)] -public partial class Vo_AllowAnything -{ - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -[ValueObject(Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowValidAndKnownInstances)] -[Instance("Invalid", 0)] -[Instance("Unknown", -1)] -public partial class Vo_AllowValidAndKnownInstances -{ - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -[ValueObject(Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowValidAndKnownInstances)] -[Instance("Invalid", 0)] -public partial class Vo_AllowValidAndKnownInstances_WithNormalizeMethod -{ - // the '-1' value was removed from the known instances (above), - // so this type demonstrates how we can normalize the input to cater for it. - private static int NormalizeInput(int input) => input == -1 ? 0 : input; - - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -[ValueObject(Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowAnything)] -public partial class VoGeneric_AllowAnything -{ - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -[ValueObject(Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowValidAndKnownInstances)] -[Instance("Invalid", 0)] -[Instance("Unknown", -1)] -public partial class VoGeneric_AllowValidAndKnownInstances -{ - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -[ValueObject(Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowValidAndKnownInstances)] -[Instance("Invalid", 0)] -public partial class VoGeneric_AllowValidAndKnownInstances_WithNormalizeMethod -{ - // the '-1' value was removed from the known instances (above), - // so this type demonstrates how we can normalize the input to cater for it. - private static int NormalizeInput(int input) => input == -1 ? 0 : input; - - private static Validation Validate(int value) => value > 0 ? Validation.Ok : Validation.Invalid("must be greater than zero"); -} - -public class Generic_DeserializationValidationTests -{ - [Theory] - [InlineData("-1")] - [InlineData("0")] - [InlineData("-100")] - public void AnythingIsAllowed(string input) - { - var vo = CreateWith(input); - vo.Value.Should().Be(Convert.ToInt32(input)); - } - - [Theory] - [InlineData("-1")] - [InlineData("0")] - public void AllowAnything(string input) - { - Action a = () => CreateWith(input); - a.Should().NotThrow(); - } - - [Theory] - [InlineData("-2")] - [InlineData("-100")] - public void AllowValidAndKnownInstances(string input) - { - Action a = () => CreateWith(input); - a.Should().Throw(); - } - - [Theory] - [InlineData("-1")] - public void AllowValidAndKnownInstances_and_run_normalize_method(string input) - { - var x = CreateWith(input); - x.Value.Should().Be(0); - } - - private static T CreateWith(string input) - { - TypeConverter c = TypeDescriptor.GetConverter(typeof(T)); - object? converted = c.ConvertFrom(input); - T t = (T)converted!; - - return t; - } -} -#endif \ No newline at end of file diff --git a/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs b/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs index 6fcb9f09..81694490 100644 --- a/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs +++ b/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs @@ -1,10 +1,11 @@ -using System; +#if NET7_0_OR_GREATER + +using System; using System.ComponentModel; using System.Threading.Tasks; using FluentAssertions; using Microsoft.Data.Sqlite; -using Xunit; -using Vogen; +using Intellenum; using Dapper; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; @@ -13,7 +14,6 @@ namespace ConsumerTests.GenericDeserializationValidationTests; -#if NET7_0_OR_GREATER public class IntDeserializationValidationTests { [Fact] @@ -35,7 +35,7 @@ public async void Deserialization_dapper_should_not_bypass_validation_fail() Func> vo = async () => (await connection.QueryAsync("SELECT 0")).AsList()[0].Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); + await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); } [Fact] @@ -68,7 +68,7 @@ public async void Deserialization_efcore_should_not_bypass_validation_fail() using (var context = new DeserializationValidationDbContext(options)) { Func> vo = async () => (await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.IntEntities!.FromSqlRaw("SELECT 0 As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); + await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); } } [Fact] @@ -94,7 +94,7 @@ public async void Deserialization_linqtodb_should_not_bypass_validation_fail() using (var context = new DeserializationValidationDataConnection(connection)) { Func> vo = async () => (await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 0 As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); + await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); } } @@ -117,13 +117,13 @@ public void TypeConversion_should_not_bypass_validation_fail() Action vo = () => converter.ConvertFrom(invalidValue); - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); + vo.Should().ThrowExactly().WithMessage("must be greater than zero"); } [Fact] public void Deserialization_systemtextjson_should_not_bypass_validation_pass() { - var validValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.From(1)); + var validValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.Item1); var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; @@ -133,17 +133,17 @@ public void Deserialization_systemtextjson_should_not_bypass_validation_pass() [Fact] public void Deserialization_systemtextjson_should_not_bypass_validation_fail() { - var invalidValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.From(1)).Replace("1", "0"); + var invalidValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.Item1).Replace("1", "0"); Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); + vo.Should().ThrowExactly().WithMessage("must be greater than zero"); } [Fact] public void Deserialization_newtonsoft_should_not_bypass_validation_pass() { - var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.From(1)); + var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.Item1); var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; @@ -153,11 +153,11 @@ public void Deserialization_newtonsoft_should_not_bypass_validation_pass() [Fact] public void Deserialization_newtonsoft_should_not_bypass_validation_fail() { - var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.From(1)).Replace("1", "0"); + var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.Item1).Replace("1", "0"); Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); + vo.Should().ThrowExactly().WithMessage("must be greater than zero"); } } #endif \ No newline at end of file diff --git a/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs b/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs index 22629d3a..c426834b 100644 --- a/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs +++ b/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs @@ -1,50 +1,35 @@ -using LinqToDB; +#if NET7_0_OR_GREATER + +using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; using LinqToDB.Mapping; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Vogen; +using Intellenum; namespace ConsumerTests.GenericDeserializationValidationTests; -#if NET7_0_OR_GREATER - #region Value Objects -[ValueObject(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Intellenum(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class MyVoInt_should_not_bypass_validation { - private static Validation validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } } -[ValueObject(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Intellenum(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Instance("Item1", "Item1")] +[Instance("Item2", "Item2")] public partial class MyVoString_should_not_bypass_validation { - private static Validation validate(string value) - { - if (value.Length > 10) - return Validation.Ok; - - return Validation.Invalid("length must be greater than ten characters"); - } } -[ValueObject(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter, deserializationStrictness: DeserializationStrictness.AllowAnything)] +[Intellenum(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] +[Instance("Item1", "Item1")] +[Instance("Item2", "Item2")] public partial class MyVoString_should_bypass_validation { - private static Validation validate(string value) - { - if (value.Length > 10) - return Validation.Ok; - - return Validation.Invalid("length must be greater than ten characters"); - } } #endregion diff --git a/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs b/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs index 9ff43d21..9e0433cb 100644 --- a/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs +++ b/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs @@ -1,10 +1,11 @@ -using System; +#if NET7_0_OR_GREATER + +using System; using System.ComponentModel; using System.Threading.Tasks; using FluentAssertions; using Microsoft.Data.Sqlite; -using Xunit; -using Vogen; +using Intellenum; using Dapper; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; @@ -13,7 +14,6 @@ namespace ConsumerTests.GenericDeserializationValidationTests; -#if NET7_0_OR_GREATER public class StringDeserializationValidationTests { [Fact] @@ -35,7 +35,7 @@ public async Task Deserialization_dapper_should_not_bypass_validation_fail() Func> vo = async () => (await connection.QueryAsync("SELECT 'abc'")).AsList()[0].Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); + await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); } [Fact] @@ -79,7 +79,7 @@ public async void Deserialization_efcore_should_not_bypass_validation_fail() using (var context = new DeserializationValidationDbContext(options)) { Func> vo = async () => (await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'abc' As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); + await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); } } [Fact] @@ -105,7 +105,7 @@ public async void Deserialization_linqtodb_should_not_bypass_validation_fail() using (var context = new DeserializationValidationDataConnection(connection)) { Func> vo = async () => (await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'abc' As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); + await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); } } @@ -128,13 +128,13 @@ public void TypeConversion_should_not_bypass_validation_fail() Action vo = () => converter.ConvertFrom(invalidValue); - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); } [Fact] public void Deserialization_systemtextjson_should_not_bypass_validation_pass() { - var validValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.From("abcdefghijk")); + var validValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.Item1); var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; @@ -144,17 +144,17 @@ public void Deserialization_systemtextjson_should_not_bypass_validation_pass() [Fact] public void Deserialization_systemtextjson_should_not_bypass_validation_fail() { - var invalidValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.From("abcdefghijk")).Replace("abcdefghijk", "abc"); + var invalidValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.Item1).Replace("abcdefghijk", "abc"); Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); } [Fact] public void Deserialization_newtonsoft_should_not_bypass_validation_pass() { - var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.From("abcdefghijk")); + var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.Item1); var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; @@ -164,11 +164,11 @@ public void Deserialization_newtonsoft_should_not_bypass_validation_pass() [Fact] public void Deserialization_newtonsoft_should_not_bypass_validation_fail() { - var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.From("abcdefghijk")).Replace("abcdefghijk", "abc"); + var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.Item1).Replace("abcdefghijk", "abc"); Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); } } #endif \ No newline at end of file diff --git a/tests/ConsumerTests/HashCodeTests.cs b/tests/ConsumerTests/HashCodeTests.cs index b4ba115b..22608bc0 100644 --- a/tests/ConsumerTests/HashCodeTests.cs +++ b/tests/ConsumerTests/HashCodeTests.cs @@ -2,174 +2,77 @@ using @double; using @bool.@byte.@short.@float.@object; using FluentAssertions; -using Vogen; -using Vogen.Tests.Types; -using Xunit; +using Intellenum; +using Intellenum.Tests.Types; namespace ConsumerTests.HashCodes { - [ValueObject(typeof(int))] - public partial struct MyStructInt { } - -#if NET7_0_OR_GREATER - [ValueObject] - public partial struct MyGenericStructInt { } -#endif - - [ValueObject(typeof(int))] - public partial struct MyStructInt2 { } - - [ValueObject(typeof(int))] - public partial class MyClassInt { } - - [ValueObject(typeof(int))] - public partial record class MyRecordClassInt { } + [Intellenum(typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + public partial class MyClassInt + { + } - [ValueObject(typeof(int))] - public partial record class MyRecordClassInt2 { } + [Intellenum(typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + public partial class MyClassInt2 + { + } - [ValueObject(typeof(int))] - public partial class MyClassInt2 { } - public class HashCodeTests { - public class WithStructs + [Fact] + public void SameClassesHaveSameHashCode() { - [Fact] - public void Hashing() - { - (Age.From(18).GetHashCode() == Age.From(18).GetHashCode()).Should().BeTrue(); - (Age.From(18).GetHashCode() == Age.From(19).GetHashCode()).Should().BeFalse(); - (Age.From(18).GetHashCode() == Score.From(1).GetHashCode()).Should().BeFalse(); - (Age.From(18).GetHashCode() == Score.From(18).GetHashCode()).Should().BeFalse(); - (@classFromEscapedNamespace.From(123).GetHashCode() == @classFromEscapedNamespace.From(123).GetHashCode()).Should().BeTrue(); - (@classFromEscapedNamespace.From(123).GetHashCode() == @classFromEscapedNamespace.From(666).GetHashCode()).Should().BeFalse(); - (@classFromEscapedNamespace.From(123).GetHashCode() == @event2.From(new @record.@struct.@float.@decimal()).GetHashCode()).Should().BeFalse(); - (@event2.From(new @record.@struct.@float.@decimal()).GetHashCode() == @classFromEscapedNamespace.From(123).GetHashCode()).Should().BeFalse(); - (@event2.From(new @record.@struct.@float.@decimal()).GetHashCode() == @event2.From(new @record.@struct.@float.@decimal()).GetHashCode()).Should().BeTrue(); - } - - [Fact] - public void SameStructsHaveSameHashCode() - { - MyStructInt.From(0).GetHashCode().Should().Be(MyStructInt.From(0).GetHashCode()); - - MyStructInt.From(-1).GetHashCode().Should().Be(MyStructInt.From(-1).GetHashCode()); - } - - /// - /// The same as record structs, GetHashCode only considers the underlying type and not the type itself. - /// This is because it's unlikely you'd want to compare two structs of two different types (unless they're boxed). - /// - [Fact] - public void DifferentStructsWithSameUnderlyingTypeAndValueHaveSameHashCode() - { - MyStructInt.From(0).GetHashCode().Should().Be(MyStructInt2.From(0).GetHashCode()); - - MyStructInt.From(-1).GetHashCode().Should().Be(MyStructInt2.From(-1).GetHashCode()); + MyClassInt.Item1.GetHashCode().Should().Be(MyClassInt.Item1.GetHashCode()); -#if NET7_0_OR_GREATER - MyGenericStructInt.From(0).GetHashCode().Should().Be(MyStructInt2.From(0).GetHashCode()); - - MyGenericStructInt.From(-1).GetHashCode().Should().Be(MyStructInt2.From(-1).GetHashCode()); -#endif - } + MyClassInt.Item2.GetHashCode().Should().Be(MyClassInt.Item2.GetHashCode()); } - public class WithClasses + /// + /// The same as record structs, GetHashCode only considers the underlying type and not the type itself. + /// + [Fact] + public void DifferentClassesWithSameUnderlyingTypeAndValueHaveDifferentHashCode() { - [Fact] - public void SameClassesHaveSameHashCode() - { - MyClassInt.From(0).GetHashCode().Should().Be(MyClassInt.From(0).GetHashCode()); - - MyClassInt.From(-1).GetHashCode().Should().Be(MyClassInt.From(-1).GetHashCode()); - - MyRecordClassInt.From(0).GetHashCode().Should().Be(MyRecordClassInt.From(0).GetHashCode()); - - MyRecordClassInt.From(-1).GetHashCode().Should().Be(MyRecordClassInt.From(-1).GetHashCode()); - } + MyClassInt.Item1.GetHashCode().Should().NotBe(MyClassInt2.Item1.GetHashCode()); - /// - /// The same as record structs, GetHashCode only considers the underlying type and not the type itself. - /// - [Fact] - public void DifferentClassesWithSameUnderlyingTypeAndValueHaveDifferentHashCode() - { - MyClassInt.From(0).GetHashCode().Should().NotBe(MyClassInt2.From(0).GetHashCode()); - - MyClassInt.From(-1).GetHashCode().Should().NotBe(MyClassInt2.From(-1).GetHashCode()); - - MyRecordClassInt.From(0).GetHashCode().Should().NotBe(MyRecordClassInt2.From(0).GetHashCode()); - - MyRecordClassInt.From(-1).GetHashCode().Should().NotBe(MyRecordClassInt2.From(-1).GetHashCode()); - } - - [Fact] - public void Storing_1() - { - var a1 = Age.From(18); - var a2 = Age.From(50); - - var d = new Dictionary - { - { a1, "hello1" }, - { a2, "hello2" } - }; - - d.Count.Should().Be(2); - - d[a1].Should().Be("hello1"); - d[a2].Should().Be("hello2"); - } - - [Fact] - public void Storing_2() - { - var a1 = Age.From(18); - var a2 = Age.From(18); - - var d = new Dictionary { { a1, "hello1" } }; - - d[a2] = "hello2"; - - d.Count.Should().Be(1); + MyClassInt.Item2.GetHashCode().Should().NotBe(MyClassInt2.Item2.GetHashCode()); + } - d[a1].Should().Be("hello2"); - } + [Fact] + public void Storing_1() + { + var a1 = Age.LegalVotingAge; + var a2 = Age.LegalDrivingAge; - [Fact] - public void Storing_3() + var d = new Dictionary { - var a1 = MyRecordClassInt.From(18); - var a2 = MyRecordClassInt.From(50); - - var d = new Dictionary - { - { a1, "hello1" }, - { a2, "hello2" } - }; + { a1, "hello1" }, + { a2, "hello2" } + }; - d.Count.Should().Be(2); + d.Count.Should().Be(2); - d[a1].Should().Be("hello1"); - d[a2].Should().Be("hello2"); - } + d[a1].Should().Be("hello1"); + d[a2].Should().Be("hello2"); + } - [Fact] - public void Storing_4() - { - var a1 = MyRecordClassInt.From(18); - var a2 = MyRecordClassInt.From(18); + [Fact] + public void Storing_2() + { + var a1 = Age.LegalVotingAge; + var a2 = Age.LegalVotingAge; - var d = new Dictionary { { a1, "hello1" } }; + var d = new Dictionary { { a1, "hello1" } }; - d[a2] = "hello2"; + d[a2] = "hello2"; - d.Count.Should().Be(1); + d.Count.Should().Be(1); - d[a1].Should().Be("hello2"); - } + d[a1].Should().Be("hello2"); } } } diff --git a/tests/ConsumerTests/IComparableTests.cs b/tests/ConsumerTests/IComparableTests.cs index f63a3a8f..660a9d18 100644 --- a/tests/ConsumerTests/IComparableTests.cs +++ b/tests/ConsumerTests/IComparableTests.cs @@ -1,244 +1,132 @@ using System; using System.Collections.Generic; using FluentAssertions; -using Vogen; -using Xunit; +using Intellenum; namespace ConsumerTests.IComparableTests { #if NET6_0_OR_GREATER - [ValueObject(typeof(DateOnly))] - public partial struct DOS1 { } -#endif - - [ValueObject(typeof(int))] - public partial struct S1 { } - - [ValueObject] - public partial struct S2 { } - - [ValueObject(typeof(int))] - public partial record struct RS1 { } - - [ValueObject] - public partial record struct RS2 { } - - [ValueObject(typeof(int))] - public partial record struct RC1 { } - - [ValueObject] - public partial record struct RC2 { } - - [ValueObject(typeof(int))] - public partial class C1 { } - - [ValueObject] - public partial class C2 { } - - public class IComparableTests + [Intellenum(typeof(DateOnly))] + public partial class DOS1 { - public class StructTests + static DOS1() { -#if NET6_0_OR_GREATER - [Fact] - public void Underlying_type_of_DateOnly_means_the_vo_is_IComparable() - { - var first = new DateOnly(2020, 1, 1); - var second = new DateOnly(2020, 1, 2); - var third = new DateOnly(2020, 1, 3); - - var l = new List(new[] { DOS1.From(first), DOS1.From(third), DOS1.From(second) }); - l[0].Value.Should().Be(first); - l[1].Value.Should().Be(third); - l[2].Value.Should().Be(second); - - l.Sort(); - - l[0].Value.Should().Be(first); - l[1].Value.Should().Be(second); - l[2].Value.Should().Be(third); - } + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + Instance("JanThird", new DateOnly(2021, 1, 3)); + } + } #endif - [Fact] - public void Underlying_type_of_int_means_the_vo_is_IComparable() - { - List l = new List(new[] {1, 3, 2}); - l.Sort(); - - l[0].Should().Be(1); - l[1].Should().Be(2); - l[2].Should().Be(3); - - var l2 = new List(new[] {S1.From(1), S1.From(3), S1.From(2)}); - l2[0].Value.Should().Be(1); - l2[1].Value.Should().Be(3); - l2[2].Value.Should().Be(2); - - l2.Sort(); - - l2[0].Value.Should().Be(1); - l2[1].Value.Should().Be(2); - l2[2].Value.Should().Be(3); - } + [Intellenum(typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + [Instance("Item3", 3)] + public partial class S1 + { + } - [Fact] - public void Underlying_type_of_default_int_means_the_vo_is_IComparable() - { - var l2 = new List(new[] { S2.From(1), S2.From(3), S2.From(2)}); - l2[0].Value.Should().Be(1); - l2[1].Value.Should().Be(3); - l2[2].Value.Should().Be(2); + [Intellenum] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + [Instance("Item3", 3)] + public partial class S2 + { + } - l2.Sort(); + [Intellenum(typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + [Instance("Item3", 3)] + public partial class C1 + { + } - l2[0].Value.Should().Be(1); - l2[1].Value.Should().Be(2); - l2[2].Value.Should().Be(3); - } - } + [Intellenum] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + [Instance("Item3", 3)] + public partial class C2 + { + } - public class RecordStructTests + public class IComparableTests + { + [Fact] + public void Underlying_type_of_int_means_the_vo_is_IComparable() { - [Fact] - public void Underlying_type_of_int_means_the_vo_is_IComparable() - { - var l = new List(new[] {RS1.From(1), RS1.From(3), RS1.From(2)}); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(3); - l[2].Value.Should().Be(2); - - l.Sort(); + var l = new List(new[] { C1.Item1, C1.Item3, C1.Item2 }); + l[0].Value.Should().Be(1); + l[1].Value.Should().Be(3); + l[2].Value.Should().Be(2); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(2); - l[2].Value.Should().Be(3); - } - - [Fact] - public void Underlying_type_of_default_int_means_the_vo_is_IComparable() - { - var l = new List(new[] { RS2.From(1), RS2.From(3), RS2.From(2)}); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(3); - l[2].Value.Should().Be(2); + l.Sort(); - l.Sort(); - - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(2); - l[2].Value.Should().Be(3); - } + l[0].Value.Should().Be(1); + l[1].Value.Should().Be(2); + l[2].Value.Should().Be(3); } - public class RecordClassTests + [Fact] + public void Underlying_type_of_default_int_means_the_vo_is_IComparable() { - [Fact] - public void Underlying_type_of_int_means_the_vo_is_IComparable() - { - var l = new List(new[] {RC1.From(1), RC1.From(3), RC1.From(2)}); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(3); - l[2].Value.Should().Be(2); + var l = new List(new[] { C2.Item1, C2.Item3, C2.Item2 }); + l[0].Value.Should().Be(1); + l[1].Value.Should().Be(3); + l[2].Value.Should().Be(2); - l.Sort(); + l.Sort(); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(2); - l[2].Value.Should().Be(3); - } - - [Fact] - public void Underlying_type_of_default_int_means_the_vo_is_IComparable() - { - var l = new List(new[] { RC2.From(1), RC2.From(3), RC2.From(2)}); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(3); - l[2].Value.Should().Be(2); - - l.Sort(); - - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(2); - l[2].Value.Should().Be(3); - } + l[0].Value.Should().Be(1); + l[1].Value.Should().Be(2); + l[2].Value.Should().Be(3); } - public class ClassTests - { - [Fact] - public void Underlying_type_of_int_means_the_vo_is_IComparable() - { - var l = new List(new[] {C1.From(1), C1.From(3), C1.From(2)}); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(3); - l[2].Value.Should().Be(2); - - l.Sort(); - - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(2); - l[2].Value.Should().Be(3); - } - - [Fact] - public void Underlying_type_of_default_int_means_the_vo_is_IComparable() - { - var l = new List(new[] { C2.From(1), C2.From(3), C2.From(2)}); - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(3); - l[2].Value.Should().Be(2); - - l.Sort(); - - l[0].Value.Should().Be(1); - l[1].Value.Should().Be(2); - l[2].Value.Should().Be(3); - } - } public class ExplicitCompareToCalls { [Fact] public void Underlying_type_of_int_means_the_vo_is_IComparable() { - var c1 = C1.From(123); - var cc1 = C1.From(123); + var c1 = C1.Item1; + var cc1 = C1.Item1; c1.CompareTo(cc1).Should().Be(0); } [Fact] public void Underlying_type_of_int_means_the_vo_is_IComparable_with_object_version_of_same_type() { - var c1 = C1.From(123); - var cc1 = C1.From(123); - c1.CompareTo((object)cc1).Should().Be(0); + var c1 = C1.Item1; + var cc1 = C1.Item1; + c1.CompareTo((object) cc1).Should().Be(0); } [Fact] public void Underlying_type_of_int_means_the_vo_is_not_IComparable_with_object_version_of_different_type() { - var c1 = C1.From(123); - Action a = () => c1.CompareTo((object)123).Should().Be(0); + var c1 = C1.Item1; + Action a = () => c1.CompareTo((object) 123).Should().Be(0); a.Should().ThrowExactly().WithMessage("Cannot compare to object as it is not of type C1*"); } [Fact] - public void Underlying_type_of_int_means_the_vo_is_IComparable_with_object_version_of_null_and_behaves_the_same_way_of_returning_1() + public void + Underlying_type_of_int_means_the_vo_is_IComparable_with_object_version_of_null_and_behaves_the_same_way_of_returning_1() { - var c1 = C1.From(123); - c1.CompareTo((object?)null).Should().Be(1); + var c1 = C1.Item1; + c1.CompareTo((object?) null).Should().Be(1); } [Fact] public void Underlying_type_of_default_int_means_the_vo_is_IComparable() { - var c1 = C2.From(123); - var cc1 = C2.From(123); + var c1 = C2.Item1; + var cc1 = C2.Item1; c1.CompareTo(cc1).Should().Be(0); } } - + } } + diff --git a/tests/ConsumerTests/Instances/InstanceFieldTests.cs b/tests/ConsumerTests/Instances/InstanceFieldTests.cs index e1333b64..21c4c125 100644 --- a/tests/ConsumerTests/Instances/InstanceFieldTests.cs +++ b/tests/ConsumerTests/Instances/InstanceFieldTests.cs @@ -1,6 +1,4 @@ using FluentAssertions; -using Vogen.Tests.Types; -using Xunit; namespace ConsumerTests.Instances; @@ -9,8 +7,6 @@ public class InstanceFieldTests [Fact] public void Type_with_two_instance_fields() { - _ = MyIntWithTwoInstanceOfInvalidAndUnspecified.From(100); - MyIntWithTwoInstanceOfInvalidAndUnspecified.Invalid.Value.Should().Be(-1); MyIntWithTwoInstanceOfInvalidAndUnspecified.Unspecified.Value.Should().Be(-2); } @@ -18,31 +14,7 @@ public void Type_with_two_instance_fields() [Fact] public void Type_with_underlying_decimal_with_two_instance_fields() { - _ = MyDecimalWithTwoInstanceOfInvalidAndUnspecified.From(100); - MyDecimalWithTwoInstanceOfInvalidAndUnspecified.Invalid.Value.Should().Be(-1.23m); MyDecimalWithTwoInstanceOfInvalidAndUnspecified.Unspecified.Value.Should().Be(-2.34m); } - - [Fact] - public void Struct_with_an_instance_field() - { - var sut = MyIntStructWithADefaultOf22.From(100); - - sut.Value.Should().Be(100); - - MyIntStructWithADefaultOf22.Default.Value.Should().Be(22); - MyIntStructWithADefaultOf22.Default2.Value.Should().Be(33); - } - - [Fact] - public void Struct_with_an_instance_field_with_reserved_names() - { - var sut = MyIntStructWithReservedNames.From(100); - - sut.Value.Should().Be(100); - - MyIntStructWithReservedNames.@event.Value.Should().Be(22); - MyIntStructWithReservedNames.@class.Value.Should().Be(33); - } } \ No newline at end of file diff --git a/tests/ConsumerTests/Instances/InstanceTests.cs b/tests/ConsumerTests/Instances/InstanceTests.cs index 9dbb0e59..6ecf04f9 100644 --- a/tests/ConsumerTests/Instances/InstanceTests.cs +++ b/tests/ConsumerTests/Instances/InstanceTests.cs @@ -1,69 +1,68 @@ using System; -using Vogen; -using Xunit; using FluentAssertions; +using Intellenum; using FluentAssertions.Execution; namespace ConsumerTests.Instances; -[ValueObject(typeof(DateTime))] +[Intellenum(typeof(DateTime))] [Instance(name: "iso8601_1", value: "2022-12-13")] [Instance(name: "iso8601_2", value: "2022-12-13T13:14:15Z")] [Instance(name: "ticks_as_long", value: 638064864000000000L)] [Instance(name: "ticks_as_int", value: 2147483647)] -public readonly partial struct DateTimeInstance +public partial class DateTimeInstance { } -[ValueObject(typeof(DateTimeOffset))] +[Intellenum(typeof(DateTimeOffset))] [Instance(name: "iso8601_1", value: "2022-12-13")] [Instance(name: "iso8601_2", value: "2022-12-13T13:14:15Z")] [Instance(name: "ticks_as_long", value: 638064864000000000L)] [Instance(name: "ticks_as_int", value: 2147483647)] -public readonly partial struct DateTimeOffsetInstance +public partial class DateTimeOffsetInstance { } -[ValueObject(typeof(float))] +[Intellenum(typeof(float))] [Instance(name: "i1", value: 1.23f)] [Instance(name: "i2", value: 2.34)] [Instance(name: "i3", value: "3.45")] [Instance(name: "i4", value: '2')] -public readonly partial struct MyFloatInstance +public partial class MyFloatInstance { } -[ValueObject(typeof(decimal))] +[Intellenum(typeof(decimal))] [Instance(name: "i1", value: 1.23f)] [Instance(name: "i2", value: 2.34)] [Instance(name: "i3", value: "3.45")] [Instance(name: "i4", value: '2')] -public readonly partial struct MyDecimalInstance +public partial class MyDecimalInstance { } -[ValueObject(typeof(double))] +[Intellenum(typeof(double))] [Instance(name: "i1", value: 1.23d)] [Instance(name: "i2", value: 2.34)] [Instance(name: "i3", value: "3.45")] [Instance(name: "i4", value: '2')] -public readonly partial struct MyDoubleInstance +public partial class MyDoubleInstance { } -[ValueObject(typeof(char))] +[Intellenum(typeof(char))] [Instance(name: "i1", value: 1)] [Instance(name: "i2", value: "2")] [Instance(name: "i3", value: '3')] -public readonly partial struct MyCharInstance +public partial class MyCharInstance { } -[ValueObject(typeof(byte))] +[Intellenum(typeof(byte))] [Instance(name: "i1", value: 1)] [Instance(name: "i2", value: "2")] [Instance(name: "i3", value: '3')] -public readonly partial struct MyByteInstance +public partial class MyByteInstance { } @@ -87,11 +86,11 @@ public void DateTime() public void DateTimeOffset() { using var _ = new AssertionScope(); - DateTimeOffsetInstance.iso8601_1.Value.Should().Be(new DateTimeOffset(2022, 12, 13, 0, 0, 0, TimeSpan.Zero)); - DateTimeOffsetInstance.iso8601_2.Value.Should().Be(new DateTimeOffset(2022, 12, 13, 13, 14, 15, TimeSpan.Zero)); - DateTimeOffsetInstance.ticks_as_long.Value.Should().Be(new DateTimeOffset(2022, 12, 13, 0, 0, 0, TimeSpan.Zero)); + DateTimeOffsetInstance.iso8601_1.Value.Should().Be(DateTimeOffsetInstance.iso8601_1); + DateTimeOffsetInstance.iso8601_2.Value.Should().Be(DateTimeOffsetInstance.iso8601_2); + DateTimeOffsetInstance.ticks_as_long.Value.Should().Be(DateTimeOffsetInstance.ticks_as_long); // ticks as an Int.MaxValue is 2147483647, which is 2,147,483,647 / 10m, which is ~214 seconds, which 3 minutes, 34 seconds - DateTimeOffsetInstance.ticks_as_int.Value.Should().BeCloseTo(new DateTimeOffset(1, 1, 1, 0, 3, 34, 0, TimeSpan.Zero), TimeSpan.FromTicks(7483647)); + DateTimeOffsetInstance.ticks_as_int.Value.Should().BeCloseTo(DateTimeOffsetInstance.ticks_as_int, TimeSpan.FromTicks(7483647)); } } diff --git a/tests/ConsumerTests/Instances/Types.cs b/tests/ConsumerTests/Instances/Types.cs index 803cf5bd..ce63634b 100644 --- a/tests/ConsumerTests/Instances/Types.cs +++ b/tests/ConsumerTests/Instances/Types.cs @@ -1,59 +1,31 @@ -using Vogen; +using Intellenum; namespace ConsumerTests.Instances; -[ValueObject(typeof(int))] +[Intellenum(typeof(int))] [Instance(name: "Invalid", value: -1)] [Instance(name: "Unspecified", value: -2)] public partial class MyIntWithTwoInstanceOfInvalidAndUnspecified { - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } } -[ValueObject(typeof(decimal))] +[Intellenum(typeof(decimal))] [Instance(name: "Invalid", value: "-1.23")] [Instance(name: "Unspecified", value: "-2.34")] public partial class MyDecimalWithTwoInstanceOfInvalidAndUnspecified { - private static Validation Validate(decimal value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } } -[ValueObject(typeof(int))] +[Intellenum(typeof(int))] [Instance(name: "Invalid", value: -1)] [Instance(name: "Unspecified", value: -2)] public partial class MyStructVoIntWithTwoInstanceOfInvalidAndUnspecified { - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } } -[ValueObject(typeof(int))] +[Intellenum(typeof(int))] [Instance(name: "Invalid", value: -1)] [Instance(name: "Unspecified", value: -2)] public partial class MyRecordClassVoIntWithTwoInstanceOfInvalidAndUnspecified { - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } } \ No newline at end of file diff --git a/tests/ConsumerTests/ModuleInitialization.cs b/tests/ConsumerTests/ModuleInitialization.cs index 8040ff07..f1aa16eb 100644 --- a/tests/ConsumerTests/ModuleInitialization.cs +++ b/tests/ConsumerTests/ModuleInitialization.cs @@ -11,80 +11,32 @@ public static class ModuleInitialization public static void Init() { SqlMapper.AddTypeHandler(new ConsumerTests.DeserializationValidationTests.MyVoInt_should_not_bypass_validation.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new ConsumerTests.DeserializationValidationTests.MyVoString_should_not_bypass_validation.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new ConsumerTests.DeserializationValidationTests.MyVoString_should_bypass_validation.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new ConsumerTests.DeserializationValidationTests.MyVoString.DapperTypeHandler()); #if NET6_0_OR_GREATER MappingSchema.Default.SetConverter(dt => TimeOnly.FromDateTime(dt)); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperDateOnlyVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperTimeOnlyVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperDateOnlyVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperTimeOnlyVo.DapperTypeHandler()); #endif #if NET7_0_OR_GREATER SqlMapper.AddTypeHandler(new ConsumerTests.GenericDeserializationValidationTests.MyVoInt_should_not_bypass_validation.DapperTypeHandler()); SqlMapper.AddTypeHandler(new ConsumerTests.GenericDeserializationValidationTests.MyVoString_should_not_bypass_validation.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new ConsumerTests.GenericDeserializationValidationTests.MyVoString_should_bypass_validation.DapperTypeHandler()); #endif - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperFooVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperCharVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperBoolVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperByteVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperDateTimeOffsetVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperDateTimeVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperIntVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperStringVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperLongVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperShortVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperFloatVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperDoubleVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperDecimalVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.ClassVos.DapperGuidVo.DapperTypeHandler()); - - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperFooVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperCharVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperBoolVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperByteVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperDateTimeOffsetVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperDateTimeVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperIntVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperStringVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperLongVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperShortVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperFloatVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperDoubleVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperDecimalVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.StructVos.DapperGuidVo.DapperTypeHandler()); - - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperFooVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperCharVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperBoolVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperByteVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperDateTimeOffsetVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperDateTimeVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperIntVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperStringVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperLongVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperShortVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperFloatVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperDoubleVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperDecimalVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordClassVos.DapperGuidVo.DapperTypeHandler()); - - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperFooVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperCharVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperBoolVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperByteVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperDateTimeOffsetVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperDateTimeVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperIntVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperStringVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperLongVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperShortVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperFloatVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperDoubleVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperDecimalVo.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new Vogen.IntegrationTests.TestTypes.RecordStructVos.DapperGuidVo.DapperTypeHandler()); - + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperFooVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperCharVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperBoolVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperByteVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperDateTimeOffsetVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperDateTimeVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperIntVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperStringVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperLongVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperShortVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperFloatVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperDoubleVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperDecimalVo.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperGuidVo.DapperTypeHandler()); } } \ No newline at end of file diff --git a/tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Class.cs b/tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Class.cs deleted file mode 100644 index 44dcabd5..00000000 --- a/tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Class.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using FluentAssertions; -using Vogen; -using Xunit; - -namespace ConsumerTests.NormalizeInput; - -public class NormalizeInputTests_Class -{ - - [Fact] - public void WithUnderlyingTypeInferred_AnalyzerDoesNotReportIssueWithUnderlyingType() => - Struct_WithDefaultedUnderlyingType.From(129).Value.Should().Be(128); - - [Fact] - public void WhenInputIsNormalized_ReturnsTheNormalizedValue() => Struct_NormalizedToMax128.From(129).Value.Should().Be(128); - - [Fact] - public void WhenInputIsNormalized_DoesNotThrowIfNormalizedToValidValue() - { - Action a = () => Struct_NormalizedToMax128WithValidation.From(129); - a.Should().NotThrow(); - } - - [Fact] - public void Class_WhenInputIsNormalized_ThrowsIfNormalizedToInvalidValue() - { - Action a = () => Class_NormalizedToMax128WithValidation.From(-1); - a.Should().ThrowExactly(); - } - - [Fact] - public void Class_WhenInputIsNormalizedWithASynonymousInputParameter_ReturnsTheNormalizedValue() => - Class_Normalized_WithSynonymousInputParam.From(129).Value.Should().Be(128); - - [Fact] - public void Class_WhenInputIsNormalizedWithASynonymousReturnType_ReturnsTheNormalizedValue() => - Class_Normalized_WithSynonymousReturnType.From(129).Value.Should().Be(128); - - [Fact] - public void Class_WhenInputIsNormalizedWithCamelCasedMethodName_ReturnsTheNormalizedValue() => - Class_Normalized_WithCamelCasedMethodName.From(129).Value.Should().Be(128); -} - -[ValueObject(underlyingType:typeof(int))] -public partial class Class_Normalized_WithSynonymousInputParam -{ - private static int NormalizeInput(Int32 input) => Math.Min(128, input); -} - -[ValueObject(underlyingType:typeof(int))] -public partial class Class_Normalized_WithSynonymousReturnType -{ - private static Int32 NormalizeInput(int input) => Math.Min(128, input); -} - -[ValueObject(underlyingType:typeof(int))] -public partial class Class_Normalized_WithCamelCasedMethodName -{ - private static int normalizeInput(int input) => Math.Min(128, input); -} - -[ValueObject(underlyingType:typeof(int))] -public partial class Class_NormalizedToMax128WithValidation -{ - private static Validation Validate(int value) => value <= 128 && value >= 0 ? Validation.Ok : Validation.Invalid("xxxx"); - - private static Int32 NormalizeInput(int input) => Math.Min(128, input); -} diff --git a/tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Struct.cs b/tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Struct.cs deleted file mode 100644 index 9d754f82..00000000 --- a/tests/ConsumerTests/NormalizeInput/NormalizeInputTests_Struct.cs +++ /dev/null @@ -1,77 +0,0 @@ -using System; -using FluentAssertions; -using Vogen; -using Xunit; - -namespace ConsumerTests.NormalizeInput; - -public class NormalizeInputTests_Struct -{ - - [Fact] - public void WhenInputIsNormalized_ReturnsTheNormalizedValue() => Struct_NormalizedToMax128.From(129).Value.Should().Be(128); - - [Fact] - public void WhenInputIsNormalized_DoesNotThrowIfNormalizedToValidValue() - { - Action a = () => Struct_NormalizedToMax128WithValidation.From(129); - a.Should().NotThrow(); - } - - [Fact] - public void Struct_WhenInputIsNormalized_ThrowsIfNormalizedToValidValue() - { - Action a = () => Struct_NormalizedToMax128WithValidation.From(-1); - a.Should().ThrowExactly(); - } - - [Fact] - public void Struct_WhenInputIsNormalizedWithASynonymousInputParameter_ReturnsTheNormalizedValue() => - Struct_Normalized_WithSynonymousInputParam.From(129).Value.Should().Be(128); - - [Fact] - public void Struct_WhenInputIsNormalizedWithASynonymousReturnType_ReturnsTheNormalizedValue() => - Struct_Normalized_WithSynonymousReturnType.From(129).Value.Should().Be(128); - - [Fact] - public void Struct_WhenInputIsNormalizedWithCamelCasedMethodName_ReturnsTheNormalizedValue() => - Struct_Normalized_WithCamelCasedMethodName.From(129).Value.Should().Be(128); -} - -[ValueObject(underlyingType:typeof(int))] -public partial struct Struct_NormalizedToMax128 -{ - private static Int32 NormalizeInput(int input) => Math.Min(128, input); -} - -[ValueObject] -public partial struct Struct_WithDefaultedUnderlyingType -{ - private static int NormalizeInput(int input) => Math.Min(128, input); -} - -[ValueObject(underlyingType:typeof(int))] -public partial struct Struct_Normalized_WithSynonymousInputParam -{ - private static int NormalizeInput(Int32 input) => Math.Min(128, input); -} - -[ValueObject(underlyingType:typeof(int))] -public partial struct Struct_Normalized_WithSynonymousReturnType -{ - private static Int32 NormalizeInput(int input) => Math.Min(128, input); -} - -[ValueObject(underlyingType:typeof(int))] -public partial struct Struct_Normalized_WithCamelCasedMethodName -{ - private static int normalizeInput(int input) => Math.Min(128, input); -} - -[ValueObject(underlyingType:typeof(int))] -public partial struct Struct_NormalizedToMax128WithValidation -{ - private static Validation Validate(int value) => value <= 128 && value >= 0 ? Validation.Ok : Validation.Invalid("xxxx"); - - private static Int32 NormalizeInput(int input) => Math.Min(128, input); -} diff --git a/tests/ConsumerTests/RecordClassCreationTests.cs b/tests/ConsumerTests/RecordClassCreationTests.cs deleted file mode 100644 index e22edaa3..00000000 --- a/tests/ConsumerTests/RecordClassCreationTests.cs +++ /dev/null @@ -1,57 +0,0 @@ -#pragma warning disable VOG025 - -using System; -using FluentAssertions; -using Vogen; -using Vogen.Tests.Types; -using Xunit; - -namespace ConsumerTests.RecordTests; - -public class RecordClassCreationTests -{ - [Fact] - public void Creation_Happy_Path_MyInt() - { - MyRecordClassInt vo1 = MyRecordClassInt.From(123); - MyRecordClassInt vo2 = MyRecordClassInt.From(123); - - vo1.Should().Be(vo2); - (vo1 == vo2).Should().BeTrue(); - } - - [Fact] - public void Creation_Happy_Path_MyString() - { - MyRecordClassString vo1 = MyRecordClassString.From("123"); - MyRecordClassString vo2 = MyRecordClassString.From("123"); - - vo1.Should().Be(vo2); - (vo1 == vo2).Should().BeTrue(); - } - - [Fact] - public void Creation_Unhappy_Path_MyString() - { - Action action = () => MyRecordClassString.From(null!); - - action.Should().Throw().WithMessage("Cannot create a value object with null."); - } - - [Fact] - public void Creation_Unhappy_Path_MyInt() - { - Action action = () => MyRecordClassInt.From(-1); - - action.Should().Throw().WithMessage("must be greater than zero"); - } - - [Fact] - public void Default_vo_throws_at_runtime() - { - MyRecordClassInt vo = (MyRecordClassInt) Activator.CreateInstance(Type.GetType("Vogen.Tests.Types.MyRecordClassInt")!)!; - Func action = () => vo.Value; - - action.Should().Throw().WithMessage("Use of uninitialized Value Object*"); - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/RecordClassTests.cs b/tests/ConsumerTests/RecordClassTests.cs deleted file mode 100644 index 94da6092..00000000 --- a/tests/ConsumerTests/RecordClassTests.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using FluentAssertions; -using Vogen; -using Xunit; - -namespace ConsumerTests.RecordTests -{ - public class RecordClassTests - { - [Fact] - public void GeneralBehaviour() - { - Types.MyRecord r1 = Types.MyRecord.From(123); - r1.Value.Should().Be(123); - - Types.MyRecord r2 = Types.MyRecord.From(123); - - r1.Should().Be(r2); - object.ReferenceEquals(r1, r2).Should().BeFalse(); - - r1.Value.Should().Be(r2.Value); - r1.GetHashCode().Should().Be(r2.GetHashCode()); - - Types.MyRecord r3 = r2 with - { - Value = 123 - }; - - r3.Should().Be(r2); - object.ReferenceEquals(r3, r2).Should().BeFalse(); - - r3.Value.Should().Be(r2.Value); - r3.GetHashCode().Should().Be(r2.GetHashCode()); - - var msr1 = Types.MyStringRecord.From(" normalizes input "); - msr1.Value.Should().Be("normalizesinput"); - } - - [Fact] - public void WithBehaviour() - { - Types.MyRecord r1 = Types.MyRecord.From(123); - Types.MyRecord r2 = r1 with - { - Value = 123 - }; - - r2.Should().Be(r1); - - Types.MyRecord r3 = r2 with - { - Value = 0 - }; - - r3.Should().NotBe(r2); - r2.Should().Be(r1); - - r3.GetHashCode().Should().NotBe(r2.GetHashCode()); - r3.Value.Should().NotBe(r2.Value); - - Action a = () => - { - Types.MyRecord _ = r1 with {Value = -1}; - }; - - - a.Should().ThrowExactly().WithMessage("must be zero or more"); - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/RecordStructCreationTests.cs b/tests/ConsumerTests/RecordStructCreationTests.cs deleted file mode 100644 index db337d18..00000000 --- a/tests/ConsumerTests/RecordStructCreationTests.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using FluentAssertions; -using Vogen; -using Vogen.Tests.Types; -using Xunit; - -namespace ConsumerTests.RecordTests; - -public class RecordStructCreationTests -{ - [Fact] - public void Creation_Happy_Path_MyInt() - { - MyRecordStructInt vo1 = MyRecordStructInt.From(123); - MyRecordStructInt vo2 = MyRecordStructInt.From(123); - - vo1.Should().Be(vo2); - (vo1 == vo2).Should().BeTrue(); - } - - [Fact] - public void Creation_Happy_Path_MyString() - { - MyRecordStructString vo1 = MyRecordStructString.From("123"); - MyRecordStructString vo2 = MyRecordStructString.From("123"); - - vo1.Should().Be(vo2); - (vo1 == vo2).Should().BeTrue(); - } - - [Fact] - public void Creation_Unhappy_Path_MyString() - { - Action action = () => MyRecordStructString.From(null!); - - action.Should().Throw().WithMessage("Cannot create a value object with null."); - } - - [Fact] - public void Creation_Unhappy_Path_MyInt() - { - Action action = () => MyRecordStructInt.From(-1); - - action.Should().Throw().WithMessage("must be greater than zero"); - } - - [Fact] - public void Default_vo_throws_at_runtime() - { - MyRecordStructInt vo = - (MyRecordStructInt) Activator.CreateInstance(Type.GetType("Vogen.Tests.Types.MyRecordStructInt")!)!; - Func action = () => vo.Value; - - action.Should().Throw().WithMessage("Use of uninitialized Value Object*"); - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/RecordStructTests.cs b/tests/ConsumerTests/RecordStructTests.cs deleted file mode 100644 index 3045f698..00000000 --- a/tests/ConsumerTests/RecordStructTests.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using FluentAssertions; -using Vogen; -using Xunit; - -namespace ConsumerTests.RecordTests; - -public class RecordStructTests -{ - [Fact] - public void GeneralBehaviour() - { - Types.MyRecordStruct r1 = Types.MyRecordStruct.From(123); - r1.Value.Should().Be(123); - - Types.MyRecordStruct r2 = Types.MyRecordStruct.From(123); - - r1.Should().Be(r2); - object.ReferenceEquals(r1, r2).Should().BeFalse(); - - r1.Value.Should().Be(r2.Value); - r1.GetHashCode().Should().Be(r2.GetHashCode()); - - Types.MyRecordStruct r3 = r2 with - { - Value = 123 - }; - - r3.Should().Be(r2); - object.ReferenceEquals(r3, r2).Should().BeFalse(); - - r3.Value.Should().Be(r2.Value); - r3.GetHashCode().Should().Be(r2.GetHashCode()); - - var msr1 = Types.MyStringRecordStruct.From(" normalizes input "); - msr1.Value.Should().Be("normalizesinput"); - } - - [Fact] - public void WithBehaviour() - { - Types.MyRecordStruct r1 = Types.MyRecordStruct.From(123); - Types.MyRecordStruct r2 = r1 with - { - Value = 123 - }; - - r2.Should().Be(r1); - - Types.MyRecordStruct r3 = r2 with - { - Value = 0 - }; - - r3.Should().NotBe(r2); - r2.Should().Be(r1); - - r3.GetHashCode().Should().NotBe(r2.GetHashCode()); - r3.Value.Should().NotBe(r2.Value); - - Action a = () => - { - Types.MyRecordStruct _ = r1 with { Value = -1 }; - }; - - - a.Should().ThrowExactly().WithMessage("must be zero or more"); - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs index 82006022..a1cb0638 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs @@ -12,71 +12,59 @@ using LinqToDB.Mapping; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Vogen.IntegrationTests.TestTypes.ClassVos; -using Xunit; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(Bar))] - public partial class AnotherFooVo { } + [Intellenum(underlyingType: typeof(Bar))] + public partial class FooVo + { + public static readonly FooVo Fred = new FooVo("Item1", new Bar(42, "Fred")); + public static readonly FooVo Wilma = new FooVo("Item2", new Bar(52, "Wilma")); + } public class AnyOtherTypeVoTests { - public static readonly Bar _bar1 = new Bar(42, "Fred"); - public static readonly Bar _wilma = new Bar(52, "Wilma"); [Fact] public void equality_between_same_value_objects() { - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); + FooVo.Fred.Equals(FooVo.Fred).Should().BeTrue(); + (FooVo.Fred == FooVo.Fred).Should().BeTrue(); - (FooVo.From(_bar1) != FooVo.From(_wilma)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_wilma)).Should().BeFalse(); + (FooVo.Fred != FooVo.Wilma).Should().BeTrue(); + (FooVo.Fred == FooVo.Wilma).Should().BeFalse(); - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); + FooVo.Fred.Equals(FooVo.Fred).Should().BeTrue(); + (FooVo.Fred == FooVo.Fred).Should().BeTrue(); - var original = FooVo.From(_bar1); - var other = FooVo.From(_bar1); + var original = FooVo.Fred; + var other = FooVo.Fred; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - FooVo.From(_bar1).Equals(AnotherFooVo.From(_bar1)).Should().BeFalse(); - } - [Fact] public void CanSerializeToString_WithNewtonsoftJsonProvider() { - NewtonsoftJsonFooVo g1 = NewtonsoftJsonFooVo.From(_bar1); + NewtonsoftJsonFooVo g1 = NewtonsoftJsonFooVo.Item1; - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); + string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - Assert.Equal(serializedGuid, serializedString); + Assert.Equal(serialized, serializedString); } - [Fact] - public void CanSerializeClassToString_WithNewtonsoftJsonProvider() - { - NewtonsoftJsonFooVoClass g1 = NewtonsoftJsonFooVoClass.From(_bar1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } [Fact] public void CanSerializeToString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonFooVo.From(_bar1); + var vo = SystemTextJsonFooVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); @@ -87,43 +75,30 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var value = _bar1; - var vo = NewtonsoftJsonFooVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); + var value = NewtonsoftJsonFooVo.Item1; - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromStringClass_WithNewtonsoftJsonProvider() - { - var value = _bar1; - var vo = NewtonsoftJsonFooVoClass.From(value); var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); + var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - Assert.Equal(vo, deserializedVo); + Assert.Equal(NewtonsoftJsonFooVo.Item1, deserializedVo); } [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var value = _bar1; - var vo = SystemTextJsonFooVo.From(value); + var value = SystemTextJsonFooVo.Item1; var serializedString = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - Assert.Equal(vo, deserializedVo); + Assert.Equal(SystemTextJsonFooVo.Item1, deserializedVo); } [Fact] public void CanSerializeToString_WithBothJsonConverters() { - var vo = BothJsonFooVo.From(_bar1); + var vo = BothJsonFooVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -138,7 +113,7 @@ public void CanSerializeToString_WithBothJsonConverters() [Fact] public void CanSerializeToStringClass_WithBothJsonConverters() { - var vo = BothJsonFooVoClass.From(_bar1); + var vo = BothJsonFooVoClass.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -153,19 +128,7 @@ public void CanSerializeToStringClass_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonFooVo.From(_bar1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverterClass_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFooVoClass.From(_bar1); + var vo = NoJsonFooVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -181,7 +144,7 @@ public void WhenNoJsonConverterClass_SystemTextJsonSerializesWithValueProperty() [Fact] public void WithTypeConverterButNoJsonConverters_NewtonsoftSerializesWithValueProperty() { - NoJsonFooVo foo = NoJsonFooVo.From(_bar1); + NoJsonFooVo foo = NoJsonFooVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); @@ -193,7 +156,7 @@ public void WithTypeConverterButNoJsonConverters_NewtonsoftSerializesWithValuePr [Fact] public void WithTypeConverterButNoJsonConverters_SystemTextJsonSerializesWithValueProperty() { - var vo = NoConverterFooVo.From(_bar1); + var vo = NoConverterFooVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -214,7 +177,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { FooField = EfCoreFooVo.From(_bar1) }; + var original = new EfCoreTestEntity { FooField = EfCoreFooVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -238,7 +201,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT '{\"Age\":42,\"Name\":\"Fred\"}'"); var value = Assert.Single(results); - Assert.Equal(value, DapperFooVo.From(_bar1)); + Assert.Equal(value, DapperFooVo.Item1); } [Fact] @@ -247,7 +210,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { FooField = LinqToDbFooVo.From(_bar1) }; + var original = new LinqToDbTestEntity { FooField = LinqToDbFooVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -272,11 +235,11 @@ public void TypeConverter_CanConvertToAndFrom() { var converter = TypeDescriptor.GetConverter(typeof(NoJsonFooVo)); - object vo = converter.ConvertFrom(_bar1); + object vo = converter.ConvertFrom(NoJsonFooVo.Item1); Assert.IsType(vo); - Assert.Equal(NoJsonFooVo.From(_bar1), vo); + Assert.Equal(NoJsonFooVo.Item1, vo); object reconverted = converter.ConvertTo(vo, typeof(Bar)); Assert.IsType(reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs index ff8508bf..4bc7b3b4 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs @@ -12,50 +12,48 @@ using LinqToDB.Mapping; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; // ReSharper disable RedundantOverflowCheckingContext // ReSharper disable ConvertToLocalFunction -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(bool))] - public partial struct AnotherBoolVo { } + [Intellenum(underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] + public partial class BoolVo { } public class BoolVoTests { [Fact] public void equality_between_same_value_objects() { - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(false) == BoolVo.From(false)).Should().BeTrue(); + BoolVo.Yes.Equals(BoolVo.Yes).Should().BeTrue(); + (BoolVo.No == BoolVo.No).Should().BeTrue(); - (BoolVo.From(true) != BoolVo.From(false)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(false)).Should().BeFalse(); + (BoolVo.Yes != BoolVo.No).Should().BeTrue(); + (BoolVo.Yes == BoolVo.No).Should().BeFalse(); - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(true)).Should().BeTrue(); + BoolVo.Yes.Equals(BoolVo.Yes).Should().BeTrue(); + (BoolVo.Yes == BoolVo.Yes).Should().BeTrue(); - var original = BoolVo.From(true); - var other = BoolVo.From(true); + var original = BoolVo.Yes; + var other = BoolVo.Yes; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - BoolVo.From(true).Equals(AnotherBoolVo.From(true)).Should().BeFalse(); - } - [Fact] public void CanSerializeToShort_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonBoolVo.From(true); + var vo = NewtonsoftJsonBoolVo.Yes; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedBool = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -66,7 +64,7 @@ public void CanSerializeToShort_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToShort_WithSystemTextJsonProvider() { - var vo = SystemTextJsonBoolVo.From(true); + var vo = SystemTextJsonBoolVo.Yes; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); @@ -78,7 +76,7 @@ public void CanSerializeToShort_WithSystemTextJsonProvider() public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() { bool value = true; - var vo = NewtonsoftJsonBoolVo.From(value); + var vo = NewtonsoftJsonBoolVo.Yes; var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); @@ -90,7 +88,7 @@ public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() public void CanDeserializeFromShort_WithSystemTextJsonProvider() { bool value = true; - var vo = SystemTextJsonBoolVo.From(value); + var vo = SystemTextJsonBoolVo.Yes; var serializedShort = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); @@ -101,7 +99,7 @@ public void CanDeserializeFromShort_WithSystemTextJsonProvider() [Fact] public void CanSerializeToShort_WithBothJsonConverters() { - var vo = BothJsonBoolVo.From(true); + var vo = BothJsonBoolVo.Yes; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -116,7 +114,7 @@ public void CanSerializeToShort_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonBoolVo.From(true); + var vo = NoJsonBoolVo.Yes; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -128,7 +126,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonBoolVo.From(true); + var vo = NoJsonBoolVo.Yes; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -140,7 +138,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterBoolVo.From(true); + var vo = NoConverterBoolVo.Yes; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -161,7 +159,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreBoolVo.From(true) }; + var original = new EfCoreTestEntity { Id = EfCoreBoolVo.Yes }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -185,7 +183,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT true"); var value = Assert.Single(results); - Assert.Equal(DapperBoolVo.From(true), value); + Assert.Equal(DapperBoolVo.Yes, value); } [Fact] @@ -194,7 +192,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbBoolVo.From(true) }; + var original = new LinqToDbTestEntity { Id = LinqToDbBoolVo.Yes }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -215,30 +213,30 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData("true", true, "True")] - [InlineData("True", true, "True")] - [InlineData("false", false, "False")] - [InlineData("False", false, "False")] - public void TypeConverter_CanConvertToAndFrom_strings(object input, bool expectedBool, string expectedString) + [InlineData("true", "True")] + [InlineData("True", "True")] + [InlineData("false", "False")] + [InlineData("False", "False")] + public void TypeConverter_CanConvertToAndFrom_strings(object input, string expectedString) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); object converted = converter.ConvertFrom(input); Assert.IsType(converted); - Assert.Equal(NoJsonBoolVo.From(expectedBool), converted); + Assert.Equal(NoJsonBoolVo.Yes, converted); object reconverted = converter.ConvertTo(converted, input.GetType()); Assert.Equal(expectedString, reconverted); } [Theory] - [InlineData(true, "True")] - [InlineData(false, "False")] + [InlineData(true, "Yes")] + [InlineData(false, "No")] public void TypeConverter_CanConvertToAndFrom_bools(bool input, string expectedString) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); - NoJsonBoolVo v = NoJsonBoolVo.From(input); + NoJsonBoolVo v = NoJsonBoolVo.FromValue(input); object asString = converter.ConvertTo(v, typeof(string)); Assert.Equal(expectedString, asString); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs index ffaf10aa..a586c86e 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs @@ -8,10 +8,9 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; @@ -19,43 +18,39 @@ // ReSharper disable RedundantOverflowCheckingContext // ReSharper disable ConvertToLocalFunction -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(byte))] - public partial struct AnotherByteVo { } + [Intellenum(underlyingType: typeof(byte))] + [Instance("Eighteen", 18)] + [Instance("Nineteen", 19)] + public partial class AnotherByteVo { } public class ByteVoTests { [Fact] public void equality_between_same_value_objects() { - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); + AnotherByteVo.Eighteen.Equals(AnotherByteVo.Eighteen).Should().BeTrue(); + (AnotherByteVo.Eighteen == AnotherByteVo.Eighteen).Should().BeTrue(); - (ByteVo.From(18) != ByteVo.From(19)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(19)).Should().BeFalse(); + (AnotherByteVo.Eighteen != AnotherByteVo.Nineteen).Should().BeTrue(); + (AnotherByteVo.Eighteen == AnotherByteVo.Nineteen).Should().BeFalse(); - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); + AnotherByteVo.Eighteen.Equals(AnotherByteVo.Eighteen).Should().BeTrue(); + (AnotherByteVo.Eighteen == AnotherByteVo.Eighteen).Should().BeTrue(); - var original = ByteVo.From(18); - var other = ByteVo.From(18); + var original = AnotherByteVo.Eighteen; + var other = AnotherByteVo.Eighteen; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - ByteVo.From(18).Equals(AnotherByteVo.From(18)).Should().BeFalse(); - } - [Fact] public void CanSerializeToShort_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonByteVo.From(123); + var vo = NewtonsoftJsonByteVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -66,7 +61,7 @@ public void CanSerializeToShort_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToShort_WithSystemTextJsonProvider() { - var vo = SystemTextJsonByteVo.From(123); + var vo = SystemTextJsonByteVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); @@ -78,7 +73,7 @@ public void CanSerializeToShort_WithSystemTextJsonProvider() public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() { byte value = 123; - var vo = NewtonsoftJsonByteVo.From(value); + var vo = NewtonsoftJsonByteVo.Item1; var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); @@ -90,7 +85,7 @@ public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() public void CanDeserializeFromShort_WithSystemTextJsonProvider() { byte value = 123; - var vo = SystemTextJsonByteVo.From(value); + var vo = SystemTextJsonByteVo.Item1; var serializedShort = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); @@ -101,7 +96,7 @@ public void CanDeserializeFromShort_WithSystemTextJsonProvider() [Fact] public void CanSerializeToShort_WithBothJsonConverters() { - var vo = BothJsonByteVo.From(123); + var vo = BothJsonByteVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -116,7 +111,7 @@ public void CanSerializeToShort_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonByteVo.From(123); + var vo = NoJsonByteVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -128,7 +123,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonByteVo.From(123); + var vo = NoJsonByteVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -140,7 +135,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterByteVo.From(123); + var vo = NoConverterByteVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -161,7 +156,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreByteVo.From(123) }; + var original = new EfCoreTestEntity { Id = EfCoreByteVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -185,7 +180,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT 123"); var value = Assert.Single(results); - Assert.Equal(DapperByteVo.From(123), value); + Assert.Equal(DapperByteVo.Item1, value); } [Fact] @@ -194,7 +189,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbByteVo.From(123) }; + var original = new LinqToDbTestEntity { Id = LinqToDbByteVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -222,7 +217,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) var converter = TypeDescriptor.GetConverter(typeof(NoJsonByteVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonByteVo.From(123), id); + Assert.Equal(NoJsonByteVo.Item1, id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs index d2647bd9..172b1fc5 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs @@ -8,54 +8,49 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; using LinqToDB.Mapping; // ReSharper disable RedundantOverflowCheckingContext // ReSharper disable ConvertToLocalFunction +// ReSharper disable EqualExpressionComparison -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(char))] - public partial struct AnotherCharVo { } + [Intellenum(underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] + public partial class AnotherCharVo { } public class CharVoTests { [Fact] public void equality_between_same_value_objects() { - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); + CharVo.A.Equals(CharVo.A).Should().BeTrue(); + (CharVo.A == CharVo.A).Should().BeTrue(); - (CharVo.From('a') != CharVo.From('b')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('b')).Should().BeFalse(); + (CharVo.A != CharVo.B).Should().BeTrue(); + (CharVo.A == CharVo.B).Should().BeFalse(); - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); + CharVo.A.Equals(CharVo.A).Should().BeTrue(); + (CharVo.A == CharVo.A).Should().BeTrue(); - var original = CharVo.From('a'); - var other = CharVo.From('a'); + var original = CharVo.A; + var other = CharVo.A; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - - [Fact] - public void equality_between_different_value_objects() - { - CharVo.From('a').Equals(AnotherCharVo.From('a')).Should().BeFalse(); - } - [Fact] public void CanSerializeToShort_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonCharVo.From('a'); + var vo = NewtonsoftJsonCharVo.A; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -66,7 +61,7 @@ public void CanSerializeToShort_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToShort_WithSystemTextJsonProvider() { - var vo = SystemTextJsonCharVo.From('a'); + var vo = SystemTextJsonCharVo.A; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); @@ -77,31 +72,28 @@ public void CanSerializeToShort_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() { - char value = 'a'; - var vo = NewtonsoftJsonCharVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); + var serializedShort = NewtonsoftJsonSerializer.SerializeObject(NewtonsoftJsonCharVo.A); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - Assert.Equal(vo, deserializedVo); + Assert.Equal(NewtonsoftJsonCharVo.A, deserializedVo); } [Fact] public void CanDeserializeFromShort_WithSystemTextJsonProvider() { - char value = 'a'; - var vo = SystemTextJsonCharVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); + var vo = SystemTextJsonCharVo.A; + var serializedShort = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - Assert.Equal(vo, deserializedVo); + Assert.Equal(SystemTextJsonCharVo.A, deserializedVo); } [Fact] public void CanSerializeToShort_WithBothJsonConverters() { - var vo = BothJsonCharVo.From('a'); + var vo = BothJsonCharVo.A; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -116,7 +108,7 @@ public void CanSerializeToShort_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonCharVo.From('a'); + var vo = NoJsonCharVo.A; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -128,7 +120,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonCharVo.From('a'); + var vo = NoJsonCharVo.A; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -140,7 +132,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterCharVo.From('a'); + var vo = NoConverterCharVo.A; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -161,7 +153,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreCharVo.From('a') }; + var original = new EfCoreTestEntity { Id = EfCoreCharVo.A }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -185,7 +177,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT 'a'"); var value = Assert.Single(results); - Assert.Equal(DapperCharVo.From('a'), value); + Assert.Equal(DapperCharVo.A, value); } [Fact] @@ -194,7 +186,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbCharVo.From('a') }; + var original = new LinqToDbTestEntity { Id = LinqToDbCharVo.A }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -221,7 +213,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) var converter = TypeDescriptor.GetConverter(typeof(NoJsonCharVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonCharVo.From('a'), id); + Assert.Equal(NoJsonCharVo.A, id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs index 699c7515..732e6ac0 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs @@ -14,7 +14,7 @@ using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; @@ -26,46 +26,44 @@ // ReSharper disable PropertyCanBeMadeInitOnly.Global // ReSharper disable SuspiciousTypeConversion.Global -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(DateOnly))] - public readonly partial struct AnotherDateOnlyVo { } + [Intellenum(underlyingType: typeof(DateOnly))] + public partial class AnotherDateOnlyVo { + static AnotherDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + + } public class DateOnlyVoTests { - private static readonly DateOnly _date1 = new DateOnly(1970, 6, 10); - private static readonly DateOnly _date2 = new DateOnly(2022, 12, 27); - [Fact] public void equality_between_same_value_objects() { - DateOnlyVo.From(_date1).Equals(DateOnlyVo.From(_date1)).Should().BeTrue(); - (DateOnlyVo.From(_date1) == DateOnlyVo.From(_date1)).Should().BeTrue(); + DateOnlyVo.JanFirst.Equals(DateOnlyVo.JanFirst).Should().BeTrue(); + (DateOnlyVo.JanFirst == DateOnlyVo.JanFirst).Should().BeTrue(); - (DateOnlyVo.From(_date1) != DateOnlyVo.From(_date2)).Should().BeTrue(); - (DateOnlyVo.From(_date1) == DateOnlyVo.From(_date2)).Should().BeFalse(); + (DateOnlyVo.JanFirst != DateOnlyVo.JanSecond).Should().BeTrue(); + (DateOnlyVo.JanFirst == DateOnlyVo.JanSecond).Should().BeFalse(); - DateOnlyVo.From(_date1).Equals(DateOnlyVo.From(_date1)).Should().BeTrue(); - (DateOnlyVo.From(_date1) == DateOnlyVo.From(_date1)).Should().BeTrue(); + DateOnlyVo.JanFirst.Equals(DateOnlyVo.JanFirst).Should().BeTrue(); + (DateOnlyVo.JanFirst == DateOnlyVo.JanFirst).Should().BeTrue(); - var original = DateOnlyVo.From(_date1); - var other = DateOnlyVo.From(_date1); + var original = DateOnlyVo.JanFirst; + var other = DateOnlyVo.JanFirst; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - DateOnlyVo.From(_date1).Equals(AnotherDateOnlyVo.From(_date1)).Should().BeFalse(); - } - #if NET7_0_OR_GREATER [Fact] public void CanSerializeToString_WithNewtonsoftJsonProvider() { - var g1 = NewtonsoftJsonDateOnlyVo.From(_date1); + var g1 = NewtonsoftJsonDateOnlyVo.JanFirst; string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); @@ -76,7 +74,7 @@ public void CanSerializeToString_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonDateOnlyVo.From(_date1); + var vo = SystemTextJsonDateOnlyVo.JanFirst; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); @@ -87,8 +85,8 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var value = _date1; - var vo = NewtonsoftJsonDateOnlyVo.From(value); + var value = NewtonsoftJsonDateOnlyVo.JanFirst.Value; + var vo = NewtonsoftJsonDateOnlyVo.JanFirst; var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); @@ -99,9 +97,8 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var value = _date1; - var vo = SystemTextJsonDateOnlyVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); + var vo = SystemTextJsonDateOnlyVo.JanFirst; + var serializedString = SystemTextJsonSerializer.Serialize(NewtonsoftJsonDateOnlyVo.JanFirst.Value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); @@ -111,7 +108,7 @@ public void CanDeserializeFromString_WithSystemTextJsonProvider() [Fact] public void CanSerializeToString_WithBothJsonConverters() { - var vo = BothJsonDateOnlyVo.From(_date1); + var vo = BothJsonDateOnlyVo.JanFirst; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -126,11 +123,11 @@ public void CanSerializeToString_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonDateOnlyVo.From(_date1); + var vo = NoJsonDateOnlyVo.JanFirst; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; + var expected = "{\"Value\":\"" + NewtonsoftJsonDateOnlyVo.JanFirst.Value.ToString("O") + "\"}"; serialized.Should().Be(expected); } @@ -138,11 +135,11 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonDateOnlyVo.From(_date1); + var vo = NoJsonDateOnlyVo.JanFirst; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - var expected = $"\"{_date1:o}\""; + var expected = $"\"{NewtonsoftJsonDateOnlyVo.JanFirst.Value:o}\""; Assert.Equal(expected, serialized); } @@ -150,12 +147,12 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterDateOnlyVo.From(_date1); + var vo = NoConverterDateOnlyVo.JanFirst; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; + var expected = "{\"Value\":\"" + NewtonsoftJsonDateOnlyVo.JanFirst.Value.ToString("O") + "\"}"; newtonsoft.Should().Be(expected); systemText.Should().Be(expected); @@ -172,7 +169,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreDateOnlyVo.From(_date1) }; + var original = new EfCoreTestEntity { Id = EfCoreDateOnlyVo.JanFirst }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -193,11 +190,11 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT '2022-01-15'"); + IEnumerable results = await connection.QueryAsync("SELECT '2021-01-01'"); DapperDateOnlyVo actual = Assert.Single(results); - var expected = DapperDateOnlyVo.From(new DateOnly(2022,01,15)); + var expected = DapperDateOnlyVo.JanFirst; actual.Should().Be(expected); } @@ -207,7 +204,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbDateOnlyVo.From(_date1) }; + var original = new LinqToDbTestEntity { Id = LinqToDbDateOnlyVo.JanFirst }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -227,17 +224,16 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } } - [Theory] - [InlineData("2022-01-15")] - public void TypeConverter_CanConvertToAndFrom(string value) + [Fact] + public void TypeConverter_CanConvertToAndFrom() { var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateOnlyVo)); - var id = converter.ConvertFrom(value); + var id = converter.ConvertFrom("2021-01-01"); Assert.IsType(id); - Assert.Equal(NoJsonDateOnlyVo.From(DateOnly.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); + Assert.Equal(NoJsonDateOnlyVo.JanFirst, id); - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); + var reconverted = converter.ConvertTo(id, typeof(string)); + Assert.Equal("2021-01-01", reconverted); } public class TestDbContext : DbContext diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs index 1a8dfa83..6e1a926e 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs @@ -9,10 +9,9 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; @@ -24,45 +23,46 @@ // ReSharper disable PropertyCanBeMadeInitOnly.Global // ReSharper disable SuspiciousTypeConversion.Global -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(DateTimeOffset))] - public readonly partial struct AnotherDateTimeOffsetVo { } + [Intellenum(underlyingType: typeof(DateTimeOffset))] + public partial class DateTimeOffsetVo + { + static DateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + Instance("SomethingElse", new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); + } + } public class DateTimeOffsetVoTests { - private static readonly DateTimeOffset _date1 = new DateTimeOffset(1970, 6, 10, 14, 01, 02, TimeSpan.Zero) + TimeSpan.FromTicks(12345678); - private static readonly DateTimeOffset _date2 = DateTimeOffset.Now.AddMinutes(42.69); - + private readonly DateTimeOffset _date1 = DateTimeOffsetVo.JanFirst.Value; + [Fact] public void equality_between_same_value_objects() { - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); + DateTimeOffsetVo.JanFirst.Equals(DateTimeOffsetVo.JanFirst).Should().BeTrue(); + (DateTimeOffsetVo.JanFirst == DateTimeOffsetVo.JanFirst).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) != DateTimeOffsetVo.From(_date2)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date2)).Should().BeFalse(); + (DateTimeOffsetVo.JanFirst != DateTimeOffsetVo.JanSecond).Should().BeTrue(); + (DateTimeOffsetVo.JanFirst == DateTimeOffsetVo.JanSecond).Should().BeFalse(); - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); + DateTimeOffsetVo.JanFirst.Equals(DateTimeOffsetVo.JanFirst).Should().BeTrue(); + (DateTimeOffsetVo.JanFirst == DateTimeOffsetVo.JanFirst).Should().BeTrue(); - var original = DateTimeOffsetVo.From(_date1); - var other = DateTimeOffsetVo.From(_date1); + var original = DateTimeOffsetVo.JanFirst; + var other = DateTimeOffsetVo.JanFirst; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - DateTimeOffsetVo.From(_date1).Equals(AnotherDateTimeOffsetVo.From(_date1)).Should().BeFalse(); - } - [Fact] public void CanSerializeToString_WithNewtonsoftJsonProvider() { - var g1 = NewtonsoftJsonDateTimeOffsetVo.From(_date1); + var g1 = NewtonsoftJsonDateTimeOffsetVo.JanFirst; string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); @@ -73,7 +73,7 @@ public void CanSerializeToString_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonDateTimeOffsetVo.From(_date1); + var vo = SystemTextJsonDateTimeOffsetVo.JanFirst; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); @@ -85,7 +85,7 @@ public void CanSerializeToString_WithSystemTextJsonProvider() public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { var value = _date1; - var vo = NewtonsoftJsonDateTimeOffsetVo.From(value); + var vo = NewtonsoftJsonDateTimeOffsetVo.JanFirst; var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); @@ -97,7 +97,7 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() public void CanDeserializeFromString_WithSystemTextJsonProvider() { var value = _date1; - var vo = SystemTextJsonDateTimeOffsetVo.From(value); + var vo = SystemTextJsonDateTimeOffsetVo.JanFirst; var serializedString = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); @@ -108,7 +108,7 @@ public void CanDeserializeFromString_WithSystemTextJsonProvider() [Fact] public void CanSerializeToString_WithBothJsonConverters() { - var vo = BothJsonDateTimeOffsetVo.From(_date1); + var vo = BothJsonDateTimeOffsetVo.JanFirst; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -123,7 +123,7 @@ public void CanSerializeToString_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonDateTimeOffsetVo.From(_date1); + var vo = NoJsonDateTimeOffsetVo.JanFirst; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -135,7 +135,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonDateTimeOffsetVo.From(_date1); + var vo = NoJsonDateTimeOffsetVo.JanFirst; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -147,7 +147,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterDateTimeOffsetVo.From(_date1); + var vo = NoConverterDateTimeOffsetVo.JanFirst; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -168,7 +168,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreDateTimeOffsetVo.From(_date1) }; + var original = new EfCoreTestEntity { Id = EfCoreDateTimeOffsetVo.JanFirst }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -193,7 +193,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() DapperDateTimeOffsetVo actual = Assert.Single(results); - var expected = DapperDateTimeOffsetVo.From(new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); + var expected = DapperDateTimeOffsetVo.SomethingElse; actual.Should().Be(expected); } @@ -203,7 +203,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeOffsetVo.From(_date1) }; + var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeOffsetVo.JanFirst }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -230,7 +230,7 @@ public void TypeConverter_CanConvertToAndFrom(string value) var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeOffsetVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonDateTimeOffsetVo.From(DateTimeOffset.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); + Assert.Equal(NoJsonDateTimeOffsetVo.SomethingElse, id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs index 66dc774d..35d526e7 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs @@ -9,10 +9,9 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; @@ -24,45 +23,48 @@ // ReSharper disable PropertyCanBeMadeInitOnly.Global // ReSharper disable SuspiciousTypeConversion.Global -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(DateTime))] - public readonly partial struct AnotherDateTimeVo { } + [Intellenum(underlyingType: typeof(DateTime))] + public partial class AnotherDateTimeVo + { + static AnotherDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } public class DateTimeVoTests { - private static readonly DateTime _date1 = new DateTime(1970, 6, 10, 14, 01, 02, DateTimeKind.Utc) + TimeSpan.FromTicks(12345678); - private static readonly DateTime _date2 = DateTime.Now.AddMinutes(42.69); - + private readonly DateTime _date1 = NoJsonDateTimeVo.Item1.Value; + [Fact] public void equality_between_same_value_objects() { - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); + DateTimeVo.Item1.Equals(DateTimeVo.Item1).Should().BeTrue(); + (DateTimeVo.Item1 == DateTimeVo.Item1).Should().BeTrue(); - (DateTimeVo.From(_date1) != DateTimeVo.From(_date2)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date2)).Should().BeFalse(); + (DateTimeVo.Item1 != DateTimeVo.Item2).Should().BeTrue(); + (DateTimeVo.Item1 == DateTimeVo.Item2).Should().BeFalse(); - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); + DateTimeVo.Item1.Equals(DateTimeVo.Item1).Should().BeTrue(); + (DateTimeVo.Item1 == DateTimeVo.Item1).Should().BeTrue(); - var original = DateTimeVo.From(_date1); - var other = DateTimeVo.From(_date1); + var original = DateTimeVo.Item1; + var other = DateTimeVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - DateTimeVo.From(_date1).Equals(AnotherDateTimeVo.From(_date1)).Should().BeFalse(); - } - [Fact] public void CanSerializeToString_WithNewtonsoftJsonProvider() { - var g1 = NewtonsoftJsonDateTimeVo.From(_date1); + var g1 = NewtonsoftJsonDateTimeVo.Item1; string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); @@ -73,7 +75,7 @@ public void CanSerializeToString_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonDateTimeVo.From(_date1); + var vo = SystemTextJsonDateTimeVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); @@ -85,7 +87,7 @@ public void CanSerializeToString_WithSystemTextJsonProvider() public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { var value = _date1; - var vo = NewtonsoftJsonDateTimeVo.From(value); + var vo = NewtonsoftJsonDateTimeVo.Item1; var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); @@ -97,7 +99,7 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() public void CanDeserializeFromString_WithSystemTextJsonProvider() { var value = _date1; - var vo = SystemTextJsonDateTimeVo.From(value); + var vo = SystemTextJsonDateTimeVo.Item1; var serializedString = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); @@ -108,7 +110,7 @@ public void CanDeserializeFromString_WithSystemTextJsonProvider() [Fact] public void CanSerializeToString_WithBothJsonConverters() { - var vo = BothJsonDateTimeVo.From(_date1); + var vo = BothJsonDateTimeVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -123,7 +125,7 @@ public void CanSerializeToString_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonDateTimeVo.From(_date1); + var vo = NoJsonDateTimeVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -135,7 +137,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonDateTimeVo.From(_date1); + var vo = NoJsonDateTimeVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -147,7 +149,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterDateTimeVo.From(_date1); + var vo = NoConverterDateTimeVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -168,7 +170,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreDateTimeVo.From(_date1) }; + var original = new EfCoreTestEntity { Id = EfCoreDateTimeVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -193,7 +195,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() DapperDateTimeVo actual = Assert.Single(results); - var expected = DapperDateTimeVo.From(new DateTime(2022,01,15,19,08,49,DateTimeKind.Utc).AddTicks(5413764)); + var expected = DapperDateTimeVo.SomethingElse; actual.Should().Be(expected); } @@ -203,7 +205,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeVo.From(_date1) }; + var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -230,7 +232,7 @@ public void TypeConverter_CanConvertToAndFrom(string value) var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonDateTimeVo.From(DateTime.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); + Assert.Equal(NoJsonDateTimeVo.Item1, id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs index 16396bed..46a6e60d 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs @@ -13,48 +13,52 @@ using LinqToDB.Mapping; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Vogen; -using Vogen.IntegrationTests.TestTypes.ClassVos; -using Xunit; +using Intellenum; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + namespace MediumTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(decimal))] - public partial struct AnotherDecimalVo { } + [Intellenum(underlyingType: typeof(decimal))] + public partial class AnotherDecimalVo + { + static AnotherDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + + } public class DecimalVoTests { [Fact] public void equality_between_same_value_objects() { - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); + DecimalVo.Item1.Equals(DecimalVo.Item1).Should().BeTrue(); + (DecimalVo.Item1 == DecimalVo.Item1).Should().BeTrue(); - (DecimalVo.From(18) != DecimalVo.From(19)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(19)).Should().BeFalse(); + (DecimalVo.Item1 != DecimalVo.Item2).Should().BeTrue(); + (DecimalVo.Item1 == DecimalVo.Item2).Should().BeFalse(); - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); + DecimalVo.Item1.Equals(DecimalVo.Item1).Should().BeTrue(); + (DecimalVo.Item1 == DecimalVo.Item1).Should().BeTrue(); - var original = DecimalVo.From(18); - var other = DecimalVo.From(18); + var original = DecimalVo.Item1; + var other = DecimalVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - DecimalVo.From(18).Equals(AnotherDecimalVo.From(18)).Should().BeFalse(); - } - [Fact] public void CanSerializeToLong_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonDecimalVo.From(123.45m); + var vo = NewtonsoftJsonDecimalVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -65,7 +69,7 @@ public void CanSerializeToLong_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToLong_WithSystemTextJsonProvider() { - var vo = SystemTextJsonDecimalVo.From(123.45m); + var vo = SystemTextJsonDecimalVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); @@ -77,7 +81,7 @@ public void CanSerializeToLong_WithSystemTextJsonProvider() public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() { var value = 123.45m; - var vo = NewtonsoftJsonDecimalVo.From(value); + var vo = NewtonsoftJsonDecimalVo.Item1; var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); @@ -89,7 +93,7 @@ public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() public void CanDeserializeFromLong_WithSystemTextJsonProvider() { var value = 123.45m; - var vo = SystemTextJsonDecimalVo.From(value); + var vo = SystemTextJsonDecimalVo.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); @@ -100,7 +104,7 @@ public void CanDeserializeFromLong_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromLong_WithSystemTextJsonProvider_treating_numbers_as_string() { - var vo = SystemTextJsonDecimalVo_Treating_number_as_string.From(123.45m); + var vo = SystemTextJsonDecimalVo_Treating_number_as_string.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); @@ -111,7 +115,7 @@ public void CanDeserializeFromLong_WithSystemTextJsonProvider_treating_numbers_a [Fact] public void CanSerializeToLong_WithBothJsonConverters() { - var vo = BothJsonDecimalVo.From(123.45m); + var vo = BothJsonDecimalVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -126,7 +130,7 @@ public void CanSerializeToLong_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonDecimalVo.From(123.45m); + var vo = NoJsonDecimalVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -138,7 +142,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonDecimalVo.From(123.45m); + var vo = NoJsonDecimalVo.Item1; string serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -150,7 +154,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterDecimalVo.From(123.45m); + var vo = NoConverterDecimalVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -171,7 +175,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreDecimalVo.From(123.45m) }; + var original = new EfCoreTestEntity { Id = EfCoreDecimalVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -196,7 +200,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT @Value", parameters); DapperDecimalVo value = Assert.Single(results); - Assert.Equal(DapperDecimalVo.From(123.45m), value); + Assert.Equal(DapperDecimalVo.Item1, value); } [Fact] @@ -205,7 +209,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbDecimalVo.From(123.45m) }; + var original = new LinqToDbTestEntity { Id = LinqToDbDecimalVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -231,7 +235,7 @@ public void TypeConverter_CanConvertToAndFromDecimal() var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); var id = converter.ConvertFrom(123.45m); Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123.45m), id); + Assert.Equal(NoJsonDecimalVo.Item1, id); var reconverted = converter.ConvertTo(id, typeof(decimal)); Assert.Equal(123.45m, reconverted); @@ -245,7 +249,7 @@ public void TypeConverter_CanConvertToAndFrom() var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); var id = converter.ConvertFrom(null!, culture, "123.45"); Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123.45m), id); + Assert.Equal(NoJsonDecimalVo.Item1, id); var reconverted = converter.ConvertTo(null, culture, id, typeof(string)); Assert.Equal("123.45", reconverted); @@ -254,7 +258,7 @@ public void TypeConverter_CanConvertToAndFrom() [Fact] public void RoundTrip_WithNsj() { - var vo = NewtonsoftJsonDecimalVo.From(123.45m); + var vo = NewtonsoftJsonDecimalVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; @@ -265,7 +269,7 @@ public void RoundTrip_WithNsj() [Fact] public void RoundTrip_WithStj() { - var vo = SystemTextJsonDecimalVo.From(123.45m); + var vo = SystemTextJsonDecimalVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; @@ -276,7 +280,7 @@ public void RoundTrip_WithStj() [Fact] public void RoundTrip_WithStj_Treating_numbers_as_string() { - var vo = SystemTextJsonDecimalVo_Treating_number_as_string.From(123.45m); + var vo = SystemTextJsonDecimalVo_Treating_number_as_string.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs index 6c89bdaa..b87be634 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs @@ -13,48 +13,46 @@ using LinqToDB.Mapping; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Vogen; -using Vogen.IntegrationTests.TestTypes.ClassVos; -using Xunit; +using Intellenum; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + namespace MediumTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(double))] - public partial struct AnotherDoubleVo { } + [Intellenum(underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] + public partial class AnotherDoubleVo { } public class DoubleVoTests { [Fact] public void equality_between_same_value_objects() { - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); + DoubleVo.Item1.Equals(DoubleVo.Item1).Should().BeTrue(); + (DoubleVo.Item1 == DoubleVo.Item1).Should().BeTrue(); - (DoubleVo.From(18) != DoubleVo.From(19)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(19)).Should().BeFalse(); + (DoubleVo.Item1 != DoubleVo.Item2).Should().BeTrue(); + (DoubleVo.Item1 == DoubleVo.Item2).Should().BeFalse(); - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); + DoubleVo.Item1.Equals(DoubleVo.Item1).Should().BeTrue(); + (DoubleVo.Item1 == DoubleVo.Item1).Should().BeTrue(); - var original = DoubleVo.From(18); - var other = DoubleVo.From(18); + var original = DoubleVo.Item1; + var other = DoubleVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - DoubleVo.From(18).Equals(AnotherDoubleVo.From(18)).Should().BeFalse(); - } - [Fact] public void CanSerializeToLong_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonDoubleVo.From(123D); + var vo = NewtonsoftJsonDoubleVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -65,7 +63,7 @@ public void CanSerializeToLong_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToLong_WithSystemTextJsonProvider() { - var vo = SystemTextJsonDoubleVo.From(123D); + var vo = SystemTextJsonDoubleVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); @@ -77,7 +75,7 @@ public void CanSerializeToLong_WithSystemTextJsonProvider() public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() { var value = 123D; - var vo = NewtonsoftJsonDoubleVo.From(value); + var vo = NewtonsoftJsonDoubleVo.Item1; var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); @@ -89,7 +87,7 @@ public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() public void CanDeserializeFromLong_WithSystemTextJsonProvider() { var value = 123D; - var vo = SystemTextJsonDoubleVo.From(value); + var vo = SystemTextJsonDoubleVo.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); @@ -100,7 +98,7 @@ public void CanDeserializeFromLong_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromLong_WithSystemTextJsonProvider_treating_numbers_as_string() { - var vo = SystemTextJsonDoubleVo_number_as_string.From(123D); + var vo = SystemTextJsonDoubleVo_number_as_string.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); @@ -111,7 +109,7 @@ public void CanDeserializeFromLong_WithSystemTextJsonProvider_treating_numbers_a [Fact] public void CanSerializeToLong_WithBothJsonConverters() { - var vo = BothJsonDoubleVo.From(123D); + var vo = BothJsonDoubleVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -126,7 +124,7 @@ public void CanSerializeToLong_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonDoubleVo.From(123D); + var vo = NoJsonDoubleVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -138,7 +136,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonDoubleVo.From(123D); + var vo = NoJsonDoubleVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -150,7 +148,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterDoubleVo.From(123D); + var vo = NoConverterDoubleVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -171,7 +169,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreDoubleVo.From(123D) }; + var original = new EfCoreTestEntity { Id = EfCoreDoubleVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -196,7 +194,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT @Value", parameters); var value = Assert.Single(results); - Assert.Equal(DapperDoubleVo.From(123.45d), value); + Assert.Equal(DapperDoubleVo.Item1, value); } [Fact] @@ -205,7 +203,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbDoubleVo.From(123) }; + var original = new LinqToDbTestEntity { Id = LinqToDbDoubleVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -235,7 +233,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) var converter = TypeDescriptor.GetConverter(typeof(NoJsonDoubleVo)); var id = converter.ConvertFrom(null!, culture, value); Assert.IsType(id); - Assert.Equal(NoJsonDoubleVo.From(123.45D), id); + Assert.Equal(NoJsonDoubleVo.Item1, id); var reconverted = converter.ConvertTo(null, culture, id, value.GetType()); Assert.Equal(value, reconverted); @@ -244,7 +242,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) [Fact] public void RoundTrip_WithNsj() { - var vo = NewtonsoftJsonDoubleVo.From(123.45); + var vo = NewtonsoftJsonDoubleVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; @@ -255,7 +253,7 @@ public void RoundTrip_WithNsj() [Fact] public void RoundTrip_WithStj() { - var vo = SystemTextJsonDoubleVo.From(123.45); + var vo = SystemTextJsonDoubleVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; @@ -266,7 +264,7 @@ public void RoundTrip_WithStj() [Fact] public void RoundTrip_WithStj_Treating_numbers_as_string() { - var vo = SystemTextJsonDoubleVo_number_as_string.From(123.45); + var vo = SystemTextJsonDoubleVo_number_as_string.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs index 50c118e7..07746b2b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs @@ -13,48 +13,47 @@ using LinqToDB.Mapping; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Vogen; -using Vogen.IntegrationTests.TestTypes.ClassVos; -using Xunit; +using Intellenum; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; +// ReSharper disable EqualExpressionComparison + +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 namespace MediumTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(float))] - public partial struct AnotherFloatVo { } + [Intellenum(underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] + public partial class AnotherFloatVo { } public class FloatVoTests { [Fact] public void equality_between_same_value_objects() { - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); + FloatVo.Item1.Equals(FloatVo.Item1).Should().BeTrue(); + (FloatVo.Item1 == FloatVo.Item1).Should().BeTrue(); - (FloatVo.From(18) != FloatVo.From(19)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(19)).Should().BeFalse(); + (FloatVo.Item1 != FloatVo.Item2).Should().BeTrue(); + (FloatVo.Item1 == FloatVo.Item2).Should().BeFalse(); - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); + FloatVo.Item1.Equals(FloatVo.Item1).Should().BeTrue(); + (FloatVo.Item1 == FloatVo.Item1).Should().BeTrue(); - var original = FloatVo.From(18); - var other = FloatVo.From(18); + var original = FloatVo.Item1; + var other = FloatVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - FloatVo.From(18).Equals(AnotherFloatVo.From(18)).Should().BeFalse(); - } - [Fact] public void CanSerializeToFloat_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonFloatVo.From(123.45f); + var vo = NewtonsoftJsonFloatVo.Item2; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedFloat = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -65,7 +64,7 @@ public void CanSerializeToFloat_WithNewtonsoftJsonProvider() [Fact] public void RoundTrip_WithNsj() { - var vo = NewtonsoftJsonFloatVo.From(123.45f); + var vo = NewtonsoftJsonFloatVo.Item2; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; @@ -76,7 +75,7 @@ public void RoundTrip_WithNsj() [Fact] public void RoundTrip_WithStj() { - var vo = SystemTextJsonFloatVo.From(123.45f); + var vo = SystemTextJsonFloatVo.Item2; string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; @@ -87,7 +86,7 @@ public void RoundTrip_WithStj() [Fact] public void CanSerializeToFloat_WithSystemTextJsonProvider() { - var vo = SystemTextJsonFloatVo.From(123); + var vo = SystemTextJsonFloatVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedFloat = SystemTextJsonSerializer.Serialize(vo.Value); @@ -99,7 +98,7 @@ public void CanSerializeToFloat_WithSystemTextJsonProvider() public void CanDeserializeFromFloat_WithNewtonsoftJsonProvider() { var value = 123; - var vo = NewtonsoftJsonFloatVo.From(value); + var vo = NewtonsoftJsonFloatVo.Item1; var serializedFloat = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedFloat); @@ -111,7 +110,7 @@ public void CanDeserializeFromFloat_WithNewtonsoftJsonProvider() public void CanDeserializeFromFloat_WithSystemTextJsonProvider() { var value = 123; - var vo = SystemTextJsonFloatVo.From(value); + var vo = SystemTextJsonFloatVo.Item1; var serializedFloat = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedFloat); @@ -122,7 +121,7 @@ public void CanDeserializeFromFloat_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromFloat_WithSystemTextJsonProvider_treating_numbers_as_string() { - var vo = SystemTextJsonFloatVo_Treating_numbers_as_string.From(123); + var vo = SystemTextJsonFloatVo_Treating_numbers_as_string.Item1; var serializedFloat = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedFloat); @@ -133,7 +132,7 @@ public void CanDeserializeFromFloat_WithSystemTextJsonProvider_treating_numbers_ [Fact] public void CanSerializeToFloat_WithBothJsonConverters() { - var vo = BothJsonFloatVo.From(123.45f); + var vo = BothJsonFloatVo.Item2; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedFloat1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -148,7 +147,7 @@ public void CanSerializeToFloat_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonFloatVo.From(123); + var vo = NoJsonFloatVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -160,7 +159,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonFloatVo.From(123); + var vo = NoJsonFloatVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -172,7 +171,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterFloatVo.From(123); + var vo = NoConverterFloatVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -193,7 +192,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreFloatVo.From(123) }; + var original = new EfCoreTestEntity { Id = EfCoreFloatVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -218,7 +217,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT @Value", parameters); var value = Assert.Single(results); - Assert.Equal(DapperFloatVo.From(123.45f), value); + Assert.Equal(DapperFloatVo.Item2, value); } [Fact] @@ -227,7 +226,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbFloatVo.From(123) }; + var original = new LinqToDbTestEntity { Id = LinqToDbFloatVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -258,7 +257,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) var id = converter.ConvertFrom(null!, culture, value); Assert.IsType(id); - Assert.Equal(NoJsonFloatVo.From(123.45f), id); + Assert.Equal(NoJsonFloatVo.Item2, id); object reconverted = converter.ConvertTo(null, culture, id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs index f7693bdf..a954c2e5 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs @@ -8,53 +8,55 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; using LinqToDB.Mapping; -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(Guid))] - public partial struct AnotherGuidVo { } + [Intellenum(underlyingType: typeof(Guid))] + public partial class AnotherGuidVo + { + static AnotherGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + + } public class GuidVoTests { - public static readonly Guid _guid1 = Guid.NewGuid(); - public static readonly Guid _guid2 = Guid.NewGuid(); [Fact] public void equality_between_same_value_objects() { - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); + GuidVo.Item1.Equals(GuidVo.Item1).Should().BeTrue(); + (GuidVo.Item1 == GuidVo.Item1).Should().BeTrue(); - (GuidVo.From(_guid1) != GuidVo.From(_guid2)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid2)).Should().BeFalse(); + (GuidVo.Item1 != GuidVo.Item2).Should().BeTrue(); + (GuidVo.Item1 == GuidVo.Item2).Should().BeFalse(); - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); + GuidVo.Item1.Equals(GuidVo.Item1).Should().BeTrue(); + (GuidVo.Item1 == GuidVo.Item1).Should().BeTrue(); - var original = GuidVo.From(_guid1); - var other = GuidVo.From(_guid1); + var original = GuidVo.Item1; + var other = GuidVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - GuidVo.From(_guid1).Equals(AnotherGuidVo.From(_guid1)).Should().BeFalse(); - } - [Fact] public void CanSerializeToString_WithNewtonsoftJsonProvider() { - var g1 = NewtonsoftJsonGuidVo.From(_guid1); + var g1 = NewtonsoftJsonGuidVo.Item1; string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); @@ -65,7 +67,7 @@ public void CanSerializeToString_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonGuidVo.From(_guid1); + var vo = SystemTextJsonGuidVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); @@ -76,8 +78,8 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var value = _guid1; - var vo = NewtonsoftJsonGuidVo.From(value); + var value = NewtonsoftJsonGuidVo.Item1.Value; + var vo = NewtonsoftJsonGuidVo.Item1; var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); @@ -88,8 +90,8 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var value = _guid1; - var vo = SystemTextJsonGuidVo.From(value); + var value = NewtonsoftJsonGuidVo.Item1.Value; + var vo = SystemTextJsonGuidVo.Item1; var serializedString = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); @@ -100,7 +102,7 @@ public void CanDeserializeFromString_WithSystemTextJsonProvider() [Fact] public void CanSerializeToString_WithBothJsonConverters() { - var vo = BothJsonGuidVo.From(_guid1); + var vo = BothJsonGuidVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -115,7 +117,7 @@ public void CanSerializeToString_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonGuidVo.From(_guid1); + var vo = NoJsonGuidVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -127,7 +129,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonGuidVo.From(_guid1); + var vo = NoJsonGuidVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -139,7 +141,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterGuidVo.From(_guid1); + var vo = NoConverterGuidVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -160,7 +162,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreGuidVo.From(_guid1) }; + var original = new EfCoreTestEntity { Id = EfCoreGuidVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -181,10 +183,10 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT '5640dad4-862a-4738-9e3c-c76dc227eb66'"); + IEnumerable results = await connection.QueryAsync("SELECT '00000000-0000-0000-0000-000000000001'"); var value = Assert.Single(results); - Assert.Equal(value, DapperGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66"))); + Assert.Equal(value, DapperGuidVo.Item1); } [Fact] @@ -193,7 +195,10 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66")) }; + var original = new LinqToDbTestEntity + { + Id = LinqToDbGuidVo.Item1 + }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -213,17 +218,16 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } } - [Theory] - [InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")] - public void TypeConverter_CanConvertToAndFrom(string value) + [Fact] + public void TypeConverter_CanConvertToAndFrom() { var converter = TypeDescriptor.GetConverter(typeof(NoJsonGuidVo)); - var id = converter.ConvertFrom(value); + var id = converter.ConvertFrom("00000000-0000-0000-0000-000000000001"); Assert.IsType(id); - Assert.Equal(NoJsonGuidVo.From(Guid.Parse(value)), id); + Assert.Equal(NoJsonGuidVo.Item1, id); - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); + var reconverted = converter.ConvertTo(id, typeof(string)); + Assert.Equal("00000000-0000-0000-0000-000000000001", reconverted); } public class TestDbContext : DbContext diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs index d462e0b2..dcb61a88 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs @@ -8,51 +8,49 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; using LinqToDB.Mapping; -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(int))] - public partial struct AnotherIntVo { } + [Intellenum(underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + public partial class AnotherIntVo { } public class IntVoTests { [Fact] public void equality_between_same_value_objects() { - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); + IntVo.Item1.Equals(IntVo.Item1).Should().BeTrue(); + (IntVo.Item1 == IntVo.Item1).Should().BeTrue(); - (IntVo.From(18) != IntVo.From(19)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(19)).Should().BeFalse(); + (IntVo.Item1 != IntVo.Item2).Should().BeTrue(); + (IntVo.Item1 == IntVo.Item2).Should().BeFalse(); - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); + IntVo.Item1.Equals(IntVo.Item1).Should().BeTrue(); + (IntVo.Item1 == IntVo.Item1).Should().BeTrue(); - var original = IntVo.From(18); - var other = IntVo.From(18); + var original = IntVo.Item1; + var other = IntVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - IntVo.From(18).Equals(AnotherIntVo.From(18)).Should().BeFalse(); - } - [Fact] public void CanSerializeToInt_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonIntVo.From(123); + var vo = NewtonsoftJsonIntVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedInt = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -63,7 +61,7 @@ public void CanSerializeToInt_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToInt_WithSystemTextJsonProvider() { - var vo = SystemTextJsonIntVo.From(123); + var vo = SystemTextJsonIntVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedInt = SystemTextJsonSerializer.Serialize(vo.Value); @@ -75,7 +73,7 @@ public void CanSerializeToInt_WithSystemTextJsonProvider() public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() { var value = 123; - var vo = NewtonsoftJsonIntVo.From(value); + var vo = NewtonsoftJsonIntVo.Item1; var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedInt); @@ -87,7 +85,7 @@ public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() public void CanDeserializeFromInt_WithSystemTextJsonProvider() { var value = 123; - var vo = SystemTextJsonIntVo.From(value); + var vo = SystemTextJsonIntVo.Item1; var serializedInt = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedInt); @@ -98,7 +96,7 @@ public void CanDeserializeFromInt_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromInt_WithSystemTextJsonProvider_treating_numbers_as_string() { - var vo = SystemTextJsonIntVo_Treating_numbers_as_string.From(123); + var vo = SystemTextJsonIntVo_Treating_numbers_as_string.Item1; var serializedInt = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedInt); @@ -109,7 +107,7 @@ public void CanDeserializeFromInt_WithSystemTextJsonProvider_treating_numbers_as [Fact] public void CanSerializeToInt_WithBothJsonConverters() { - var vo = BothJsonIntVo.From(123); + var vo = BothJsonIntVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedInt1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -124,7 +122,7 @@ public void CanSerializeToInt_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonIntVo.From(123); + var vo = NoJsonIntVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -136,7 +134,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonIntVo.From(123); + var vo = NoJsonIntVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -148,7 +146,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterIntVo.From(123); + var vo = NoConverterIntVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -169,7 +167,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreIntVo.From(123) }; + var original = new EfCoreTestEntity { Id = EfCoreIntVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -193,7 +191,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT 123"); var value = Assert.Single(results); - Assert.Equal(DapperIntVo.From(123), value); + Assert.Equal(DapperIntVo.Item1, value); } [Fact] @@ -202,7 +200,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbIntVo.From(123) }; + var original = new LinqToDbTestEntity { Id = LinqToDbIntVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -230,7 +228,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) var converter = TypeDescriptor.GetConverter(typeof(NoJsonIntVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonIntVo.From(123), id); + Assert.Equal(NoJsonIntVo.Item1, id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs index 3a96f811..615daacd 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs @@ -8,51 +8,49 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; using LinqToDB.Mapping; -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(long))] - public partial struct AnotherLongVo { } + [Intellenum(underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + public partial class AnotherLongVo { } public class LongVoTests { [Fact] public void equality_between_same_value_objects() { - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); + LongVo.Item1.Equals(LongVo.Item1).Should().BeTrue(); + (LongVo.Item1 == LongVo.Item1).Should().BeTrue(); - (LongVo.From(18) != LongVo.From(19)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(19)).Should().BeFalse(); + (LongVo.Item1 != LongVo.Item2).Should().BeTrue(); + (LongVo.Item1 == LongVo.Item2).Should().BeFalse(); - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); + LongVo.Item1.Equals(LongVo.Item1).Should().BeTrue(); + (LongVo.Item1 == LongVo.Item1).Should().BeTrue(); - var original = LongVo.From(18); - var other = LongVo.From(18); + var original = LongVo.Item1; + var other = LongVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - LongVo.From(18).Equals(AnotherLongVo.From(18)).Should().BeFalse(); - } - [Fact] public void CanSerializeToLong_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonLongVo.From(123L); + var vo = NewtonsoftJsonLongVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -63,7 +61,7 @@ public void CanSerializeToLong_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToLong_WithSystemTextJsonProvider() { - var vo = SystemTextJsonLongVo.From(123L); + var vo = SystemTextJsonLongVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); @@ -75,7 +73,7 @@ public void CanSerializeToLong_WithSystemTextJsonProvider() public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() { var value = 123L; - var vo = NewtonsoftJsonLongVo.From(value); + var vo = NewtonsoftJsonLongVo.Item1; var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); @@ -86,7 +84,7 @@ public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromLong_WithSystemTextJsonProvider() { - var vo = SystemTextJsonLongVo.From(123L); + var vo = SystemTextJsonLongVo.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); @@ -97,7 +95,7 @@ public void CanDeserializeFromLong_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromLong_WithSystemTextJsonProvider_treating_numbers_as_string() { - var vo = SystemTextJsonLongVo_Treating_numbers_as_string.From(123L); + var vo = SystemTextJsonLongVo_Treating_numbers_as_string.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); @@ -108,7 +106,7 @@ public void CanDeserializeFromLong_WithSystemTextJsonProvider_treating_numbers_a [Fact] public void CanSerializeToLong_WithBothJsonConverters() { - var vo = BothJsonLongVo.From(123L); + var vo = BothJsonLongVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -123,7 +121,7 @@ public void CanSerializeToLong_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonLongVo.From(123L); + var vo = NoJsonLongVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -135,7 +133,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonLongVo.From(123L); + var vo = NoJsonLongVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -147,7 +145,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterLongVo.From(123L); + var vo = NoConverterLongVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -168,7 +166,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreLongVo.From(123L) }; + var original = new EfCoreTestEntity { Id = EfCoreLongVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -192,7 +190,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() IEnumerable results = await connection.QueryAsync("SELECT 123"); var value = Assert.Single(results); - Assert.Equal(DapperLongVo.From(123L), value); + Assert.Equal(DapperLongVo.Item1, value); } [Fact] @@ -201,7 +199,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbLongVo.From(123) }; + var original = new LinqToDbTestEntity { Id = LinqToDbLongVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -229,7 +227,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) var converter = TypeDescriptor.GetConverter(typeof(NoJsonLongVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonLongVo.From(123L), id); + Assert.Equal(NoJsonLongVo.Item1, id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs index 302eb085..0717d214 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs @@ -8,51 +8,57 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; using LinqToDB.Mapping; -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(short))] - public partial struct AnotherShortVo { } + [Intellenum(underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] + public partial class AnotherShortVo { } public class ShortVoTests { [Fact] - public void equality_between_same_value_objects() + public void equality_between_same_enums() { - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); + ShortVo.Item1.Equals(ShortVo.Item1).Should().BeTrue(); + (ShortVo.Item1 == ShortVo.Item1).Should().BeTrue(); - (ShortVo.From(18) != ShortVo.From(19)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(19)).Should().BeFalse(); + (ShortVo.Item1 != ShortVo.Item2).Should().BeTrue(); + (ShortVo.Item1 == ShortVo.Item2).Should().BeFalse(); - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); + ShortVo.Item1.Equals(ShortVo.Item1).Should().BeTrue(); + (ShortVo.Item1 == ShortVo.Item1).Should().BeTrue(); - var original = ShortVo.From(18); - var other = ShortVo.From(18); + var original = ShortVo.Item1; + var other = ShortVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } [Fact] - public void equality_between_different_value_objects() + public void equality_between_different_enums() { - ShortVo.From(18).Equals(AnotherShortVo.From(18)).Should().BeFalse(); + // the implicit cast to short means this is true + ShortVo.Item1.Equals(AnotherShortVo.Item1).Should().BeTrue(); } [Fact] public void CanSerializeToShort_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonShortVo.From(123); + var vo = NewtonsoftJsonShortVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -63,7 +69,7 @@ public void CanSerializeToShort_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToShort_WithSystemTextJsonProvider() { - var vo = SystemTextJsonShortVo.From(123); + var vo = SystemTextJsonShortVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); @@ -74,8 +80,8 @@ public void CanSerializeToShort_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() { - short value = 123; - var vo = NewtonsoftJsonShortVo.From(value); + short value = 1; + var vo = NewtonsoftJsonShortVo.Item1; var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); @@ -86,8 +92,8 @@ public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromShort_WithSystemTextJsonProvider() { - short value = 123; - var vo = SystemTextJsonShortVo.From(value); + short value = 1; + var vo = SystemTextJsonShortVo.Item1; var serializedShort = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); @@ -98,7 +104,7 @@ public void CanDeserializeFromShort_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromShort_WithSystemTextJsonProvider_treating_numbers_as_string() { - var vo = SystemTextJsonShortVo_Treating_numbers_as_string.From((short) 123); + var vo = SystemTextJsonShortVo_Treating_numbers_as_string.Item1; var serializedShort = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); @@ -109,7 +115,7 @@ public void CanDeserializeFromShort_WithSystemTextJsonProvider_treating_numbers_ [Fact] public void CanSerializeToShort_WithBothJsonConverters() { - var vo = BothJsonShortVo.From(123); + var vo = BothJsonShortVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -124,7 +130,7 @@ public void CanSerializeToShort_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonShortVo.From(123); + var vo = NoJsonShortVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -136,7 +142,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonShortVo.From(123); + var vo = NoJsonShortVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -148,7 +154,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterShortVo.From(123); + var vo = NoConverterShortVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -169,7 +175,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreShortVo.From(123) }; + var original = new EfCoreTestEntity { Id = EfCoreShortVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -190,10 +196,10 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT 123"); + IEnumerable results = await connection.QueryAsync("SELECT 1"); var value = Assert.Single(results); - Assert.Equal(DapperShortVo.From(123), value); + Assert.Equal(DapperShortVo.Item1, value); } [Fact] @@ -202,7 +208,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbShortVo.From(123) }; + var original = new LinqToDbTestEntity { Id = LinqToDbShortVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -223,14 +229,14 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData((short)123)] - [InlineData("123")] + [InlineData((short)1)] + [InlineData("1")] public void TypeConverter_CanConvertToAndFrom(object value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonShortVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonShortVo.From(123), id); + Assert.Equal(NoJsonShortVo.Item1, id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs index a033e0e8..e96e0131 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs @@ -8,51 +8,49 @@ using FluentAssertions; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; using LinqToDB.Mapping; -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +// ReSharper disable EqualExpressionComparison +#pragma warning disable 1718 + +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(string))] - public partial struct AnotherStringVo { } + [Intellenum(underlyingType: typeof(string))] + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] + public partial class AnotherStringVo { } public class StringVoTests { [Fact] public void equality_between_same_value_objects() { - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); + StringVo.Item1.Equals(StringVo.Item1).Should().BeTrue(); + (StringVo.Item1 == StringVo.Item1).Should().BeTrue(); - (StringVo.From("hello!") != StringVo.From("world!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("world!")).Should().BeFalse(); + (StringVo.Item1 != StringVo.Item2).Should().BeTrue(); + (StringVo.Item1 == StringVo.Item2).Should().BeFalse(); - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); + StringVo.Item1.Equals(StringVo.Item1).Should().BeTrue(); + (StringVo.Item1 == StringVo.Item1).Should().BeTrue(); - var original = StringVo.From("hello!"); - var other = StringVo.From("hello!"); + var original = StringVo.Item1; + var other = StringVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - StringVo.From("hello!").Equals(AnotherStringVo.From("hello!")).Should().BeFalse(); - } - [Fact] public void CanSerializeToString_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonStringVo.From("foo!"); + var vo = NewtonsoftJsonStringVo.Item1; string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); string serializedString = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -63,7 +61,7 @@ public void CanSerializeToString_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonStringVo.From("foo!"); + var vo = SystemTextJsonStringVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); @@ -74,8 +72,8 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var value = "foo!"; - var vo = NewtonsoftJsonStringVo.From(value); + var value = "Item1!"; + var vo = NewtonsoftJsonStringVo.Item1; var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); @@ -86,8 +84,8 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var value = "foo!"; - var vo = SystemTextJsonStringVo.From(value); + var value = "Item1!"; + var vo = SystemTextJsonStringVo.Item1; var serializedString = SystemTextJsonSerializer.Serialize(value); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); @@ -98,7 +96,7 @@ public void CanDeserializeFromString_WithSystemTextJsonProvider() [Fact] public void CanSerializeToString_WithBothJsonConverters() { - var vo = BothJsonStringVo.From("foo!"); + var vo = BothJsonStringVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -113,7 +111,7 @@ public void CanSerializeToString_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonStringVo.From("foo!"); + var vo = NoJsonStringVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); @@ -125,7 +123,7 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonStringVo.From("foo!"); + var vo = NoJsonStringVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); @@ -137,7 +135,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterStringVo.From("foo!"); + var vo = NoConverterStringVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); @@ -158,7 +156,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreStringVo.From("foo!") }; + var original = new EfCoreTestEntity { Id = EfCoreStringVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -179,10 +177,10 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT 'foo!'"); + IEnumerable results = await connection.QueryAsync("SELECT 'Item1!'"); var value = Assert.Single(results); - Assert.Equal(DapperStringVo.From("foo!"), value); + Assert.Equal(DapperStringVo.Item1, value); } [Fact] @@ -191,7 +189,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbStringVo.From("foo!") }; + var original = new LinqToDbTestEntity { Id = LinqToDbStringVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -212,14 +210,14 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData("")] - [InlineData("123")] + [InlineData("Item1!")] + [InlineData("Item2!")] public void TypeConverter_CanConvertToAndFrom(object value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonStringVo)); var id = converter.ConvertFrom(value); Assert.IsType(id); - Assert.Equal(NoJsonStringVo.From(value!.ToString()), id); + Assert.Equal(NoJsonStringVo.FromValue(value!.ToString()), id); var reconverted = converter.ConvertTo(id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs index 6fff5120..3116eaa4 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs @@ -14,7 +14,7 @@ using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.ClassVos; +using Intellenum.IntegrationTests.TestTypes.ClassVos; using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider.SQLite; @@ -26,46 +26,45 @@ // ReSharper disable PropertyCanBeMadeInitOnly.Global // ReSharper disable SuspiciousTypeConversion.Global -namespace Vogen.IntegrationTests.SerializationAndConversionTests.ClassVos +namespace Intellenum.IntegrationTests.SerializationAndConversionTests.ClassVos { - [ValueObject(underlyingType: typeof(TimeOnly))] - public readonly partial struct AnotherTimeOnlyVo { } + [Intellenum(underlyingType: typeof(TimeOnly))] + public partial class AnotherTimeOnlyVo + { + static AnotherTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + + } public class TimeOnlyVoTests { - private static readonly TimeOnly _time1 = new TimeOnly(13, 12, 59, 123); - private static readonly TimeOnly _time2 = new TimeOnly(1, 59, 58, 123); - [Fact] public void equality_between_same_value_objects() { - TimeOnlyVo.From(_time1).Equals(TimeOnlyVo.From(_time1)).Should().BeTrue(); - (TimeOnlyVo.From(_time1) == TimeOnlyVo.From(_time1)).Should().BeTrue(); + TimeOnlyVo.Item1.Equals(TimeOnlyVo.Item1).Should().BeTrue(); + (TimeOnlyVo.Item1 == TimeOnlyVo.Item1).Should().BeTrue(); - (TimeOnlyVo.From(_time1) != TimeOnlyVo.From(_time2)).Should().BeTrue(); - (TimeOnlyVo.From(_time1) == TimeOnlyVo.From(_time2)).Should().BeFalse(); + (TimeOnlyVo.Item1 != TimeOnlyVo.Item2).Should().BeTrue(); + (TimeOnlyVo.Item1 == TimeOnlyVo.Item2).Should().BeFalse(); - TimeOnlyVo.From(_time1).Equals(TimeOnlyVo.From(_time1)).Should().BeTrue(); - (TimeOnlyVo.From(_time1) == TimeOnlyVo.From(_time1)).Should().BeTrue(); + TimeOnlyVo.Item1.Equals(TimeOnlyVo.Item1).Should().BeTrue(); + (TimeOnlyVo.Item1 == TimeOnlyVo.Item1).Should().BeTrue(); - var original = TimeOnlyVo.From(_time1); - var other = TimeOnlyVo.From(_time1); + var original = TimeOnlyVo.Item1; + var other = TimeOnlyVo.Item1; ((original as IEquatable).Equals(other)).Should().BeTrue(); ((other as IEquatable).Equals(original)).Should().BeTrue(); } - [Fact] - public void equality_between_different_value_objects() - { - TimeOnlyVo.From(_time1).Equals(AnotherTimeOnlyVo.From(_time1)).Should().BeFalse(); - } - #if NET7_0_OR_GREATER [Fact] public void CanSerializeToString_WithNewtonsoftJsonProvider() { - var g1 = NewtonsoftJsonTimeOnlyVo.From(_time1); + var g1 = NewtonsoftJsonTimeOnlyVo.Item1; string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); @@ -76,7 +75,7 @@ public void CanSerializeToString_WithNewtonsoftJsonProvider() [Fact] public void CanSerializeToString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonTimeOnlyVo.From(_time1); + var vo = SystemTextJsonTimeOnlyVo.Item1; string serializedVo = SystemTextJsonSerializer.Serialize(vo); string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); @@ -87,9 +86,8 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var value = _time1; - var vo = NewtonsoftJsonTimeOnlyVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); + var vo = NewtonsoftJsonTimeOnlyVo.Item1; + var serializedString = NewtonsoftJsonSerializer.SerializeObject(NewtonsoftJsonTimeOnlyVo.Item1.Value.ToString()); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); @@ -99,9 +97,8 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var value = _time1; - var vo = SystemTextJsonTimeOnlyVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); + var vo = SystemTextJsonTimeOnlyVo.Item1; + var serializedString = SystemTextJsonSerializer.Serialize(SystemTextJsonTimeOnlyVo.Item1.Value.ToString()); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); @@ -111,7 +108,7 @@ public void CanDeserializeFromString_WithSystemTextJsonProvider() [Fact] public void CanSerializeToString_WithBothJsonConverters() { - var vo = BothJsonTimeOnlyVo.From(_time1); + var vo = BothJsonTimeOnlyVo.Item1; var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); @@ -126,11 +123,11 @@ public void CanSerializeToString_WithBothJsonConverters() [Fact] public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() { - var vo = NoJsonTimeOnlyVo.From(_time1); + var vo = NoJsonTimeOnlyVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _time1.ToString("o") + "\"}"; + var expected = "{\"Value\":\"" + NoJsonTimeOnlyVo.Item1.Value.ToString("o") + "\"}"; serialized.Should().Be(expected); } @@ -138,11 +135,11 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { - var vo = NoJsonTimeOnlyVo.From(_time1); + var vo = NoJsonTimeOnlyVo.Item1; var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - var expected = $"\"{_time1:o}\""; + var expected = $"\"{NoJsonTimeOnlyVo.Item1.Value:o}\""; Assert.Equal(expected, serialized); } @@ -150,12 +147,12 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() [Fact] public void WhenNoTypeConverter_SerializesWithValueProperty() { - var vo = NoConverterTimeOnlyVo.From(_time1); + var vo = NoConverterTimeOnlyVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _time1.ToString("o") + "\"}"; + var expected = "{\"Value\":\"" + NoConverterTimeOnlyVo.Item1.Value.ToString("o") + "\"}"; newtonsoft.Should().Be(expected); systemText.Should().Be(expected); @@ -172,7 +169,7 @@ public void WhenEfCoreValueConverterUsesValueConverter() .UseSqlite(connection) .Options; - var original = new EfCoreTestEntity { Id = EfCoreTimeOnlyVo.From(_time1) }; + var original = new EfCoreTestEntity { Id = EfCoreTimeOnlyVo.Item1 }; using (var context = new TestDbContext(options)) { context.Database.EnsureCreated(); @@ -193,11 +190,11 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT '13:12:11.999'"); + IEnumerable results = await connection.QueryAsync("SELECT '05:06:07.08'"); DapperTimeOnlyVo actual = Assert.Single(results); - var expected = DapperTimeOnlyVo.From(new TimeOnly(13,12,11, 999)); + var expected = DapperTimeOnlyVo.Item2; actual.Should().Be(expected); } @@ -207,7 +204,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - var original = new LinqToDbTestEntity { Id = LinqToDbTimeOnlyVo.From(_time1) }; + var original = new LinqToDbTestEntity { Id = LinqToDbTimeOnlyVo.Item1 }; using (var context = new DataConnection( SQLiteTools.GetDataProvider("SQLite.MS"), connection, @@ -228,7 +225,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData("13:12:11.123")] + [InlineData("05:06:07.08")] public void TypeConverter_CanConvertToAndFrom(string value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonTimeOnlyVo)); @@ -236,7 +233,7 @@ public void TypeConverter_CanConvertToAndFrom(string value) Assert.IsType(voAsObject); - NoJsonTimeOnlyVo expected = NoJsonTimeOnlyVo.From(TimeOnly.Parse(value, CultureInfo.InvariantCulture)); + NoJsonTimeOnlyVo expected = NoJsonTimeOnlyVo.Item2; Assert.Equal(expected, voAsObject); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/BoolVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/BoolVo.cs index 2dc05ac6..c2866008 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/BoolVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/BoolVo.cs @@ -1,29 +1,47 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class BoolVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class NoConverterBoolVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class NoJsonBoolVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class NewtonsoftJsonBoolVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class SystemTextJsonBoolVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class BothJsonBoolVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class EfCoreBoolVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class DapperBoolVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(bool))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] public partial class LinqToDbBoolVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ByteVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ByteVo.cs index af28d48d..bdd3e39c 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ByteVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ByteVo.cs @@ -1,32 +1,52 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class ByteVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoConverterByteVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoJsonByteVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NewtonsoftJsonByteVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonByteVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(byte), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(byte), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonByteVo_Treating_numbers_as_string { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class BothJsonByteVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class EfCoreByteVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class DapperByteVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(byte))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(byte))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class LinqToDbByteVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/CharVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/CharVo.cs index 3f9767aa..b618ad58 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/CharVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/CharVo.cs @@ -1,29 +1,47 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class CharVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class NoConverterCharVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class NoJsonCharVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class NewtonsoftJsonCharVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class SystemTextJsonCharVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class BothJsonCharVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class EfCoreCharVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class DapperCharVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(char))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(char))] + [Instance("A", 'a')] + [Instance("B", 'b')] public partial class LinqToDbCharVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateOnlyVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateOnlyVo.cs index 7705816d..4ea3ae85 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateOnlyVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateOnlyVo.cs @@ -2,34 +2,98 @@ using System; -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateOnly))] - public partial class DateOnlyVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateOnly))] + public partial class DateOnlyVo + { + static DateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateOnly))] - public partial class NoConverterDateOnlyVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateOnly))] + public partial class NoConverterDateOnlyVo + { + static NoConverterDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateOnly))] - public partial class NoJsonDateOnlyVo { } + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(DateOnly))] + public partial class NoJsonDateOnlyVo + { + static NoJsonDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateOnly))] - public partial class NewtonsoftJsonDateOnlyVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateOnly))] + public partial class NewtonsoftJsonDateOnlyVo + { + static NewtonsoftJsonDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateOnly))] - public partial class SystemTextJsonDateOnlyVo { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateOnly))] + public partial class SystemTextJsonDateOnlyVo + { + static SystemTextJsonDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateOnly))] - public partial class BothJsonDateOnlyVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateOnly))] + public partial class BothJsonDateOnlyVo + { + static BothJsonDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateOnly))] - public partial class EfCoreDateOnlyVo { } + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateOnly))] + public partial class EfCoreDateOnlyVo + { + static EfCoreDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateOnly))] - public partial class DapperDateOnlyVo { } + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateOnly))] + public partial class DapperDateOnlyVo + { + static DapperDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateOnly))] - public partial class LinqToDbDateOnlyVo { } + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateOnly))] + public partial class LinqToDbDateOnlyVo + { + static LinqToDbDateOnlyVo() + { + Instance("JanFirst", new DateOnly(2021, 1, 1)); + Instance("JanSecond", new DateOnly(2021, 1, 2)); + } + } } #endif diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeOffsetVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeOffsetVo.cs index 5de696e6..a1b89e35 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeOffsetVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeOffsetVo.cs @@ -1,31 +1,96 @@ using System; -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial class DateTimeOffsetVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] + public partial class DateTimeOffsetVo + { + static DateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + } + } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial class NoConverterDateTimeOffsetVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] + public partial class NoConverterDateTimeOffsetVo + { + static NoConverterDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + } + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTimeOffset))] - public partial class NoJsonDateTimeOffsetVo { } + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTimeOffset))] + public partial class NoJsonDateTimeOffsetVo + { + static NoJsonDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + Instance("SomethingElse", new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTimeOffset))] - public partial class NewtonsoftJsonDateTimeOffsetVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTimeOffset))] + public partial class NewtonsoftJsonDateTimeOffsetVo + { + static NewtonsoftJsonDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial class SystemTextJsonDateTimeOffsetVo { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] + public partial class SystemTextJsonDateTimeOffsetVo + { + static SystemTextJsonDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial class BothJsonDateTimeOffsetVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] + public partial class BothJsonDateTimeOffsetVo + { + static BothJsonDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + } + } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial class EfCoreDateTimeOffsetVo { } + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTimeOffset))] + public partial class EfCoreDateTimeOffsetVo + { + static EfCoreDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + } + } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTimeOffset))] - public partial class DapperDateTimeOffsetVo { } + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTimeOffset))] + public partial class DapperDateTimeOffsetVo + { + static DapperDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + Instance("SomethingElse", new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); + } + } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial class LinqToDbDateTimeOffsetVo { } -} + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTimeOffset))] + public partial class LinqToDbDateTimeOffsetVo + { + static LinqToDbDateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + } + } +} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs index 55aea574..4d5fade8 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs @@ -1,31 +1,95 @@ using System; -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial class DateTimeVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateTime))] + public partial class DateTimeVo + { + static DateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial class NoConverterDateTimeVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(DateTime))] + public partial class NoConverterDateTimeVo + { + static NoConverterDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTime))] - public partial class NoJsonDateTimeVo { } + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTime))] + public partial class NoJsonDateTimeVo + { + static NoJsonDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTime))] - public partial class NewtonsoftJsonDateTimeVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTime))] + public partial class NewtonsoftJsonDateTimeVo + { + static NewtonsoftJsonDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial class SystemTextJsonDateTimeVo { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTime))] + public partial class SystemTextJsonDateTimeVo + { + static SystemTextJsonDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial class BothJsonDateTimeVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTime))] + public partial class BothJsonDateTimeVo + { + static BothJsonDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTime))] - public partial class EfCoreDateTimeVo { } + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTime))] + public partial class EfCoreDateTimeVo + { + static EfCoreDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTime))] - public partial class DapperDateTimeVo { } + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTime))] + public partial class DapperDateTimeVo + { + static DapperDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + Instance("SomethingElse", new DateTime(2022,01,15,19,08,49,DateTimeKind.Utc).AddTicks(5413764)); + } + } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTime))] - public partial class LinqToDbDateTimeVo { } -} + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTime))] + public partial class LinqToDbDateTimeVo + { + static LinqToDbDateTimeVo() + { + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + } + } +} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DecimalVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DecimalVo.cs index da762a21..d9d00a81 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DecimalVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DecimalVo.cs @@ -1,32 +1,103 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial class DecimalVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(decimal))] + public partial class DecimalVo + { + static DecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial class NoConverterDecimalVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(decimal))] + public partial class NoConverterDecimalVo + { + static NoConverterDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(decimal))] - public partial class NoJsonDecimalVo { } + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(decimal))] + public partial class NoJsonDecimalVo + { + static NoJsonDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(decimal))] - public partial class NewtonsoftJsonDecimalVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(decimal))] + public partial class NewtonsoftJsonDecimalVo + { + static NewtonsoftJsonDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial class SystemTextJsonDecimalVo { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(decimal))] + public partial class SystemTextJsonDecimalVo + { + static SystemTextJsonDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(decimal), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] - public partial class SystemTextJsonDecimalVo_Treating_number_as_string { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(decimal), + customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + public partial class SystemTextJsonDecimalVo_Treating_number_as_string + { + static SystemTextJsonDecimalVo_Treating_number_as_string() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial class BothJsonDecimalVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(decimal))] + public partial class BothJsonDecimalVo + { + static BothJsonDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(decimal))] - public partial class EfCoreDecimalVo { } + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(decimal))] + public partial class EfCoreDecimalVo + { + static EfCoreDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(decimal))] - public partial class DapperDecimalVo { } + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(decimal))] + public partial class DapperDecimalVo + { + static DapperDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(decimal))] - public partial class LinqToDbDecimalVo { } + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(decimal))] + public partial class LinqToDbDecimalVo + { + static LinqToDbDecimalVo() + { + Instance("Item1", 1.1m); + Instance("Item2", 2.2m); + } + } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DoubleVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DoubleVo.cs index 4629e041..241868f3 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DoubleVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DoubleVo.cs @@ -1,32 +1,55 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] - public partial class DoubleVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] + public partial class DoubleVo + { + + } + + [Intellenum(conversions: Conversions.None, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class NoConverterDoubleVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class NoJsonDoubleVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class NewtonsoftJsonDoubleVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class SystemTextJsonDoubleVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(double), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(double), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class SystemTextJsonDoubleVo_number_as_string { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class BothJsonDoubleVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class EfCoreDoubleVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class DapperDoubleVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(double))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(double))] + [Instance("Item1", 1.1d)] + [Instance("Item2", 2.2d)] public partial class LinqToDbDoubleVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FloatVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FloatVo.cs index 156dd7d4..0f204a9b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FloatVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FloatVo.cs @@ -1,32 +1,52 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class FloatVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class NoConverterFloatVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class NoJsonFloatVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class NewtonsoftJsonFloatVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class SystemTextJsonFloatVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(float), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(float), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class SystemTextJsonFloatVo_Treating_numbers_as_string { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class BothJsonFloatVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class EfCoreFloatVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class DapperFloatVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(float))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(float))] + [Instance("Item1", 1.1f)] + [Instance("Item2", 2.2f)] public partial class LinqToDbFloatVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FooVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FooVo.cs index a9185adc..91b89b5f 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FooVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/FooVo.cs @@ -1,40 +1,109 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos -{ - public record struct Bar(int Age, string Name); - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial class FooVo { } +using System; - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial class NoConverterFooVo { } +namespace Intellenum.IntegrationTests.TestTypes.ClassVos +{ + public record struct Bar(int Age, string Name) : IComparable + { + public int CompareTo(Bar other) => Age.CompareTo(other.Age); + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial class NoJsonFooVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(Bar))] + public partial class FooVo + { + static FooVo() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial class NoJsonFooVoClass { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(Bar))] + public partial class NoConverterFooVo + { + static NoConverterFooVo() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial class NewtonsoftJsonFooVo { } + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] + public partial class NoJsonFooVo + { + static NoJsonFooVo() + { + Instance("Item1", new NoJsonFooVo(new Bar(42, "Fred"))); + Instance("Item2", new NoJsonFooVo(new Bar(2, "Two"))); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial class NewtonsoftJsonFooVoClass { } + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] + public partial class NewtonsoftJsonFooVo + { + static NewtonsoftJsonFooVo() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial class SystemTextJsonFooVo { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(Bar))] + public partial class SystemTextJsonFooVo + { + static SystemTextJsonFooVo() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial class BothJsonFooVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] + public partial class BothJsonFooVo + { + static BothJsonFooVo() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial class BothJsonFooVoClass { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] + public partial class BothJsonFooVoClass + { + static BothJsonFooVoClass() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Bar))] - public partial class EfCoreFooVo { } + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Bar))] + public partial class EfCoreFooVo + { + static EfCoreFooVo() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Bar))] - public partial class DapperFooVo { } + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Bar))] + public partial class DapperFooVo + { + static DapperFooVo() + { + Instance("Item1", new DapperFooVo(new Bar(42, "Fred"))); + Instance("Item2", new DapperFooVo(new Bar(2, "Two"))); + } + } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Bar))] - public partial class LinqToDbFooVo { } + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Bar))] + public partial class LinqToDbFooVo + { + static LinqToDbFooVo() + { + Instance("Item1", new Bar(1, "One")); + Instance("Item2", new Bar(2, "Two")); + } + } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/GuidVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/GuidVo.cs index 1ada0ebd..681bd213 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/GuidVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/GuidVo.cs @@ -1,31 +1,96 @@ using System; -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial class GuidVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(Guid))] + public partial class GuidVo + { + static GuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial class NoConverterGuidVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(Guid))] + public partial class NoConverterGuidVo + { + static NoConverterGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Guid))] - public partial class NoJsonGuidVo { } + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(Guid))] + public partial class NoJsonGuidVo + { + static NoJsonGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Guid))] - public partial class NewtonsoftJsonGuidVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Guid))] + public partial class NewtonsoftJsonGuidVo + { + static NewtonsoftJsonGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial class SystemTextJsonGuidVo { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(Guid))] + public partial class SystemTextJsonGuidVo + { + static SystemTextJsonGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial class BothJsonGuidVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Guid))] + public partial class BothJsonGuidVo + { + static BothJsonGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Guid))] - public partial class EfCoreGuidVo { } + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Guid))] + public partial class EfCoreGuidVo + { + static EfCoreGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Guid))] - public partial class DapperGuidVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Guid))] - public partial class LinqToDbGuidVo { } + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Guid))] + public partial class DapperGuidVo + { + static DapperGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + + } + + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Guid))] + public partial class LinqToDbGuidVo + { + static LinqToDbGuidVo() + { + Instance("Item1", new Guid("00000000-0000-0000-0000-000000000001")); + Instance("Item2", new Guid("00000000-0000-0000-0000-000000000002")); + } + } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/IntoVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/IntoVo.cs index 8b56cf63..ef823bb7 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/IntoVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/IntoVo.cs @@ -1,32 +1,52 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class IntVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoConverterIntVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoJsonIntVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NewtonsoftJsonIntVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonIntVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(int), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(int), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonIntVo_Treating_numbers_as_string { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class BothJsonIntVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class EfCoreIntVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class DapperIntVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(int))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(int))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class LinqToDbIntVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/LongVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/LongVo.cs index 6545c459..3514315f 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/LongVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/LongVo.cs @@ -1,32 +1,52 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class LongVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoConverterLongVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoJsonLongVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NewtonsoftJsonLongVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonLongVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(long), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(long), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonLongVo_Treating_numbers_as_string { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class BothJsonLongVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class EfCoreLongVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class DapperLongVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(long))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(long))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class LinqToDbLongVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ShortVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ShortVo.cs index 1769be3e..82bff771 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ShortVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/ShortVo.cs @@ -1,32 +1,52 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class ShortVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoConverterShortVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NoJsonShortVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class NewtonsoftJsonShortVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonShortVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(short), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(short), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class SystemTextJsonShortVo_Treating_numbers_as_string { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class BothJsonShortVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class EfCoreShortVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class DapperShortVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(short))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(short))] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class LinqToDbShortVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/StringVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/StringVo.cs index 7eb72e6c..89b0013b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/StringVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/StringVo.cs @@ -1,29 +1,47 @@ -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(string))] + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] public partial class StringVo { } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.None, underlyingType: typeof(string))] + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] public partial class NoConverterStringVo { } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(string))] + [Instance("Item1", "Item1!")] + [Instance("Item2", "Item2!")] public partial class NoJsonStringVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(string))] + [Instance("Item1", "Item1!")] + [Instance("Item2", "Item2!")] public partial class NewtonsoftJsonStringVo { } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(string))] + [Instance("Item1", "Item1!")] + [Instance("Item2", "Item2!")] public partial class SystemTextJsonStringVo { } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(string))] + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] public partial class BothJsonStringVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(string))] + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] public partial class EfCoreStringVo { } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(string))] + [Instance("Item1", "Item1!")] + [Instance("Item2", "Item2!")] public partial class DapperStringVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(string))] + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(string))] + [Instance("Item1", "Item1")] + [Instance("Item2", "Item2")] public partial class LinqToDbStringVo { } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs index 04c37566..43f0ca43 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs @@ -2,34 +2,101 @@ using System; -namespace Vogen.IntegrationTests.TestTypes.ClassVos +namespace Intellenum.IntegrationTests.TestTypes.ClassVos { - [ValueObject(conversions: Conversions.None, underlyingType: typeof(TimeOnly))] - public partial class TimeOnlyVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(TimeOnly))] + public partial class TimeOnlyVo + { + static TimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } - [ValueObject(conversions: Conversions.None, underlyingType: typeof(TimeOnly))] - public partial class NoConverterTimeOnlyVo { } + [Intellenum(conversions: Conversions.None, underlyingType: typeof(TimeOnly))] + public partial class NoConverterTimeOnlyVo + { + static NoConverterTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(TimeOnly))] - public partial class NoJsonTimeOnlyVo { } + [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(TimeOnly))] + public partial class NoJsonTimeOnlyVo + { + static NoJsonTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(TimeOnly))] - public partial class NewtonsoftJsonTimeOnlyVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(TimeOnly))] + public partial class NewtonsoftJsonTimeOnlyVo + { + static NewtonsoftJsonTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(TimeOnly))] - public partial class SystemTextJsonTimeOnlyVo { } + [Intellenum(conversions: Conversions.SystemTextJson, underlyingType: typeof(TimeOnly))] + public partial class SystemTextJsonTimeOnlyVo + { + static SystemTextJsonTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(TimeOnly))] - public partial class BothJsonTimeOnlyVo { } - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(TimeOnly))] - public partial class EfCoreTimeOnlyVo { } + [Intellenum(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(TimeOnly))] + public partial class BothJsonTimeOnlyVo + { + static BothJsonTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(TimeOnly))] - public partial class DapperTimeOnlyVo { } - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(TimeOnly))] - public partial class LinqToDbTimeOnlyVo { } + [Intellenum(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(TimeOnly))] + public partial class EfCoreTimeOnlyVo + { + static EfCoreTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } + + [Intellenum(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(TimeOnly))] + public partial class DapperTimeOnlyVo + { + static DapperTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } + + [Intellenum(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(TimeOnly))] + public partial class LinqToDbTimeOnlyVo + { + static LinqToDbTimeOnlyVo() + { + Instance("Item1", new TimeOnly(1, 2, 3, 4)); + Instance("Item2", new TimeOnly(5, 6, 7, 8)); + } + } } -#endif + + +#endif \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs index 2a8659e0..a6817e76 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs @@ -2,7 +2,6 @@ using System; using System.Text.Json; using FluentAssertions; -using Xunit; namespace MediumTests.SerializationAndConversionTests; @@ -10,63 +9,31 @@ public class ComplexSerializationTests { public class Complex { - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonBoolVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonBoolVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonBoolVo.From(true); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonByteVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonByteVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonByteVo.From(1); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonCharVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonCharVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonCharVo.From('2'); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeOffsetVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeOffsetVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeOffsetVo.From(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeVo.From(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDecimalVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDecimalVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDecimalVo.From(3.33m); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDoubleVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDoubleVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDoubleVo.From(4.44d); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFloatVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFloatVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFloatVo.From(5.55f); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFooVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFooVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFooVo.From(new Vogen.IntegrationTests.TestTypes.ClassVos.Bar(42, "Fred")); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonGuidVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonGuidVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonGuidVo.From(Guid.Empty); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonIntVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonIntVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonIntVo.From(6); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonLongVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonLongVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonLongVo.From(7L); - public Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonStringVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonStringVo { get; set; } = Vogen.IntegrationTests.TestTypes.ClassVos.SystemTextJsonStringVo.From("8"); - - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonBoolVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonBoolVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonBoolVo.From(true); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonByteVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonByteVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonByteVo.From(1); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonCharVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonCharVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonCharVo.From('2'); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDateTimeOffsetVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDateTimeOffsetVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDateTimeOffsetVo.From(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDateTimeVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDateTimeVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDateTimeVo.From(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDecimalVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDecimalVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDecimalVo.From(3.33m); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDoubleVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDoubleVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonDoubleVo.From(4.44d); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonFloatVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonFloatVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonFloatVo.From(5.55f); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonFooVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonFooVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonFooVo.From(new Vogen.IntegrationTests.TestTypes.RecordClassVos.Bar(42, "Fred")); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonGuidVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonGuidVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonGuidVo.From(Guid.Empty); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonIntVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonIntVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonIntVo.From(6); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonLongVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonLongVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonLongVo.From(7L); - public Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonStringVo Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonStringVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordClassVos.SystemTextJsonStringVo.From("8"); - - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonBoolVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonBoolVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonBoolVo.From(true); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonByteVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonByteVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonByteVo.From(1); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonCharVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonCharVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonCharVo.From('2'); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDateTimeOffsetVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDateTimeOffsetVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDateTimeOffsetVo.From(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDateTimeVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDateTimeVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDateTimeVo.From(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDecimalVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDecimalVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDecimalVo.From(3.33m); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDoubleVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDoubleVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonDoubleVo.From(4.44d); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonFloatVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonFloatVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonFloatVo.From(5.55f); - - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonFooVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonFooVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonFooVo.From(new Vogen.IntegrationTests.TestTypes.StructVos.Bar(42, "Fred")); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonGuidVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonGuidVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonGuidVo.From(Guid.Empty); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonIntVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonIntVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonIntVo.From(6); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonLongVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonLongVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonLongVo.From(7L); - public Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonStringVo Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonStringVo { get; set; } = Vogen.IntegrationTests.TestTypes.StructVos.SystemTextJsonStringVo.From("8"); - - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonBoolVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonBoolVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonBoolVo.From(true); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonByteVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonByteVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonByteVo.From(1); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonCharVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonCharVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonCharVo.From('2'); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDateTimeOffsetVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDateTimeOffsetVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDateTimeOffsetVo.From(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDateTimeVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDateTimeVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDateTimeVo.From(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDecimalVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDecimalVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDecimalVo.From(3.33m); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDoubleVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDoubleVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonDoubleVo.From(4.44d); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonFloatVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonFloatVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonFloatVo.From(5.55f); - - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonFooVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonFooVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonFooVo.From(new Vogen.IntegrationTests.TestTypes.RecordStructVos.Bar(42, "Fred")); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonGuidVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonGuidVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonGuidVo.From(Guid.Empty); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonIntVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonIntVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonIntVo.From(6); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonLongVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonLongVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonLongVo.From(7L); - public Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonStringVo Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonStringVo { get; set; } = Vogen.IntegrationTests.TestTypes.RecordStructVos.SystemTextJsonStringVo.From("8"); + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonBoolVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonBoolVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonBoolVo.Yes; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonByteVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonByteVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonByteVo.Item1; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonCharVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonCharVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonCharVo.B; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeOffsetVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeOffsetVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeOffsetVo.JanFirst; + + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeVo + Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeVo { get; set; } = + Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDateTimeVo.Item1; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDecimalVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDecimalVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDecimalVo.Item1; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDoubleVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDoubleVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonDoubleVo.Item1; + + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFloatVo + Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFloatVo { get; set; } = + Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFloatVo.Item1; + + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFooVo + Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFooVo { get; set; } = + Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonFooVo.Item1; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonGuidVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonGuidVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonGuidVo.Item1; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonIntVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonIntVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonIntVo.Item1; + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonLongVo Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonLongVo { get; set; } = Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonLongVo.Item1; + + public Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonStringVo + Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonStringVo { get; set; } = + Intellenum.IntegrationTests.TestTypes.ClassVos.SystemTextJsonStringVo.Item1; } [Fact] @@ -92,53 +59,5 @@ public void CanSerializeAndDeserialize() deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonIntVo.Value.Should().Be(6); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonLongVo.Value.Should().Be(7L); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonStringVo.Value.Should().Be("8"); - - - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonBoolVo.Value.Should().Be(true); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonBoolVo.Value.Should().Be(true); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonByteVo.Value.Should().Be(1); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonCharVo.Value.Should().Be('2'); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDateTimeOffsetVo.Value.Should().Be(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDateTimeVo.Value.Should().Be(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDecimalVo.Value.Should().Be(3.33m); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonDoubleVo.Value.Should().Be(4.44d); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonFloatVo.Value.Should().Be(5.55f); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonFooVo.Value.Age.Should().Be(42); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonFooVo.Value.Name.Should().Be("Fred"); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonGuidVo.Value.Should().Be(Guid.Empty); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonIntVo.Value.Should().Be(6); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonLongVo.Value.Should().Be(7L); - deserialized.Vogen_IntegrationTests_TestTypes_RecordClassVos_SystemTextJsonStringVo.Value.Should().Be("8"); - - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonBoolVo.Value.Should().Be(true); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonByteVo.Value.Should().Be(1); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonCharVo.Value.Should().Be('2'); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDateTimeOffsetVo.Value.Should().Be(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDateTimeVo.Value.Should().Be(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDecimalVo.Value.Should().Be(3.33m); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonDoubleVo.Value.Should().Be(4.44d); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonFloatVo.Value.Should().Be(5.55f); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonFooVo.Value.Age.Should().Be(42); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonFooVo.Value.Name.Should().Be("Fred"); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonGuidVo.Value.Should().Be(Guid.Empty); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonIntVo.Value.Should().Be(6); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonLongVo.Value.Should().Be(7L); - deserialized.Vogen_IntegrationTests_TestTypes_StructVos_SystemTextJsonStringVo.Value.Should().Be("8"); - - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonBoolVo.Value.Should().Be(true); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonByteVo.Value.Should().Be(1); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonCharVo.Value.Should().Be('2'); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDateTimeOffsetVo.Value.Should().Be(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDateTimeVo.Value.Should().Be(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDecimalVo.Value.Should().Be(3.33m); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonDoubleVo.Value.Should().Be(4.44d); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonFloatVo.Value.Should().Be(5.55f); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonFooVo.Value.Age.Should().Be(42); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonFooVo.Value.Name.Should().Be("Fred"); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonGuidVo.Value.Should().Be(Guid.Empty); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonIntVo.Value.Should().Be(6); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonLongVo.Value.Should().Be(7L); - deserialized.Vogen_IntegrationTests_TestTypes_RecordStructVos_SystemTextJsonStringVo.Value.Should().Be("8"); - } } \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs index a768fa92..c10fb741 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs @@ -1,77 +1,110 @@ #nullable disable using FluentAssertions; -using Vogen; -using Xunit; +using Intellenum; using JsonSerializer = System.Text.Json.JsonSerializer; namespace MediumTests.SerializationAndConversionTests; -[ValueObject(underlyingType: typeof(double), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Intellenum(underlyingType: typeof(double), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Instance("Item1", 720742592373919744)] +[Instance("Item2", 2.2d)] public partial class DoubleHolderId_string { } -[ValueObject(underlyingType: typeof(decimal), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Intellenum(underlyingType: typeof(decimal), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] public partial class DecimalHolderId_string { + static DecimalHolderId_string() + { + Instance("Item1", 720742592373919744m); + Instance("Item2", 2.2m); + } } -[ValueObject(underlyingType: typeof(float), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Intellenum(underlyingType: typeof(float), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Instance("Item1", 720742592373919744)] +[Instance("Item2", 2.2f)] public partial class FloatHolderId_string { } -[ValueObject(underlyingType: typeof(long), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Intellenum(underlyingType: typeof(long), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Instance("Item1", 720742592373919744)] +[Instance("Item2", 2)] public partial class LongHolderId_string { } -[ValueObject(underlyingType: typeof(short), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Intellenum(underlyingType: typeof(short), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Instance("Item1", 123)] +[Instance("Item2", 321)] public partial class ShortHolderId_string { } -[ValueObject(underlyingType: typeof(int), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Intellenum(underlyingType: typeof(int), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Instance("Item1", 321)] +[Instance("Item2", 2)] public partial class IntHolderId_string { } -[ValueObject(underlyingType: typeof(byte), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Intellenum(underlyingType: typeof(byte), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class ByteHolderId_string { } -[ValueObject(underlyingType: typeof(double))] +[Intellenum(underlyingType: typeof(double))] +[Instance("Item1", 720742592373919744)] +[Instance("Item2", 2.2d)] public partial class DoubleHolderId_normal { } -[ValueObject(underlyingType: typeof(decimal))] +[Intellenum(underlyingType: typeof(decimal))] public partial class DecimalHolderId_normal { + static DecimalHolderId_normal() + { + Instance("Item1", 720742592373919744m); + Instance("Item2", 2.2m); + } } -[ValueObject(underlyingType: typeof(float))] +[Intellenum(underlyingType: typeof(float))] +[Instance("Item1", 720742592373919744)] +[Instance("Item2", 2.2f)] public partial class FloatHolderId_normal { } -[ValueObject(underlyingType: typeof(long))] +[Intellenum(underlyingType: typeof(long))] +[Instance("Item1", 720742592373919744)] +[Instance("Item2", 2)] public partial class LongHolderId_normal { } -[ValueObject(underlyingType: typeof(short))] +[Intellenum(underlyingType: typeof(short))] +[Instance("Item1", 123)] +[Instance("Item2", 321)] public partial class ShortHolderId_normal { } -[ValueObject(underlyingType: typeof(int))] +[Intellenum(underlyingType: typeof(int))] +[Instance("Item1", 321)] +[Instance("Item2", 2)] public partial class IntHolderId_normal { } -[ValueObject(underlyingType: typeof(byte))] +[Intellenum(underlyingType: typeof(byte))] +[Instance("Item1", 123)] +[Instance("Item2", 2)] public partial class ByteHolderId_normal { } @@ -100,12 +133,12 @@ public class CustomizationTests [Fact] public void CanSerializeAndDeserializeAsString() { - var holderId = DoubleHolderId_string.From(42); + var holderId = DoubleHolderId_string.Item1; string serialized = JsonSerializer.Serialize(holderId); var deserialized = JsonSerializer.Deserialize(serialized); - deserialized.Value.Should().Be(42); + deserialized.Value.Should().Be(DoubleHolderId_string.Item1); } [Fact] @@ -113,21 +146,21 @@ public void CanSerializeAndDeserializeWhenVoIsInAComplexObject() { var container = new Container { - ByteHolder_as_a_string = ByteHolderId_string.From(123), - DecimalHolder_as_a_string = DecimalHolderId_string.From(720742592373919744), - DoubleHolder_as_a_string = DoubleHolderId_string.From(720742592373919744), - FloatHolder_as_a_string = FloatHolderId_string.From(720742592373919744), - IntHolder_as_a_string = IntHolderId_string.From(321), - LongHolder_as_a_string = LongHolderId_string.From(720742592373919744), - ShortHolder_as_a_string = ShortHolderId_string.From(123), - - ByteHolder_normal = ByteHolderId_normal.From(123), - DecimalHolder_normal = DecimalHolderId_normal.From(720742592373919744), - DoubleHolder_normal = DoubleHolderId_normal.From(720742592373919744), - FloatHolder_normal = FloatHolderId_normal.From(720742592373919744), - IntHolder_normal = IntHolderId_normal.From(321), - LongHolder_normal = LongHolderId_normal.From(720742592373919744), - ShortHolder_normal = ShortHolderId_normal.From(123), + ByteHolder_as_a_string = ByteHolderId_string.Item1, + DecimalHolder_as_a_string = DecimalHolderId_string.Item1, + DoubleHolder_as_a_string = DoubleHolderId_string.Item1, + FloatHolder_as_a_string = FloatHolderId_string.Item1, + IntHolder_as_a_string = IntHolderId_string.Item1, + LongHolder_as_a_string = LongHolderId_string.Item1, + ShortHolder_as_a_string = ShortHolderId_string.Item1, + + ByteHolder_normal = ByteHolderId_normal.Item1, + DecimalHolder_normal = DecimalHolderId_normal.Item1, + DoubleHolder_normal = DoubleHolderId_normal.Item1, + FloatHolder_normal = FloatHolderId_normal.Item1, + IntHolder_normal = IntHolderId_normal.Item1, + LongHolder_normal = LongHolderId_normal.Item1, + ShortHolder_normal = ShortHolderId_normal.Item1, }; string serialized = JsonSerializer.Serialize(container); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/AnyOtherTypeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/AnyOtherTypeVoTests.cs deleted file mode 100644 index fc1c3d96..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/AnyOtherTypeVoTests.cs +++ /dev/null @@ -1,320 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(Bar))] - public partial record class AnotherFooVo { } - - public class AnyOtherTypeVoTests - { - public static readonly Bar _bar1 = new Bar(42, "Fred"); - public static readonly Bar _wilma = new Bar(52, "Wilma"); - [Fact] - public void equality_between_same_value_objects() - { - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); - - (FooVo.From(_bar1) != FooVo.From(_wilma)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_wilma)).Should().BeFalse(); - - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); - - var original = FooVo.From(_bar1); - var other = FooVo.From(_bar1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - FooVo.From(_bar1).Equals(AnotherFooVo.From(_bar1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - NewtonsoftJsonFooVo g1 = NewtonsoftJsonFooVo.From(_bar1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeClassToString_WithNewtonsoftJsonProvider() - { - NewtonsoftJsonFooVoClass g1 = NewtonsoftJsonFooVoClass.From(_bar1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonFooVo.From(_bar1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _bar1; - var vo = NewtonsoftJsonFooVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromStringClass_WithNewtonsoftJsonProvider() - { - var value = _bar1; - var vo = NewtonsoftJsonFooVoClass.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _bar1; - var vo = SystemTextJsonFooVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonFooVo.From(_bar1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void CanSerializeToStringClass_WithBothJsonConverters() - { - var vo = BothJsonFooVoClass.From(_bar1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFooVo.From(_bar1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverterClass_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFooVoClass.From(_bar1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - /// - /// There is no way for newtonsoft, via a type converter, to convert - /// the underlying non-native type to json. - /// - [Fact] - public void WithTypeConverterButNoJsonConverters_NewtonsoftSerializesWithValueProperty() - { - NoJsonFooVo foo = NoJsonFooVo.From(_bar1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WithTypeConverterButNoJsonConverters_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoConverterFooVo.From(_bar1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { FooField = EfCoreFooVo.From(_bar1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.FooField, retrieved.FooField); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '{\"Age\":42,\"Name\":\"Fred\"}'"); - - var value = Assert.Single(results); - Assert.Equal(value, DapperFooVo.From(_bar1)); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { FooField = LinqToDbFooVo.From(_bar1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.FooField, retrieved.FooField); - } - } - - [Fact] - public void TypeConverter_CanConvertToAndFrom() - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonFooVo)); - - object vo = converter.ConvertFrom(_bar1); - - Assert.IsType(vo); - - Assert.Equal(NoJsonFooVo.From(_bar1), vo); - - object reconverted = converter.ConvertTo(vo, typeof(Bar)); - Assert.IsType(reconverted); - Assert.Equal(((NoJsonFooVo) vo).Value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.FooField) - .HasConversion(new EfCoreFooVo.EfCoreValueConverter()); - }); - } - } - - public class EfCoreTestEntity - { - public int Id { get; set; } - - public EfCoreFooVo FooField { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(LinqToDbFooVo.LinqToDbValueConverter))] - public LinqToDbFooVo FooField { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/BoolVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/BoolVoTests.cs deleted file mode 100644 index 7965ce63..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/BoolVoTests.cs +++ /dev/null @@ -1,280 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(bool))] - public partial struct AnotherBoolVo { } - - public class BoolVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(false) == BoolVo.From(false)).Should().BeTrue(); - - (BoolVo.From(true) != BoolVo.From(false)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(false)).Should().BeFalse(); - - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(true)).Should().BeTrue(); - - var original = BoolVo.From(true); - var other = BoolVo.From(true); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - BoolVo.From(true).Equals(AnotherBoolVo.From(true)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonBoolVo.From(true); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedBool = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedBool); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonBoolVo.From(true); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - bool value = true; - var vo = NewtonsoftJsonBoolVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - bool value = true; - var vo = SystemTextJsonBoolVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonBoolVo.From(true); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonBoolVo.From(true); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":true}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonBoolVo.From(true); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterBoolVo.From(true); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":true}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreBoolVo.From(true) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT true"); - - var value = Assert.Single(results); - Assert.Equal(DapperBoolVo.From(true), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbBoolVo.From(true) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("true", true, "True")] - [InlineData("True", true, "True")] - [InlineData("false", false, "False")] - [InlineData("False", false, "False")] - public void TypeConverter_CanConvertToAndFrom_strings(object input, bool expectedBool, string expectedString) - { - TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); - - object converted = converter.ConvertFrom(input); - Assert.IsType(converted); - Assert.Equal(NoJsonBoolVo.From(expectedBool), converted); - - object reconverted = converter.ConvertTo(converted, input.GetType()); - Assert.Equal(expectedString, reconverted); - } - - [Theory] - [InlineData(true, "True")] - [InlineData(false, "False")] - public void TypeConverter_CanConvertToAndFrom_bools(bool input, string expectedString) - { - TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); - - NoJsonBoolVo v = NoJsonBoolVo.From(input); - - object asString = converter.ConvertTo(v, typeof(string)); - Assert.Equal(expectedString, asString); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreBoolVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreBoolVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Boolean)] - [ValueConverter(ConverterType = typeof(LinqToDbBoolVo.LinqToDbValueConverter))] - public LinqToDbBoolVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ByteVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ByteVoTests.cs deleted file mode 100644 index c4ada914..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ByteVoTests.cs +++ /dev/null @@ -1,264 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(byte))] - public partial struct AnotherByteVo { } - - public class ByteVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); - - (ByteVo.From(18) != ByteVo.From(19)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(19)).Should().BeFalse(); - - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); - - var original = ByteVo.From(18); - var other = ByteVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - ByteVo.From(18).Equals(AnotherByteVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonByteVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonByteVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - byte value = 123; - var vo = NewtonsoftJsonByteVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - byte value = 123; - var vo = SystemTextJsonByteVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonByteVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonByteVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonByteVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterByteVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreByteVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperByteVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbByteVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((byte) 123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonByteVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonByteVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreByteVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreByteVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Byte)] - [ValueConverter(ConverterType = typeof(LinqToDbByteVo.LinqToDbValueConverter))] - public LinqToDbByteVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/CharVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/CharVoTests.cs deleted file mode 100644 index d5575f4d..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/CharVoTests.cs +++ /dev/null @@ -1,263 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(char))] - public partial struct AnotherCharVo { } - - public class CharVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); - - (CharVo.From('a') != CharVo.From('b')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('b')).Should().BeFalse(); - - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); - - var original = CharVo.From('a'); - var other = CharVo.From('a'); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - CharVo.From('a').Equals(AnotherCharVo.From('a')).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonCharVo.From('a'); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonCharVo.From('a'); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - char value = 'a'; - var vo = NewtonsoftJsonCharVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - char value = 'a'; - var vo = SystemTextJsonCharVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonCharVo.From('a'); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonCharVo.From('a'); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"a\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonCharVo.From('a'); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterCharVo.From('a'); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"a\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreCharVo.From('a') }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 'a'"); - - var value = Assert.Single(results); - Assert.Equal(DapperCharVo.From('a'), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbCharVo.From('a') }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((char) 97)] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonCharVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonCharVo.From('a'), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreCharVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreCharVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Char)] - [ValueConverter(ConverterType = typeof(LinqToDbCharVo.LinqToDbValueConverter))] - public LinqToDbCharVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeOffsetVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeOffsetVoTests.cs deleted file mode 100644 index c6a4a6fe..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeOffsetVoTests.cs +++ /dev/null @@ -1,272 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable EqualExpressionComparison -// ReSharper disable RedundantCast -// ReSharper disable ArrangeMethodOrOperatorBody -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable PropertyCanBeMadeInitOnly.Global -// ReSharper disable SuspiciousTypeConversion.Global - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(DateTimeOffset))] - public readonly partial struct AnotherDateTimeOffsetVo { } - - public class DateTimeOffsetVoTests - { - private static readonly DateTimeOffset _date1 = new DateTimeOffset(1970, 6, 10, 14, 01, 02, TimeSpan.Zero) + TimeSpan.FromTicks(12345678); - private static readonly DateTimeOffset _date2 = DateTimeOffset.Now.AddMinutes(42.69); - - [Fact] - public void equality_between_same_value_objects() - { - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - - (DateTimeOffsetVo.From(_date1) != DateTimeOffsetVo.From(_date2)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date2)).Should().BeFalse(); - - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - - var original = DateTimeOffsetVo.From(_date1); - var other = DateTimeOffsetVo.From(_date1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DateTimeOffsetVo.From(_date1).Equals(AnotherDateTimeOffsetVo.From(_date1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonDateTimeOffsetVo.From(_date1); - - string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serialized, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDateTimeOffsetVo.From(_date1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _date1; - var vo = NewtonsoftJsonDateTimeOffsetVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _date1; - var vo = SystemTextJsonDateTimeOffsetVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonDateTimeOffsetVo.From(_date1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDateTimeOffsetVo.From(_date1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDateTimeOffsetVo.From(_date1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{_date1:o}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDateTimeOffsetVo.From(_date1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDateTimeOffsetVo.From(_date1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '2022-01-15 19:08:49.5413764'"); - - DapperDateTimeOffsetVo actual = Assert.Single(results); - - var expected = DapperDateTimeOffsetVo.From(new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); - actual.Should().Be(expected); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeOffsetVo.From(_date1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("2022-01-15T19:08:49.5413764+00:00")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeOffsetVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDateTimeOffsetVo.From(DateTimeOffset.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDateTimeOffsetVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDateTimeOffsetVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.DateTimeOffset)] - [ValueConverter(ConverterType = typeof(LinqToDbDateTimeOffsetVo.LinqToDbValueConverter))] - public LinqToDbDateTimeOffsetVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeVoTests.cs deleted file mode 100644 index e823e60a..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DateTimeVoTests.cs +++ /dev/null @@ -1,272 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable EqualExpressionComparison -// ReSharper disable RedundantCast -// ReSharper disable ArrangeMethodOrOperatorBody -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable PropertyCanBeMadeInitOnly.Global -// ReSharper disable SuspiciousTypeConversion.Global - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(DateTime))] - public readonly partial struct AnotherDateTimeVo { } - - public class DateTimeVoTests - { - private static readonly DateTime _date1 = new DateTime(1970, 6, 10, 14, 01, 02, DateTimeKind.Utc) + TimeSpan.FromTicks(12345678); - private static readonly DateTime _date2 = DateTime.Now.AddMinutes(42.69); - - [Fact] - public void equality_between_same_value_objects() - { - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); - - (DateTimeVo.From(_date1) != DateTimeVo.From(_date2)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date2)).Should().BeFalse(); - - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); - - var original = DateTimeVo.From(_date1); - var other = DateTimeVo.From(_date1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DateTimeVo.From(_date1).Equals(AnotherDateTimeVo.From(_date1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonDateTimeVo.From(_date1); - - string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serialized, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDateTimeVo.From(_date1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _date1; - var vo = NewtonsoftJsonDateTimeVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _date1; - var vo = SystemTextJsonDateTimeVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonDateTimeVo.From(_date1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDateTimeVo.From(_date1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - serialized.Should().Be(expected); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDateTimeVo.From(_date1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{_date1:o}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDateTimeVo.From(_date1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - newtonsoft.Should().Be(expected); - systemText.Should().Be(expected); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDateTimeVo.From(_date1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '2022-01-15 19:08:49.5413764'"); - - DapperDateTimeVo actual = Assert.Single(results); - - var expected = DapperDateTimeVo.From(new DateTime(2022,01,15,19,08,49,DateTimeKind.Utc).AddTicks(5413764)); - actual.Should().Be(expected); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeVo.From(_date1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("2022-01-15T19:08:49.5413764+00:00")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDateTimeVo.From(DateTime.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDateTimeVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDateTimeVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.DateTime)] - [ValueConverter(ConverterType = typeof(LinqToDbDateTimeVo.LinqToDbValueConverter))] - public LinqToDbDateTimeVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DecimalVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DecimalVoTests.cs deleted file mode 100644 index b33960d6..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DecimalVoTests.cs +++ /dev/null @@ -1,294 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(decimal))] - public partial struct AnotherDecimalVo { } - - public class DecimalVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); - - (DecimalVo.From(18) != DecimalVo.From(19)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(19)).Should().BeFalse(); - - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); - - var original = DecimalVo.From(18); - var other = DecimalVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DecimalVo.From(18).Equals(AnotherDecimalVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonDecimalVo.From(123m); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDecimalVo.From(123m); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123m; - var vo = NewtonsoftJsonDecimalVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123m; - var vo = SystemTextJsonDecimalVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonDecimalVo.From(123m); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDecimalVo.From(123m); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDecimalVo.From(123m); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDecimalVo.From(123m); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDecimalVo.From(123m) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperDecimalVo.From(123m), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDecimalVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public void TypeConverter_CanConvertToAndFromDecimal() - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom(123.45m); - Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123.45m), id); - - var reconverted = converter.ConvertTo(id, typeof(decimal)); - Assert.Equal(123.45m, reconverted); - } - - [Fact] - public void TypeConverter_CanConvertToAndFrom() - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom("123"); - Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123m), id); - - var reconverted = converter.ConvertTo(id, typeof(string)); - Assert.Equal("123", reconverted); - } - - [Fact] - public void RoundTrip_WithNsj() - { - var vo = NewtonsoftJsonDecimalVo.From(123.45m); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45m); - } - - [Fact] - public void RoundTrip_WithStj() - { - var vo = SystemTextJsonDecimalVo.From(123.45m); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45m); - } - - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDecimalVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDecimalVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Decimal)] - [ValueConverter(ConverterType = typeof(LinqToDbDecimalVo.LinqToDbValueConverter))] - public LinqToDbDecimalVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DoubleVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DoubleVoTests.cs deleted file mode 100644 index 12b09b50..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/DoubleVoTests.cs +++ /dev/null @@ -1,286 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(double))] - public partial struct AnotherDoubleVo { } - - public class DoubleVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); - - (DoubleVo.From(18) != DoubleVo.From(19)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(19)).Should().BeFalse(); - - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); - - var original = DoubleVo.From(18); - var other = DoubleVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DoubleVo.From(18).Equals(AnotherDoubleVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonDoubleVo.From(123D); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDoubleVo.From(123D); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123D; - var vo = NewtonsoftJsonDoubleVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123D; - var vo = SystemTextJsonDoubleVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonDoubleVo.From(123D); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDoubleVo.From(123D); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDoubleVo.From(123D); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDoubleVo.From(123D); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDoubleVo.From(123D) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperDoubleVo.From(123D), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDoubleVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123.45D)] - [InlineData("123.45")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var culture = new CultureInfo("en-US"); - - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDoubleVo)); - var id = converter.ConvertFrom(null!, culture, value); - Assert.IsType(id); - Assert.Equal(NoJsonDoubleVo.From(123.45D), id); - - var reconverted = converter.ConvertTo(null, culture, id, value.GetType()); - Assert.Equal(value, reconverted); - } - - [Fact] - public void RoundTrip_WithNsj() - { - var vo = NewtonsoftJsonDoubleVo.From(123.45); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45); - } - - [Fact] - public void RoundTrip_WithStj() - { - var vo = SystemTextJsonDoubleVo.From(123.45); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDoubleVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDoubleVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Double)] - [ValueConverter(ConverterType = typeof(LinqToDbDoubleVo.LinqToDbValueConverter))] - public LinqToDbDoubleVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/FloatVoTests.cs deleted file mode 100644 index 359b275f..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/FloatVoTests.cs +++ /dev/null @@ -1,286 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(float))] - public partial struct AnotherFloatVo { } - - public class FloatVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); - - (FloatVo.From(18) != FloatVo.From(19)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(19)).Should().BeFalse(); - - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); - - var original = FloatVo.From(18); - var other = FloatVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - FloatVo.From(18).Equals(AnotherFloatVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToFloat_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonFloatVo.From(123.45f); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedFloat = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedFloat); - } - - [Fact] - public void RoundTrip_WithNsj() - { - var vo = NewtonsoftJsonFloatVo.From(123.45f); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45f); - } - - [Fact] - public void RoundTrip_WithStj() - { - var vo = SystemTextJsonFloatVo.From(123.45f); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45f); - } - - [Fact] - public void CanSerializeToFloat_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonFloatVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedFloat = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedFloat).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromFloat_WithNewtonsoftJsonProvider() - { - var value = 123; - var vo = NewtonsoftJsonFloatVo.From(value); - var serializedFloat = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedFloat); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromFloat_WithSystemTextJsonProvider() - { - var value = 123; - var vo = SystemTextJsonFloatVo.From(value); - var serializedFloat = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedFloat); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToFloat_WithBothJsonConverters() - { - var vo = BothJsonFloatVo.From(123.45f); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedFloat1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedFloat2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedFloat1); - Assert.Equal(serializedVo2, serializedFloat2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFloatVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonFloatVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterFloatVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreFloatVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperFloatVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbFloatVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((float)123.45)] - [InlineData("123.45")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var culture = new CultureInfo("en-US"); - - var converter = TypeDescriptor.GetConverter(typeof(NoJsonFloatVo)); - var id = converter.ConvertFrom(null!, culture, value); - Assert.IsType(id); - Assert.Equal(NoJsonFloatVo.From(123.45f), id); - - var reconverted = converter.ConvertTo(null, culture, id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreFloatVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreFloatVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Single)] - [ValueConverter(ConverterType = typeof(LinqToDbFloatVo.LinqToDbValueConverter))] - public LinqToDbFloatVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/GuidVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/GuidVoTests.cs deleted file mode 100644 index 818ef27b..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/GuidVoTests.cs +++ /dev/null @@ -1,262 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(Guid))] - public partial struct AnotherGuidVo { } - - public class GuidVoTests - { - public static readonly Guid _guid1 = Guid.NewGuid(); - public static readonly Guid _guid2 = Guid.NewGuid(); - [Fact] - public void equality_between_same_value_objects() - { - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); - - (GuidVo.From(_guid1) != GuidVo.From(_guid2)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid2)).Should().BeFalse(); - - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); - - var original = GuidVo.From(_guid1); - var other = GuidVo.From(_guid1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - GuidVo.From(_guid1).Equals(AnotherGuidVo.From(_guid1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonGuidVo.From(_guid1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonGuidVo.From(_guid1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _guid1; - var vo = NewtonsoftJsonGuidVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _guid1; - var vo = SystemTextJsonGuidVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonGuidVo.From(_guid1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonGuidVo.From(_guid1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonGuidVo.From(_guid1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterGuidVo.From(_guid1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreGuidVo.From(_guid1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '5640dad4-862a-4738-9e3c-c76dc227eb66'"); - - var value = Assert.Single(results); - Assert.Equal(value, DapperGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66"))); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66")) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonGuidVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonGuidVo.From(Guid.Parse(value)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreGuidVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreGuidVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Guid)] - [ValueConverter(ConverterType = typeof(LinqToDbGuidVo.LinqToDbValueConverter))] - public LinqToDbGuidVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/IntVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/IntVoTests.cs deleted file mode 100644 index 30a08f01..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/IntVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(int))] - public partial struct AnotherIntVo { } - - public class IntVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); - - (IntVo.From(18) != IntVo.From(19)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(19)).Should().BeFalse(); - - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); - - var original = IntVo.From(18); - var other = IntVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - IntVo.From(18).Equals(AnotherIntVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToInt_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonIntVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedInt = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedInt); - } - - [Fact] - public void CanSerializeToInt_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonIntVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedInt = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedInt).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() - { - var value = 123; - var vo = NewtonsoftJsonIntVo.From(value); - var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromInt_WithSystemTextJsonProvider() - { - var value = 123; - var vo = SystemTextJsonIntVo.From(value); - var serializedInt = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToInt_WithBothJsonConverters() - { - var vo = BothJsonIntVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedInt1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedInt2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedInt1); - Assert.Equal(serializedVo2, serializedInt2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonIntVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonIntVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterIntVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreIntVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperIntVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbIntVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonIntVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonIntVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreIntVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreIntVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int32)] - [ValueConverter(ConverterType = typeof(LinqToDbIntVo.LinqToDbValueConverter))] - public LinqToDbIntVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/LongVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/LongVoTests.cs deleted file mode 100644 index a9987fb7..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/LongVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(long))] - public partial struct AnotherLongVo { } - - public class LongVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); - - (LongVo.From(18) != LongVo.From(19)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(19)).Should().BeFalse(); - - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); - - var original = LongVo.From(18); - var other = LongVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - LongVo.From(18).Equals(AnotherLongVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonLongVo.From(123L); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonLongVo.From(123L); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123L; - var vo = NewtonsoftJsonLongVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123L; - var vo = SystemTextJsonLongVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonLongVo.From(123L); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonLongVo.From(123L); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonLongVo.From(123L); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterLongVo.From(123L); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreLongVo.From(123L) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperLongVo.From(123L), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbLongVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123L)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonLongVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonLongVo.From(123L), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreLongVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreLongVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int64)] - [ValueConverter(ConverterType = typeof(LinqToDbLongVo.LinqToDbValueConverter))] - public LinqToDbLongVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ShortVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ShortVoTests.cs deleted file mode 100644 index 35e072b6..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/ShortVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(short))] - public partial struct AnotherShortVo { } - - public class ShortVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); - - (ShortVo.From(18) != ShortVo.From(19)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(19)).Should().BeFalse(); - - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); - - var original = ShortVo.From(18); - var other = ShortVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - ShortVo.From(18).Equals(AnotherShortVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonShortVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonShortVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - short value = 123; - var vo = NewtonsoftJsonShortVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - short value = 123; - var vo = SystemTextJsonShortVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonShortVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonShortVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonShortVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterShortVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreShortVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperShortVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbShortVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((short)123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonShortVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonShortVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreShortVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreShortVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int16)] - [ValueConverter(ConverterType = typeof(LinqToDbShortVo.LinqToDbValueConverter))] - public LinqToDbShortVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/StringVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/StringVoTests.cs deleted file mode 100644 index 980f26bd..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/StringVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordClassVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordClassVos -{ - [ValueObject(underlyingType: typeof(string))] - public partial struct AnotherStringVo { } - - public class StringVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); - - (StringVo.From("hello!") != StringVo.From("world!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("world!")).Should().BeFalse(); - - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); - - var original = StringVo.From("hello!"); - var other = StringVo.From("hello!"); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - StringVo.From("hello!").Equals(AnotherStringVo.From("hello!")).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonStringVo.From("foo!"); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonStringVo.From("foo!"); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = "foo!"; - var vo = NewtonsoftJsonStringVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = "foo!"; - var vo = SystemTextJsonStringVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonStringVo.From("foo!"); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonStringVo.From("foo!"); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonStringVo.From("foo!"); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterStringVo.From("foo!"); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreStringVo.From("foo!") }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 'foo!'"); - - var value = Assert.Single(results); - Assert.Equal(DapperStringVo.From("foo!"), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbStringVo.From("foo!") }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("")] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonStringVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonStringVo.From(value!.ToString()), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreStringVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreStringVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(LinqToDbStringVo.LinqToDbValueConverter))] - public LinqToDbStringVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/BoolVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/BoolVo.cs deleted file mode 100644 index 537f74ff..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/BoolVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] - public partial record class BoolVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] - public partial record class NoConverterBoolVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(bool))] - public partial record class NoJsonBoolVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(bool))] - public partial record class NewtonsoftJsonBoolVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(bool))] - public partial record class SystemTextJsonBoolVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(bool))] - public partial record class BothJsonBoolVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(bool))] - public partial record class EfCoreBoolVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(bool))] - public partial record class DapperBoolVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(bool))] - public partial record class LinqToDbBoolVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ByteVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ByteVo.cs deleted file mode 100644 index e84bce08..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ByteVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] - public partial record class ByteVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] - public partial record class NoConverterByteVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(byte))] - public partial record class NoJsonByteVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(byte))] - public partial record class NewtonsoftJsonByteVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(byte))] - public partial record class SystemTextJsonByteVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(byte))] - public partial record class BothJsonByteVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(byte))] - public partial record class EfCoreByteVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(byte))] - public partial record class DapperByteVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(byte))] - public partial record class LinqToDbByteVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/CharVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/CharVo.cs deleted file mode 100644 index bdfe5201..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/CharVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] - public partial record class CharVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] - public partial record class NoConverterCharVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(char))] - public partial record class NoJsonCharVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(char))] - public partial record class NewtonsoftJsonCharVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(char))] - public partial record class SystemTextJsonCharVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(char))] - public partial record class BothJsonCharVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(char))] - public partial record class EfCoreCharVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(char))] - public partial record class DapperCharVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(char))] - public partial record class LinqToDbCharVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeOffsetVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeOffsetVo.cs deleted file mode 100644 index c9f12c41..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeOffsetVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial record class DateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial record class NoConverterDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTimeOffset))] - public partial record class NoJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTimeOffset))] - public partial record class NewtonsoftJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial record class SystemTextJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial record class BothJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial record class EfCoreDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTimeOffset))] - public partial record class DapperDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial record class LinqToDbDateTimeOffsetVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeVo.cs deleted file mode 100644 index 3bb92051..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DateTimeVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial record class DateTimeVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial record class NoConverterDateTimeVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTime))] - public partial record class NoJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTime))] - public partial record class NewtonsoftJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial record class SystemTextJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial record class BothJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTime))] - public partial record class EfCoreDateTimeVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTime))] - public partial record class DapperDateTimeVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTime))] - public partial record class LinqToDbDateTimeVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DecimalVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DecimalVo.cs deleted file mode 100644 index 22d3a21d..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DecimalVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial record class DecimalVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial record class NoConverterDecimalVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(decimal))] - public partial record class NoJsonDecimalVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(decimal))] - public partial record class NewtonsoftJsonDecimalVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial record class SystemTextJsonDecimalVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial record class BothJsonDecimalVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(decimal))] - public partial record class EfCoreDecimalVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(decimal))] - public partial record class DapperDecimalVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(decimal))] - public partial record class LinqToDbDecimalVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DoubleVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DoubleVo.cs deleted file mode 100644 index a17a08ac..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/DoubleVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] - public partial record class DoubleVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] - public partial record class NoConverterDoubleVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(double))] - public partial record class NoJsonDoubleVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(double))] - public partial record class NewtonsoftJsonDoubleVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(double))] - public partial record class SystemTextJsonDoubleVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(double))] - public partial record class BothJsonDoubleVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(double))] - public partial record class EfCoreDoubleVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(double))] - public partial record class DapperDoubleVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(double))] - public partial record class LinqToDbDoubleVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FloatVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FloatVo.cs deleted file mode 100644 index 8d729589..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FloatVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] - public partial record class FloatVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] - public partial record class NoConverterFloatVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(float))] - public partial record class NoJsonFloatVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(float))] - public partial record class NewtonsoftJsonFloatVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(float))] - public partial record class SystemTextJsonFloatVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(float))] - public partial record class BothJsonFloatVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(float))] - public partial record class EfCoreFloatVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(float))] - public partial record class DapperFloatVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(float))] - public partial record class LinqToDbFloatVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FooVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FooVo.cs deleted file mode 100644 index 906f2ec5..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/FooVo.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - public record struct Bar(int Age, string Name); - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial record class FooVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial record class NoConverterFooVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial record class NoJsonFooVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial record class NoJsonFooVoClass { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial record class NewtonsoftJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial record class NewtonsoftJsonFooVoClass { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial record class SystemTextJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial record class BothJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial record class BothJsonFooVoClass { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Bar))] - public partial record class EfCoreFooVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Bar))] - public partial record class DapperFooVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Bar))] - public partial record class LinqToDbFooVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/GuidVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/GuidVo.cs deleted file mode 100644 index 9d098d08..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/GuidVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial record class GuidVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial record class NoConverterGuidVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Guid))] - public partial record class NoJsonGuidVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Guid))] - public partial record class NewtonsoftJsonGuidVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial record class SystemTextJsonGuidVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial record class BothJsonGuidVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Guid))] - public partial record class EfCoreGuidVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Guid))] - public partial record class DapperGuidVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Guid))] - public partial record class LinqToDbGuidVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/IntoVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/IntoVo.cs deleted file mode 100644 index 8f0d5991..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/IntoVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] - public partial record class IntVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] - public partial record class NoConverterIntVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(int))] - public partial record class NoJsonIntVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(int))] - public partial record class NewtonsoftJsonIntVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(int))] - public partial record class SystemTextJsonIntVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(int))] - public partial record class BothJsonIntVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(int))] - public partial record class EfCoreIntVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(int))] - public partial record class DapperIntVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(int))] - public partial record class LinqToDbIntVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/LongVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/LongVo.cs deleted file mode 100644 index dbbf8a41..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/LongVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] - public partial record class LongVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] - public partial record class NoConverterLongVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(long))] - public partial record class NoJsonLongVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(long))] - public partial record class NewtonsoftJsonLongVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(long))] - public partial record class SystemTextJsonLongVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(long))] - public partial record class BothJsonLongVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(long))] - public partial record class EfCoreLongVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(long))] - public partial record class DapperLongVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(long))] - public partial record class LinqToDbLongVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ShortVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ShortVo.cs deleted file mode 100644 index 0f827c1a..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/ShortVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] - public partial record class ShortVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] - public partial record class NoConverterShortVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(short))] - public partial record class NoJsonShortVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(short))] - public partial record class NewtonsoftJsonShortVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(short))] - public partial record class SystemTextJsonShortVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(short))] - public partial record class BothJsonShortVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(short))] - public partial record class EfCoreShortVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(short))] - public partial record class DapperShortVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(short))] - public partial record class LinqToDbShortVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/StringVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/StringVo.cs deleted file mode 100644 index 5538a212..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordClassVos/Types/StringVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordClassVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] - public partial record class StringVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] - public partial record class NoConverterStringVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(string))] - public partial record class NoJsonStringVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(string))] - public partial record class NewtonsoftJsonStringVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(string))] - public partial record class SystemTextJsonStringVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(string))] - public partial record class BothJsonStringVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(string))] - public partial record class EfCoreStringVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(string))] - public partial record class DapperStringVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(string))] - public partial record class LinqToDbStringVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/AnyOtherTypeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/AnyOtherTypeVoTests.cs deleted file mode 100644 index 7655d0ea..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/AnyOtherTypeVoTests.cs +++ /dev/null @@ -1,322 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -// ReSharper disable EqualExpressionComparison - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(Bar))] - public partial record struct AnotherFooVo { } - - public class AnyOtherTypeVoTests - { - private static readonly Bar _bar1 = new Bar(42, "Fred"); - private static readonly Bar _wilma = new Bar(52, "Wilma"); - - [Fact] - public void equality_between_same_value_objects() - { - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); - - (FooVo.From(_bar1) != FooVo.From(_wilma)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_wilma)).Should().BeFalse(); - - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); - - var original = FooVo.From(_bar1); - var other = FooVo.From(_bar1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - FooVo.From(_bar1).Equals(AnotherFooVo.From(_bar1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - NewtonsoftJsonFooVo g1 = NewtonsoftJsonFooVo.From(_bar1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeClassToString_WithNewtonsoftJsonProvider() - { - NewtonsoftJsonFooVoClass g1 = NewtonsoftJsonFooVoClass.From(_bar1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonFooVo.From(_bar1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _bar1; - var vo = NewtonsoftJsonFooVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromStringClass_WithNewtonsoftJsonProvider() - { - var value = _bar1; - var vo = NewtonsoftJsonFooVoClass.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _bar1; - var vo = SystemTextJsonFooVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonFooVo.From(_bar1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void CanSerializeToStringClass_WithBothJsonConverters() - { - var vo = BothJsonFooVoClass.From(_bar1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFooVo.From(_bar1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverterClass_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFooVoClass.From(_bar1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - /// - /// There is no way for newtonsoft, via a type converter, to convert - /// the underlying non-native type to json. - /// - [Fact] - public void WithTypeConverterButNoJsonConverters_NewtonsoftSerializesWithValueProperty() - { - NoJsonFooVo foo = NoJsonFooVo.From(_bar1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WithTypeConverterButNoJsonConverters_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoConverterFooVo.From(_bar1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { FooField = EfCoreFooVo.From(_bar1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.FooField, retrieved.FooField); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '{\"Age\":42,\"Name\":\"Fred\"}'"); - - var value = Assert.Single(results); - Assert.Equal(value, DapperFooVo.From(_bar1)); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { FooField = LinqToDbFooVo.From(_bar1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.FooField, retrieved.FooField); - } - } - - [Fact] - public void TypeConverter_CanConvertToAndFrom() - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonFooVo)); - - object vo = converter.ConvertFrom(_bar1); - - Assert.IsType(vo); - - Assert.Equal(NoJsonFooVo.From(_bar1), vo); - - object reconverted = converter.ConvertTo(vo, typeof(Bar)); - Assert.IsType(reconverted); - Assert.Equal(((NoJsonFooVo) vo).Value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.FooField) - .HasConversion(new EfCoreFooVo.EfCoreValueConverter()); - }); - } - } - - public class EfCoreTestEntity - { - public int Id { get; set; } - - public EfCoreFooVo FooField { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(LinqToDbFooVo.LinqToDbValueConverter))] - public LinqToDbFooVo FooField { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/BoolVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/BoolVoTests.cs deleted file mode 100644 index b32939bf..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/BoolVoTests.cs +++ /dev/null @@ -1,280 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(bool))] - public partial struct AnotherBoolVo { } - - public class BoolVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(false) == BoolVo.From(false)).Should().BeTrue(); - - (BoolVo.From(true) != BoolVo.From(false)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(false)).Should().BeFalse(); - - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(true)).Should().BeTrue(); - - var original = BoolVo.From(true); - var other = BoolVo.From(true); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - BoolVo.From(true).Equals(AnotherBoolVo.From(true)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonBoolVo.From(true); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedBool = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedBool); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonBoolVo.From(true); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - bool value = true; - var vo = NewtonsoftJsonBoolVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - bool value = true; - var vo = SystemTextJsonBoolVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonBoolVo.From(true); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonBoolVo.From(true); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":true}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonBoolVo.From(true); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{(bool)vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterBoolVo.From(true); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":true}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreBoolVo.From(true) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT true"); - - var value = Assert.Single(results); - Assert.Equal(DapperBoolVo.From(true), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbBoolVo.From(true) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("true", true, "True")] - [InlineData("True", true, "True")] - [InlineData("false", false, "False")] - [InlineData("False", false, "False")] - public void TypeConverter_CanConvertToAndFrom_strings(object input, bool expectedBool, string expectedString) - { - TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); - - object converted = converter.ConvertFrom(input); - Assert.IsType(converted); - Assert.Equal(NoJsonBoolVo.From(expectedBool), converted); - - object reconverted = converter.ConvertTo(converted, input.GetType()); - Assert.Equal(expectedString, reconverted); - } - - [Theory] - [InlineData(true, "True")] - [InlineData(false, "False")] - public void TypeConverter_CanConvertToAndFrom_bools(bool input, string expectedString) - { - TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); - - NoJsonBoolVo v = NoJsonBoolVo.From(input); - - object asString = converter.ConvertTo(v, typeof(string)); - Assert.Equal(expectedString, asString); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreBoolVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreBoolVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Boolean)] - [ValueConverter(ConverterType = typeof(LinqToDbBoolVo.LinqToDbValueConverter))] - public LinqToDbBoolVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ByteVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ByteVoTests.cs deleted file mode 100644 index 18d5e937..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ByteVoTests.cs +++ /dev/null @@ -1,264 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(byte))] - public partial struct AnotherByteVo { } - - public class ByteVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); - - (ByteVo.From(18) != ByteVo.From(19)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(19)).Should().BeFalse(); - - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); - - var original = ByteVo.From(18); - var other = ByteVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - ByteVo.From(18).Equals(AnotherByteVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonByteVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonByteVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - byte value = 123; - var vo = NewtonsoftJsonByteVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - byte value = 123; - var vo = SystemTextJsonByteVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonByteVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonByteVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonByteVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterByteVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreByteVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperByteVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbByteVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((byte) 123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonByteVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonByteVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreByteVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreByteVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Byte)] - [ValueConverter(ConverterType = typeof(LinqToDbByteVo.LinqToDbValueConverter))] - public LinqToDbByteVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/CharVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/CharVoTests.cs deleted file mode 100644 index 0b4183cd..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/CharVoTests.cs +++ /dev/null @@ -1,263 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(char))] - public partial struct AnotherCharVo { } - - public class CharVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); - - (CharVo.From('a') != CharVo.From('b')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('b')).Should().BeFalse(); - - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); - - var original = CharVo.From('a'); - var other = CharVo.From('a'); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - CharVo.From('a').Equals(AnotherCharVo.From('a')).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonCharVo.From('a'); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonCharVo.From('a'); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - char value = 'a'; - var vo = NewtonsoftJsonCharVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - char value = 'a'; - var vo = SystemTextJsonCharVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonCharVo.From('a'); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonCharVo.From('a'); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"a\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonCharVo.From('a'); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterCharVo.From('a'); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"a\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreCharVo.From('a') }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 'a'"); - - var value = Assert.Single(results); - Assert.Equal(DapperCharVo.From('a'), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbCharVo.From('a') }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((char) 97)] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonCharVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonCharVo.From('a'), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreCharVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreCharVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Char)] - [ValueConverter(ConverterType = typeof(LinqToDbCharVo.LinqToDbValueConverter))] - public LinqToDbCharVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeOffsetVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeOffsetVoTests.cs deleted file mode 100644 index 8a745f67..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeOffsetVoTests.cs +++ /dev/null @@ -1,272 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable EqualExpressionComparison -// ReSharper disable RedundantCast -// ReSharper disable ArrangeMethodOrOperatorBody -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable PropertyCanBeMadeInitOnly.Global -// ReSharper disable SuspiciousTypeConversion.Global - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(DateTimeOffset))] - public readonly partial struct AnotherDateTimeOffsetVo { } - - public class DateTimeOffsetVoTests - { - private static readonly DateTimeOffset _date1 = new DateTimeOffset(1970, 6, 10, 14, 01, 02, TimeSpan.Zero) + TimeSpan.FromTicks(12345678); - private static readonly DateTimeOffset _date2 = DateTimeOffset.Now.AddMinutes(42.69); - - [Fact] - public void equality_between_same_value_objects() - { - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - - (DateTimeOffsetVo.From(_date1) != DateTimeOffsetVo.From(_date2)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date2)).Should().BeFalse(); - - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - - var original = DateTimeOffsetVo.From(_date1); - var other = DateTimeOffsetVo.From(_date1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DateTimeOffsetVo.From(_date1).Equals(AnotherDateTimeOffsetVo.From(_date1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonDateTimeOffsetVo.From(_date1); - - string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serialized, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDateTimeOffsetVo.From(_date1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _date1; - var vo = NewtonsoftJsonDateTimeOffsetVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _date1; - var vo = SystemTextJsonDateTimeOffsetVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonDateTimeOffsetVo.From(_date1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDateTimeOffsetVo.From(_date1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDateTimeOffsetVo.From(_date1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{_date1:o}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDateTimeOffsetVo.From(_date1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDateTimeOffsetVo.From(_date1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '2022-01-15 19:08:49.5413764'"); - - DapperDateTimeOffsetVo actual = Assert.Single(results); - - var expected = DapperDateTimeOffsetVo.From(new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); - actual.Should().Be(expected); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeOffsetVo.From(_date1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("2022-01-15T19:08:49.5413764+00:00")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeOffsetVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDateTimeOffsetVo.From(DateTimeOffset.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDateTimeOffsetVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDateTimeOffsetVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.DateTimeOffset)] - [ValueConverter(ConverterType = typeof(LinqToDbDateTimeOffsetVo.LinqToDbValueConverter))] - public LinqToDbDateTimeOffsetVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeVoTests.cs deleted file mode 100644 index 6b8538dd..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DateTimeVoTests.cs +++ /dev/null @@ -1,272 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable EqualExpressionComparison -// ReSharper disable RedundantCast -// ReSharper disable ArrangeMethodOrOperatorBody -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable PropertyCanBeMadeInitOnly.Global -// ReSharper disable SuspiciousTypeConversion.Global - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(DateTime))] - public readonly partial struct AnotherDateTimeVo { } - - public class DateTimeVoTests - { - private static readonly DateTime _date1 = new DateTime(1970, 6, 10, 14, 01, 02, DateTimeKind.Utc) + TimeSpan.FromTicks(12345678); - private static readonly DateTime _date2 = DateTime.Now.AddMinutes(42.69); - - [Fact] - public void equality_between_same_value_objects() - { - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); - - (DateTimeVo.From(_date1) != DateTimeVo.From(_date2)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date2)).Should().BeFalse(); - - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); - - var original = DateTimeVo.From(_date1); - var other = DateTimeVo.From(_date1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DateTimeVo.From(_date1).Equals(AnotherDateTimeVo.From(_date1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonDateTimeVo.From(_date1); - - string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serialized, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDateTimeVo.From(_date1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _date1; - var vo = NewtonsoftJsonDateTimeVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _date1; - var vo = SystemTextJsonDateTimeVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonDateTimeVo.From(_date1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDateTimeVo.From(_date1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - serialized.Should().Be(expected); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDateTimeVo.From(_date1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{_date1:o}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDateTimeVo.From(_date1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - newtonsoft.Should().Be(expected); - systemText.Should().Be(expected); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDateTimeVo.From(_date1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '2022-01-15 19:08:49.5413764'"); - - DapperDateTimeVo actual = Assert.Single(results); - - var expected = DapperDateTimeVo.From(new DateTime(2022,01,15,19,08,49,DateTimeKind.Utc).AddTicks(5413764)); - actual.Should().Be(expected); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeVo.From(_date1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("2022-01-15T19:08:49.5413764+00:00")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDateTimeVo.From(DateTime.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDateTimeVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDateTimeVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.DateTime)] - [ValueConverter(ConverterType = typeof(LinqToDbDateTimeVo.LinqToDbValueConverter))] - public LinqToDbDateTimeVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DecimalVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DecimalVoTests.cs deleted file mode 100644 index 5e3fff3f..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DecimalVoTests.cs +++ /dev/null @@ -1,294 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(decimal))] - public partial record struct AnotherDecimalVo { } - - public class DecimalVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); - - (DecimalVo.From(18) != DecimalVo.From(19)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(19)).Should().BeFalse(); - - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); - - var original = DecimalVo.From(18); - var other = DecimalVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DecimalVo.From(18).Equals(AnotherDecimalVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonDecimalVo.From(123m); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDecimalVo.From(123m); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123m; - var vo = NewtonsoftJsonDecimalVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123m; - var vo = SystemTextJsonDecimalVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonDecimalVo.From(123m); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDecimalVo.From(123m); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDecimalVo.From(123m); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDecimalVo.From(123m); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDecimalVo.From(123m) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperDecimalVo.From(123m), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDecimalVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public void TypeConverter_CanConvertToAndFromDecimal() - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom(123.45m); - Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123.45m), id); - - var reconverted = converter.ConvertTo(id, typeof(decimal)); - Assert.Equal(123.45m, reconverted); - } - - [Fact] - public void TypeConverter_CanConvertToAndFrom() - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom("123"); - Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123m), id); - - var reconverted = converter.ConvertTo(id, typeof(string)); - Assert.Equal("123", reconverted); - } - - [Fact] - public void RoundTrip_WithNsj() - { - var vo = NewtonsoftJsonDecimalVo.From(123.45m); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45m); - } - - [Fact] - public void RoundTrip_WithStj() - { - var vo = SystemTextJsonDecimalVo.From(123.45m); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45m); - } - - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDecimalVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDecimalVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Decimal)] - [ValueConverter(ConverterType = typeof(LinqToDbDecimalVo.LinqToDbValueConverter))] - public LinqToDbDecimalVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DoubleVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DoubleVoTests.cs deleted file mode 100644 index 5494493e..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/DoubleVoTests.cs +++ /dev/null @@ -1,286 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(double))] - public partial struct AnotherDoubleVo { } - - public class DoubleVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); - - (DoubleVo.From(18) != DoubleVo.From(19)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(19)).Should().BeFalse(); - - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); - - var original = DoubleVo.From(18); - var other = DoubleVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DoubleVo.From(18).Equals(AnotherDoubleVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonDoubleVo.From(123D); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDoubleVo.From(123D); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123D; - var vo = NewtonsoftJsonDoubleVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123D; - var vo = SystemTextJsonDoubleVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonDoubleVo.From(123D); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDoubleVo.From(123D); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDoubleVo.From(123D); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDoubleVo.From(123D); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDoubleVo.From(123D) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperDoubleVo.From(123D), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDoubleVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123.45D)] - [InlineData("123.45")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var culture = new CultureInfo("en-US"); - - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDoubleVo)); - var id = converter.ConvertFrom(null!, culture, value); - Assert.IsType(id); - Assert.Equal(NoJsonDoubleVo.From(123.45D), id); - - var reconverted = converter.ConvertTo(null, culture, id, value.GetType()); - Assert.Equal(value, reconverted); - } - - [Fact] - public void RoundTrip_WithNsj() - { - var vo = NewtonsoftJsonDoubleVo.From(123.45); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45); - } - - [Fact] - public void RoundTrip_WithStj() - { - var vo = SystemTextJsonDoubleVo.From(123.45); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDoubleVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDoubleVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Double)] - [ValueConverter(ConverterType = typeof(LinqToDbDoubleVo.LinqToDbValueConverter))] - public LinqToDbDoubleVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/FloatVoTests.cs deleted file mode 100644 index 3e0898c8..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/FloatVoTests.cs +++ /dev/null @@ -1,286 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(float))] - public partial struct AnotherFloatVo { } - - public class FloatVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); - - (FloatVo.From(18) != FloatVo.From(19)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(19)).Should().BeFalse(); - - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); - - var original = FloatVo.From(18); - var other = FloatVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - FloatVo.From(18).Equals(AnotherFloatVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToFloat_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonFloatVo.From(123.45f); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedFloat = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedFloat); - } - - [Fact] - public void RoundTrip_WithNsj() - { - var vo = NewtonsoftJsonFloatVo.From(123.45f); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45f); - } - - [Fact] - public void RoundTrip_WithStj() - { - var vo = SystemTextJsonFloatVo.From(123.45f); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - - deserializedVo.Value.Should().Be(123.45f); - } - - [Fact] - public void CanSerializeToFloat_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonFloatVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedFloat = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedFloat).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromFloat_WithNewtonsoftJsonProvider() - { - var value = 123; - var vo = NewtonsoftJsonFloatVo.From(value); - var serializedFloat = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedFloat); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromFloat_WithSystemTextJsonProvider() - { - var value = 123; - var vo = SystemTextJsonFloatVo.From(value); - var serializedFloat = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedFloat); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToFloat_WithBothJsonConverters() - { - var vo = BothJsonFloatVo.From(123.45f); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedFloat1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedFloat2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedFloat1); - Assert.Equal(serializedVo2, serializedFloat2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFloatVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonFloatVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterFloatVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreFloatVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperFloatVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbFloatVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((float)123.45)] - [InlineData("123.45")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var culture = new CultureInfo("en-US"); - - var converter = TypeDescriptor.GetConverter(typeof(NoJsonFloatVo)); - var id = converter.ConvertFrom(null!, culture, value); - Assert.IsType(id); - Assert.Equal(NoJsonFloatVo.From(123.45f), id); - - var reconverted = converter.ConvertTo(null, culture, id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreFloatVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreFloatVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Single)] - [ValueConverter(ConverterType = typeof(LinqToDbFloatVo.LinqToDbValueConverter))] - public LinqToDbFloatVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/GuidVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/GuidVoTests.cs deleted file mode 100644 index 89e8a9e3..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/GuidVoTests.cs +++ /dev/null @@ -1,262 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(Guid))] - public partial struct AnotherGuidVo { } - - public class GuidVoTests - { - public static readonly Guid _guid1 = Guid.NewGuid(); - public static readonly Guid _guid2 = Guid.NewGuid(); - [Fact] - public void equality_between_same_value_objects() - { - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); - - (GuidVo.From(_guid1) != GuidVo.From(_guid2)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid2)).Should().BeFalse(); - - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); - - var original = GuidVo.From(_guid1); - var other = GuidVo.From(_guid1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - GuidVo.From(_guid1).Equals(AnotherGuidVo.From(_guid1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonGuidVo.From(_guid1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonGuidVo.From(_guid1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _guid1; - var vo = NewtonsoftJsonGuidVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _guid1; - var vo = SystemTextJsonGuidVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonGuidVo.From(_guid1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonGuidVo.From(_guid1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonGuidVo.From(_guid1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterGuidVo.From(_guid1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreGuidVo.From(_guid1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '5640dad4-862a-4738-9e3c-c76dc227eb66'"); - - var value = Assert.Single(results); - Assert.Equal(value, DapperGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66"))); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66")) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonGuidVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonGuidVo.From(Guid.Parse(value)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreGuidVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreGuidVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Guid)] - [ValueConverter(ConverterType = typeof(LinqToDbGuidVo.LinqToDbValueConverter))] - public LinqToDbGuidVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/IntVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/IntVoTests.cs deleted file mode 100644 index 3f0be733..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/IntVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(int))] - public partial struct AnotherIntVo { } - - public class IntVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); - - (IntVo.From(18) != IntVo.From(19)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(19)).Should().BeFalse(); - - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); - - var original = IntVo.From(18); - var other = IntVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - IntVo.From(18).Equals(AnotherIntVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToInt_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonIntVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedInt = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedInt); - } - - [Fact] - public void CanSerializeToInt_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonIntVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedInt = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedInt).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() - { - var value = 123; - var vo = NewtonsoftJsonIntVo.From(value); - var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromInt_WithSystemTextJsonProvider() - { - var value = 123; - var vo = SystemTextJsonIntVo.From(value); - var serializedInt = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToInt_WithBothJsonConverters() - { - var vo = BothJsonIntVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedInt1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedInt2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedInt1); - Assert.Equal(serializedVo2, serializedInt2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonIntVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonIntVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterIntVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreIntVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperIntVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbIntVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonIntVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonIntVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreIntVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreIntVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int32)] - [ValueConverter(ConverterType = typeof(LinqToDbIntVo.LinqToDbValueConverter))] - public LinqToDbIntVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/LongVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/LongVoTests.cs deleted file mode 100644 index 31e267aa..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/LongVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(long))] - public partial struct AnotherLongVo { } - - public class LongVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); - - (LongVo.From(18) != LongVo.From(19)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(19)).Should().BeFalse(); - - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); - - var original = LongVo.From(18); - var other = LongVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - LongVo.From(18).Equals(AnotherLongVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonLongVo.From(123L); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonLongVo.From(123L); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123L; - var vo = NewtonsoftJsonLongVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123L; - var vo = SystemTextJsonLongVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonLongVo.From(123L); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonLongVo.From(123L); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonLongVo.From(123L); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterLongVo.From(123L); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreLongVo.From(123L) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperLongVo.From(123L), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbLongVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123L)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonLongVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonLongVo.From(123L), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreLongVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreLongVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int64)] - [ValueConverter(ConverterType = typeof(LinqToDbLongVo.LinqToDbValueConverter))] - public LinqToDbLongVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ShortVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ShortVoTests.cs deleted file mode 100644 index 73461a37..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/ShortVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(short))] - public partial struct AnotherShortVo { } - - public class ShortVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); - - (ShortVo.From(18) != ShortVo.From(19)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(19)).Should().BeFalse(); - - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); - - var original = ShortVo.From(18); - var other = ShortVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - ShortVo.From(18).Equals(AnotherShortVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonShortVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonShortVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - short value = 123; - var vo = NewtonsoftJsonShortVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - short value = 123; - var vo = SystemTextJsonShortVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonShortVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonShortVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonShortVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterShortVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreShortVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperShortVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbShortVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((short)123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonShortVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonShortVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreShortVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreShortVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int16)] - [ValueConverter(ConverterType = typeof(LinqToDbShortVo.LinqToDbValueConverter))] - public LinqToDbShortVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/StringVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/StringVoTests.cs deleted file mode 100644 index 97512387..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/StringVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.RecordStructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.RecordStructVos -{ - [ValueObject(underlyingType: typeof(string))] - public partial record struct AnotherStringVo { } - - public class StringVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); - - (StringVo.From("hello!") != StringVo.From("world!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("world!")).Should().BeFalse(); - - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); - - var original = StringVo.From("hello!"); - var other = StringVo.From("hello!"); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - StringVo.From("hello!").Equals(AnotherStringVo.From("hello!")).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonStringVo.From("foo!"); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonStringVo.From("foo!"); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = "foo!"; - var vo = NewtonsoftJsonStringVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = "foo!"; - var vo = SystemTextJsonStringVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonStringVo.From("foo!"); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonStringVo.From("foo!"); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonStringVo.From("foo!"); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterStringVo.From("foo!"); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreStringVo.From("foo!") }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 'foo!'"); - - var value = Assert.Single(results); - Assert.Equal(DapperStringVo.From("foo!"), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbStringVo.From("foo!") }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("")] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonStringVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonStringVo.From(value!.ToString()), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreStringVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreStringVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(LinqToDbStringVo.LinqToDbValueConverter))] - public LinqToDbStringVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/BoolVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/BoolVo.cs deleted file mode 100644 index 58c4ddb8..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/BoolVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] - public partial record struct BoolVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] - public partial record struct NoConverterBoolVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(bool))] - public partial record struct NoJsonBoolVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(bool))] - public partial record struct NewtonsoftJsonBoolVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(bool))] - public partial record struct SystemTextJsonBoolVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(bool))] - public partial record struct BothJsonBoolVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(bool))] - public partial record struct EfCoreBoolVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(bool))] - public partial record struct DapperBoolVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(bool))] - public partial record struct LinqToDbBoolVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ByteVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ByteVo.cs deleted file mode 100644 index f6945294..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ByteVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] - public partial record struct ByteVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] - public partial record struct NoConverterByteVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(byte))] - public partial record struct NoJsonByteVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(byte))] - public partial record struct NewtonsoftJsonByteVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(byte))] - public partial record struct SystemTextJsonByteVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(byte))] - public partial record struct BothJsonByteVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(byte))] - public partial record struct EfCoreByteVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(byte))] - public partial record struct DapperByteVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(byte))] - public partial record struct LinqToDbByteVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/CharVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/CharVo.cs deleted file mode 100644 index 641e3cbe..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/CharVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] - public partial record struct CharVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] - public partial record struct NoConverterCharVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(char))] - public partial record struct NoJsonCharVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(char))] - public partial record struct NewtonsoftJsonCharVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(char))] - public partial record struct SystemTextJsonCharVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(char))] - public partial record struct BothJsonCharVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(char))] - public partial record struct EfCoreCharVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(char))] - public partial record struct DapperCharVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(char))] - public partial record struct LinqToDbCharVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeOffsetVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeOffsetVo.cs deleted file mode 100644 index 03923c8e..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeOffsetVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial record struct DateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial record struct NoConverterDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTimeOffset))] - public partial record struct NoJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTimeOffset))] - public partial record struct NewtonsoftJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial record struct SystemTextJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial record struct BothJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial record struct EfCoreDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTimeOffset))] - public partial record struct DapperDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial record struct LinqToDbDateTimeOffsetVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeVo.cs deleted file mode 100644 index c672ca3d..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DateTimeVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial record struct DateTimeVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial record struct NoConverterDateTimeVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTime))] - public partial record struct NoJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTime))] - public partial record struct NewtonsoftJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial record struct SystemTextJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial record struct BothJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTime))] - public partial record struct EfCoreDateTimeVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTime))] - public partial record struct DapperDateTimeVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTime))] - public partial record struct LinqToDbDateTimeVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DecimalVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DecimalVo.cs deleted file mode 100644 index ea7bc322..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DecimalVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial record struct DecimalVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial record struct NoConverterDecimalVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(decimal))] - public partial record struct NoJsonDecimalVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(decimal))] - public partial record struct NewtonsoftJsonDecimalVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial record struct SystemTextJsonDecimalVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial record struct BothJsonDecimalVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(decimal))] - public partial record struct EfCoreDecimalVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(decimal))] - public partial record struct DapperDecimalVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(decimal))] - public partial record struct LinqToDbDecimalVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DoubleVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DoubleVo.cs deleted file mode 100644 index 9b6bd7db..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/DoubleVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] - public partial record struct DoubleVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] - public partial record struct NoConverterDoubleVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(double))] - public partial record struct NoJsonDoubleVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(double))] - public partial record struct NewtonsoftJsonDoubleVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(double))] - public partial record struct SystemTextJsonDoubleVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(double))] - public partial record struct BothJsonDoubleVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(double))] - public partial record struct EfCoreDoubleVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(double))] - public partial record struct DapperDoubleVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(double))] - public partial record struct LinqToDbDoubleVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FloatVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FloatVo.cs deleted file mode 100644 index 740e8b44..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FloatVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] - public partial record struct FloatVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] - public partial record struct NoConverterFloatVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(float))] - public partial record struct NoJsonFloatVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(float))] - public partial record struct NewtonsoftJsonFloatVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(float))] - public partial record struct SystemTextJsonFloatVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(float))] - public partial record struct BothJsonFloatVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(float))] - public partial record struct EfCoreFloatVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(float))] - public partial record struct DapperFloatVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(float))] - public partial record struct LinqToDbFloatVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FooVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FooVo.cs deleted file mode 100644 index 5534ac0d..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/FooVo.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - public record struct Bar(int Age, string Name); - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial record struct FooVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial record struct NoConverterFooVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial record struct NoJsonFooVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial record struct NoJsonFooVoClass { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial record struct NewtonsoftJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial record struct NewtonsoftJsonFooVoClass { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial record struct SystemTextJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial record struct BothJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial record struct BothJsonFooVoClass { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Bar))] - public partial record struct EfCoreFooVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Bar))] - public partial record struct DapperFooVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Bar))] - public partial record struct LinqToDbFooVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/GuidVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/GuidVo.cs deleted file mode 100644 index 92a68992..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/GuidVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial record struct GuidVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial record struct NoConverterGuidVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Guid))] - public partial record struct NoJsonGuidVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Guid))] - public partial record struct NewtonsoftJsonGuidVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial record struct SystemTextJsonGuidVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial record struct BothJsonGuidVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Guid))] - public partial record struct EfCoreGuidVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Guid))] - public partial record struct DapperGuidVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Guid))] - public partial record struct LinqToDbGuidVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/IntoVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/IntoVo.cs deleted file mode 100644 index 7db93fd8..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/IntoVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] - public partial record struct IntVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] - public partial record struct NoConverterIntVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(int))] - public partial record struct NoJsonIntVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(int))] - public partial record struct NewtonsoftJsonIntVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(int))] - public partial record struct SystemTextJsonIntVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(int))] - public partial record struct BothJsonIntVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(int))] - public partial record struct EfCoreIntVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(int))] - public partial record struct DapperIntVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(int))] - public partial record struct LinqToDbIntVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/LongVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/LongVo.cs deleted file mode 100644 index a764578b..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/LongVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] - public partial record struct LongVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] - public partial record struct NoConverterLongVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(long))] - public partial record struct NoJsonLongVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(long))] - public partial record struct NewtonsoftJsonLongVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(long))] - public partial record struct SystemTextJsonLongVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(long))] - public partial record struct BothJsonLongVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(long))] - public partial record struct EfCoreLongVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(long))] - public partial record struct DapperLongVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(long))] - public partial record struct LinqToDbLongVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ShortVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ShortVo.cs deleted file mode 100644 index a0fb5f88..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/ShortVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] - public partial record struct ShortVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] - public partial record struct NoConverterShortVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(short))] - public partial record struct NoJsonShortVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(short))] - public partial record struct NewtonsoftJsonShortVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(short))] - public partial record struct SystemTextJsonShortVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(short))] - public partial record struct BothJsonShortVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(short))] - public partial record struct EfCoreShortVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(short))] - public partial record struct DapperShortVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(short))] - public partial record struct LinqToDbShortVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/StringVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/StringVo.cs deleted file mode 100644 index 08c2e054..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/RecordStructVos/Types/StringVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.RecordStructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] - public partial record struct StringVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] - public partial record struct NoConverterStringVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(string))] - public partial record struct NoJsonStringVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(string))] - public partial record struct NewtonsoftJsonStringVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(string))] - public partial record struct SystemTextJsonStringVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(string))] - public partial record struct BothJsonStringVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(string))] - public partial record struct EfCoreStringVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(string))] - public partial record struct DapperStringVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(string))] - public partial record struct LinqToDbStringVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/AnyOtherTypeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/AnyOtherTypeVoTests.cs deleted file mode 100644 index f4e30a2c..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/AnyOtherTypeVoTests.cs +++ /dev/null @@ -1,320 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Vogen.IntegrationTests.TestTypes.StructVos; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(Bar))] - public partial struct AnotherFooVo { } - - public class AnyOtherTypeVoTests - { - public static readonly Bar _bar1 = new Bar(42, "Fred"); - public static readonly Bar _wilma = new Bar(52, "Wilma"); - [Fact] - public void equality_between_same_value_objects() - { - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); - - (FooVo.From(_bar1) != FooVo.From(_wilma)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_wilma)).Should().BeFalse(); - - FooVo.From(_bar1).Equals(FooVo.From(_bar1)).Should().BeTrue(); - (FooVo.From(_bar1) == FooVo.From(_bar1)).Should().BeTrue(); - - var original = FooVo.From(_bar1); - var other = FooVo.From(_bar1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - FooVo.From(_bar1).Equals(AnotherFooVo.From(_bar1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - NewtonsoftJsonFooVo g1 = NewtonsoftJsonFooVo.From(_bar1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeClassToString_WithNewtonsoftJsonProvider() - { - NewtonsoftJsonFooVoClass g1 = NewtonsoftJsonFooVoClass.From(_bar1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonFooVo.From(_bar1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _bar1; - var vo = NewtonsoftJsonFooVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromStringClass_WithNewtonsoftJsonProvider() - { - var value = _bar1; - var vo = NewtonsoftJsonFooVoClass.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _bar1; - var vo = SystemTextJsonFooVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonFooVo.From(_bar1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void CanSerializeToStringClass_WithBothJsonConverters() - { - var vo = BothJsonFooVoClass.From(_bar1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFooVo.From(_bar1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverterClass_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFooVoClass.From(_bar1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - /// - /// There is no way for newtonsoft, via a type converter, to convert - /// the underlying non-native type to json. - /// - [Fact] - public void WithTypeConverterButNoJsonConverters_NewtonsoftSerializesWithValueProperty() - { - NoJsonFooVo foo = NoJsonFooVo.From(_bar1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WithTypeConverterButNoJsonConverters_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoConverterFooVo.From(_bar1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { FooField = EfCoreFooVo.From(_bar1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.FooField, retrieved.FooField); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '{\"Age\":42,\"Name\":\"Fred\"}'"); - - var value = Assert.Single(results); - Assert.Equal(value, DapperFooVo.From(_bar1)); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { FooField = LinqToDbFooVo.From(_bar1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.FooField, retrieved.FooField); - } - } - - [Fact] - public void TypeConverter_CanConvertToAndFrom() - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonFooVo)); - - object vo = converter.ConvertFrom(_bar1); - - Assert.IsType(vo); - - Assert.Equal(NoJsonFooVo.From(_bar1), vo); - - object reconverted = converter.ConvertTo(vo, typeof(Bar)); - Assert.IsType(reconverted); - Assert.Equal(((NoJsonFooVo) vo).Value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.FooField) - .HasConversion(new EfCoreFooVo.EfCoreValueConverter()); - }); - } - } - - public class EfCoreTestEntity - { - public int Id { get; set; } - - public EfCoreFooVo FooField { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(LinqToDbFooVo.LinqToDbValueConverter))] - public LinqToDbFooVo FooField { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/BoolVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/BoolVoTests.cs deleted file mode 100644 index b474ff65..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/BoolVoTests.cs +++ /dev/null @@ -1,280 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(bool))] - public partial struct AnotherBoolVo { } - - public class BoolVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(false) == BoolVo.From(false)).Should().BeTrue(); - - (BoolVo.From(true) != BoolVo.From(false)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(false)).Should().BeFalse(); - - BoolVo.From(true).Equals(BoolVo.From(true)).Should().BeTrue(); - (BoolVo.From(true) == BoolVo.From(true)).Should().BeTrue(); - - var original = BoolVo.From(true); - var other = BoolVo.From(true); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - BoolVo.From(true).Equals(AnotherBoolVo.From(true)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonBoolVo.From(true); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedBool = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedBool); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonBoolVo.From(true); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - bool value = true; - var vo = NewtonsoftJsonBoolVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - bool value = true; - var vo = SystemTextJsonBoolVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonBoolVo.From(true); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonBoolVo.From(true); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":true}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonBoolVo.From(true); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{(bool)vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterBoolVo.From(true); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":true}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreBoolVo.From(true) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT true"); - - var value = Assert.Single(results); - Assert.Equal(DapperBoolVo.From(true), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbBoolVo.From(true) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("true", true, "True")] - [InlineData("True", true, "True")] - [InlineData("false", false, "False")] - [InlineData("False", false, "False")] - public void TypeConverter_CanConvertToAndFrom_strings(object input, bool expectedBool, string expectedString) - { - TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); - - object converted = converter.ConvertFrom(input); - Assert.IsType(converted); - Assert.Equal(NoJsonBoolVo.From(expectedBool), converted); - - object reconverted = converter.ConvertTo(converted, input.GetType()); - Assert.Equal(expectedString, reconverted); - } - - [Theory] - [InlineData(true, "True")] - [InlineData(false, "False")] - public void TypeConverter_CanConvertToAndFrom_bools(bool input, string expectedString) - { - TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); - - NoJsonBoolVo v = NoJsonBoolVo.From(input); - - object asString = converter.ConvertTo(v, typeof(string)); - Assert.Equal(expectedString, asString); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreBoolVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreBoolVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Boolean)] - [ValueConverter(ConverterType = typeof(LinqToDbBoolVo.LinqToDbValueConverter))] - public LinqToDbBoolVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/ByteVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/ByteVoTests.cs deleted file mode 100644 index a4a2f05b..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/ByteVoTests.cs +++ /dev/null @@ -1,264 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(byte))] - public partial struct AnotherByteVo { } - - public class ByteVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); - - (ByteVo.From(18) != ByteVo.From(19)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(19)).Should().BeFalse(); - - ByteVo.From(18).Equals(ByteVo.From(18)).Should().BeTrue(); - (ByteVo.From(18) == ByteVo.From(18)).Should().BeTrue(); - - var original = ByteVo.From(18); - var other = ByteVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - ByteVo.From(18).Equals(AnotherByteVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonByteVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonByteVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - byte value = 123; - var vo = NewtonsoftJsonByteVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - byte value = 123; - var vo = SystemTextJsonByteVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonByteVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonByteVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonByteVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterByteVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreByteVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperByteVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbByteVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((byte) 123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonByteVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonByteVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreByteVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreByteVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Byte)] - [ValueConverter(ConverterType = typeof(LinqToDbByteVo.LinqToDbValueConverter))] - public LinqToDbByteVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/CharVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/CharVoTests.cs deleted file mode 100644 index de3251e8..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/CharVoTests.cs +++ /dev/null @@ -1,263 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable RedundantOverflowCheckingContext -// ReSharper disable ConvertToLocalFunction - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(char))] - public partial struct AnotherCharVo { } - - public class CharVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); - - (CharVo.From('a') != CharVo.From('b')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('b')).Should().BeFalse(); - - CharVo.From('a').Equals(CharVo.From('a')).Should().BeTrue(); - (CharVo.From('a') == CharVo.From('a')).Should().BeTrue(); - - var original = CharVo.From('a'); - var other = CharVo.From('a'); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - - [Fact] - public void equality_between_different_value_objects() - { - CharVo.From('a').Equals(AnotherCharVo.From('a')).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonCharVo.From('a'); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonCharVo.From('a'); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - char value = 'a'; - var vo = NewtonsoftJsonCharVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - char value = 'a'; - var vo = SystemTextJsonCharVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonCharVo.From('a'); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonCharVo.From('a'); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"a\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonCharVo.From('a'); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterCharVo.From('a'); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"a\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreCharVo.From('a') }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 'a'"); - - var value = Assert.Single(results); - Assert.Equal(DapperCharVo.From('a'), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbCharVo.From('a') }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((char) 97)] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonCharVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonCharVo.From('a'), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreCharVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreCharVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Char)] - [ValueConverter(ConverterType = typeof(LinqToDbCharVo.LinqToDbValueConverter))] - public LinqToDbCharVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeOffsetVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeOffsetVoTests.cs deleted file mode 100644 index d2e3ee55..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeOffsetVoTests.cs +++ /dev/null @@ -1,272 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable EqualExpressionComparison -// ReSharper disable RedundantCast -// ReSharper disable ArrangeMethodOrOperatorBody -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable PropertyCanBeMadeInitOnly.Global -// ReSharper disable SuspiciousTypeConversion.Global - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(DateTimeOffset))] - public readonly partial struct AnotherDateTimeOffsetVo { } - - public class DateTimeOffsetVoTests - { - private static readonly DateTimeOffset _date1 = new DateTimeOffset(1970, 6, 10, 14, 01, 02, TimeSpan.Zero) + TimeSpan.FromTicks(12345678); - private static readonly DateTimeOffset _date2 = DateTimeOffset.Now.AddMinutes(42.69); - - [Fact] - public void equality_between_same_value_objects() - { - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - - (DateTimeOffsetVo.From(_date1) != DateTimeOffsetVo.From(_date2)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date2)).Should().BeFalse(); - - DateTimeOffsetVo.From(_date1).Equals(DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - (DateTimeOffsetVo.From(_date1) == DateTimeOffsetVo.From(_date1)).Should().BeTrue(); - - var original = DateTimeOffsetVo.From(_date1); - var other = DateTimeOffsetVo.From(_date1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DateTimeOffsetVo.From(_date1).Equals(AnotherDateTimeOffsetVo.From(_date1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonDateTimeOffsetVo.From(_date1); - - string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serialized, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDateTimeOffsetVo.From(_date1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _date1; - var vo = NewtonsoftJsonDateTimeOffsetVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _date1; - var vo = SystemTextJsonDateTimeOffsetVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonDateTimeOffsetVo.From(_date1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDateTimeOffsetVo.From(_date1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDateTimeOffsetVo.From(_date1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{_date1:o}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDateTimeOffsetVo.From(_date1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDateTimeOffsetVo.From(_date1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '2022-01-15 19:08:49.5413764'"); - - DapperDateTimeOffsetVo actual = Assert.Single(results); - - var expected = DapperDateTimeOffsetVo.From(new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); - actual.Should().Be(expected); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeOffsetVo.From(_date1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("2022-01-15T19:08:49.5413764+00:00")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeOffsetVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDateTimeOffsetVo.From(DateTimeOffset.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDateTimeOffsetVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDateTimeOffsetVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.DateTimeOffset)] - [ValueConverter(ConverterType = typeof(LinqToDbDateTimeOffsetVo.LinqToDbValueConverter))] - public LinqToDbDateTimeOffsetVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeVoTests.cs deleted file mode 100644 index 37eaaf8a..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DateTimeVoTests.cs +++ /dev/null @@ -1,272 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; -// ReSharper disable EqualExpressionComparison -// ReSharper disable RedundantCast -// ReSharper disable ArrangeMethodOrOperatorBody -// ReSharper disable UnusedAutoPropertyAccessor.Global -// ReSharper disable PropertyCanBeMadeInitOnly.Global -// ReSharper disable SuspiciousTypeConversion.Global - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(DateTime))] - public readonly partial struct AnotherDateTimeVo { } - - public class DateTimeVoTests - { - private static readonly DateTime _date1 = new DateTime(1970, 6, 10, 14, 01, 02, DateTimeKind.Utc) + TimeSpan.FromTicks(12345678); - private static readonly DateTime _date2 = DateTime.Now.AddMinutes(42.69); - - [Fact] - public void equality_between_same_value_objects() - { - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); - - (DateTimeVo.From(_date1) != DateTimeVo.From(_date2)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date2)).Should().BeFalse(); - - DateTimeVo.From(_date1).Equals(DateTimeVo.From(_date1)).Should().BeTrue(); - (DateTimeVo.From(_date1) == DateTimeVo.From(_date1)).Should().BeTrue(); - - var original = DateTimeVo.From(_date1); - var other = DateTimeVo.From(_date1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DateTimeVo.From(_date1).Equals(AnotherDateTimeVo.From(_date1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonDateTimeVo.From(_date1); - - string serialized = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serialized, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDateTimeVo.From(_date1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _date1; - var vo = NewtonsoftJsonDateTimeVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _date1; - var vo = SystemTextJsonDateTimeVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonDateTimeVo.From(_date1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDateTimeVo.From(_date1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - serialized.Should().Be(expected); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDateTimeVo.From(_date1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{_date1:o}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDateTimeVo.From(_date1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; - - newtonsoft.Should().Be(expected); - systemText.Should().Be(expected); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDateTimeVo.From(_date1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '2022-01-15 19:08:49.5413764'"); - - DapperDateTimeVo actual = Assert.Single(results); - - var expected = DapperDateTimeVo.From(new DateTime(2022,01,15,19,08,49,DateTimeKind.Utc).AddTicks(5413764)); - actual.Should().Be(expected); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDateTimeVo.From(_date1) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("2022-01-15T19:08:49.5413764+00:00")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDateTimeVo.From(DateTime.ParseExact(value, "O", CultureInfo.InvariantCulture)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDateTimeVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDateTimeVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.DateTime)] - [ValueConverter(ConverterType = typeof(LinqToDbDateTimeVo.LinqToDbValueConverter))] - public LinqToDbDateTimeVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DecimalVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DecimalVoTests.cs deleted file mode 100644 index 5837aee8..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DecimalVoTests.cs +++ /dev/null @@ -1,273 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(decimal))] - public partial struct AnotherDecimalVo { } - - public class DecimalVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); - - (DecimalVo.From(18) != DecimalVo.From(19)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(19)).Should().BeFalse(); - - DecimalVo.From(18).Equals(DecimalVo.From(18)).Should().BeTrue(); - (DecimalVo.From(18) == DecimalVo.From(18)).Should().BeTrue(); - - var original = DecimalVo.From(18); - var other = DecimalVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DecimalVo.From(18).Equals(AnotherDecimalVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonDecimalVo.From(123M); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDecimalVo.From(123M); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123M; - var vo = NewtonsoftJsonDecimalVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123M; - var vo = SystemTextJsonDecimalVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonDecimalVo.From(123M); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDecimalVo.From(123M); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDecimalVo.From(123M); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDecimalVo.From(123M); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDecimalVo.From(123M) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperDecimalVo.From(123M), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDecimalVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public void TypeConverter_CanConvertToAndFromDecimal() - { - decimal value = 123m; - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123M), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - [Theory] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDecimalVo.From(123M), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDecimalVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDecimalVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Decimal)] - [ValueConverter(ConverterType = typeof(LinqToDbDecimalVo.LinqToDbValueConverter))] - public LinqToDbDecimalVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DoubleVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DoubleVoTests.cs deleted file mode 100644 index c5da7cea..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/DoubleVoTests.cs +++ /dev/null @@ -1,264 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(double))] - public partial struct AnotherDoubleVo { } - - public class DoubleVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); - - (DoubleVo.From(18) != DoubleVo.From(19)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(19)).Should().BeFalse(); - - DoubleVo.From(18).Equals(DoubleVo.From(18)).Should().BeTrue(); - (DoubleVo.From(18) == DoubleVo.From(18)).Should().BeTrue(); - - var original = DoubleVo.From(18); - var other = DoubleVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - DoubleVo.From(18).Equals(AnotherDoubleVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonDoubleVo.From(123D); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonDoubleVo.From(123D); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123D; - var vo = NewtonsoftJsonDoubleVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123D; - var vo = SystemTextJsonDoubleVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonDoubleVo.From(123D); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonDoubleVo.From(123D); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonDoubleVo.From(123D); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterDoubleVo.From(123D); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreDoubleVo.From(123D) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperDoubleVo.From(123D), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbDoubleVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123.45D)] - [InlineData("123.45")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var culture = new CultureInfo("en-US"); - - var converter = TypeDescriptor.GetConverter(typeof(NoJsonDoubleVo)); - var id = converter.ConvertFrom(null!, culture, value); - Assert.IsType(id); - Assert.Equal(NoJsonDoubleVo.From(123.45D), id); - - var reconverted = converter.ConvertTo(null, culture, id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreDoubleVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreDoubleVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Double)] - [ValueConverter(ConverterType = typeof(LinqToDbDoubleVo.LinqToDbValueConverter))] - public LinqToDbDoubleVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/FloatVoTests.cs deleted file mode 100644 index d9ffd3d0..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/FloatVoTests.cs +++ /dev/null @@ -1,264 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(float))] - public partial struct AnotherFloatVo { } - - public class FloatVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); - - (FloatVo.From(18) != FloatVo.From(19)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(19)).Should().BeFalse(); - - FloatVo.From(18).Equals(FloatVo.From(18)).Should().BeTrue(); - (FloatVo.From(18) == FloatVo.From(18)).Should().BeTrue(); - - var original = FloatVo.From(18); - var other = FloatVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - FloatVo.From(18).Equals(AnotherFloatVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToInt_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonFloatVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedInt = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedInt); - } - - [Fact] - public void CanSerializeToInt_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonFloatVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedInt = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedInt).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() - { - var value = 123; - var vo = NewtonsoftJsonFloatVo.From(value); - var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromInt_WithSystemTextJsonProvider() - { - var value = 123; - var vo = SystemTextJsonFloatVo.From(value); - var serializedInt = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToInt_WithBothJsonConverters() - { - var vo = BothJsonFloatVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedInt1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedInt2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedInt1); - Assert.Equal(serializedVo2, serializedInt2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonFloatVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonFloatVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterFloatVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreFloatVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperFloatVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbFloatVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((float)123.45)] - [InlineData("123.45")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var culture = new CultureInfo("en-US"); - - var converter = TypeDescriptor.GetConverter(typeof(NoJsonFloatVo)); - var id = converter.ConvertFrom(null!, culture, value); - Assert.IsType(id); - Assert.Equal(NoJsonFloatVo.From(123.45f), id); - - var reconverted = converter.ConvertTo(null, culture, id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreFloatVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreFloatVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Single)] - [ValueConverter(ConverterType = typeof(LinqToDbFloatVo.LinqToDbValueConverter))] - public LinqToDbFloatVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/GuidVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/GuidVoTests.cs deleted file mode 100644 index c0ddfb98..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/GuidVoTests.cs +++ /dev/null @@ -1,262 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(Guid))] - public partial struct AnotherGuidVo { } - - public class GuidVoTests - { - public static readonly Guid _guid1 = Guid.NewGuid(); - public static readonly Guid _guid2 = Guid.NewGuid(); - [Fact] - public void equality_between_same_value_objects() - { - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); - - (GuidVo.From(_guid1) != GuidVo.From(_guid2)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid2)).Should().BeFalse(); - - GuidVo.From(_guid1).Equals(GuidVo.From(_guid1)).Should().BeTrue(); - (GuidVo.From(_guid1) == GuidVo.From(_guid1)).Should().BeTrue(); - - var original = GuidVo.From(_guid1); - var other = GuidVo.From(_guid1); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - GuidVo.From(_guid1).Equals(AnotherGuidVo.From(_guid1)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var g1 = NewtonsoftJsonGuidVo.From(_guid1); - - string serializedGuid = NewtonsoftJsonSerializer.SerializeObject(g1); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(g1.Value); - - Assert.Equal(serializedGuid, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonGuidVo.From(_guid1); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = _guid1; - var vo = NewtonsoftJsonGuidVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = _guid1; - var vo = SystemTextJsonGuidVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonGuidVo.From(_guid1); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonGuidVo.From(_guid1); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonGuidVo.From(_guid1); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterGuidVo.From(_guid1); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreGuidVo.From(_guid1) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT '5640dad4-862a-4738-9e3c-c76dc227eb66'"); - - var value = Assert.Single(results); - Assert.Equal(value, DapperGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66"))); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbGuidVo.From(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66")) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")] - public void TypeConverter_CanConvertToAndFrom(string value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonGuidVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonGuidVo.From(Guid.Parse(value)), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreGuidVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreGuidVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Guid)] - [ValueConverter(ConverterType = typeof(LinqToDbGuidVo.LinqToDbValueConverter))] - public LinqToDbGuidVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/IntVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/IntVoTests.cs deleted file mode 100644 index 47311ce7..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/IntVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(int))] - public partial struct AnotherIntVo { } - - public class IntVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); - - (IntVo.From(18) != IntVo.From(19)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(19)).Should().BeFalse(); - - IntVo.From(18).Equals(IntVo.From(18)).Should().BeTrue(); - (IntVo.From(18) == IntVo.From(18)).Should().BeTrue(); - - var original = IntVo.From(18); - var other = IntVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - IntVo.From(18).Equals(AnotherIntVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToInt_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonIntVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedInt = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedInt); - } - - [Fact] - public void CanSerializeToInt_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonIntVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedInt = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedInt).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() - { - var value = 123; - var vo = NewtonsoftJsonIntVo.From(value); - var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromInt_WithSystemTextJsonProvider() - { - var value = 123; - var vo = SystemTextJsonIntVo.From(value); - var serializedInt = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedInt); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToInt_WithBothJsonConverters() - { - var vo = BothJsonIntVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedInt1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedInt2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedInt1); - Assert.Equal(serializedVo2, serializedInt2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonIntVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonIntVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterIntVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreIntVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperIntVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbIntVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonIntVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonIntVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreIntVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreIntVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int32)] - [ValueConverter(ConverterType = typeof(LinqToDbIntVo.LinqToDbValueConverter))] - public LinqToDbIntVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/LongVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/LongVoTests.cs deleted file mode 100644 index 4843e4d6..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/LongVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(long))] - public partial struct AnotherLongVo { } - - public class LongVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); - - (LongVo.From(18) != LongVo.From(19)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(19)).Should().BeFalse(); - - LongVo.From(18).Equals(LongVo.From(18)).Should().BeTrue(); - (LongVo.From(18) == LongVo.From(18)).Should().BeTrue(); - - var original = LongVo.From(18); - var other = LongVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - LongVo.From(18).Equals(AnotherLongVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToLong_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonLongVo.From(123L); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedLong = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedLong); - } - - [Fact] - public void CanSerializeToLong_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonLongVo.From(123L); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedLong = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedLong).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() - { - var value = 123L; - var vo = NewtonsoftJsonLongVo.From(value); - var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromLong_WithSystemTextJsonProvider() - { - var value = 123L; - var vo = SystemTextJsonLongVo.From(value); - var serializedLong = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedLong); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToLong_WithBothJsonConverters() - { - var vo = BothJsonLongVo.From(123L); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedLong1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedLong2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedLong1); - Assert.Equal(serializedVo2, serializedLong2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonLongVo.From(123L); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonLongVo.From(123L); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterLongVo.From(123L); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreLongVo.From(123L) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperLongVo.From(123L), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbLongVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData(123L)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonLongVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonLongVo.From(123L), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreLongVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreLongVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int64)] - [ValueConverter(ConverterType = typeof(LinqToDbLongVo.LinqToDbValueConverter))] - public LinqToDbLongVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/ShortVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/ShortVoTests.cs deleted file mode 100644 index 9f5f6b67..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/ShortVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(short))] - public partial struct AnotherShortVo { } - - public class ShortVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); - - (ShortVo.From(18) != ShortVo.From(19)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(19)).Should().BeFalse(); - - ShortVo.From(18).Equals(ShortVo.From(18)).Should().BeTrue(); - (ShortVo.From(18) == ShortVo.From(18)).Should().BeTrue(); - - var original = ShortVo.From(18); - var other = ShortVo.From(18); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - ShortVo.From(18).Equals(AnotherShortVo.From(18)).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToShort_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonShortVo.From(123); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedShort = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedShort); - } - - [Fact] - public void CanSerializeToShort_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonShortVo.From(123); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedShort = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedShort).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() - { - short value = 123; - var vo = NewtonsoftJsonShortVo.From(value); - var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromShort_WithSystemTextJsonProvider() - { - short value = 123; - var vo = SystemTextJsonShortVo.From(value); - var serializedShort = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedShort); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToShort_WithBothJsonConverters() - { - var vo = BothJsonShortVo.From(123); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedShort1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedShort2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedShort1); - Assert.Equal(serializedVo2, serializedShort2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonShortVo.From(123); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonShortVo.From(123); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterShortVo.From(123); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":" + vo.Value + "}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreShortVo.From(123) }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 123"); - - var value = Assert.Single(results); - Assert.Equal(DapperShortVo.From(123), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbShortVo.From(123) }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData((short)123)] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonShortVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonShortVo.From(123), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreShortVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreShortVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.Int16)] - [ValueConverter(ConverterType = typeof(LinqToDbShortVo.LinqToDbValueConverter))] - public LinqToDbShortVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/StringVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/StringVoTests.cs deleted file mode 100644 index 2a3cce36..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/StringVoTests.cs +++ /dev/null @@ -1,261 +0,0 @@ -#nullable disable -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Threading.Tasks; -using Dapper; -using FluentAssertions; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; -using Xunit; -using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; -using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; -using Vogen.IntegrationTests.TestTypes.StructVos; -using LinqToDB; -using LinqToDB.Data; -using LinqToDB.DataProvider.SQLite; -using LinqToDB.Mapping; - -namespace Vogen.IntegrationTests.SerializationAndConversionTests.StructVos -{ - [ValueObject(underlyingType: typeof(string))] - public partial struct AnotherStringVo { } - - public class StringVoTests - { - [Fact] - public void equality_between_same_value_objects() - { - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); - - (StringVo.From("hello!") != StringVo.From("world!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("world!")).Should().BeFalse(); - - StringVo.From("hello!").Equals(StringVo.From("hello!")).Should().BeTrue(); - (StringVo.From("hello!") == StringVo.From("hello!")).Should().BeTrue(); - - var original = StringVo.From("hello!"); - var other = StringVo.From("hello!"); - - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); - } - - [Fact] - public void equality_between_different_value_objects() - { - StringVo.From("hello!").Equals(AnotherStringVo.From("hello!")).Should().BeFalse(); - } - - [Fact] - public void CanSerializeToString_WithNewtonsoftJsonProvider() - { - var vo = NewtonsoftJsonStringVo.From("foo!"); - - string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); - string serializedString = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - Assert.Equal(serializedVo, serializedString); - } - - [Fact] - public void CanSerializeToString_WithSystemTextJsonProvider() - { - var vo = SystemTextJsonStringVo.From("foo!"); - - string serializedVo = SystemTextJsonSerializer.Serialize(vo); - string serializedString = SystemTextJsonSerializer.Serialize(vo.Value); - - serializedVo.Equals(serializedString).Should().BeTrue(); - } - - [Fact] - public void CanDeserializeFromString_WithNewtonsoftJsonProvider() - { - var value = "foo!"; - var vo = NewtonsoftJsonStringVo.From(value); - var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); - - var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanDeserializeFromString_WithSystemTextJsonProvider() - { - var value = "foo!"; - var vo = SystemTextJsonStringVo.From(value); - var serializedString = SystemTextJsonSerializer.Serialize(value); - - var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - - Assert.Equal(vo, deserializedVo); - } - - [Fact] - public void CanSerializeToString_WithBothJsonConverters() - { - var vo = BothJsonStringVo.From("foo!"); - - var serializedVo1 = NewtonsoftJsonSerializer.SerializeObject(vo); - var serializedString1 = NewtonsoftJsonSerializer.SerializeObject(vo.Value); - - var serializedVo2 = SystemTextJsonSerializer.Serialize(vo); - var serializedString2 = SystemTextJsonSerializer.Serialize(vo.Value); - - Assert.Equal(serializedVo1, serializedString1); - Assert.Equal(serializedVo2, serializedString2); - } - - [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() - { - var vo = NoJsonStringVo.From("foo!"); - - var serialized = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() - { - var vo = NoJsonStringVo.From("foo!"); - - var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - - var expected = $"\"{vo.Value}\""; - - Assert.Equal(expected, serialized); - } - - [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() - { - var vo = NoConverterStringVo.From("foo!"); - - var newtonsoft = SystemTextJsonSerializer.Serialize(vo); - var systemText = SystemTextJsonSerializer.Serialize(vo); - - var expected = "{\"Value\":\"" + vo.Value + "\"}"; - - Assert.Equal(expected, newtonsoft); - Assert.Equal(expected, systemText); - } - - [Fact] - public void WhenEfCoreValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - var original = new EfCoreTestEntity { Id = EfCoreStringVo.From("foo!") }; - using (var context = new TestDbContext(options)) - { - context.Database.EnsureCreated(); - context.Entities.Add(original); - context.SaveChanges(); - } - using (var context = new TestDbContext(options)) - { - var all = context.Entities.ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Fact] - public async Task WhenDapperValueConverterUsesValueConverter() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - IEnumerable results = await connection.QueryAsync("SELECT 'foo!'"); - - var value = Assert.Single(results); - Assert.Equal(DapperStringVo.From("foo!"), value); - } - - [Fact] - public void WhenLinqToDbValueConverterUsesValueConverter() - { - var connection = new SqliteConnection("DataSource=:memory:"); - connection.Open(); - - var original = new LinqToDbTestEntity { Id = LinqToDbStringVo.From("foo!") }; - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - context.CreateTable(); - context.Insert(original); - } - using (var context = new DataConnection( - SQLiteTools.GetDataProvider("SQLite.MS"), - connection, - disposeConnection: false)) - { - var all = context.GetTable().ToList(); - var retrieved = Assert.Single(all); - Assert.Equal(original.Id, retrieved.Id); - } - } - - [Theory] - [InlineData("")] - [InlineData("123")] - public void TypeConverter_CanConvertToAndFrom(object value) - { - var converter = TypeDescriptor.GetConverter(typeof(NoJsonStringVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonStringVo.From(value!.ToString()), id); - - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); - } - - public class TestDbContext : DbContext - { - public DbSet Entities { get; set; } - - public TestDbContext(DbContextOptions options) : base(options) - { - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder - .Entity(builder => - { - builder - .Property(x => x.Id) - .HasConversion(new EfCoreStringVo.EfCoreValueConverter()) - .ValueGeneratedNever(); - }); - } - } - - public class EfCoreTestEntity - { - public EfCoreStringVo Id { get; set; } - } - - public class LinqToDbTestEntity - { - [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(LinqToDbStringVo.LinqToDbValueConverter))] - public LinqToDbStringVo Id { get; set; } - } - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/BoolVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/BoolVo.cs deleted file mode 100644 index 31a53a5d..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/BoolVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] - public partial struct BoolVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(bool))] - public partial struct NoConverterBoolVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(bool))] - public partial struct NoJsonBoolVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(bool))] - public partial struct NewtonsoftJsonBoolVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(bool))] - public partial struct SystemTextJsonBoolVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(bool))] - public partial struct BothJsonBoolVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(bool))] - public partial struct EfCoreBoolVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(bool))] - public partial struct DapperBoolVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(bool))] - public partial struct LinqToDbBoolVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ByteVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ByteVo.cs deleted file mode 100644 index 50179f6e..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ByteVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] - public partial struct ByteVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(byte))] - public partial struct NoConverterByteVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(byte))] - public partial struct NoJsonByteVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(byte))] - public partial struct NewtonsoftJsonByteVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(byte))] - public partial struct SystemTextJsonByteVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(byte))] - public partial struct BothJsonByteVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(byte))] - public partial struct EfCoreByteVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(byte))] - public partial struct DapperByteVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(byte))] - public partial struct LinqToDbByteVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/CharVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/CharVo.cs deleted file mode 100644 index 59f347ca..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/CharVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] - public partial struct CharVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(char))] - public partial struct NoConverterCharVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(char))] - public partial struct NoJsonCharVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(char))] - public partial struct NewtonsoftJsonCharVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(char))] - public partial struct SystemTextJsonCharVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(char))] - public partial struct BothJsonCharVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(char))] - public partial struct EfCoreCharVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(char))] - public partial struct DapperCharVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(char))] - public partial struct LinqToDbCharVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeOffsetVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeOffsetVo.cs deleted file mode 100644 index 11a13475..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeOffsetVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial struct DateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTimeOffset))] - public partial struct NoConverterDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTimeOffset))] - public partial struct NoJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTimeOffset))] - public partial struct NewtonsoftJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial struct SystemTextJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTimeOffset))] - public partial struct BothJsonDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial struct EfCoreDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTimeOffset))] - public partial struct DapperDateTimeOffsetVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTimeOffset))] - public partial struct LinqToDbDateTimeOffsetVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeVo.cs deleted file mode 100644 index 00457345..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DateTimeVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial struct DateTimeVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(DateTime))] - public partial struct NoConverterDateTimeVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(DateTime))] - public partial struct NoJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(DateTime))] - public partial struct NewtonsoftJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial struct SystemTextJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(DateTime))] - public partial struct BothJsonDateTimeVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(DateTime))] - public partial struct EfCoreDateTimeVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(DateTime))] - public partial struct DapperDateTimeVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(DateTime))] - public partial struct LinqToDbDateTimeVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DecimalVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DecimalVo.cs deleted file mode 100644 index ccb8aff6..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DecimalVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial struct DecimalVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(decimal))] - public partial struct NoConverterDecimalVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(decimal))] - public partial struct NoJsonDecimalVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(decimal))] - public partial struct NewtonsoftJsonDecimalVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial struct SystemTextJsonDecimalVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(decimal))] - public partial struct BothJsonDecimalVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(decimal))] - public partial struct EfCoreDecimalVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(decimal))] - public partial struct DapperDecimalVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(decimal))] - public partial struct LinqToDbDecimalVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DoubleVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DoubleVo.cs deleted file mode 100644 index c32b7661..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/DoubleVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] - public partial struct DoubleVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(double))] - public partial struct NoConverterDoubleVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(double))] - public partial struct NoJsonDoubleVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(double))] - public partial struct NewtonsoftJsonDoubleVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(double))] - public partial struct SystemTextJsonDoubleVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(double))] - public partial struct BothJsonDoubleVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(double))] - public partial struct EfCoreDoubleVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(double))] - public partial struct DapperDoubleVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(double))] - public partial struct LinqToDbDoubleVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FloatVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FloatVo.cs deleted file mode 100644 index 4d843e95..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FloatVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] - public partial struct FloatVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(float))] - public partial struct NoConverterFloatVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(float))] - public partial struct NoJsonFloatVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(float))] - public partial struct NewtonsoftJsonFloatVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(float))] - public partial struct SystemTextJsonFloatVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(float))] - public partial struct BothJsonFloatVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(float))] - public partial struct EfCoreFloatVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(float))] - public partial struct DapperFloatVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(float))] - public partial struct LinqToDbFloatVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FooVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FooVo.cs deleted file mode 100644 index 185a1808..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/FooVo.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - public record struct Bar(int Age, string Name); - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial struct FooVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Bar))] - public partial struct NoConverterFooVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial struct NoJsonFooVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] - public partial class NoJsonFooVoClass { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial struct NewtonsoftJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Bar))] - public partial class NewtonsoftJsonFooVoClass { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial struct SystemTextJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial struct BothJsonFooVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Bar))] - public partial class BothJsonFooVoClass { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Bar))] - public partial struct EfCoreFooVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Bar))] - public partial struct DapperFooVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Bar))] - public partial struct LinqToDbFooVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/GuidVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/GuidVo.cs deleted file mode 100644 index 710de375..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/GuidVo.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial struct GuidVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(Guid))] - public partial struct NoConverterGuidVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(Guid))] - public partial struct NoJsonGuidVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(Guid))] - public partial struct NewtonsoftJsonGuidVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial struct SystemTextJsonGuidVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(Guid))] - public partial struct BothJsonGuidVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(Guid))] - public partial struct EfCoreGuidVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(Guid))] - public partial struct DapperGuidVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(Guid))] - public partial struct LinqToDbGuidVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/IntoVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/IntoVo.cs deleted file mode 100644 index d1067cb0..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/IntoVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] - public partial struct IntVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(int))] - public partial struct NoConverterIntVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(int))] - public partial struct NoJsonIntVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(int))] - public partial struct NewtonsoftJsonIntVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(int))] - public partial struct SystemTextJsonIntVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(int))] - public partial struct BothJsonIntVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(int))] - public partial struct EfCoreIntVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(int))] - public partial struct DapperIntVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(int))] - public partial struct LinqToDbIntVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/LongVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/LongVo.cs deleted file mode 100644 index 4dc62d4f..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/LongVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] - public partial struct LongVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(long))] - public partial struct NoConverterLongVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(long))] - public partial struct NoJsonLongVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(long))] - public partial struct NewtonsoftJsonLongVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(long))] - public partial struct SystemTextJsonLongVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(long))] - public partial struct BothJsonLongVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(long))] - public partial struct EfCoreLongVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(long))] - public partial struct DapperLongVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(long))] - public partial struct LinqToDbLongVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ShortVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ShortVo.cs deleted file mode 100644 index b2f48787..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/ShortVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] - public partial struct ShortVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(short))] - public partial struct NoConverterShortVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(short))] - public partial struct NoJsonShortVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(short))] - public partial struct NewtonsoftJsonShortVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(short))] - public partial struct SystemTextJsonShortVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(short))] - public partial struct BothJsonShortVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(short))] - public partial struct EfCoreShortVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(short))] - public partial struct DapperShortVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(short))] - public partial struct LinqToDbShortVo { } -} diff --git a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/StringVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/StringVo.cs deleted file mode 100644 index 6f1bbd17..00000000 --- a/tests/ConsumerTests/SerializationAndConversionTests/StructVos/Types/StringVo.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Vogen.IntegrationTests.TestTypes.StructVos -{ - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] - public partial struct StringVo { } - - [ValueObject(conversions: Conversions.None, underlyingType: typeof(string))] - public partial struct NoConverterStringVo { } - - [ValueObject(conversions: Conversions.TypeConverter, underlyingType: typeof(string))] - public partial struct NoJsonStringVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson, underlyingType: typeof(string))] - public partial struct NewtonsoftJsonStringVo { } - - [ValueObject(conversions: Conversions.SystemTextJson, underlyingType: typeof(string))] - public partial struct SystemTextJsonStringVo { } - - [ValueObject(conversions: Conversions.NewtonsoftJson | Conversions.SystemTextJson, underlyingType: typeof(string))] - public partial struct BothJsonStringVo { } - - [ValueObject(conversions: Conversions.EfCoreValueConverter, underlyingType: typeof(string))] - public partial struct EfCoreStringVo { } - - [ValueObject(conversions: Conversions.DapperTypeHandler, underlyingType: typeof(string))] - public partial struct DapperStringVo { } - - [ValueObject(conversions: Conversions.LinqToDbValueConverter, underlyingType: typeof(string))] - public partial struct LinqToDbStringVo { } -} diff --git a/tests/ConsumerTests/ToStringTests/BasicFunctionality.cs b/tests/ConsumerTests/ToStringTests/BasicFunctionality.cs index 7547ea08..1247d759 100644 --- a/tests/ConsumerTests/ToStringTests/BasicFunctionality.cs +++ b/tests/ConsumerTests/ToStringTests/BasicFunctionality.cs @@ -1,7 +1,5 @@ - -using FluentAssertions; -using Vogen.Tests.Types; -using Xunit; +using FluentAssertions; +using Intellenum.Tests.Types; namespace ConsumerTests.ToStringTests; @@ -10,12 +8,10 @@ public class BasicFunctionality [Fact] public void ToString_uses_generated_method() { - Age.From(18).ToString().Should().Be("18"); - Age.From(100).ToString().Should().Be("100"); - Age.From(1_000).ToString().Should().Be("1000"); + Age.LegalVotingAge.ToString().Should().Be("LegalVotingAge"); + Age.LegalDrivingAge.ToString().Should().Be("LegalDrivingAge"); - Name.From("fred").ToString().Should().Be("fred"); - Name.From("barney").ToString().Should().Be("barney"); - Name.From("wilma").ToString().Should().Be("wilma"); + NameType.FirstAndLast.ToString().Should().Be("FirstAndLast"); + NameType.Nickname.ToString().Should().Be("Nickname"); } } \ No newline at end of file diff --git a/tests/ConsumerTests/ToStringTests/Derivation.cs b/tests/ConsumerTests/ToStringTests/Derivation.cs index 8368c721..0a188708 100644 --- a/tests/ConsumerTests/ToStringTests/Derivation.cs +++ b/tests/ConsumerTests/ToStringTests/Derivation.cs @@ -1,6 +1,5 @@ using FluentAssertions; -using Vogen; -using Xunit; +using Intellenum; namespace ConsumerTests.ToStringTests; @@ -8,19 +7,15 @@ public class Derivation { [Fact] public void ToString_uses_users_method() => - Vo.From(18).ToString().Should().Be("ToString_Vo!"); + Vo.Item1.ToString().Should().Be("ToString_Vo!"); [Fact] public void ToString_uses_derived_method() => - Vod1.From(18).ToString().Should().Be("derived1!"); + Vod1.Item3.ToString().Should().Be("derived1!"); [Fact] public void ToString_uses_least_derived_method() => - Vod2.From(18).ToString().Should().Be("derived2!"); - - [Fact] - public void ToString_on_a_record_uses_least_derived_method() => - Vor1.From(18).ToString().Should().Be("Vor1!"); + Vod2.Item5.ToString().Should().Be("derived2!"); } public class D1 @@ -37,18 +32,24 @@ public class D3 : D2 { } -[ValueObject] +[Intellenum] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class Vo { public override string ToString() => "ToString_Vo!"; } -[ValueObject] +[Intellenum] +[Instance("Item3", 3)] +[Instance("Item4", 4)] public partial class Vod1 : D1 { } -[ValueObject] +[Intellenum] +[Instance("Item5", 5)] +[Instance("Item6", 6)] public partial class Vod2 : D3 { } @@ -57,15 +58,3 @@ public record R1 { public override string ToString() => "R1!"; } - -[ValueObject] -public partial record struct RS1 -{ - public override string ToString() => "RS1!"; -} - -[ValueObject] -public partial record Vor1 : R1 -{ - public sealed override string ToString() => "Vor1!"; -} diff --git a/tests/ConsumerTests/TryParseTests/Tests.cs b/tests/ConsumerTests/TryParseTests/Tests.cs index 5ed838f7..1331727c 100644 --- a/tests/ConsumerTests/TryParseTests/Tests.cs +++ b/tests/ConsumerTests/TryParseTests/Tests.cs @@ -1,8 +1,7 @@ using System; using System.Globalization; using FluentAssertions; -using Vogen; -using Xunit; +using Intellenum; namespace ConsumerTests.TryParseTests; @@ -11,41 +10,29 @@ public class Tests [Fact] public void Integers() { - { - IntVoNoValidation.TryParse("1024", out var ivo).Should().BeTrue(); - ivo.Value.Should().Be(1024); - } - - { - IntVoNoValidation.TryParse("1,000", NumberStyles.AllowThousands, null, out var ivo).Should().BeTrue(); - ivo.Value.Should().Be(1000); - } + IntVoNoValidation.TryParse("1", out var ivo).Should().BeTrue(); + ivo.Value.Should().Be(1); } [Fact] public void Decimals() { { - DecimalVo.TryParse("1024.56", out var ivo).Should().BeTrue(); - ivo.Value.Should().Be(1024.56m); - } - - { - DecimalVo.TryParse("1,000.25", NumberStyles.Number, null, out var ivo).Should().BeTrue(); - ivo.Value.Should().Be(1000.25m); + DecimalVo.TryParse("1.23", out var ivo).Should().BeTrue(); + ivo.Value.Should().Be(1.23m); } { - DecimalVo.TryParse("1.000,25", NumberStyles.Number, new CultureInfo("de"), out var ivo).Should().BeTrue(); - ivo.Value.Should().Be(1000.25m); + DecimalVo.TryParse("1,23", NumberStyles.Number, new CultureInfo("de"), out var ivo).Should().BeTrue(); + ivo.Value.Should().Be(1.23m); } } [Fact] public void Bytes() { - ByteVo.TryParse("12", out var ivo).Should().BeTrue(); - ivo.Value.Should().Be(12); + ByteVo.TryParse("1", out var ivo).Should().BeTrue(); + ivo.Value.Should().Be(1); } [Fact] @@ -58,25 +45,14 @@ public void Chars() [Fact] public void Double() { - DoubleVo.TryParse("123.45", out var ivo).Should().BeTrue(); - ivo.Value.Should().Be(123.45); + DoubleVo.TryParse("1.23", out var ivo).Should().BeTrue(); + ivo.Value.Should().Be(1.23); } [Fact] public void When_parsing_fails() { IntVo.TryParse("fifty", out var ivo).Should().BeFalse(); - - Action a = () => _ = ivo.Value; - - a.Should().ThrowExactly(); - } - - [Fact] - public void When_parsing_succeeds_but_fails_validation() - { - Action a = () => IntVo.TryParse("100", out var _); - - a.Should().ThrowExactly().WithMessage("must be less than 100"); + ivo.Should().BeNull(); } } \ No newline at end of file diff --git a/tests/ConsumerTests/TryParseTests/Types.cs b/tests/ConsumerTests/TryParseTests/Types.cs index 93be9e9e..1aabf59b 100644 --- a/tests/ConsumerTests/TryParseTests/Types.cs +++ b/tests/ConsumerTests/TryParseTests/Types.cs @@ -1,31 +1,42 @@ -using System; -using System.Globalization; -using FluentAssertions; -using Vogen; -using Xunit; +using Intellenum; namespace ConsumerTests.TryParseTests; -[ValueObject(typeof(int))] -public partial struct IntVoNoValidation +[Intellenum(typeof(int))] +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class IntVoNoValidation { } -[ValueObject(typeof(int))] -public partial struct IntVo +[Intellenum(typeof(int))] +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class IntVo { - private static Validation Validate(int input) => - input < 100 ? Validation.Ok : Validation.Invalid("must be less than 100"); } -[ValueObject(typeof(byte))] -public partial struct ByteVo { } +[Intellenum(typeof(byte))] +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class ByteVo { } -[ValueObject(typeof(char))] -public partial struct CharVo { } +[Intellenum(typeof(char))] +[Instance("Item1", 'a')] +[Instance("Item2", 2)] +public partial class CharVo { } -[ValueObject(typeof(decimal))] -public partial struct DecimalVo { } +[Intellenum(typeof(decimal))] +public partial class DecimalVo +{ + static DecimalVo() + { + Instance("Item1", 1.23m); + Instance("Item2", 3.21m); + } +} -[ValueObject(typeof(double))] -public partial struct DoubleVo { } \ No newline at end of file +[Intellenum(typeof(double))] +[Instance("Item1", 1.23d)] +[Instance("Item2", 3.21d)] +public partial class DoubleVo { } \ No newline at end of file diff --git a/tests/ConsumerTests/Types/Age.cs b/tests/ConsumerTests/Types/Age.cs index 95d430e9..34a983b9 100644 --- a/tests/ConsumerTests/Types/Age.cs +++ b/tests/ConsumerTests/Types/Age.cs @@ -1,7 +1,8 @@ -namespace Vogen.Tests.Types; +namespace Intellenum.Tests.Types; -[ValueObject(typeof(int))] +[Intellenum(typeof(int))] +[Instance("LegalVotingAge", 18)] +[Instance("LegalDrivingAge", 17)] public partial class Age { - private static Validation Validate(int value) => value >= 18 ? Validation.Ok : Validation.Invalid("Must be 18 or over"); } \ No newline at end of file diff --git a/tests/ConsumerTests/Types/Anything.cs b/tests/ConsumerTests/Types/Anything.cs index 0babf247..1cd6f143 100644 --- a/tests/ConsumerTests/Types/Anything.cs +++ b/tests/ConsumerTests/Types/Anything.cs @@ -1,6 +1,8 @@ -namespace Vogen.Tests.Types; +namespace Intellenum.Tests.Types; -[ValueObject(typeof(int))] +[Intellenum(typeof(int))] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class Anything { } \ No newline at end of file diff --git a/tests/ConsumerTests/Types/CustomerId.cs b/tests/ConsumerTests/Types/CustomerId.cs deleted file mode 100644 index 42e441cb..00000000 --- a/tests/ConsumerTests/Types/CustomerId.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(int))] -public partial struct CustomerId { } \ No newline at end of file diff --git a/tests/ConsumerTests/Types/CustomerType.cs b/tests/ConsumerTests/Types/CustomerType.cs new file mode 100644 index 00000000..5e285d2f --- /dev/null +++ b/tests/ConsumerTests/Types/CustomerType.cs @@ -0,0 +1,6 @@ +namespace Intellenum.Tests.Types; + +[Intellenum(typeof(int))] +[Instance("Standard", 1)] +[Instance("Gold", 2)] +public partial class CustomerType { } \ No newline at end of file diff --git a/tests/ConsumerTests/Types/Dave.cs b/tests/ConsumerTests/Types/Dave.cs deleted file mode 100644 index db60cbd8..00000000 --- a/tests/ConsumerTests/Types/Dave.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Vogen.Tests.Types; - -[ValueObject(typeof(string))] -public partial class Dave -{ - private static Validation Validate(string value) => value.StartsWith("dave ", StringComparison.OrdinalIgnoreCase) || - value.StartsWith("david ", StringComparison.OrdinalIgnoreCase) - ? Validation.Ok - : Validation.Invalid("must be a dave or david"); -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/EightiesDate.cs b/tests/ConsumerTests/Types/EightiesDate.cs deleted file mode 100644 index 58ce4775..00000000 --- a/tests/ConsumerTests/Types/EightiesDate.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace Vogen.Tests.Types; - -[ValueObject(typeof(DateTime))] -public partial class EightiesDate -{ - private static Validation Validate(DateTime value) => value.Year is >= 1980 and <= 1989 - ? Validation.Ok - : Validation.Invalid("Must be a date in the 80's"); -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/EscapedTypesAndNamespaces.cs b/tests/ConsumerTests/Types/EscapedTypesAndNamespaces.cs index e66114c0..93e721ff 100644 --- a/tests/ConsumerTests/Types/EscapedTypesAndNamespaces.cs +++ b/tests/ConsumerTests/Types/EscapedTypesAndNamespaces.cs @@ -1,21 +1,37 @@ #pragma warning disable IDE1006 // Naming Styles -using Vogen; +using System; +using Intellenum; +using record.@struct.@float; namespace record.@struct.@float { - public readonly record struct @decimal(); + public readonly record struct @decimal() : IComparable<@decimal> + { + public int CompareTo(@decimal other) => throw new NotImplementedException(); + } + } namespace @double { - public readonly record struct @decimal(); + public readonly record struct @decimal() : IComparable<@decimal> + { + public int CompareTo(@decimal other) => throw new NotImplementedException(); + } - [ValueObject(typeof(@decimal))] + [Intellenum(typeof(@decimal))] public partial class classFromEscapedNamespaceWithReservedUnderlyingType { + static classFromEscapedNamespaceWithReservedUnderlyingType() + { + Instance("One", new @decimal()); + Instance("Two", new @decimal()); + } } - [ValueObject] + [Intellenum] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class classFromEscapedNamespace { } @@ -23,18 +39,28 @@ public partial class classFromEscapedNamespace namespace @bool.@byte.@short.@float.@object { - [ValueObject] + [Intellenum] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class @class { } - [ValueObject] + [Intellenum] + [Instance("Item1", 1)] + [Instance("Item2", 2)] public partial class @event { } - [ValueObject(typeof(record.@struct.@float.@decimal))] + [Intellenum(typeof(record.@struct.@float.@decimal))] public partial class @event2 { + static event2() + { + Instance("Item1", new @decimal()); + Instance("Item2", new @decimal()); + + } } } \ No newline at end of file diff --git a/tests/ConsumerTests/Types/MinimalValidation.cs b/tests/ConsumerTests/Types/MinimalValidation.cs deleted file mode 100644 index b3f4ee1f..00000000 --- a/tests/ConsumerTests/Types/MinimalValidation.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(int))] -public partial class MinimalValidation -{ - private static Validation Validate(int value) => - value != 1 ? Validation.Ok : Validation.Invalid(); -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/MyInt.cs b/tests/ConsumerTests/Types/MyInt.cs index bef1c977..330b52ac 100644 --- a/tests/ConsumerTests/Types/MyInt.cs +++ b/tests/ConsumerTests/Types/MyInt.cs @@ -1,27 +1,17 @@ -namespace Vogen.Tests.Types; +namespace Intellenum.Tests.Types; -[ValueObject(typeof(int))] +[Intellenum(typeof(int))] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class MyInt { - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } } #if NET7_0_OR_GREATER -[ValueObject] +[Intellenum] +[Instance("Item1", 1)] +[Instance("Item2", 2)] public partial class MyIntGeneric { - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } } #endif \ No newline at end of file diff --git a/tests/ConsumerTests/Types/MyIntStruct.cs b/tests/ConsumerTests/Types/MyIntStruct.cs deleted file mode 100644 index 7b23bd9b..00000000 --- a/tests/ConsumerTests/Types/MyIntStruct.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(int))] -public readonly partial struct MyIntStruct -{ - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/MyIntStructWithADefaultOf22.cs b/tests/ConsumerTests/Types/MyIntStructWithADefaultOf22.cs deleted file mode 100644 index 513aec72..00000000 --- a/tests/ConsumerTests/Types/MyIntStructWithADefaultOf22.cs +++ /dev/null @@ -1,30 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(int))] -[Instance(name: "Default", value: 22)] -[Instance(name: "Default2", value: 33)] -public partial struct MyIntStructWithADefaultOf22 -{ - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} - -[ValueObject(typeof(int))] -[Instance(name: "event", value: 22)] -[Instance(name: "class", value: 33)] -public partial struct MyIntStructWithReservedNames -{ - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} - diff --git a/tests/ConsumerTests/Types/MyRecord.cs b/tests/ConsumerTests/Types/MyRecord.cs deleted file mode 100644 index e6686529..00000000 --- a/tests/ConsumerTests/Types/MyRecord.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Vogen; - -namespace ConsumerTests.Types; - -[ValueObject] -public partial record MyRecord -{ - private static Validation Validate(int value) => value >= 0 ? Validation.Ok : Validation.Invalid("must be zero or more"); -} - -[ValueObject(typeof(string))] -public partial record MyStringRecord -{ - private static Validation Validate(string value) => !value.Contains(" ") ? Validation.Ok : Validation.Invalid("no spaces allowed"); - private static string NormalizeInput(string value) => value.Replace(" ", ""); -} - -[ValueObject] -public partial record MyRecordStruct -{ - private static Validation Validate(int value) => value >= 0 ? Validation.Ok : Validation.Invalid("must be zero or more"); -} - -[ValueObject(typeof(string))] -public partial record MyStringRecordStruct -{ - private static Validation Validate(string value) => !value.Contains(" ") ? Validation.Ok : Validation.Invalid("no spaces allowed"); - private static string NormalizeInput(string value) => value.Replace(" ", ""); -} diff --git a/tests/ConsumerTests/Types/MyRecordClassInt.cs b/tests/ConsumerTests/Types/MyRecordClassInt.cs deleted file mode 100644 index 324f1a66..00000000 --- a/tests/ConsumerTests/Types/MyRecordClassInt.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(int))] -public partial class MyRecordClassInt -{ - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} - - -[ValueObject(typeof(int))] -public partial class MyRecordStructInt -{ - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/MyRecordClassString.cs b/tests/ConsumerTests/Types/MyRecordClassString.cs deleted file mode 100644 index e37e1499..00000000 --- a/tests/ConsumerTests/Types/MyRecordClassString.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(string))] -public partial record class MyRecordClassString -{ - private static Validation Validate(string value) - { - if (value.Length > 0) - return Validation.Ok; - - return Validation.Invalid("length must be greater than zero"); - } -} - -[ValueObject(typeof(string))] -public partial record class MyRecordStructString -{ - private static Validation Validate(string value) - { - if (value.Length > 0) - return Validation.Ok; - - return Validation.Invalid("length must be greater than zero"); - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/MyString.cs b/tests/ConsumerTests/Types/MyString.cs index 6a43ce8c..6351902c 100644 --- a/tests/ConsumerTests/Types/MyString.cs +++ b/tests/ConsumerTests/Types/MyString.cs @@ -1,13 +1,8 @@ -namespace Vogen.Tests.Types; +namespace Intellenum.Tests.Types; -[ValueObject(typeof(string))] +[Intellenum(typeof(string))] +[Instance("Item1", "Item1")] +[Instance("Item2", "Item2")] public partial class MyString { - private static Validation Validate(string value) - { - if (value.Length > 0) - return Validation.Ok; - - return Validation.Invalid("length must be greater than zero"); - } } \ No newline at end of file diff --git a/tests/ConsumerTests/Types/Name.cs b/tests/ConsumerTests/Types/Name.cs deleted file mode 100644 index aca0b184..00000000 --- a/tests/ConsumerTests/Types/Name.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(string))] -[Instance(name: "Invalid", value: "xxx")] -public partial class Name -{ - private static Validation Validate(string value) - { - if (value.Length > 0) - return Validation.Ok; - - return Validation.Invalid("name cannot be empty"); - } -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/NameType.cs b/tests/ConsumerTests/Types/NameType.cs new file mode 100644 index 00000000..df958372 --- /dev/null +++ b/tests/ConsumerTests/Types/NameType.cs @@ -0,0 +1,9 @@ +namespace Intellenum.Tests.Types; + +[Intellenum(typeof(string))] +[Instance(name: "FirstAndLast", value: "F&L")] +[Instance(name: "Nickname", value: "INFORMAL")] +[Instance(name: "Invalid", value: "xxx")] +public partial class NameType +{ +} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/Number.cs b/tests/ConsumerTests/Types/Number.cs index 0d97473b..9f2bac09 100644 --- a/tests/ConsumerTests/Types/Number.cs +++ b/tests/ConsumerTests/Types/Number.cs @@ -1,7 +1,9 @@ -namespace Vogen.Tests.Types; +namespace Intellenum.Tests.Types; -[ValueObject(typeof(int))] -public readonly partial struct Number +[Intellenum(typeof(int))] +[Instance("Item1", 1)] +[Instance("Item2", 2)] +public partial class Number { } @@ -9,7 +11,7 @@ public readonly partial struct Number // /// // /// A Value Object that is not supported // /// -// [ValueObject(typeof(List))] +// [Intellenum(typeof(List))] // public partial class Daves // { // private static Validation Validate(List value) => value.Count > 0 ? Validation.Ok : Validation.Invalid("no dave's found"); diff --git a/tests/ConsumerTests/Types/Score.cs b/tests/ConsumerTests/Types/Score.cs deleted file mode 100644 index 71b1bb39..00000000 --- a/tests/ConsumerTests/Types/Score.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Vogen.Tests.Types; - -[ValueObject(typeof(int))] -public partial class Score -{ - private static Validation Validate(int value) => value >= 0 ? Validation.Ok : Validation.Invalid("Score must be zero or more"); -} \ No newline at end of file diff --git a/tests/ConsumerTests/Types/ScoreType.cs b/tests/ConsumerTests/Types/ScoreType.cs new file mode 100644 index 00000000..d8ffae4d --- /dev/null +++ b/tests/ConsumerTests/Types/ScoreType.cs @@ -0,0 +1,10 @@ +namespace Intellenum.Tests.Types; +using Intellenum; +using System; + +[Intellenum(typeof(string))] +[Instance("Points", "Points")] +[Instance("Percentage", "Percentage")] +public partial class ScoreType +{ +} \ No newline at end of file diff --git a/tests/ConsumerTests/ValidationTests.cs b/tests/ConsumerTests/ValidationTests.cs deleted file mode 100644 index 838bef11..00000000 --- a/tests/ConsumerTests/ValidationTests.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System; -using FluentAssertions; -using Vogen.Tests.Types; -using Xunit; - -namespace Vogen.Tests.ValidationTests; - -public class ValidationTests -{ - [Fact] - public void Validation_passes() - { - Action camelCase = () => MyVo_validate_with_camelCase_method_name.From(-1); - Action pascalCase = () => MyVo_validate_with_PascalCase_method_name.From(-1); - - camelCase.Should().ThrowExactly().WithMessage("must be greater than zero"); - pascalCase.Should().ThrowExactly().WithMessage("must be greater than zero"); - } - - [Fact] - public void Validation_can_have_different_synonyms_for_input() - { - Action vo = () => MyVo_validate_with_synonymous_input_parameter.From(-1); - - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); - } - - [Fact] - public void Validation() - { - Func act = () => Age.From(12); - act.Should().ThrowExactly(); - - Func act2 = () => EightiesDate.From(new DateTime(1990, 1, 1)); - act2.Should().ThrowExactly(); - - Func act3 = () => EightiesDate.From(new DateTime(1985, 6, 10)); - act3.Should().NotThrow(); - - string[] validDaves = new[] { "dave grohl", "david beckham", "david bowie" }; - foreach (var name in validDaves) - { - Func act4 = () => Dave.From(name); - act4.Should().NotThrow(); - } - - string[] invalidDaves = new[] { "dafid jones", "fred flintstone", "davidoff cool water" }; - foreach (var name in invalidDaves) - { - Func act5 = () => Dave.From(name); - act5.Should().ThrowExactly(); - } - - Func act6 = () => MinimalValidation.From(1); - act6.Should().ThrowExactly().WithMessage("[none provided]"); - } - - [Fact] - public void No_validation() - { - Func act = () => Anything.From(int.MaxValue); - act.Should().NotThrow(); - } -} - -[ValueObject(typeof(int))] -public partial class MyVo_validate_with_camelCase_method_name -{ - private static Validation validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} - -[ValueObject(typeof(int))] -public partial class MyVo_validate_with_synonymous_input_parameter -{ - // the input type is synonymous the same as the underlying type - private static Validation validate(Int32 value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} - - -[ValueObject(typeof(int))] -public partial class MyVo_validate_with_PascalCase_method_name -{ - private static Validation Validate(int value) - { - if (value > 0) - return Validation.Ok; - - return Validation.Invalid("must be greater than zero"); - } -} diff --git a/tests/ScratchSnapshotTests/Explicit_instance_tests.cs b/tests/ScratchSnapshotTests/Explicit_instance_tests.cs index 5d0c3da1..e0803032 100644 --- a/tests/ScratchSnapshotTests/Explicit_instance_tests.cs +++ b/tests/ScratchSnapshotTests/Explicit_instance_tests.cs @@ -110,7 +110,7 @@ private static Task RunTest(string source) => new SnapshotRunner() .WithSource(source) .IgnoreInitialCompilationErrors() - //.IgnoreFinalCompilationErrors() + .IgnoreFinalCompilationErrors() .RunOnAllFrameworks(); } } \ No newline at end of file diff --git a/tests/ScratchSnapshotTests/GeneralTests.cs b/tests/ScratchSnapshotTests/GeneralTests.cs index 19d4d434..fd4aca55 100644 --- a/tests/ScratchSnapshotTests/GeneralTests.cs +++ b/tests/ScratchSnapshotTests/GeneralTests.cs @@ -7,6 +7,201 @@ namespace ScratchSnapshotTests [UsesVerify] public class GeneralTests { + [Fact] + public Task test_int_again() + { + var source = """ +using System; +using Intellenum; + +namespace record.@struct.@float +{ + public readonly record struct @decimal() : IComparable<@decimal> + { + public int CompareTo(@decimal other) => throw new NotImplementedException(); + } + +} + +namespace SomethingElse +{ + + [Intellenum(typeof(record.@struct.@float.@decimal))] + public partial class @event2 + { + static @event2() + { + Instance("One", new record.@struct.@float.@decimal()); + Instance("Two", new record.@struct.@float.@decimal()); + } + } +} +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task test_short() + { + var source = """ +using System; +using Intellenum; + +namespace SomethingElse +{ + [Intellenum(underlyingType: typeof(short))] + [Instance("Item1", 123)] + [Instance("Item2", 321)] + public partial class ShortHolderId_normal + { + } +} +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task test_short_id_as_string() + { + var source = """ +using System; +using Intellenum; + +namespace SomethingElse +{ + [Intellenum(underlyingType: typeof(short), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] + [Instance("Item1", 123)] + [Instance("Item2", 321)] + public partial class ShortHolderId_string + { + } +} +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task date_time_offset() + { + var source = """ +using System; +using Intellenum; + +namespace SomethingElse +{ + [Intellenum(underlyingType: typeof(DateTimeOffset))] + public partial class DateTimeOffsetVo + { + static DateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + Instance("SomethingElse", new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); + } + } +} +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + //.IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task test_datetimeoffset() + { + var source = """ +using System; +using Intellenum; + + [Intellenum(underlyingType: typeof(DateTimeOffset))] + public partial class DateTimeOffsetVo + { + static DateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); + Instance("SomethingElse", new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); + } + } +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task test_bool() + { + var source = """ +using System; +using Intellenum; + + [Intellenum(underlyingType: typeof(bool))] + [Instance("No", false)] + [Instance("Yes", true)] + public partial class BoolVo { } +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task test1() + { + var source = """ +using System; +using Intellenum; + +[Intellenum(underlyingType: typeof(decimal), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] +public partial class DecimalHolderId_string +{ + static DecimalHolderId_string() + { + Instance("Item1", 720742592373919744m); + Instance("Item2", 2.2m); + } +} +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + [Fact] public Task custom_type_literal_new3() { @@ -27,6 +222,127 @@ public record class Foo(string Name, int Age) : IComparable { public int CompareTo(Foo other) => Age.CompareTo(other.Age); } + +public class Tester +{ +public FooEnum _myField = FooEnum.Item1; +} +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task custom_type_literal_new4() + { + var source = """ +using System; +using Intellenum; + +namespace Whatever; + + [Intellenum(underlyingType: typeof(Bar))] + public partial class FooVo + { + public static readonly FooVo Fred = new FooVo("Item1", new Bar(42, "Fred")); + public static readonly FooVo Wilma = new FooVo("Item2", new Bar(52, "Wilma")); + } + + public record class Bar(int Age, string Name) : IComparable + { + public int CompareTo(Bar other) => Age.CompareTo(other.Age); + } +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task custom_type_literal_new5() + { + var source = """ +using System; +using Intellenum; + +namespace Whatever; + + [Intellenum] + public partial class FooEnum + { + public static readonly FooEnum Fred = new FooEnum("Item1", new Foo("Fred", 42)); + public static readonly FooEnum Wilma = new FooEnum("Item2", new Foo("Wilma", 52)); + } + + public record class Foo(string Name, int Age) : IComparable + { + public int CompareTo(Foo other) => Age.CompareTo(other.Age); + } +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task custom_type_literal_new_explicit_field_name() + { + var source = """ +using System; +using Intellenum; + +namespace Whatever; + + [Intellenum] + public partial class FooEnum + { + public static readonly FooEnum Instance1 = new FooEnum(1); + public static readonly FooEnum Instance2 = new FooEnum(2); + public static readonly FooEnum Instance3 = new FooEnum("INSTANCE 3!!", 3); + } +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + .IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + + [Fact] + public Task custom_type_literal_new_infer_field_name() + { + var source = """ +using System; +using Intellenum; + +namespace Whatever; + + [Intellenum] + public partial class FooEnum + { + public static readonly FooEnum Fred = new FooEnum(new Foo("Fred", 42)); + public static readonly FooEnum Wilma = new FooEnum(new Foo("Wilma", 52)); + } + + public record class Foo(string Name, int Age) : IComparable + { + public int CompareTo(Foo other) => Age.CompareTo(other.Age); + } """; return new SnapshotRunner() diff --git a/tests/ScratchSnapshotTests/SnapshotRunner.cs b/tests/ScratchSnapshotTests/SnapshotRunner.cs index 1ebe1d13..97680e67 100644 --- a/tests/ScratchSnapshotTests/SnapshotRunner.cs +++ b/tests/ScratchSnapshotTests/SnapshotRunner.cs @@ -112,7 +112,7 @@ public async Task RunOn(params TargetFramework[] frameworks) //todo: remove #if DEBUG - // await File.WriteAllTextAsync(@"c:\git\intellenum\tests\scratchsnapshottests\test.cs", output); + //await File.WriteAllTextAsync(@"c:\git\intellenum\tests\scratchsnapshottests\test.cs", output); #endif diagnostics.Should().BeEmpty(@$"because the following source code should compile on {eachFramework}: " + _source); diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt index 3e39f41e..050316f7 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -172,7 +182,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -184,13 +194,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Standard): + case ("Standard"): instance = CustomerType.Standard; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Platinum): + case ("Platinum"): instance = CustomerType.Platinum; return true; default: @@ -317,22 +327,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -417,6 +411,134 @@ yield return Platinum; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt index fdf1595c..845d6057 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -168,7 +178,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -180,10 +190,10 @@ _ => false { switch (name) { - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; default: @@ -309,22 +319,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -404,6 +398,134 @@ yield return Diamond; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt index 6a2476a9..cc74dd3f 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -172,7 +182,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -184,13 +194,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Standard): + case ("Standard"): instance = CustomerType.Standard; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; default: @@ -317,22 +327,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -413,6 +407,134 @@ yield return Gold; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt index 74c6b9f2..a5600ed7 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -168,7 +178,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -180,10 +190,10 @@ _ => false { switch (name) { - case nameof(CustomerType.Standard): + case ("Standard"): instance = CustomerType.Standard; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; default: @@ -309,22 +319,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - public static global::System.Collections.Generic.IEnumerable List() @@ -396,6 +390,134 @@ yield return Gold; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt index 74c6b9f2..a5600ed7 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -168,7 +178,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -180,10 +190,10 @@ _ => false { switch (name) { - case nameof(CustomerType.Standard): + case ("Standard"): instance = CustomerType.Standard; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; default: @@ -309,22 +319,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - public static global::System.Collections.Generic.IEnumerable List() @@ -396,6 +390,134 @@ yield return Gold; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt index 9216d6f2..3701ebc6 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -172,7 +182,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -184,13 +194,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; default: @@ -317,22 +327,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -417,6 +411,134 @@ yield return Diamond; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt index d4b005fb..c277c6ef 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.String _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.String ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.String ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.String value) + private CustomerType(System.String value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.String value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -172,7 +182,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -184,13 +194,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; default: @@ -215,7 +225,7 @@ _ => false { if (value is null) { - throw new global::System.InvalidOperationException("Cannot create a value object with null."); + throw new IntellenumCreationFailedException("Cannot create an Intellenum instance with a null."); } @@ -236,7 +246,7 @@ _ => false { if (value is null) { - throw new global::System.InvalidOperationException("Cannot create a value object with null."); + throw new IntellenumCreationFailedException("Cannot create an Intellenum instance with a null."); } @@ -325,22 +335,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -421,6 +415,8 @@ yield return Diamond; + + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt index 594ccf4d..dd8d9911 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt @@ -49,20 +49,22 @@ namespace @double #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private @class(string name, System.Int32 value) + private @class(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private @class(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"@class has no matching instances with a value of '{value}'"); } /// @@ -176,7 +186,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"@class has no matching instances named '{name}'"); } /// @@ -188,16 +198,16 @@ _ => false { switch (name) { - case nameof(@class.@struct): + case ("@struct"): instance = @class.@struct; return true; - case nameof(@class.@double): + case ("@double"): instance = @class.@double; return true; - case nameof(@class.@event): + case ("@event"): instance = @class.@event; return true; - case nameof(@class.@void): + case ("@void"): instance = @class.@void; return true; default: @@ -325,22 +335,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -430,6 +424,92 @@ yield return @void; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class @classDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt index 1f70d9ae..74e68dc8 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt @@ -47,20 +47,22 @@ using System; #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -83,7 +85,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -100,14 +102,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -118,7 +128,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -170,7 +180,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -182,13 +192,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; default: @@ -315,22 +325,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -415,6 +409,92 @@ yield return Diamond; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt index bc90640c..324db39e 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -172,7 +182,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -184,13 +194,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; default: @@ -317,22 +327,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -417,6 +411,92 @@ yield return Diamond; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt index e33945d8..9751e7c5 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -180,7 +190,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -192,19 +202,19 @@ _ => false { switch (name) { - case nameof(CustomerType.Unspecified): + case ("Unspecified"): instance = CustomerType.Unspecified; return true; - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; - case nameof(CustomerType.Legacy): + case ("Legacy"): instance = CustomerType.Legacy; return true; default: @@ -333,22 +343,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -449,6 +443,92 @@ yield return Legacy; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt index 594ccf4d..5bdd8616 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt @@ -49,20 +49,22 @@ namespace @double #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private @class(string name, System.Int32 value) + private @class(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private @class(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"@class has no matching instances with a value of '{value}'"); } /// @@ -176,7 +186,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"@class has no matching instances named '{name}'"); } /// @@ -188,16 +198,16 @@ _ => false { switch (name) { - case nameof(@class.@struct): + case ("@struct"): instance = @class.@struct; return true; - case nameof(@class.@double): + case ("@double"): instance = @class.@double; return true; - case nameof(@class.@event): + case ("@event"): instance = @class.@event; return true; - case nameof(@class.@void): + case ("@void"): instance = @class.@void; return true; default: @@ -325,22 +335,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -430,6 +424,134 @@ yield return @void; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class @classDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt index 1f70d9ae..90d5d0a4 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt @@ -47,20 +47,22 @@ using System; #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -83,7 +85,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -100,14 +102,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -118,7 +128,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -170,7 +180,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -182,13 +192,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; default: @@ -315,22 +325,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -415,6 +409,134 @@ yield return Diamond; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt index bc90640c..9610c0d0 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -172,7 +182,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -184,13 +194,13 @@ _ => false { switch (name) { - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; default: @@ -317,22 +327,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -417,6 +411,134 @@ yield return Diamond; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt index e33945d8..0f2b3fbb 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -180,7 +190,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -192,19 +202,19 @@ _ => false { switch (name) { - case nameof(CustomerType.Unspecified): + case ("Unspecified"): instance = CustomerType.Unspecified; return true; - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; - case nameof(CustomerType.Diamond): + case ("Diamond"): instance = CustomerType.Diamond; return true; - case nameof(CustomerType.Legacy): + case ("Legacy"): instance = CustomerType.Legacy; return true; default: @@ -333,22 +343,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -449,6 +443,134 @@ yield return Legacy; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt index f75fcff2..64f827ab 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -168,7 +178,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -180,10 +190,10 @@ _ => false { switch (name) { - case nameof(CustomerType.@class): + case ("@class"): instance = CustomerType.@class; return true; - case nameof(CustomerType.@event): + case ("@event"): instance = CustomerType.@event; return true; default: @@ -309,22 +319,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -404,6 +398,134 @@ yield return @event; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt index 8c7e43de..62aab702 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt @@ -49,20 +49,22 @@ namespace @double #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private @class(string name, System.Int32 value) + private @class(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private @class(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"@class has no matching instances with a value of '{value}'"); } /// @@ -172,7 +182,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"@class has no matching instances named '{name}'"); } /// @@ -184,13 +194,13 @@ _ => false { switch (name) { - case nameof(@class.@struct): + case ("@struct"): instance = @class.@struct; return true; - case nameof(@class.@event): + case ("@event"): instance = @class.@event; return true; - case nameof(@class.@void): + case ("@void"): instance = @class.@void; return true; default: @@ -317,22 +327,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -417,6 +411,134 @@ yield return @void; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out @class result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class @classDebugView { diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt index efc5f392..f1674c85 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt @@ -47,20 +47,22 @@ using System; #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -83,7 +85,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -100,14 +102,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -118,7 +128,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -166,7 +176,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -178,10 +188,10 @@ _ => false { switch (name) { - case nameof(CustomerType.Normal): + case ("Normal"): instance = CustomerType.Normal; return true; - case nameof(CustomerType.Gold): + case ("Gold"): instance = CustomerType.Gold; return true; default: @@ -307,22 +317,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -402,6 +396,134 @@ yield return Gold; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt index 2d46bc8d..487ec4ab 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int32 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int32 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int32 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int32 value) + private CustomerType(System.Int32 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int32 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -180,7 +190,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -192,19 +202,19 @@ _ => false { switch (name) { - case nameof(CustomerType.Unspecified): + case ("Unspecified"): instance = CustomerType.Unspecified; return true; - case nameof(CustomerType.Unspecified1): + case ("Unspecified1"): instance = CustomerType.Unspecified1; return true; - case nameof(CustomerType.Unspecified2): + case ("Unspecified2"): instance = CustomerType.Unspecified2; return true; - case nameof(CustomerType.Unspecified3): + case ("Unspecified3"): instance = CustomerType.Unspecified3; return true; - case nameof(CustomerType.Preferred): + case ("Preferred"): instance = CustomerType.Preferred; return true; default: @@ -333,22 +343,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -449,6 +443,134 @@ yield return Preferred; + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int32.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt index 6ae655ff..8aa33326 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt @@ -49,20 +49,22 @@ namespace Whatever #endif private readonly global::System.Boolean _isInitialized; private readonly System.Int64 _value; + - /// - /// Gets the underlying value if set, otherwise a is thrown. - /// - public System.Int64 ValueChecked - { - [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - [global::System.Diagnostics.DebuggerStepThroughAttribute] - get - { - if(!_isInitialized) Throw(); - return _value; - } - } + +// /// +// /// Gets the underlying value if set, otherwise a is thrown. +// /// +// public System.Int64 ValueChecked +// { +// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] +// [global::System.Diagnostics.DebuggerStepThroughAttribute] +// get +// { +// if(!_isInitialized) Throw(); +// return _value; +// } +// } /// /// Gets the underlying value if set, otherwise default @@ -85,7 +87,7 @@ private void Throw() global::System.String message = "Use of uninitialized Value Object."; #endif - throw new IntellenumValidationException(message); + throw new IntellenumUninitialisedException(message); } @@ -102,14 +104,22 @@ private void Throw() } [global::System.Diagnostics.DebuggerStepThroughAttribute] - private CustomerType(string name, System.Int64 value) + private CustomerType(System.Int64 value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private CustomerType(string enumName, System.Int64 value) { _value = value; - Name = name; + Name = enumName; _isInitialized = true; } - public string Name { get; } + public string Name { get; private set; } /// /// Builds an instance from an enum value. @@ -120,7 +130,7 @@ private void Throw() { bool b = TryFromValue(value, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums with a value of '{value}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances with a value of '{value}'"); } /// @@ -180,7 +190,7 @@ _ => false { bool b = TryFromName(name, out var ret); if(b) return ret; - throw new global::System.InvalidOperationException($"No matching enums named '{name}'"); + throw new IntellenumMatchFailedException($"CustomerType has no matching instances named '{name}'"); } /// @@ -192,19 +202,19 @@ _ => false { switch (name) { - case nameof(CustomerType.Unspecified): + case ("Unspecified"): instance = CustomerType.Unspecified; return true; - case nameof(CustomerType.Unspecified1): + case ("Unspecified1"): instance = CustomerType.Unspecified1; return true; - case nameof(CustomerType.Unspecified2): + case ("Unspecified2"): instance = CustomerType.Unspecified2; return true; - case nameof(CustomerType.Unspecified3): + case ("Unspecified3"): instance = CustomerType.Unspecified3; return true; - case nameof(CustomerType.Cust42): + case ("Cust42"): instance = CustomerType.Cust42; return true; default: @@ -333,22 +343,6 @@ _ => false } } - private void EnsureInitialized() - { - if (!_isInitialized) - { -#if DEBUG - global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; -#else - global::System.String message = "Use of uninitialized Value Object."; -#endif - - // todo: remove this - throw new global::System.InvalidOperationException(message); - } - } - - // instance... @@ -459,6 +453,134 @@ yield return Cust42; } + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int64.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int64.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(global::System.ReadOnlySpan s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int64.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.Globalization.NumberStyles style, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int64.TryParse(s, style, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int64.TryParse(s, provider, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out CustomerType result) { + if(System.Int64.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + internal sealed class CustomerTypeDebugView { From 8ed73640c4170058f6c5dfebb94266cf7cb593f1 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Thu, 30 Mar 2023 23:41:04 +0100 Subject: [PATCH 3/8] More tests running --- .../IntellenumValidationException.cs | 22 --- src/Intellenum/Generators/ClassGenerator.cs | 14 -- .../StringDeserializationValidationTests.cs | 64 ++------- .../IntDeserializationValidationTests.cs | 82 +---------- .../SharedTypes.cs | 21 +-- .../StringDeserializationValidationTests.cs | 127 +++--------------- tests/ConsumerTests/ModuleInitialization.cs | 2 +- .../ClassVos/DecimalVoTests.cs | 20 +-- .../ClassVos/DoubleVoTests.cs | 16 +-- .../ClassVos/FloatVoTests.cs | 16 +-- .../ComplexSerializationTests.cs | 2 +- .../CustomizationTests.cs | 2 +- ...ces_using_Instance_attributes.verified.txt | 14 -- ...stances_using_Instance_method.verified.txt | 14 -- ...using_a_mixture_of_mechanisms.verified.txt | 14 -- ....Explicit_instances_using_new.verified.txt | 14 -- ...tances_using_target_typed_new.verified.txt | 14 -- ...ests.int_created_successfully.verified.txt | 14 -- ...s.string_created_successfully.verified.txt | 14 -- ...es_can_have_reserved_keywords.verified.txt | 14 -- .../GeneralTests.No_namespace.verified.txt | 14 -- ...al_class_created_successfully.verified.txt | 14 -- ...neralTests.Produces_instances.verified.txt | 14 -- ...es_can_have_reserved_keywords.verified.txt | 14 -- .../GeneralTests.No_namespace.verified.txt | 14 -- ...al_class_created_successfully.verified.txt | 14 -- ...neralTests.Produces_instances.verified.txt | 14 -- ...es_can_have_reserved_keywords.verified.txt | 14 -- ...es_can_have_reserved_keywords.verified.txt | 14 -- ...icAttributeTests.No_namespace.verified.txt | 14 -- ...ibuteTests.Produces_instances.verified.txt | 14 -- ...tances_with_derived_attribute.verified.txt | 14 -- 32 files changed, 77 insertions(+), 591 deletions(-) diff --git a/src/Intellenum.SharedTypes/IntellenumValidationException.cs b/src/Intellenum.SharedTypes/IntellenumValidationException.cs index 64ac9b7a..f8cd5058 100644 --- a/src/Intellenum.SharedTypes/IntellenumValidationException.cs +++ b/src/Intellenum.SharedTypes/IntellenumValidationException.cs @@ -25,28 +25,6 @@ protected IntellenumException( } } -[Serializable] -public class IntellenumValidationException : IntellenumException -{ - public IntellenumValidationException() - { - } - - public IntellenumValidationException(string message) : base(message) - { - } - - public IntellenumValidationException(string message, Exception inner) : base(message, inner) - { - } - - protected IntellenumValidationException( - SerializationInfo info, - StreamingContext context) : base(info, context) - { - } -} - [Serializable] public class IntellenumUninitialisedException : IntellenumException { diff --git a/src/Intellenum/Generators/ClassGenerator.cs b/src/Intellenum/Generators/ClassGenerator.cs index 7f570e09..66c3b90c 100644 --- a/src/Intellenum/Generators/ClassGenerator.cs +++ b/src/Intellenum/Generators/ClassGenerator.cs @@ -35,20 +35,6 @@ public string BuildClass(VoWorkItem item, TypeDeclarationSyntax tds) {InstanceGeneration.GeneratePrivateConstructionInitialisationIfNeeded(item)} -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public {itemUnderlyingType} ValueChecked -// {{ -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// {{ -// if(!_isInitialized) Throw(); -// return _value; -// }} -// }} - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs b/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs index fe395b62..9f0391f9 100644 --- a/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs +++ b/tests/ConsumerTests/DeserializationValidationTests/StringDeserializationValidationTests.cs @@ -22,11 +22,11 @@ public async void Deserialization_dapper() var actual = (await connection.QueryAsync("SELECT 'Item1!'")).AsList()[0].Value; - actual.Should().Be("Item11"); + actual.Should().Be("Item1!"); } [Fact] - public async void Deserialization_efcore_should_not_bypass_validation_pass() + public async void Deserialization_efcore() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); @@ -37,89 +37,53 @@ public async void Deserialization_efcore_should_not_bypass_validation_pass() using (var context = new DeserializationValidationDbContext(options)) { - var actual = await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'abcdefghijk' As Id")); - actual.Id!.Value.Should().Be("Item1"); + DeserializationValidationTestStringEntity actual = await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'Item1!' As Id")); + actual.Id!.Value.Should().Be("Item1!"); } } [Fact] - public async void Deserialization_efcore_should_not_bypass_validation_fail() - { - var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - using (var context = new DeserializationValidationDbContext(options)) - { - Func> vo = async () => (await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'abc' As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); - } - } - [Fact] - public async void Deserialization_linqtodb_should_not_bypass_validation_pass() + public async void Deserialization_linqtodb() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); using (var context = new DeserializationValidationDataConnection(connection)) { - var actual = await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'abcdefghijk' As Id")); - actual.Id!.Value.Should().Be("Item1"); + var actual = await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'Item1!' As Id")); + actual.Id!.Value.Should().Be("Item1!"); } } [Fact] - public void TypeConversion_should_not_bypass_validation_pass() + public void TypeConversion() { var converter = TypeDescriptor.GetConverter(typeof(MyVoString)); - var validValue = "Item1"; + var validValue = "Item1!"; var actual = ((MyVoString?) converter.ConvertFrom(validValue))!.Value; - actual.Should().Be("Item1"); + actual.Should().Be("Item1!"); } [Fact] - public void Deserialization_systemtextjson_should_not_bypass_validation_pass() + public void Deserialization_systemtextjson() { var validValue = SystemTextJsonSerializer.Serialize(MyVoString.Item1); var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; - actual.Should().Be("Item1"); + actual.Should().Be("Item1!"); } - [Fact] - public void Deserialization_systemtextjson_should_not_bypass_validation_fail() - { - var invalidValue = SystemTextJsonSerializer.Serialize(MyVoString.Item1).Replace("Item1", "abc"); - - Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); - - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); - } [Fact] - public void Deserialization_newtonsoft_should_not_bypass_validation_pass() + public void Deserialization_newtonsoft() { var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString.Item1); var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; - actual.Should().Be("Item1"); - } - - [Fact] - public void Deserialization_newtonsoft_should_not_bypass_validation_fail() - { - var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString.Item1).Replace("Item1", "abc"); - - Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; - - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + actual.Should().Be("Item1!"); } - } \ No newline at end of file diff --git a/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs b/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs index 81694490..147dc175 100644 --- a/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs +++ b/tests/ConsumerTests/GenericDeserializationValidationTests/IntDeserializationValidationTests.cs @@ -28,18 +28,7 @@ public async void Deserialization_dapper_should_not_bypass_validation_pass() } [Fact] - public async void Deserialization_dapper_should_not_bypass_validation_fail() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - Func> vo = async () => (await connection.QueryAsync("SELECT 0")).AsList()[0].Value; - - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); - } - - [Fact] - public async void Deserialization_efcore_should_not_bypass_validation_pass() + public async void Deserialization_efcore() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); @@ -56,23 +45,7 @@ public async void Deserialization_efcore_should_not_bypass_validation_pass() } [Fact] - public async void Deserialization_efcore_should_not_bypass_validation_fail() - { - var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - using (var context = new DeserializationValidationDbContext(options)) - { - Func> vo = async () => (await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.IntEntities!.FromSqlRaw("SELECT 0 As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); - } - } - [Fact] - public async void Deserialization_linqtodb_should_not_bypass_validation_pass() + public async void Deserialization_linqtodb() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); @@ -85,21 +58,7 @@ public async void Deserialization_linqtodb_should_not_bypass_validation_pass() } [Fact] - public async void Deserialization_linqtodb_should_not_bypass_validation_fail() - { - var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - //var original = new TestEntity { Id = LinqToDbStringVo.From("foo!") }; - using (var context = new DeserializationValidationDataConnection(connection)) - { - Func> vo = async () => (await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 0 As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("must be greater than zero"); - } - } - - [Fact] - public void TypeConversion_should_not_bypass_validation_pass() + public void TypeConversion() { var converter = TypeDescriptor.GetConverter(typeof(MyVoInt_should_not_bypass_validation)); var validValue = 1; @@ -110,18 +69,7 @@ public void TypeConversion_should_not_bypass_validation_pass() } [Fact] - public void TypeConversion_should_not_bypass_validation_fail() - { - var converter = TypeDescriptor.GetConverter(typeof(MyVoInt_should_not_bypass_validation)); - var invalidValue = 0; - - Action vo = () => converter.ConvertFrom(invalidValue); - - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); - } - - [Fact] - public void Deserialization_systemtextjson_should_not_bypass_validation_pass() + public void Deserialization_systemtextjson() { var validValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.Item1); @@ -131,17 +79,7 @@ public void Deserialization_systemtextjson_should_not_bypass_validation_pass() } [Fact] - public void Deserialization_systemtextjson_should_not_bypass_validation_fail() - { - var invalidValue = SystemTextJsonSerializer.Serialize(MyVoInt_should_not_bypass_validation.Item1).Replace("1", "0"); - - Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); - - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); - } - - [Fact] - public void Deserialization_newtonsoft_should_not_bypass_validation_pass() + public void Deserialization_newtonsoft() { var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.Item1); @@ -149,15 +87,5 @@ public void Deserialization_newtonsoft_should_not_bypass_validation_pass() actual.Should().Be(1); } - - [Fact] - public void Deserialization_newtonsoft_should_not_bypass_validation_fail() - { - var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoInt_should_not_bypass_validation.Item1).Replace("1", "0"); - - Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; - - vo.Should().ThrowExactly().WithMessage("must be greater than zero"); - } } #endif \ No newline at end of file diff --git a/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs b/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs index c426834b..ffad3ec3 100644 --- a/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs +++ b/tests/ConsumerTests/GenericDeserializationValidationTests/SharedTypes.cs @@ -19,16 +19,9 @@ public partial class MyVoInt_should_not_bypass_validation } [Intellenum(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] -[Instance("Item1", "Item1")] -[Instance("Item2", "Item2")] -public partial class MyVoString_should_not_bypass_validation -{ -} - -[Intellenum(Conversions.DapperTypeHandler | Conversions.EfCoreValueConverter | Conversions.LinqToDbValueConverter | Conversions.NewtonsoftJson | Conversions.SystemTextJson | Conversions.TypeConverter)] -[Instance("Item1", "Item1")] -[Instance("Item2", "Item2")] -public partial class MyVoString_should_bypass_validation +[Instance("Item1", "Item1!")] +[Instance("Item2", "Item2!")] +public partial class MyVoString { } #endregion @@ -59,7 +52,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { builder .Property(x => x.Id) - .HasConversion(new MyVoString_should_not_bypass_validation.EfCoreValueConverter()) + .HasConversion(new MyVoString.EfCoreValueConverter()) .ValueGeneratedNever(); }); } @@ -88,7 +81,7 @@ public class DeserializationValidationTestIntEntity public class DeserializationValidationTestStringEntity { - public MyVoString_should_not_bypass_validation? Id { get; set; } + public MyVoString? Id { get; set; } } #endregion @@ -103,8 +96,8 @@ public class DeserializationValidationTestLinqToDbTestIntEntity public class DeserializationValidationTestLinqToDbTestStringEntity { [Column(DataType = DataType.VarChar)] - [ValueConverter(ConverterType = typeof(MyVoString_should_not_bypass_validation.LinqToDbValueConverter))] - public MyVoString_should_not_bypass_validation? Id { get; set; } + [ValueConverter(ConverterType = typeof(MyVoString.LinqToDbValueConverter))] + public MyVoString? Id { get; set; } } #endregion #endregion diff --git a/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs b/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs index 9e0433cb..b9151cff 100644 --- a/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs +++ b/tests/ConsumerTests/GenericDeserializationValidationTests/StringDeserializationValidationTests.cs @@ -17,40 +17,18 @@ namespace ConsumerTests.GenericDeserializationValidationTests; public class StringDeserializationValidationTests { [Fact] - public async void Deserialization_dapper_should_not_bypass_validation_pass() + public async void Deserialization_dapper() { using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - var actual = (await connection.QueryAsync("SELECT 'abcdefghijk'")).AsList()[0].Value; + var actual = (await connection.QueryAsync("SELECT 'Item1!'")).AsList()[0].Value; - actual.Should().Be("abcdefghijk"); + actual.Should().Be("Item1!"); } [Fact] - public async Task Deserialization_dapper_should_not_bypass_validation_fail() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - Func> vo = async () => (await connection.QueryAsync("SELECT 'abc'")).AsList()[0].Value; - - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); - } - - [Fact] - public async Task Deserialization_dapper_should_bypass_validation_fail() - { - using var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - MyVoString_should_bypass_validation? vo = (await connection.QueryAsync("SELECT 'abc'")).AsList()[0]; - - vo.Value.Should().Be("abc"); - } - - [Fact] - public async void Deserialization_efcore_should_not_bypass_validation_pass() + public async void Deserialization_efcore() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); @@ -61,114 +39,53 @@ public async void Deserialization_efcore_should_not_bypass_validation_pass() using (var context = new DeserializationValidationDbContext(options)) { - var actual = await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'abcdefghijk' As Id")); - actual.Id!.Value.Should().Be("abcdefghijk"); + var actual = await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'Item1!' As Id")); + actual.Id!.Value.Should().Be("Item1!"); } } [Fact] - public async void Deserialization_efcore_should_not_bypass_validation_fail() - { - var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - var options = new DbContextOptionsBuilder() - .UseSqlite(connection) - .Options; - - using (var context = new DeserializationValidationDbContext(options)) - { - Func> vo = async () => (await Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync(context.StringEntities!.FromSqlRaw("SELECT 'abc' As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); - } - } - [Fact] - public async void Deserialization_linqtodb_should_not_bypass_validation_pass() + public async void Deserialization_linqtodb() { var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); using (var context = new DeserializationValidationDataConnection(connection)) { - var actual = await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'abcdefghijk' As Id")); - actual.Id!.Value.Should().Be("abcdefghijk"); + var actual = await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'Item1!' As Id")); + actual.Id!.Value.Should().Be("Item1!"); } } [Fact] - public async void Deserialization_linqtodb_should_not_bypass_validation_fail() - { - var connection = new SqliteConnection("DataSource=:memory:"); - await connection.OpenAsync(); - - //var original = new TestEntity { Id = LinqToDbStringVo.From("foo!") }; - using (var context = new DeserializationValidationDataConnection(connection)) - { - Func> vo = async () => (await LinqToDB.AsyncExtensions.SingleAsync(context.FromSql("SELECT 'abc' As Id"))).Id!.Value; - await vo.Should().ThrowExactlyAsync().WithMessage("length must be greater than ten characters"); - } - } - - [Fact] - public void TypeConversion_should_not_bypass_validation_pass() - { - var converter = TypeDescriptor.GetConverter(typeof(MyVoString_should_not_bypass_validation)); - var validValue = "abcdefghijk"; - - var actual = ((MyVoString_should_not_bypass_validation?) converter.ConvertFrom(validValue))!.Value; - - actual.Should().Be("abcdefghijk"); - } - - [Fact] - public void TypeConversion_should_not_bypass_validation_fail() - { - var converter = TypeDescriptor.GetConverter(typeof(MyVoString_should_not_bypass_validation)); - var invalidValue = "abc"; - - Action vo = () => converter.ConvertFrom(invalidValue); - - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); - } - - [Fact] - public void Deserialization_systemtextjson_should_not_bypass_validation_pass() - { - var validValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.Item1); - - var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; - - actual.Should().Be("abcdefghijk"); - } - - [Fact] - public void Deserialization_systemtextjson_should_not_bypass_validation_fail() + public void TypeConversion() { - var invalidValue = SystemTextJsonSerializer.Serialize(MyVoString_should_not_bypass_validation.Item1).Replace("abcdefghijk", "abc"); + var converter = TypeDescriptor.GetConverter(typeof(MyVoString)); + var validValue = "Item1!"; - Action vo = () => SystemTextJsonSerializer.Deserialize(invalidValue); + var actual = ((MyVoString?) converter.ConvertFrom(validValue))!.Value; - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + actual.Should().Be("Item1!"); } [Fact] - public void Deserialization_newtonsoft_should_not_bypass_validation_pass() + public void Deserialization_systemtextjson() { - var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.Item1); + var validValue = SystemTextJsonSerializer.Serialize(MyVoString.Item1); - var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; + var actual = SystemTextJsonSerializer.Deserialize(validValue)!.Value; - actual.Should().Be("abcdefghijk"); + actual.Should().Be("Item1!"); } [Fact] - public void Deserialization_newtonsoft_should_not_bypass_validation_fail() + public void Deserialization_newtonsoft() { - var invalidValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString_should_not_bypass_validation.Item1).Replace("abcdefghijk", "abc"); + var validValue = NewtonsoftJsonSerializer.SerializeObject(MyVoString.Item1); - Func vo = () => NewtonsoftJsonSerializer.DeserializeObject(invalidValue)!.Value; + var actual = NewtonsoftJsonSerializer.DeserializeObject(validValue)!.Value; - vo.Should().ThrowExactly().WithMessage("length must be greater than ten characters"); + actual.Should().Be("Item1!"); } } #endif \ No newline at end of file diff --git a/tests/ConsumerTests/ModuleInitialization.cs b/tests/ConsumerTests/ModuleInitialization.cs index f1aa16eb..735c15ff 100644 --- a/tests/ConsumerTests/ModuleInitialization.cs +++ b/tests/ConsumerTests/ModuleInitialization.cs @@ -21,7 +21,7 @@ public static void Init() #if NET7_0_OR_GREATER SqlMapper.AddTypeHandler(new ConsumerTests.GenericDeserializationValidationTests.MyVoInt_should_not_bypass_validation.DapperTypeHandler()); - SqlMapper.AddTypeHandler(new ConsumerTests.GenericDeserializationValidationTests.MyVoString_should_not_bypass_validation.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new ConsumerTests.GenericDeserializationValidationTests.MyVoString.DapperTypeHandler()); #endif SqlMapper.AddTypeHandler(new Intellenum.IntegrationTests.TestTypes.ClassVos.DapperFooVo.DapperTypeHandler()); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs index 46a6e60d..2a8a183b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs @@ -80,7 +80,7 @@ public void CanSerializeToLong_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() { - var value = 123.45m; + var value = 1.1m; var vo = NewtonsoftJsonDecimalVo.Item1; var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); @@ -92,7 +92,7 @@ public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromLong_WithSystemTextJsonProvider() { - var value = 123.45m; + var value = 1.1m; var vo = SystemTextJsonDecimalVo.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(value); @@ -196,7 +196,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - var parameters = new { Value = 123.45m }; + var parameters = new { Value = 1.1m }; IEnumerable results = await connection.QueryAsync("SELECT @Value", parameters); DapperDecimalVo value = Assert.Single(results); @@ -233,12 +233,12 @@ public void WhenLinqToDbValueConverterUsesValueConverter() public void TypeConverter_CanConvertToAndFromDecimal() { var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom(123.45m); + var id = converter.ConvertFrom(1.1m); Assert.IsType(id); Assert.Equal(NoJsonDecimalVo.Item1, id); var reconverted = converter.ConvertTo(id, typeof(decimal)); - Assert.Equal(123.45m, reconverted); + Assert.Equal(1.1m, reconverted); } [Fact] @@ -247,12 +247,12 @@ public void TypeConverter_CanConvertToAndFrom() var culture = new CultureInfo("en-US"); var converter = TypeDescriptor.GetConverter(typeof(NoJsonDecimalVo)); - var id = converter.ConvertFrom(null!, culture, "123.45"); + var id = converter.ConvertFrom(null!, culture, "1.1"); Assert.IsType(id); Assert.Equal(NoJsonDecimalVo.Item1, id); var reconverted = converter.ConvertTo(null, culture, id, typeof(string)); - Assert.Equal("123.45", reconverted); + Assert.Equal("1.1", reconverted); } [Fact] @@ -263,7 +263,7 @@ public void RoundTrip_WithNsj() string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - deserializedVo.Value.Should().Be(123.45m); + deserializedVo.Value.Should().Be(1.1m); } [Fact] @@ -274,7 +274,7 @@ public void RoundTrip_WithStj() string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - deserializedVo.Value.Should().Be(123.45m); + deserializedVo.Value.Should().Be(1.1m); } [Fact] @@ -285,7 +285,7 @@ public void RoundTrip_WithStj_Treating_numbers_as_string() string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - deserializedVo.Value.Should().Be(123.45m); + deserializedVo.Value.Should().Be(1.1m); } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs index b87be634..d5aca87d 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs @@ -74,7 +74,7 @@ public void CanSerializeToLong_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() { - var value = 123D; + var value = 1.1D; var vo = NewtonsoftJsonDoubleVo.Item1; var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); @@ -86,7 +86,7 @@ public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromLong_WithSystemTextJsonProvider() { - var value = 123D; + var value = 1.1D; var vo = SystemTextJsonDoubleVo.Item1; var serializedLong = SystemTextJsonSerializer.Serialize(value); @@ -190,7 +190,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - var parameters = new { Value = 123.45d }; + var parameters = new { Value = 1.1D }; IEnumerable results = await connection.QueryAsync("SELECT @Value", parameters); var value = Assert.Single(results); @@ -224,8 +224,8 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData(123.45D)] - [InlineData("123.45")] + [InlineData(1.1D)] + [InlineData("1.1D")] public void TypeConverter_CanConvertToAndFrom(object value) { var culture = new CultureInfo("en-US"); @@ -247,7 +247,7 @@ public void RoundTrip_WithNsj() string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - deserializedVo.Value.Should().Be(123.45); + deserializedVo.Value.Should().Be(1.1D); } [Fact] @@ -258,7 +258,7 @@ public void RoundTrip_WithStj() string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - deserializedVo.Value.Should().Be(123.45); + deserializedVo.Value.Should().Be(1.1D); } [Fact] @@ -269,7 +269,7 @@ public void RoundTrip_WithStj_Treating_numbers_as_string() string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - deserializedVo.Value.Should().Be(123.45); + deserializedVo.Value.Should().Be(1.1D); } public class TestDbContext : DbContext diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs index 07746b2b..0510752a 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs @@ -69,7 +69,7 @@ public void RoundTrip_WithNsj() string serializedVo = NewtonsoftJsonSerializer.SerializeObject(vo); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedVo)!; - deserializedVo.Value.Should().Be(123.45f); + deserializedVo.Value.Should().Be(2.2f); } [Fact] @@ -80,7 +80,7 @@ public void RoundTrip_WithStj() string serializedVo = SystemTextJsonSerializer.Serialize(vo); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedVo)!; - deserializedVo.Value.Should().Be(123.45f); + deserializedVo.Value.Should().Be(2.2f); } [Fact] @@ -97,7 +97,7 @@ public void CanSerializeToFloat_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromFloat_WithNewtonsoftJsonProvider() { - var value = 123; + var value = 1.1f; var vo = NewtonsoftJsonFloatVo.Item1; var serializedFloat = NewtonsoftJsonSerializer.SerializeObject(value); @@ -109,7 +109,7 @@ public void CanDeserializeFromFloat_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromFloat_WithSystemTextJsonProvider() { - var value = 123; + var value = 1.1f; var vo = SystemTextJsonFloatVo.Item1; var serializedFloat = SystemTextJsonSerializer.Serialize(value); @@ -213,7 +213,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - var parameters = new { Value = 123.45f }; + var parameters = new { Value = 2.2f }; IEnumerable results = await connection.QueryAsync("SELECT @Value", parameters); var value = Assert.Single(results); @@ -247,8 +247,8 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData((float)123.45)] - [InlineData("123.45")] + [InlineData((float)1.1)] + [InlineData("1.1")] public void TypeConverter_CanConvertToAndFrom(object value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonFloatVo)); @@ -257,7 +257,7 @@ public void TypeConverter_CanConvertToAndFrom(object value) var id = converter.ConvertFrom(null!, culture, value); Assert.IsType(id); - Assert.Equal(NoJsonFloatVo.Item2, id); + Assert.Equal(NoJsonFloatVo.Item1, id); object reconverted = converter.ConvertTo(null, culture, id, value.GetType()); Assert.Equal(value, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs index a6817e76..64a5fdc5 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs @@ -47,7 +47,7 @@ public void CanSerializeAndDeserialize() deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonBoolVo.Value.Should().Be(true); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonBoolVo.Value.Should().Be(true); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonByteVo.Value.Should().Be(1); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonCharVo.Value.Should().Be('2'); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonCharVo.Value.Should().Be('b'); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeOffsetVo.Value.Should().Be(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeVo.Value.Should().Be(new DateTime(2020, 12, 13, 23, 59, 59, 999)); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDecimalVo.Value.Should().Be(3.33m); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs index c10fb741..77ebf57b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs @@ -167,7 +167,7 @@ public void CanSerializeAndDeserializeWhenVoIsInAComplexObject() var deserialized = JsonSerializer.Deserialize(serialized); - deserialized.ByteHolder_as_a_string.Value.Should().Be(123); + deserialized.ByteHolder_as_a_string.Value.Should().Be(1); deserialized.ByteHolder_normal.Value.Should().Be(123); } } diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt index 050316f7..8b729c0e 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt index 845d6057..778e248c 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt index cc74dd3f..ecd9cd57 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt index a5600ed7..077e27e1 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt index a5600ed7..077e27e1 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt index 3701ebc6..c6039ac7 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt index c277c6ef..e0ce7a40 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.String ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt index dd8d9911..9da4a788 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt @@ -52,20 +52,6 @@ namespace @double -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt index 74e68dc8..ec41ad54 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.No_namespace.verified.txt @@ -50,20 +50,6 @@ using System; -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt index 324db39e..8fadf4f4 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt index 9751e7c5..063ff441 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v6.0/GeneralTests.Produces_instances.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt index 5bdd8616..ff94b6fe 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Namespace_names_can_have_reserved_keywords.verified.txt @@ -52,20 +52,6 @@ namespace @double -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt index 90d5d0a4..ce324fbd 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.No_namespace.verified.txt @@ -50,20 +50,6 @@ using System; -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt index 9610c0d0..36dc9a2d 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Partial_partial_class_created_successfully.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt index 0f2b3fbb..33e42028 100644 --- a/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt +++ b/tests/SnapshotTests/GeneralStuff/snapshots/snap-v7.0/GeneralTests.Produces_instances.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt index 64f827ab..b90d0c1d 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Instance_names_can_have_reserved_keywords.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt index 62aab702..26147f91 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Namespace_names_can_have_reserved_keywords.verified.txt @@ -52,20 +52,6 @@ namespace @double -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt index f1674c85..54a381f8 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.No_namespace.verified.txt @@ -50,20 +50,6 @@ using System; -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt index 487ec4ab..4fd2fb4c 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int32 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// diff --git a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt index 8aa33326..34a33c54 100644 --- a/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt +++ b/tests/SnapshotTests/snapshots/snap-v7.0/GenericAttributeTests.Produces_instances_with_derived_attribute.verified.txt @@ -52,20 +52,6 @@ namespace Whatever -// /// -// /// Gets the underlying value if set, otherwise a is thrown. -// /// -// public System.Int64 ValueChecked -// { -// [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] -// [global::System.Diagnostics.DebuggerStepThroughAttribute] -// get -// { -// if(!_isInitialized) Throw(); -// return _value; -// } -// } - /// /// Gets the underlying value if set, otherwise default /// From 8b3756cb3183e55efe4caa40405df776672a8893 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Sat, 1 Apr 2023 10:50:56 +0100 Subject: [PATCH 4/8] More tests passing --- README.md | 7 +- Scratch/GeneralTests.cs | 24 +- Scratch/Scratch.csproj | 4 +- Scratch/aaaaa.cs | 447 ++++++++++++++++++ .../ClassVos/AnyOtherTypeVoTests.cs | 2 +- .../ClassVos/BoolVoTests.cs | 6 +- .../ClassVos/ByteVoTests.cs | 10 +- .../ClassVos/DateTimeVoTests.cs | 13 +- .../ClassVos/IntVoTests.cs | 10 +- .../ClassVos/LongVoTests.cs | 8 +- .../ClassVos/Types/DateTimeVo.cs | 2 + .../Benchmarks/Benchmarks.csproj | 2 +- 12 files changed, 506 insertions(+), 29 deletions(-) create mode 100644 Scratch/aaaaa.cs diff --git a/README.md b/README.md index 461f3c2f..e781dcfe 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,12 @@ _note that EnumGenerators isn't here as we use the standard C# enum to get its v | **Intellenums** | **0.3198 ns** | **0.0103 ns** | **0.0096 ns** | **-** | ### What does `ToString` return? -It returns the name of the instance. +It returns the **name** of the instance. +There is also a TypeConverter; when this is asked to convert an instance to a `string', +it returns the **value** of the instance as a string. + +### What can the `TypeConverters` convert to and from? +They can convert an underlying type back to a matching enum. > NOTE: Intellenum is in pre-release at the moment, so probably isn't production ready and the API might (and probably will) change. diff --git a/Scratch/GeneralTests.cs b/Scratch/GeneralTests.cs index b407d572..ef43231e 100644 --- a/Scratch/GeneralTests.cs +++ b/Scratch/GeneralTests.cs @@ -1,7 +1,28 @@ -using FluentAssertions; +#nullable disable + +using System.ComponentModel; +using FluentAssertions; +using Intellenum; +using Intellenum.IntegrationTests.TestTypes.ClassVos; namespace Scratch; +// [Intellenum(conversions: Conversions.TypeConverter, underlyingType: typeof(Bar))] +// public partial class NoJsonFooVo +// { +// static NoJsonFooVo() +// { +// Instance("Item1", new NoJsonFooVo(new Bar(42, "Fred"))); +// Instance("Item2", new NoJsonFooVo(new Bar(2, "Two"))); +// } +// } + +public record struct Bar(int Age, string Name) : IComparable +{ + public int CompareTo(Bar other) => Age.CompareTo(other.Age); +} + + public class GeneralTests { @@ -11,6 +32,7 @@ enum ECustomerType Gold } + [Fact] public void ToStringTest() { diff --git a/Scratch/Scratch.csproj b/Scratch/Scratch.csproj index 3178a28a..7aff3dcb 100644 --- a/Scratch/Scratch.csproj +++ b/Scratch/Scratch.csproj @@ -17,7 +17,7 @@ - $(MSBuildProjectDirectory)local-global-packages + $(MSBuildProjectDirectory)\..\local-global-packages @@ -26,7 +26,7 @@ - + diff --git a/Scratch/aaaaa.cs b/Scratch/aaaaa.cs new file mode 100644 index 00000000..a0cb54a6 --- /dev/null +++ b/Scratch/aaaaa.cs @@ -0,0 +1,447 @@ +// ------------------------------------------------------------------------------ +// +// This code was generated by a source generator named Intellenum (https://github.com/SteveDunn/Intellenum) +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +// ------------------------------------------------------------------------------ + +// Suppress warnings about [Obsolete] member usage in generated code. +#pragma warning disable CS0618 + +// Suppress warnings for 'Override methods on comparable types'. +#pragma warning disable CA1036 + +// Suppress Error MA0097 : A class that implements IComparable or IComparable should override comparison operators +#pragma warning disable MA0097 + +// Suppress warning for 'The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.' +// The generator copies signatures from the BCL, e.g. for `TryParse`, and some of those have nullable annotations. +#pragma warning disable CS8669 + +// Suppress warnings about CS1591: Missing XML comment for publicly visible type or member 'Type_or_Member' +#pragma warning disable CS1591 + +using Intellenum; +using System; + +namespace Intellenum.IntegrationTests.TestTypes.ClassVos +{ + + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Intellenum", "0.0.0.0")] + [global::System.ComponentModel.TypeConverter(typeof(NoJsonDateTimeVoTypeConverter))] + + [global::System.Diagnostics.DebuggerTypeProxyAttribute(typeof(NoJsonDateTimeVoDebugView))] + [global::System.Diagnostics.DebuggerDisplayAttribute("Underlying type: System.DateTime, Value = { _value }")] + public partial class NoJsonDateTimeVo : + global::System.IEquatable, + global::System.IEquatable, + global::System.IComparable, + global::System.IComparable + { + private static readonly System.Lazy> _namesToValues = new( () => + new() + { + { "Item1", Item1.Value }, +{ "Item2", Item2.Value }, + + }); + private static readonly System.Lazy> _namesToEnums = new( () => + new() + { + { "Item1", Item1 }, +{ "Item2", Item2 }, + + }); + private static readonly System.Lazy> _enumsToValues = new( () => + new() + { + { Item1, Item1.Value }, +{ Item2, Item2.Value }, + + }); + private static readonly System.Lazy> _valuesToNames = new( () => + new() + { + { Item1.Value, "Item1" }, +{ Item2.Value, "Item2" }, + + }); + private static readonly System.Lazy> _valuesToEnums = new( () => + new() + { + { Item1.Value, Item1 }, +{ Item2.Value, Item2 }, + + }); + +#if DEBUG + private readonly global::System.Diagnostics.StackTrace _stackTrace = null; +#endif + private readonly global::System.Boolean _isInitialized; + private readonly System.DateTime _value; + + + + /// + /// Gets the underlying value if set, otherwise default + /// + public System.DateTime Value + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + [global::System.Diagnostics.DebuggerStepThroughAttribute] + get + { + return _value; + } + } + +private void Throw() +{ +#if DEBUG + global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? ""; +#else + global::System.String message = "Use of uninitialized Value Object."; +#endif + + throw new IntellenumUninitialisedException(message); + +} + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + public NoJsonDateTimeVo() + { +#if DEBUG + _stackTrace = new global::System.Diagnostics.StackTrace(); +#endif + _isInitialized = false; + _value = default; + Name = "[UNDEFINED]"; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private NoJsonDateTimeVo(System.DateTime value) + { + _value = value; + Name = "[INFERRED-TO-BE-REPLACED!]"; + _isInitialized = true; + } + + [global::System.Diagnostics.DebuggerStepThroughAttribute] + private NoJsonDateTimeVo(string enumName, System.DateTime value) + { + _value = value; + Name = enumName; + _isInitialized = true; + } + + public string Name { get; private set; } + + /// + /// Builds an instance from an enum value. + /// + /// The value. + /// The matching enum, or an exception. + public static NoJsonDateTimeVo FromValue(System.DateTime value) + { + bool b = _valuesToEnums.Value.TryGetValue(value, out var ret); + if(b) return ret; + throw new IntellenumMatchFailedException($"NoJsonDateTimeVo has no matching instances with a value of '{value}'"); + } + + /// + /// Tries to get an instance based on value. + /// + /// The value. + /// The matching enum, or an exception. + public static bool TryFromValue(System.DateTime value, out NoJsonDateTimeVo instance) + { + return _valuesToEnums.Value.TryGetValue(value, out instance); + } + + public static bool IsDefined(System.DateTime value) + { + return _valuesToEnums.Value.TryGetValue(value, out _); + } + + public void Deconstruct(out string Name, out System.DateTime Value) + { + Name = this.Name; + Value = this.Value; + } + + /// + /// Gets the matching instance based on name. + /// + /// The name. + /// The matching enum, or an exception. + public static NoJsonDateTimeVo FromName(string name) + { + bool b = TryFromName(name, out var ret); + if(b) return ret; + throw new IntellenumMatchFailedException($"NoJsonDateTimeVo has no matching instances named '{name}'"); + } + + /// + /// Tries to get the matching instance from a name. + /// + /// The name. + /// The matching enum, or an exception. + public static bool TryFromName(string name, out NoJsonDateTimeVo instance) + { + return _namesToEnums.Value.TryGetValue(name, out instance); + } + + public static bool IsNamedDefined(string name) + { + return TryFromName(name, out _); + } + + + /// + /// Builds an instance from the provided underlying type. + /// + /// The underlying type. + /// An instance of this type. + private static NoJsonDateTimeVo From(string name, System.DateTime value) + { + + + NoJsonDateTimeVo instance = new NoJsonDateTimeVo(name, value); + + return instance; + } + + // placeholder method used by the source generator + // to generate physical instances (e.g. public static readonly MyEnum Item1 = new...) + private static void Instance(string name, System.DateTime value) + { + } + + // only called internally when something has been deserialized into + // its primitive type. + private static NoJsonDateTimeVo Deserialize(System.DateTime value) + { + + + if(value == Item1.Value) return Item1; + if(value == Item2.Value) return Item2; + + + return FromValue(value); + } + + public global::System.Boolean Equals(NoJsonDateTimeVo other) + { + if (ReferenceEquals(null, other)) + { + return false; + } + + // It's possible to create uninitialized instances via converters such as EfCore (HasDefaultValue), which call Equals. + // We treat anything uninitialized as not equal to anything, even other uninitialized instances of this type. + if(!_isInitialized || !other._isInitialized) return false; + + if (ReferenceEquals(this, other)) + { + return true; + } + + return GetType() == other.GetType() && global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + public global::System.Boolean Equals(System.DateTime primitive) => Value.Equals(primitive); + + public override global::System.Boolean Equals(global::System.Object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + + if (ReferenceEquals(this, obj)) + { + return true; + } + + if (obj.GetType() != GetType()) + { + return false; + } + + return Equals((NoJsonDateTimeVo) obj); + } + + public static global::System.Boolean operator ==(NoJsonDateTimeVo left, NoJsonDateTimeVo right) => Equals(left, right); + public static global::System.Boolean operator !=(NoJsonDateTimeVo left, NoJsonDateTimeVo right) => !Equals(left, right); + + public static global::System.Boolean operator ==(NoJsonDateTimeVo left, System.DateTime right) => Equals(left.Value, right); + public static global::System.Boolean operator !=(NoJsonDateTimeVo left, System.DateTime right) => !Equals(left.Value, right); + + public static global::System.Boolean operator ==(System.DateTime left, NoJsonDateTimeVo right) => Equals(left, right.Value); + public static global::System.Boolean operator !=(System.DateTime left, NoJsonDateTimeVo right) => !Equals(left, right.Value); + + public static global::System.Boolean operator <(NoJsonDateTimeVo left, NoJsonDateTimeVo right) => left.CompareTo(right) < 0; + public static global::System.Boolean operator <=(NoJsonDateTimeVo left, NoJsonDateTimeVo right) => left.CompareTo(right) <= 0; + public static global::System.Boolean operator >(NoJsonDateTimeVo left, NoJsonDateTimeVo right) => left.CompareTo(right) > 0; + public static global::System.Boolean operator >=(NoJsonDateTimeVo left, NoJsonDateTimeVo right) => left.CompareTo(right) >= 0; + + public static explicit operator NoJsonDateTimeVo(System.DateTime value) => FromValue(value); + public static implicit operator System.DateTime(NoJsonDateTimeVo value) => value.Value; + + public int CompareTo(NoJsonDateTimeVo other) => Value.CompareTo(other.Value); + public int CompareTo(object other) { + if(other == null) return 1; + if(other is NoJsonDateTimeVo x) return CompareTo(x); + throw new global::System.ArgumentException("Cannot compare to object as it is not of type NoJsonDateTimeVo", nameof(other)); + } + + public override global::System.Int32 GetHashCode() + { + unchecked // Overflow is fine, just wrap + { + global::System.Int32 hash = (global::System.Int32) 2166136261; + hash = (hash * 16777619) ^ Value.GetHashCode(); + hash = (hash * 16777619) ^ GetType().GetHashCode(); + hash = (hash * 16777619) ^ global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(); + return hash; + } + } + + +// instance... + +public static readonly NoJsonDateTimeVo Item3 = new NoJsonDateTimeVo("Item3",new DateTime(2022, 01, 15, 19, 08, 49, DateTimeKind.Utc).AddTicks(5413764)); +public static readonly NoJsonDateTimeVo Item1 = new NoJsonDateTimeVo("Item1",new DateTime(2019, 12, 13, 14, 15, 16)); + +// instance... + +public static readonly NoJsonDateTimeVo Item2 = new NoJsonDateTimeVo("Item2",new DateTime(2020, 12, 13, 14, 15, 16)); + + + public static global::System.Collections.Generic.IEnumerable List() + { + yield return Item1; +yield return Item2; + + } + + /// Returns the name of the enum. + public override global::System.String ToString() => Name; + + + + + class NoJsonDateTimeVoTypeConverter : global::System.ComponentModel.TypeConverter + { + public override global::System.Boolean CanConvertFrom(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Type sourceType) + { + return sourceType == typeof(global::System.DateTime) || sourceType == typeof(global::System.String) || base.CanConvertFrom(context, sourceType); + } + + public override global::System.Object ConvertFrom(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Globalization.CultureInfo culture, global::System.Object value) + { + return value switch + { + global::System.String stringValue when !global::System.String.IsNullOrEmpty(stringValue) && + global::System.DateTime.TryParseExact(stringValue, "O", + global::System.Globalization.CultureInfo.InvariantCulture, + global::System.Globalization.DateTimeStyles.RoundtripKind, out var result) => + NoJsonDateTimeVo.Deserialize(result), + global::System.DateTime dateTimeValue => NoJsonDateTimeVo.Deserialize(dateTimeValue), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Type sourceType) + { + return sourceType == typeof(global::System.DateTime) || sourceType == typeof(global::System.String) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(global::System.ComponentModel.ITypeDescriptorContext context, global::System.Globalization.CultureInfo culture, global::System.Object value, global::System.Type destinationType) + { + if (value is NoJsonDateTimeVo idValue) + { + if (destinationType == typeof(global::System.DateTime)) + { + return idValue.Value; + } + + if (destinationType == typeof(global::System.String)) + { + return idValue.Value.ToString("O", global::System.Globalization.CultureInfo.InvariantCulture); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + + + + + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out NoJsonDateTimeVo result) { + if(System.DateTime.TryParse(s, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + /// + /// + /// + /// + /// The value created via the method. + /// + /// Thrown when the value can be parsed, but is not valid. + public static global::System.Boolean TryParse(string s, global::System.IFormatProvider provider, global::System.Globalization.DateTimeStyles styles, +#if NETCOREAPP3_0_OR_GREATER +[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] +#endif + out NoJsonDateTimeVo result) { + if(System.DateTime.TryParse(s, provider, styles, out var r)) { + result = FromValue(r); + return true; + } + + result = default; + return false; + } + + + internal sealed class NoJsonDateTimeVoDebugView + { + private readonly NoJsonDateTimeVo _t; + + NoJsonDateTimeVoDebugView(NoJsonDateTimeVo t) + { + _t = t; + } + + public global::System.String UnderlyingType => "System.DateTime"; + public System.DateTime Value => _t.Value ; + + public global::System.String Conversions => @"[global::System.ComponentModel.TypeConverter(typeof(NoJsonDateTimeVoTypeConverter))] +"; + } + } +} \ No newline at end of file diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs index a1cb0638..f9db6411 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs @@ -235,7 +235,7 @@ public void TypeConverter_CanConvertToAndFrom() { var converter = TypeDescriptor.GetConverter(typeof(NoJsonFooVo)); - object vo = converter.ConvertFrom(NoJsonFooVo.Item1); + object vo = converter.ConvertFrom(NoJsonFooVo.Item1.Value); Assert.IsType(vo); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs index 4bc7b3b4..b3c016f0 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs @@ -230,9 +230,9 @@ public void TypeConverter_CanConvertToAndFrom_strings(object input, string expec } [Theory] - [InlineData(true, "Yes")] - [InlineData(false, "No")] - public void TypeConverter_CanConvertToAndFrom_bools(bool input, string expectedString) + [InlineData(true, "True")] + [InlineData(false, "False")] + public void TypeConverter_CanConvertToAndFrom_bools_and_the_string_output_conversion_is_the_string_representation_of_the_VALUE_as_opposed_to_ToString_which_is_the_name(bool input, string expectedString) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs index a586c86e..120184dc 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs @@ -72,7 +72,7 @@ public void CanSerializeToShort_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() { - byte value = 123; + byte value = 1; var vo = NewtonsoftJsonByteVo.Item1; var serializedShort = NewtonsoftJsonSerializer.SerializeObject(value); @@ -84,7 +84,7 @@ public void CanDeserializeFromShort_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromShort_WithSystemTextJsonProvider() { - byte value = 123; + byte value = 1; var vo = SystemTextJsonByteVo.Item1; var serializedShort = SystemTextJsonSerializer.Serialize(value); @@ -177,7 +177,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT 123"); + IEnumerable results = await connection.QueryAsync("SELECT 1"); var value = Assert.Single(results); Assert.Equal(DapperByteVo.Item1, value); @@ -210,8 +210,8 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData((byte) 123)] - [InlineData("123")] + [InlineData((byte) 1)] + [InlineData("1")] public void TypeConverter_CanConvertToAndFrom(object value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonByteVo)); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs index 35d526e7..2d1e19f1 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs @@ -227,15 +227,16 @@ public void WhenLinqToDbValueConverterUsesValueConverter() [Theory] [InlineData("2022-01-15T19:08:49.5413764+00:00")] - public void TypeConverter_CanConvertToAndFrom(string value) + public void TypeConverter_CanConvertToAndFrom(string inputString) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonDateTimeVo)); - var id = converter.ConvertFrom(value); - Assert.IsType(id); - Assert.Equal(NoJsonDateTimeVo.Item1, id); + + var instance = converter.ConvertFrom(inputString); + Assert.IsType(instance); + Assert.Equal(NoJsonDateTimeVo.Item3, instance); - var reconverted = converter.ConvertTo(id, value.GetType()); - Assert.Equal(value, reconverted); + var reconverted = converter.ConvertTo(instance, inputString.GetType()); + Assert.Equal(inputString, reconverted); } public class TestDbContext : DbContext diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs index dcb61a88..5d9282ce 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs @@ -72,7 +72,7 @@ public void CanSerializeToInt_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() { - var value = 123; + var value = 1; var vo = NewtonsoftJsonIntVo.Item1; var serializedInt = NewtonsoftJsonSerializer.SerializeObject(value); @@ -84,7 +84,7 @@ public void CanDeserializeFromInt_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromInt_WithSystemTextJsonProvider() { - var value = 123; + var value = 1; var vo = SystemTextJsonIntVo.Item1; var serializedInt = SystemTextJsonSerializer.Serialize(value); @@ -188,7 +188,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT 123"); + IEnumerable results = await connection.QueryAsync("SELECT 1"); var value = Assert.Single(results); Assert.Equal(DapperIntVo.Item1, value); @@ -221,8 +221,8 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData(123)] - [InlineData("123")] + [InlineData(1)] + [InlineData("1")] public void TypeConverter_CanConvertToAndFrom(object value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonIntVo)); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs index 615daacd..16c5be41 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs @@ -72,7 +72,7 @@ public void CanSerializeToLong_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromLong_WithNewtonsoftJsonProvider() { - var value = 123L; + var value = 1L; var vo = NewtonsoftJsonLongVo.Item1; var serializedLong = NewtonsoftJsonSerializer.SerializeObject(value); @@ -187,7 +187,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT 123"); + IEnumerable results = await connection.QueryAsync("SELECT 1"); var value = Assert.Single(results); Assert.Equal(DapperLongVo.Item1, value); @@ -220,8 +220,8 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData(123L)] - [InlineData("123")] + [InlineData(1L)] + [InlineData("1")] public void TypeConverter_CanConvertToAndFrom(object value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonLongVo)); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs index 4d5fade8..5abc2f49 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs @@ -29,6 +29,8 @@ static NoJsonDateTimeVo() { Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); + // 2022-01-15T19:08:49.5413764+00:00 + Instance("Item3", new DateTime(2022, 01, 15, 19, 08, 49, DateTimeKind.Local).AddTicks(5413764)); } } diff --git a/tests/Intellenum.Benchmarks/Benchmarks/Benchmarks.csproj b/tests/Intellenum.Benchmarks/Benchmarks/Benchmarks.csproj index 52f943d4..7cfede2e 100644 --- a/tests/Intellenum.Benchmarks/Benchmarks/Benchmarks.csproj +++ b/tests/Intellenum.Benchmarks/Benchmarks/Benchmarks.csproj @@ -19,7 +19,7 @@ - $(MSBuildProjectDirectory)local-global-packages + $(MSBuildProjectDirectory)/../../../local-global-packages From 619cfa533af6fcda2e6c8ac2996fd2af49140354 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 4 Apr 2023 07:11:27 +0100 Subject: [PATCH 5/8] More tests passing --- README.md | 15 ++++++++-- src/Intellenum/Generators/ClassGenerator.cs | 3 ++ src/Intellenum/Util.cs | 27 ------------------ .../ClassVos/AnyOtherTypeVoTests.cs | 10 ++++--- .../ClassVos/BoolVoTests.cs | 6 ++-- .../ClassVos/ByteVoTests.cs | 8 +++--- .../ClassVos/CharVoTests.cs | 8 +++--- .../ClassVos/DateOnlyVoTests.cs | 6 ++-- .../ClassVos/DateTimeOffsetVoTests.cs | 12 +++++--- .../ClassVos/DateTimeVoTests.cs | 8 +++--- .../ClassVos/DecimalVoTests.cs | 8 +++--- .../ClassVos/DoubleVoTests.cs | 12 ++++---- .../ClassVos/FloatVoTests.cs | 8 +++--- .../ClassVos/GuidVoTests.cs | 6 ++-- .../ClassVos/IntVoTests.cs | 6 ++-- .../ClassVos/LongVoTests.cs | 6 ++-- .../ClassVos/ShortVoTests.cs | 6 ++-- .../ClassVos/StringVoTests.cs | 6 ++-- .../ClassVos/TimeOnlyVoTests.cs | 19 +++++++------ .../ClassVos/Types/DateTimeVo.cs | 2 +- .../ComplexSerializationTests.cs | 22 +++++++-------- tests/ScratchSnapshotTests/GeneralTests.cs | 28 +++++++++++++++++++ tests/ScratchSnapshotTests/SnapshotRunner.cs | 2 +- 23 files changed, 128 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index e781dcfe..8e630298 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,6 @@ public partial class CustomerType } } ``` - ## Features * `FromName()` and `FromValue()` (and `TryFrom...`) @@ -203,7 +202,19 @@ There is also a TypeConverter; when this is asked to convert an instance to a `s it returns the **value** of the instance as a string. ### What can the `TypeConverters` convert to and from? -They can convert an underlying type back to a matching enum. +They can convert an underlying type back to a matching enum. + +### Can it serialize/deserialize? +Yes, it can. There's various ways to do this, including: +* System.Text.Json +* Newtonsoft.Json +* Dapper +* Entity Framework Core +* Linq2Db +* TypeConverters + +Right now, Intellenum serializes using the `Value` property just like native enums. + > NOTE: Intellenum is in pre-release at the moment, so probably isn't production ready and the API might (and probably will) change. diff --git a/src/Intellenum/Generators/ClassGenerator.cs b/src/Intellenum/Generators/ClassGenerator.cs index 66c3b90c..17f205fd 100644 --- a/src/Intellenum/Generators/ClassGenerator.cs +++ b/src/Intellenum/Generators/ClassGenerator.cs @@ -88,6 +88,9 @@ private void Throw() _isInitialized = true; }} + // exclude from serialization + + public string Name {{ get; private set; }} /// diff --git a/src/Intellenum/Util.cs b/src/Intellenum/Util.cs index c679ba6d..c1ca21b8 100644 --- a/src/Intellenum/Util.cs +++ b/src/Intellenum/Util.cs @@ -92,8 +92,6 @@ public static string GenerateAnyConversionAttributes(TypeDeclarationSyntax tds, return sb.ToString(); } - public static string GenerateAnyConversionAttributesForDebuggerProxy(VoWorkItem item) => item.Conversions.ToString(); - public static string GenerateAnyConversionBodies(TypeDeclarationSyntax tds, VoWorkItem item) { StringBuilder sb = new StringBuilder(); @@ -105,31 +103,6 @@ public static string GenerateAnyConversionBodies(TypeDeclarationSyntax tds, VoWo return sb.ToString(); } - public static string GenerateDebuggerProxyForStructs(TypeDeclarationSyntax tds, VoWorkItem item) - { - string code = $@"internal sealed class {item.VoTypeName}DebugView - {{ - private readonly {item.VoTypeName} _t; - - {item.VoTypeName}DebugView({item.VoTypeName} t) - {{ - _t = t; - }} - - public global::System.Boolean IsInitialized => _t._isInitialized; - public global::System.String UnderlyingType => ""{item.UnderlyingTypeFullName}""; - public global::System.String Value => _t._isInitialized ? _t._value.ToString() : ""[not initialized]"" ; - - #if DEBUG - public global::System.String CreatedWith => _t._stackTrace?.ToString() ?? ""the From method""; - #endif - - public global::System.String Conversions => @""{Util.GenerateAnyConversionAttributesForDebuggerProxy(item)}""; - }}"; - - return code; - } - public static string GenerateDebuggerProxyForClasses(TypeDeclarationSyntax tds, VoWorkItem item) { string code = $@"internal sealed class {item.VoTypeName}DebugView diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs index f9db6411..3e99e530 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/AnyOtherTypeVoTests.cs @@ -126,13 +126,15 @@ public void CanSerializeToStringClass_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonFooVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; + var expected = """ + {"Value":{"Age":42,"Name":"Fred"},"Name":"Item1"} + """; Assert.Equal(expected, serialized); } @@ -148,7 +150,7 @@ public void WithTypeConverterButNoJsonConverters_NewtonsoftSerializesWithValuePr var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; + var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"},\"Name\":\"Item1\"}"; Assert.Equal(expected, serialized); } @@ -161,7 +163,7 @@ public void WithTypeConverterButNoJsonConverters_SystemTextJsonSerializesWithVal var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":{\"Age\":42,\"Name\":\"Fred\"}}"; + var expected = "{\"Value\":{\"Age\":1,\"Name\":\"One\"},\"Name\":\"Item1\"}"; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs index b3c016f0..8debac08 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs @@ -112,13 +112,13 @@ public void CanSerializeToShort_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonBoolVo.Yes; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":true}"; + var expected = "{\"Value\":true,\"Name\":\"Yes\"}"; Assert.Equal(expected, serialized); } @@ -136,7 +136,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterBoolVo.Yes; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs index 120184dc..048bac62 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs @@ -109,13 +109,13 @@ public void CanSerializeToShort_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonByteVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = "{\"Value\":" + vo.Value + ",\"Name\":\"Item1\"}"; Assert.Equal(expected, serialized); } @@ -133,14 +133,14 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterByteVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = "{\"Value\":" + vo.Value + ",\"Name\":\"Item1\"}"; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs index 172b1fc5..ba7cf6d2 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/CharVoTests.cs @@ -106,13 +106,13 @@ public void CanSerializeToShort_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonCharVo.A; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"a\"}"; + var expected = "{\"Value\":\"a\",\"Name\":\"A\"}"; Assert.Equal(expected, serialized); } @@ -130,14 +130,14 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterCharVo.A; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"a\"}"; + var expected = "{\"Value\":\"a\",\"Name\":\"A\"}"; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs index 732e6ac0..c4bd3ffb 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs @@ -121,13 +121,13 @@ public void CanSerializeToString_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonDateOnlyVo.JanFirst; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + NewtonsoftJsonDateOnlyVo.JanFirst.Value.ToString("O") + "\"}"; + var expected = "{\"Value\":\"" + NewtonsoftJsonDateOnlyVo.JanFirst.Value.ToString("O") + "\",\"Name\":\"JanFirst\"}"; serialized.Should().Be(expected); } @@ -145,7 +145,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterDateOnlyVo.JanFirst; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs index 6e1a926e..91b85885 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs @@ -1,9 +1,11 @@ #nullable disable using System; +using System.Buffers.Text; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; +using System.Text.Unicode; using System.Threading.Tasks; using Dapper; using FluentAssertions; @@ -30,7 +32,7 @@ public partial class DateTimeOffsetVo { static DateTimeOffsetVo() { - Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero).AddTicks(123)); Instance("JanSecond", new DateTimeOffset(2019, 1, 2, 14, 15, 16, TimeSpan.Zero)); Instance("SomethingElse", new DateTimeOffset(2022,01,15,19,08,49, TimeSpan.Zero).AddTicks(5413764)); } @@ -121,13 +123,15 @@ public void CanSerializeToString_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonDateTimeOffsetVo.JanFirst; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; + var expected = """ + {"Value":"2019-01-01T14:15:16+00:00","Name":"JanFirst"} + """; Assert.Equal(expected, serialized); } @@ -145,7 +149,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterDateTimeOffsetVo.JanFirst; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs index 2d1e19f1..614f963e 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs @@ -40,7 +40,7 @@ static AnotherDateTimeVo() public class DateTimeVoTests { - private readonly DateTime _date1 = NoJsonDateTimeVo.Item1.Value; + private readonly DateTime _date1 = new DateTime(2019, 12, 13, 14, 15, 16, DateTimeKind.Utc) + TimeSpan.FromTicks(12345678); [Fact] public void equality_between_same_value_objects() @@ -123,13 +123,13 @@ public void CanSerializeToString_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonDateTimeVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; + var expected = "{\"Value\":\"" + _date1.ToString("O") + "\",\"Name\":\"Item1\"}"; serialized.Should().Be(expected); } @@ -147,7 +147,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterDateTimeVo.Item1; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs index 2a8a183b..65f17e52 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DecimalVoTests.cs @@ -128,13 +128,13 @@ public void CanSerializeToLong_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithNameAndValueProperty() { var vo = NoJsonDecimalVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = string.Format(CultureInfo.InvariantCulture, "{{\"Value\":{0}}}", vo.Value); + var expected = """{"Value":1.1,"Name":"Item1"}"""; Assert.Equal(expected, serialized); } @@ -152,14 +152,14 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterDecimalVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = string.Format(CultureInfo.InvariantCulture, "{{\"Value\":{0}}}", vo.Value); + var expected = """{"Value":1.1,"Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs index d5aca87d..af31081b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs @@ -122,13 +122,13 @@ public void CanSerializeToLong_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonDoubleVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = """{"Value":1.1,"Name":"Item1"}"""; Assert.Equal(expected, serialized); } @@ -146,14 +146,14 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterDoubleVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = """{"Value":1.1,"Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); @@ -225,13 +225,13 @@ public void WhenLinqToDbValueConverterUsesValueConverter() [Theory] [InlineData(1.1D)] - [InlineData("1.1D")] + [InlineData("1.1")] public void TypeConverter_CanConvertToAndFrom(object value) { var culture = new CultureInfo("en-US"); var converter = TypeDescriptor.GetConverter(typeof(NoJsonDoubleVo)); - var id = converter.ConvertFrom(null!, culture, value); + object id = converter.ConvertFrom(null!, culture, value); Assert.IsType(id); Assert.Equal(NoJsonDoubleVo.Item1, id); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs index 0510752a..6cf5a229 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs @@ -145,13 +145,13 @@ public void CanSerializeToFloat_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonFloatVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = """{"Value":1.1,"Name":"Item1"}"""; Assert.Equal(expected, serialized); } @@ -169,14 +169,14 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoTypeConverter_SerializesWithValueAndNameProperty() { var vo = NoConverterFloatVo.Item1; var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = """{"Value":1.1,"Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs index a954c2e5..202aea5d 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs @@ -115,13 +115,13 @@ public void CanSerializeToString_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonGuidVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + vo.Value + "\"}"; + var expected = "{\"Value\":\"" + vo.Value + "\",\"Name\":\"Item1\"}"; Assert.Equal(expected, serialized); } @@ -139,7 +139,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterGuidVo.Item1; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs index 5d9282ce..88428d9c 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs @@ -120,13 +120,13 @@ public void CanSerializeToInt_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonIntVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = "{\"Value\":" + vo.Value + ",\"Name\":\"Item1\"}"; Assert.Equal(expected, serialized); } @@ -144,7 +144,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterIntVo.Item1; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs index 16c5be41..d319e599 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs @@ -119,13 +119,13 @@ public void CanSerializeToLong_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonLongVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = "{\"Value\":" + vo.Value + ",\"Name\":\"Item1\"}"; Assert.Equal(expected, serialized); } @@ -143,7 +143,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterLongVo.Item1; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs index 0717d214..c3bd57a4 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs @@ -128,13 +128,13 @@ public void CanSerializeToShort_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonShortVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = "{\"Value\":" + vo.Value + ",\"Name\":\"Item1\"}"; Assert.Equal(expected, serialized); } @@ -152,7 +152,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterShortVo.Item1; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs index e96e0131..38442434 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs @@ -109,13 +109,13 @@ public void CanSerializeToString_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonStringVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + vo.Value + "\"}"; + var expected = "{\"Value\":\"" + vo.Value + "\",\"Name\":\"Item1\"}"; Assert.Equal(expected, serialized); } @@ -133,7 +133,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterStringVo.Item1; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs index 3116eaa4..48bf06d3 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs @@ -41,6 +41,9 @@ static AnotherTimeOnlyVo() public class TimeOnlyVoTests { + private static readonly TimeOnly _time1 = new TimeOnly(13, 12, 59, 123); + private static readonly TimeOnly _time2 = new TimeOnly(1, 59, 58, 123); + [Fact] public void equality_between_same_value_objects() { @@ -86,23 +89,21 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var vo = NewtonsoftJsonTimeOnlyVo.Item1; - var serializedString = NewtonsoftJsonSerializer.SerializeObject(NewtonsoftJsonTimeOnlyVo.Item1.Value.ToString()); + var serializedString = NewtonsoftJsonSerializer.SerializeObject(NewtonsoftJsonTimeOnlyVo.Item1); var deserializedVo = NewtonsoftJsonSerializer.DeserializeObject(serializedString); - Assert.Equal(vo, deserializedVo); + Assert.Equal(deserializedVo, NewtonsoftJsonTimeOnlyVo.Item1); } [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var vo = SystemTextJsonTimeOnlyVo.Item1; - var serializedString = SystemTextJsonSerializer.Serialize(SystemTextJsonTimeOnlyVo.Item1.Value.ToString()); + var serializedString = SystemTextJsonSerializer.Serialize(SystemTextJsonTimeOnlyVo.Item1); var deserializedVo = SystemTextJsonSerializer.Deserialize(serializedString); - Assert.Equal(vo, deserializedVo); + Assert.Equal(deserializedVo, SystemTextJsonTimeOnlyVo.Item1); } [Fact] @@ -121,13 +122,13 @@ public void CanSerializeToString_WithBothJsonConverters() } [Fact] - public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperties() { var vo = NoJsonTimeOnlyVo.Item1; var serialized = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + NoJsonTimeOnlyVo.Item1.Value.ToString("o") + "\"}"; + var expected = "{\"Value\":\"" + NoJsonTimeOnlyVo.Item1.Value.ToString("o") + "\",\"Name\":\"Item1\"}"; serialized.Should().Be(expected); } @@ -145,7 +146,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() } [Fact] - public void WhenNoTypeConverter_SerializesWithValueProperty() + public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() { var vo = NoConverterTimeOnlyVo.Item1; diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs index 5abc2f49..61f4966e 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/DateTimeVo.cs @@ -27,7 +27,7 @@ public partial class NoJsonDateTimeVo { static NoJsonDateTimeVo() { - Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16)); + Instance("Item1", new DateTime(2019, 12, 13, 14, 15, 16, DateTimeKind.Utc) + TimeSpan.FromTicks(12345678)); Instance("Item2", new DateTime(2020, 12, 13, 14, 15, 16)); // 2022-01-15T19:08:49.5413764+00:00 Instance("Item3", new DateTime(2022, 01, 15, 19, 08, 49, DateTimeKind.Local).AddTicks(5413764)); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs index 64a5fdc5..ced98f3c 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ComplexSerializationTests.cs @@ -48,16 +48,16 @@ public void CanSerializeAndDeserialize() deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonBoolVo.Value.Should().Be(true); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonByteVo.Value.Should().Be(1); deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonCharVo.Value.Should().Be('b'); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeOffsetVo.Value.Should().Be(new DateTimeOffset(2020, 12, 13, 23, 59, 59, 999, TimeSpan.Zero)); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeVo.Value.Should().Be(new DateTime(2020, 12, 13, 23, 59, 59, 999)); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDecimalVo.Value.Should().Be(3.33m); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDoubleVo.Value.Should().Be(4.44d); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFloatVo.Value.Should().Be(5.55f); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFooVo.Value.Age.Should().Be(42); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFooVo.Value.Name.Should().Be("Fred"); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonGuidVo.Value.Should().Be(Guid.Empty); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonIntVo.Value.Should().Be(6); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonLongVo.Value.Should().Be(7L); - deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonStringVo.Value.Should().Be("8"); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeOffsetVo.Value.Should().Be(new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero)); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDateTimeVo.Value.Should().Be(new DateTime(2019, 12, 13, 14, 15, 16)); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDecimalVo.Value.Should().Be(1.1m); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonDoubleVo.Value.Should().Be(1.1d); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFloatVo.Value.Should().Be(1.1f); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFooVo.Value.Age.Should().Be(1); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonFooVo.Value.Name.Should().Be("One"); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonGuidVo.Value.Should().Be(new Guid("00000000-0000-0000-0000-000000000001")); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonIntVo.Value.Should().Be(1); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonLongVo.Value.Should().Be(1L); + deserialized.Vogen_IntegrationTests_TestTypes_ClassVos_SystemTextJsonStringVo.Value.Should().Be("Item1!"); } } \ No newline at end of file diff --git a/tests/ScratchSnapshotTests/GeneralTests.cs b/tests/ScratchSnapshotTests/GeneralTests.cs index fd4aca55..5117e362 100644 --- a/tests/ScratchSnapshotTests/GeneralTests.cs +++ b/tests/ScratchSnapshotTests/GeneralTests.cs @@ -128,6 +128,34 @@ static DateTimeOffsetVo() } + [Fact] + public Task Instance_methods_with_more_complex_expressions() + { + var source = """ +using System; +using Intellenum; + +namespace SomethingElse +{ + [Intellenum(underlyingType: typeof(DateTimeOffset))] + public partial class DateTimeOffsetVo + { + static DateTimeOffsetVo() + { + Instance("JanFirst", new DateTimeOffset(2019, 1, 1, 14, 15, 16, TimeSpan.Zero).AddTicks(123)); + } + } +} +"""; + + return new SnapshotRunner() + .WithSource(source) + .IgnoreInitialCompilationErrors() + //.IgnoreFinalCompilationErrors() + .RunOnAllFrameworks(); + + } + [Fact] public Task test_datetimeoffset() { diff --git a/tests/ScratchSnapshotTests/SnapshotRunner.cs b/tests/ScratchSnapshotTests/SnapshotRunner.cs index 97680e67..01e18f04 100644 --- a/tests/ScratchSnapshotTests/SnapshotRunner.cs +++ b/tests/ScratchSnapshotTests/SnapshotRunner.cs @@ -112,7 +112,7 @@ public async Task RunOn(params TargetFramework[] frameworks) //todo: remove #if DEBUG - //await File.WriteAllTextAsync(@"c:\git\intellenum\tests\scratchsnapshottests\test.cs", output); + await File.WriteAllTextAsync(@"c:\git\intellenum\tests\scratchsnapshottests\test.cs", output); #endif diagnostics.Should().BeEmpty(@$"because the following source code should compile on {eachFramework}: " + _source); From 19bd067010c905099d2a34305eb710f98c62f506 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 4 Apr 2023 08:35:11 +0100 Subject: [PATCH 6/8] All tests passing --- .../ClassVos/BoolVoTests.cs | 19 ++++++++-- .../ClassVos/ByteVoTests.cs | 4 +-- .../ClassVos/DateOnlyVoTests.cs | 2 +- .../ClassVos/DateTimeOffsetVoTests.cs | 9 +++-- .../ClassVos/DateTimeVoTests.cs | 6 ++-- .../ClassVos/DoubleVoTests.cs | 8 +++++ .../ClassVos/FloatVoTests.cs | 4 +++ .../ClassVos/GuidVoTests.cs | 2 +- .../ClassVos/IntVoTests.cs | 2 +- .../ClassVos/LongVoTests.cs | 2 +- .../ClassVos/ShortVoTests.cs | 2 +- .../ClassVos/StringVoTests.cs | 2 +- .../ClassVos/TimeOnlyVoTests.cs | 6 ++-- .../ClassVos/Types/TimeOnlyVo.cs | 36 +++++++++---------- .../CustomizationTests.cs | 2 +- 15 files changed, 65 insertions(+), 41 deletions(-) diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs index 8debac08..3de3eb4e 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/BoolVoTests.cs @@ -143,7 +143,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":true}"; + var expected = """{"Value":true,"Name":"Yes"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); @@ -215,15 +215,28 @@ public void WhenLinqToDbValueConverterUsesValueConverter() [Theory] [InlineData("true", "True")] [InlineData("True", "True")] + public void TypeConverter_CanConvertToAndFrom_strings_1(object input, string expectedString) + { + TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); + + object converted = converter.ConvertFrom(input); + Assert.IsType(converted); + Assert.Equal(NoJsonBoolVo.Yes, converted); + + object reconverted = converter.ConvertTo(converted, input.GetType()); + Assert.Equal(expectedString, reconverted); + } + + [Theory] [InlineData("false", "False")] [InlineData("False", "False")] - public void TypeConverter_CanConvertToAndFrom_strings(object input, string expectedString) + public void TypeConverter_CanConvertToAndFrom_strings_2(object input, string expectedString) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(NoJsonBoolVo)); object converted = converter.ConvertFrom(input); Assert.IsType(converted); - Assert.Equal(NoJsonBoolVo.Yes, converted); + Assert.Equal(NoJsonBoolVo.No, converted); object reconverted = converter.ConvertTo(converted, input.GetType()); Assert.Equal(expectedString, reconverted); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs index 048bac62..4a5e7e5b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ByteVoTests.cs @@ -42,8 +42,8 @@ public void equality_between_same_value_objects() var original = AnotherByteVo.Eighteen; var other = AnotherByteVo.Eighteen; - ((original as IEquatable).Equals(other)).Should().BeTrue(); - ((other as IEquatable).Equals(original)).Should().BeTrue(); + ((original as IEquatable).Equals(other)).Should().BeTrue(); + ((other as IEquatable).Equals(original)).Should().BeTrue(); } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs index c4bd3ffb..dd78ffe3 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateOnlyVoTests.cs @@ -152,7 +152,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + NewtonsoftJsonDateOnlyVo.JanFirst.Value.ToString("O") + "\"}"; + var expected = """{"Value":"2021-01-01","Name":"JanFirst"}"""; newtonsoft.Should().Be(expected); systemText.Should().Be(expected); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs index 91b85885..8b1441dd 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeOffsetVoTests.cs @@ -86,7 +86,7 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var value = _date1; + var value = NewtonsoftJsonDateTimeOffsetVo.JanFirst.Value; var vo = NewtonsoftJsonDateTimeOffsetVo.JanFirst; var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); @@ -98,7 +98,7 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var value = _date1; + var value = NewtonsoftJsonDateTimeOffsetVo.JanFirst.Value; var vo = SystemTextJsonDateTimeOffsetVo.JanFirst; var serializedString = SystemTextJsonSerializer.Serialize(value); @@ -143,8 +143,7 @@ public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() var serialized = NewtonsoftJsonSerializer.SerializeObject(vo); - var expected = $"\"{_date1:o}\""; - + var expected = "\"2019-01-01T14:15:16.0000000+00:00\""; Assert.Equal(expected, serialized); } @@ -156,7 +155,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; + var expected = """{"Value":"2019-01-01T14:15:16+00:00","Name":"JanFirst"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs index 614f963e..5a83f0f0 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DateTimeVoTests.cs @@ -86,7 +86,7 @@ public void CanSerializeToString_WithSystemTextJsonProvider() [Fact] public void CanDeserializeFromString_WithNewtonsoftJsonProvider() { - var value = _date1; + var value = NewtonsoftJsonDateTimeVo.Item1.Value; var vo = NewtonsoftJsonDateTimeVo.Item1; var serializedString = NewtonsoftJsonSerializer.SerializeObject(value); @@ -98,7 +98,7 @@ public void CanDeserializeFromString_WithNewtonsoftJsonProvider() [Fact] public void CanDeserializeFromString_WithSystemTextJsonProvider() { - var value = _date1; + var value = NewtonsoftJsonDateTimeVo.Item1.Value; var vo = SystemTextJsonDateTimeVo.Item1; var serializedString = SystemTextJsonSerializer.Serialize(value); @@ -154,7 +154,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + _date1.ToString("O") + "\"}"; + var expected = """{"Value":"2019-12-13T14:15:16","Name":"Item1"}"""; newtonsoft.Should().Be(expected); systemText.Should().Be(expected); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs index af31081b..0738f9d2 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/DoubleVoTests.cs @@ -128,7 +128,11 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperti var serialized = SystemTextJsonSerializer.Serialize(vo); +#if !NET5_0_OR_GREATER + var expected = """{"Value":1.1000000000000001,"Name":"Item1"}"""; +#else var expected = """{"Value":1.1,"Name":"Item1"}"""; +#endif Assert.Equal(expected, serialized); } @@ -153,7 +157,11 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); +#if !NET5_0_OR_GREATER + var expected = """{"Value":1.1000000000000001,"Name":"Item1"}"""; +#else var expected = """{"Value":1.1,"Name":"Item1"}"""; +#endif Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs index 6cf5a229..571e727a 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs @@ -151,7 +151,11 @@ public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueAndNameProperti var serialized = SystemTextJsonSerializer.Serialize(vo); +#if !NET5_0_OR_GREATER + var expected = """{"Value":1.10000002,"Name":"Item1"}"""; +#else var expected = """{"Value":1.1,"Name":"Item1"}"""; +#endif Assert.Equal(expected, serialized); } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs index 202aea5d..97323a7a 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/GuidVoTests.cs @@ -146,7 +146,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + vo.Value + "\"}"; + var expected = """{"Value":"00000000-0000-0000-0000-000000000001","Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs index 88428d9c..d9ade1c2 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/IntVoTests.cs @@ -151,7 +151,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = """{"Value":1,"Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs index d319e599..33b06980 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/LongVoTests.cs @@ -150,7 +150,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = """{"Value":1,"Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs index c3bd57a4..8f911d1d 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/ShortVoTests.cs @@ -159,7 +159,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":" + vo.Value + "}"; + var expected = """{"Value":1,"Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs index 38442434..0e16d957 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/StringVoTests.cs @@ -140,7 +140,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + vo.Value + "\"}"; + var expected = """{"Value":"Item1","Name":"Item1"}"""; Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs index 48bf06d3..83769f63 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/TimeOnlyVoTests.cs @@ -153,7 +153,7 @@ public void WhenNoJsonConverter_SerializesWithValueAndNameProperties() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); - var expected = "{\"Value\":\"" + NoConverterTimeOnlyVo.Item1.Value.ToString("o") + "\"}"; + var expected = """{"Value":"01:02:03.0040000","Name":"Item1"}"""; newtonsoft.Should().Be(expected); systemText.Should().Be(expected); @@ -191,7 +191,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() using var connection = new SqliteConnection("DataSource=:memory:"); await connection.OpenAsync(); - IEnumerable results = await connection.QueryAsync("SELECT '05:06:07.08'"); + IEnumerable results = await connection.QueryAsync("SELECT '05:06:07.008'"); DapperTimeOnlyVo actual = Assert.Single(results); @@ -226,7 +226,7 @@ public void WhenLinqToDbValueConverterUsesValueConverter() } [Theory] - [InlineData("05:06:07.08")] + [InlineData("05:06:07.008")] public void TypeConverter_CanConvertToAndFrom(string value) { var converter = TypeDescriptor.GetConverter(typeof(NoJsonTimeOnlyVo)); diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs index 43f0ca43..6f23068b 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/Types/TimeOnlyVo.cs @@ -9,8 +9,8 @@ public partial class TimeOnlyVo { static TimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -19,8 +19,8 @@ public partial class NoConverterTimeOnlyVo { static NoConverterTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -29,8 +29,8 @@ public partial class NoJsonTimeOnlyVo { static NoJsonTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -39,8 +39,8 @@ public partial class NewtonsoftJsonTimeOnlyVo { static NewtonsoftJsonTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -49,8 +49,8 @@ public partial class SystemTextJsonTimeOnlyVo { static SystemTextJsonTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -60,8 +60,8 @@ public partial class BothJsonTimeOnlyVo { static BothJsonTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -71,8 +71,8 @@ public partial class EfCoreTimeOnlyVo { static EfCoreTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -81,8 +81,8 @@ public partial class DapperTimeOnlyVo { static DapperTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } @@ -91,8 +91,8 @@ public partial class LinqToDbTimeOnlyVo { static LinqToDbTimeOnlyVo() { - Instance("Item1", new TimeOnly(1, 2, 3, 4)); - Instance("Item2", new TimeOnly(5, 6, 7, 8)); + Instance("Item1", new TimeOnly(1, 2, 3, 04)); + Instance("Item2", new TimeOnly(5, 6, 7, 08)); } } } diff --git a/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs index 77ebf57b..c474e774 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/CustomizationTests.cs @@ -6,7 +6,7 @@ namespace MediumTests.SerializationAndConversionTests; [Intellenum(underlyingType: typeof(double), customizations: Customizations.TreatNumberAsStringInSystemTextJson)] -[Instance("Item1", 720742592373919744)] +[Instance("Item1", 720742592373919744d)] [Instance("Item2", 2.2d)] public partial class DoubleHolderId_string { From c23e84d6a578d6ab679b2efd16e2e61d194e0588 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 4 Apr 2023 22:24:17 +0100 Subject: [PATCH 7/8] Fix tests --- .../SerializationAndConversionTests/ClassVos/FloatVoTests.cs | 4 ++++ ....Explicit_instances_using_Instance_attributes.verified.txt | 3 +++ ...ests.Explicit_instances_using_Instance_method.verified.txt | 3 +++ ...licit_instances_using_a_mixture_of_mechanisms.verified.txt | 3 +++ ...t_instance_tests.Explicit_instances_using_new.verified.txt | 3 +++ ...sts.Explicit_instances_using_target_typed_new.verified.txt | 3 +++ .../GeneralTests.int_created_successfully.verified.txt | 3 +++ .../GeneralTests.string_created_successfully.verified.txt | 3 +++ 8 files changed, 25 insertions(+) diff --git a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs index 571e727a..71d09e4f 100644 --- a/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs +++ b/tests/ConsumerTests/SerializationAndConversionTests/ClassVos/FloatVoTests.cs @@ -180,7 +180,11 @@ public void WhenNoTypeConverter_SerializesWithValueAndNameProperty() var newtonsoft = SystemTextJsonSerializer.Serialize(vo); var systemText = SystemTextJsonSerializer.Serialize(vo); +#if !NET5_0_OR_GREATER + var expected = """{"Value":1.10000002,"Name":"Item1"}"""; +#else var expected = """{"Value":1.1,"Name":"Item1"}"""; +#endif Assert.Equal(expected, newtonsoft); Assert.Equal(expected, systemText); diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt index 8b729c0e..ded868d8 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt @@ -105,6 +105,9 @@ private void Throw() _isInitialized = true; } + // exclude from serialization + + public string Name { get; private set; } /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt index 778e248c..e6bdfcb4 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt @@ -105,6 +105,9 @@ private void Throw() _isInitialized = true; } + // exclude from serialization + + public string Name { get; private set; } /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt index ecd9cd57..4c92cdf5 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt @@ -105,6 +105,9 @@ private void Throw() _isInitialized = true; } + // exclude from serialization + + public string Name { get; private set; } /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt index 077e27e1..13a0883d 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt @@ -105,6 +105,9 @@ private void Throw() _isInitialized = true; } + // exclude from serialization + + public string Name { get; private set; } /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt index 077e27e1..13a0883d 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_target_typed_new.verified.txt @@ -105,6 +105,9 @@ private void Throw() _isInitialized = true; } + // exclude from serialization + + public string Name { get; private set; } /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt index c6039ac7..4f6ce4a6 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt @@ -105,6 +105,9 @@ private void Throw() _isInitialized = true; } + // exclude from serialization + + public string Name { get; private set; } /// diff --git a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt index e0ce7a40..ef6fbf61 100644 --- a/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt +++ b/tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt @@ -105,6 +105,9 @@ private void Throw() _isInitialized = true; } + // exclude from serialization + + public string Name { get; private set; } /// From ebfb4aa58cfad9ffb38189cbe407067606ac85ba Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Tue, 4 Apr 2023 22:31:52 +0100 Subject: [PATCH 8/8] Fix analyzer --- src/Intellenum/AnalyzerReleases.Shipped.md | 2 +- src/Intellenum/AnalyzerReleases.Unshipped.md | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Intellenum/AnalyzerReleases.Shipped.md b/src/Intellenum/AnalyzerReleases.Shipped.md index 33c0f807..2097f773 100644 --- a/src/Intellenum/AnalyzerReleases.Shipped.md +++ b/src/Intellenum/AnalyzerReleases.Shipped.md @@ -1,7 +1,7 @@ ; Shipped analyzer releases ; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md -## Release 3.0.3 +## Release 0.0.1 ### New Rules diff --git a/src/Intellenum/AnalyzerReleases.Unshipped.md b/src/Intellenum/AnalyzerReleases.Unshipped.md index d56a1ce3..cc67b67f 100644 --- a/src/Intellenum/AnalyzerReleases.Unshipped.md +++ b/src/Intellenum/AnalyzerReleases.Unshipped.md @@ -5,4 +5,3 @@ Rule ID | Category | Severity | Notes --------|----------|----------|------- -AddValidationMethod | Usage | Info | AddValidationAnalyzer \ No newline at end of file