From 279c326ac3188a6396a2bbce1f49bf0d6b34360f Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Sat, 30 Sep 2023 13:31:39 +0500 Subject: [PATCH] add test check Current Work From ImplicitOperator to Class --- .../WhenMappingRecordRegression.cs | 69 +++++++++++++++++-- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/src/Mapster.Tests/WhenMappingRecordRegression.cs b/src/Mapster.Tests/WhenMappingRecordRegression.cs index 5bc1a9bb..a0f76c5a 100644 --- a/src/Mapster.Tests/WhenMappingRecordRegression.cs +++ b/src/Mapster.Tests/WhenMappingRecordRegression.cs @@ -206,12 +206,30 @@ public void TSousreIsObjectUpdateUseDynamicCast() _result.X.ShouldBe(123); } - TestClassPublicCtr SomemapWithDynamic(object source) + /// + /// https://github.com/MapsterMapper/Mapster/issues/569 + /// + [TestMethod] + public void ImplicitOperatorCurrentWorkFromClass() { - var dest = new TestClassPublicCtr { X = 321 }; - var dest1 = source.Adapt(dest,source.GetType(),dest.GetType()); - - return dest; + var guid = Guid.NewGuid(); + + var pocoWithGuid1 = new PocoWithGuid { Id = guid }; + var pocoWithId2 = new PocoWithId { Id = new Id(guid) }; + + var pocoWithId1 = pocoWithGuid1.Adapt(); + var pocoWithGuid2 = pocoWithId2.Adapt(); + + pocoWithId1.Id.ToString().Equals(guid.ToString()).ShouldBeTrue(); + pocoWithGuid2.Id.Equals(guid).ShouldBeTrue(); + + + var _result = pocoWithId1.Adapt(pocoWithGuid2); + + _result.Id.ToString().Equals(guid.ToString()).ShouldBeTrue(); // Guid value transmitted + object.ReferenceEquals(_result, pocoWithGuid2).ShouldBeTrue(); // Not created new instanse from class pocoWithGuid2 + _result.ShouldBeOfType(); + } @@ -225,6 +243,7 @@ public void DetectFakeRecord() var _destination = new FakeRecord { X = 300 }; var _result = _source.Adapt(_destination); _destination.X.ShouldBe(200); + object.ReferenceEquals(_destination, _result).ShouldBeTrue(); } @@ -271,11 +290,51 @@ TestClassPublicCtr Somemap(object source) #endregion NowNotWorking + + #region TestMethods + + TestClassPublicCtr SomemapWithDynamic(object source) + { + var dest = new TestClassPublicCtr { X = 321 }; + var dest1 = source.Adapt(dest, source.GetType(), dest.GetType()); + + object.ReferenceEquals(dest,dest1).ShouldBeTrue(); // Is not new instanse + + return dest; + } + + + #endregion TestMethods } #region TestClasses + class PocoWithGuid + { + public Guid Id { get; init; } + } + + class PocoWithId + { + public Id Id { get; init; } + } + + class Id + { + private readonly Guid _guid; + + public Id(Guid id) => _guid = id; + + public static implicit operator Id(Guid value) => new(value); + public static implicit operator Guid(Id value) => value._guid; + + public override string ToString() => _guid.ToString(); + } + + + + public class FakeRecord { protected FakeRecord(FakeRecord fake) { }