diff --git a/demo/Primify.Benchmarks.Demo/Primify.Benchmarks.Demo.csproj b/demo/Primify.Benchmarks.Demo/Primify.Benchmarks.Demo.csproj index 5510c50..fcdb994 100644 --- a/demo/Primify.Benchmarks.Demo/Primify.Benchmarks.Demo.csproj +++ b/demo/Primify.Benchmarks.Demo/Primify.Benchmarks.Demo.csproj @@ -8,7 +8,7 @@ - + diff --git a/demo/Primify.Demo/Primify.Demo.csproj b/demo/Primify.Demo/Primify.Demo.csproj index bd86cc4..cd73cd7 100644 --- a/demo/Primify.Demo/Primify.Demo.csproj +++ b/demo/Primify.Demo/Primify.Demo.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/Primify.Generators/PrimifyGenerator.cs b/src/Primify.Generators/PrimifyGenerator.cs index b8020f0..ca4600d 100644 --- a/src/Primify.Generators/PrimifyGenerator.cs +++ b/src/Primify.Generators/PrimifyGenerator.cs @@ -186,7 +186,7 @@ ImmutableArray typeDeclarations var toStringOverride = GenerateToStringOverride(); var liteDbInitializer = GenerateLiteDbInitializer(wrapperName, wrapperArgument); var implicitCasting = GenerateImplicitCasting(wrapperName, wrapperArgument); - var explicitCasting = GenerateExplicitCasting(wrapperName, wrapperArgument); + var implicitExplicitCasting = GenerateImplicitExplicitCasting(wrapperName, wrapperArgument); // Build up the source code var code = $$""" @@ -226,7 +226,7 @@ namespace {{namespaceName}}; {{implicitCasting}} - {{explicitCasting}} + {{implicitExplicitCasting}} {{toStringOverride}} @@ -317,10 +317,10 @@ private static string GenerateToStringOverride() => public override string ToString() => Value.ToString(); """; - private static string GenerateExplicitCasting(string name, string argument) => + private static string GenerateImplicitExplicitCasting(string name, string argument) => $""" - public static explicit operator {name}({argument} value) => From(value); - public static explicit operator {argument}({name} value) => value.Value; + public static implicit operator {name}({argument} value) => From(value); + public static implicit operator {argument}({name} value) => value.Value; """; private static string GenerateImplicitCasting(string name, string argument) @@ -376,11 +376,9 @@ private static string GenerateImplicitCasting(string name, string argument) return $""" // Casting for BSON - public static implicit operator LiteDB.BsonValue({name} value) => - {toBson}; + public static implicit operator LiteDB.BsonValue({name} value) => {toBson}; - public static implicit operator {name}(LiteDB.BsonValue value) - {fromBsonImplementation} + public static implicit operator {name}(LiteDB.BsonValue value) {fromBsonImplementation} """; } diff --git a/src/Primify/.idea/.idea.Primify.dir/.idea/.gitignore b/src/Primify/.idea/.idea.Primify.dir/.idea/.gitignore new file mode 100644 index 0000000..3e5889a --- /dev/null +++ b/src/Primify/.idea/.idea.Primify.dir/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/.idea.Primify.iml +/contentModel.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/src/Primify/.idea/.idea.Primify.dir/.idea/indexLayout.xml b/src/Primify/.idea/.idea.Primify.dir/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/src/Primify/.idea/.idea.Primify.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/Primify/.idea/.idea.Primify.dir/.idea/vcs.xml b/src/Primify/.idea/.idea.Primify.dir/.idea/vcs.xml new file mode 100644 index 0000000..8fe5bdb --- /dev/null +++ b/src/Primify/.idea/.idea.Primify.dir/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/Primify/Primify.csproj b/src/Primify/Primify.csproj index ad1f416..590f023 100644 --- a/src/Primify/Primify.csproj +++ b/src/Primify/Primify.csproj @@ -3,14 +3,14 @@ net9.0 Primify - 1.4.23 + 1.5.0 true true - + diff --git a/tests/Primify.Tests/Class1Tests.cs b/tests/Primify.Tests/Class1Tests.cs index 64242b3..624fbac 100644 --- a/tests/Primify.Tests/Class1Tests.cs +++ b/tests/Primify.Tests/Class1Tests.cs @@ -34,6 +34,37 @@ public void Class1_CreatesType_WhenFromIsCalled() Assert.Equal(expectedValue, result.Value); } + [Fact] + public void Class1_SuccessfullyConverts_WhenSetImplicitly() + { + // Arrange + int expectedValue = 1001; + + // Act + Class1 result = expectedValue; + testOutputHelper.WriteLine(result.ToString()); + + // Assert + Assert.Equal(expectedValue, result.Value); + Assert.Equal(expectedValue, result.Value); + } + + [Fact] + public void Class1_SuccessfullyConverts_WhenDereferencedImplicitly() + { + // Arrange + int expectedValue = 1001; + var input = Class1.From(expectedValue); + + // Act + int result = input; + testOutputHelper.WriteLine(result.ToString()); + + // Assert + Assert.Equal(expectedValue, result); + Assert.Equal(expectedValue, result); + } + [Fact] public void Class1_CreatesType_WhenExplicitlyCastToFromType() { @@ -74,16 +105,16 @@ public void Class1WithPredefinedProperty_IgnoresReadonly_WhenSerializedWithSyste Assert.Equal(expectedValue, result.Value); - // System.Text.Json serialization - var json = JsonSerializer.Serialize(result); - testOutputHelper.WriteLine("\nSystem.Text.Json serialization:"); - testOutputHelper.WriteLine(json); + // System.Text.Json serialization + var json = JsonSerializer.Serialize(result); + testOutputHelper.WriteLine("\nSystem.Text.Json serialization:"); + testOutputHelper.WriteLine(json); - // System.Text.Json deserialization - var stjDeserialized = JsonSerializer.Deserialize(json); - testOutputHelper.WriteLine("\nSystem.Text.Json deserialized value:"); - testOutputHelper.WriteLine(stjDeserialized?.ToString() ?? "null"); - Assert.Equal(expectedValue, stjDeserialized?.Value); + // System.Text.Json deserialization + var stjDeserialized = JsonSerializer.Deserialize(json); + testOutputHelper.WriteLine("\nSystem.Text.Json deserialized value:"); + testOutputHelper.WriteLine(stjDeserialized?.ToString() ?? "null"); + Assert.Equal(expectedValue, stjDeserialized?.Value); } [Fact] @@ -104,7 +135,8 @@ public void Class1WithPredefinedProperty_IgnoresReadonly_WhenSerializedNewtonsof testOutputHelper.WriteLine(newtonsoftJson); // Newtonsoft.Json deserialization - var njsDeserialized = Newtonsoft.Json.JsonConvert.DeserializeObject(newtonsoftJson); + var njsDeserialized = + Newtonsoft.Json.JsonConvert.DeserializeObject(newtonsoftJson); testOutputHelper.WriteLine("\nNewtonsoft.Json deserialized value:"); testOutputHelper.WriteLine(njsDeserialized?.ToString() ?? "null"); Assert.Equal(expectedValue, njsDeserialized?.Value); @@ -122,27 +154,28 @@ public void Class1WithPredefinedProperty_IgnoresReadonly_WhenSerializedNewtonsof Assert.Equal(expectedValue, result.Value); - // BSON serialization - using var ms = new MemoryStream(); - using (var writer = new Newtonsoft.Json.Bson.BsonDataWriter(ms)) - { - var serializer = new Newtonsoft.Json.JsonSerializer(); - serializer.Serialize(writer, result); - } - var bsonBytes = ms.ToArray(); - var bsonBase64 = Convert.ToBase64String(bsonBytes); - testOutputHelper.WriteLine("\nBSON serialization (Base64):"); - testOutputHelper.WriteLine(bsonBase64); - - // BSON deserialization - using var ms2 = new MemoryStream(bsonBytes); - using (var reader = new Newtonsoft.Json.Bson.BsonDataReader(ms2)) - { - var serializer = new Newtonsoft.Json.JsonSerializer(); - var bsonDeserialized = serializer.Deserialize(reader); - testOutputHelper.WriteLine("\nBSON deserialized value:"); - testOutputHelper.WriteLine(bsonDeserialized?.ToString() ?? "null"); - Assert.Equal(expectedValue, bsonDeserialized?.Value); - } + // BSON serialization + using var ms = new MemoryStream(); + using (var writer = new Newtonsoft.Json.Bson.BsonDataWriter(ms)) + { + var serializer = new Newtonsoft.Json.JsonSerializer(); + serializer.Serialize(writer, result); + } + + var bsonBytes = ms.ToArray(); + var bsonBase64 = Convert.ToBase64String(bsonBytes); + testOutputHelper.WriteLine("\nBSON serialization (Base64):"); + testOutputHelper.WriteLine(bsonBase64); + + // BSON deserialization + using var ms2 = new MemoryStream(bsonBytes); + using (var reader = new Newtonsoft.Json.Bson.BsonDataReader(ms2)) + { + var serializer = new Newtonsoft.Json.JsonSerializer(); + var bsonDeserialized = serializer.Deserialize(reader); + testOutputHelper.WriteLine("\nBSON deserialized value:"); + testOutputHelper.WriteLine(bsonDeserialized?.ToString() ?? "null"); + Assert.Equal(expectedValue, bsonDeserialized?.Value); + } } } diff --git a/tests/Primify.Tests/Primify.Tests.csproj b/tests/Primify.Tests/Primify.Tests.csproj index 0847438..044bc97 100644 --- a/tests/Primify.Tests/Primify.Tests.csproj +++ b/tests/Primify.Tests/Primify.Tests.csproj @@ -14,10 +14,10 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all