using System; using JetBrains.Rd.Impl; using JetBrains.Rd.Reflection; using JetBrains.Serialization; using NUnit.Framework; namespace Test.RdFramework.Reflection { [TestFixture] public class PolymorphicScalarBuiltInSerializersTest : RdReflectionTestBase { [Test] public void TestClass() { WithExts((c, s) => { c.Class.Value = new Type1(); var x = c.Class.Value; var y = s.Class.Value; Assert.AreEqual(x.GetType(), y.GetType()); Assert.AreNotEqual(x.Mark, y.Mark); Assert.AreNotSame(x, y); }); } [Test] public void TestInterface() { WithExts((c, s) => { c.Interface.Value = new Type1(); var x = c.Interface.Value; var y = s.Interface.Value; Assert.AreEqual(x.GetType(), y.GetType()); Assert.AreNotEqual(x.Mark, y.Mark); Assert.AreNotSame(x, y); }); } /// /// Types should relay on reflection serializer even if built in serializer exists in the base type. /// Types should match exactly to avoid issues at runtime. /// [Test] public void TestReflectionSerializer() { WithExts((c, s) => { c.Interface.Value = new TypeReflectionSerializer(); var x = c.Interface.Value; var y = s.Interface.Value; Assert.AreEqual(x.GetType(), y.GetType()); Assert.AreNotSame(x, y); }); } public override void SetUp() { base.SetUp(); AddType(typeof(Base)); AddType(typeof(Type1)); AddType(typeof(TypeReflectionSerializer)); } [RdExt] public class TestExt : RdExtReflectionBindableBase { public RdProperty Interface { get; } internal RdProperty Class { get; } public RdProperty Array { get; } } public interface IBase { /// /// This Guid always change during intrinsic deserialization /// Guid Mark { get; set; } } [AssertBuiltInType(BuiltInSerializers.BuiltInType.Methods)] public class Base : IBase { public Guid Mark { get; set; } public static Base Read(UnsafeReader reader) { Assert.AreEqual("base", reader.ReadString()); return new Base { Mark = Guid.NewGuid() }; } public void Write(UnsafeWriter writer) => writer.WriteString("base"); } [AssertBuiltInType(BuiltInSerializers.BuiltInType.Methods)] public class Type1 : Base { public new static Type1 Read(UnsafeReader reader) { Assert.AreEqual("type1", reader.ReadString()); return new Type1 { Mark = Guid.NewGuid() }; } public new void Write(UnsafeWriter writer) => writer.WriteString("type1"); } [AssertBuiltInType(BuiltInSerializers.BuiltInType.None)] public class TypeReflectionSerializer : Base { } } }