ensure that it is not possible to create an instance with an existing global identifier

This commit is contained in:
Michael Becker 2025-01-04 21:14:10 -05:00
parent 4c656d8d8b
commit d69a392e0c
3 changed files with 20 additions and 5 deletions

View File

@ -32,6 +32,7 @@ using System.Text;
using System.Text.Json.Nodes;
using System.Xml.XPath;
using MBS.Core;
using MBS.Core.Collections;
using MBS.Core.Extensibility;
using MBS.Core.IO;
using Mocha.Core.MethodImplementations;
@ -510,6 +511,11 @@ public abstract class Oms
}
public InstanceHandle CreateInstanceOf(IInstanceReference ir_class, Guid guid)
{
if (TryGetInstance(guid, out InstanceHandle ihAlreadyExists))
{
throw new DuplicateKeyException();
}
if (GetParentClass(ir_class) != GetInstance(KnownInstanceGuids.Classes.Class))
{
throw new InvalidOperationException("cannot create instance of something that is not a Class");

View File

@ -16,8 +16,8 @@
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
using System.Runtime.CompilerServices;
using Mocha.Core.OmsImplementations.Mini;
using MBS.Core.Collections;
using Mocha.Core.Oop;
namespace Mocha.Core.Tests;
@ -168,4 +168,13 @@ public class BasicTests : OmsTestsBase
Assert.That(ihTextAttribute2, Is.EqualTo(ihTextAttribute));
}
}
[Test]
public void Create_Instance_with_Existing_Global_Identifier_Fails()
{
Assert.Throws<DuplicateKeyException>(delegate ()
{
InstanceHandle c_Attribute = Oms.CreateInstanceOf(Oms.GetInstance<Class>(KnownInstanceGuids.Classes.Class), KnownInstanceGuids.Classes.Attribute);
});
}
}

View File

@ -51,13 +51,13 @@ public abstract class OmsTestsBase
// create the Test Class
InstanceHandle c_Class = Oms.GetInstance(KnownInstanceGuids.Classes.Class);
InstanceHandle irTestClass = Oms.CreateInstanceOf(c_Class, TEST_CLASS_GUID);
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
Assert.That(irTestClass, Is.Not.EqualTo(InstanceHandle.Empty));
InstanceHandle a_Name = Oms.GetInstance(KnownAttributeGuids.Text.Name);
Oms.SetAttributeValue(irTestClass, a_Name, "Test Class");
InstanceHandle irTestClass2 = Oms.CreateInstanceOf(c_Class, TEST_CLASS2_GUID);
InstanceHandle irTestClass2 = Oms.GetInstance(TEST_CLASS2_GUID);
Assert.That(irTestClass2, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.SetAttributeValue(irTestClass2, Oms.GetInstance(KnownAttributeGuids.Text.Name), "Test Class 2");