using System; using Mocha.Core.Oop; using Mocha.Testing; using Newtonsoft.Json.Serialization; namespace Mocha.Core.Tests; public class DerivedInstanceTests : OmsTestsBase { Class TEST_CLASS_DERIVED; InstanceKey ik_derived; protected override void AfterSetup() { base.AfterSetup(); TEST_CLASS_DERIVED = Oms.CreateClass("Test Class (Derived)"); ik_derived = Oms.GetInstanceKey(TEST_CLASS_DERIVED); Oms.SetAttributeValue(TEST_CLASS_DERIVED, Oms.GetInstance(KnownAttributeGuids.Boolean.Derived), true); // Derived classes are represented only by InstanceKeys with the form {ClassId}!{DerivedData}, where // {DerivedData} is a Base64 string encoding all the attributes and relationships associated with the // derived instance. // ? Should we be concerned with memory leaks given the lazy implementation of GetInstance on a derived // ? InstanceKey essentially allocates a new InstanceHandle each time it's called ??? Oms.AddAttribute(TEST_CLASS_DERIVED, Oms.GetInstance(KnownAttributeGuids.Text.Name)); } const string TEST_DERIVED_VALUE = "Test Class Derived #1"; [Test] public void Derived_Instance_Test_get_Text_Attribute_Value() { InstanceHandle iTestClassDerived = Oms.CreateInstanceOf(TEST_CLASS_DERIVED); Oms.SetAttributeValue(iTestClassDerived, Oms.GetInstance(KnownAttributeGuids.Text.Name), TEST_DERIVED_VALUE); InstanceKey ik = Oms.GetInstanceKey(iTestClassDerived); InstanceHandle h2 = Oms.GetInstance(ik); Dictionary? dict = Oms.GetDerivedData(h2); string attVName = Oms.GetAttributeValue(h2, Oms.GetInstance(KnownAttributeGuids.Text.Name)); Assert.That(attVName, Is.EqualTo(TEST_DERIVED_VALUE)); } [Test] public void Derived_Instance_Test__instance_key_Parse() { string ikStr = String.Format("{0}!FQAAABVUZXN0IENsYXNzIERlcml2ZWQgIzE=", ik_derived.InstanceIndex); InstanceKey ik = InstanceKey.Parse(ikStr); InstanceHandle ih = Oms.GetInstance(ik); string attVName = Oms.GetAttributeValue(ik, Oms.GetInstance(KnownAttributeGuids.Text.Name)); Assert.That(attVName, Is.EqualTo(TEST_DERIVED_VALUE)); } }