161 lines
6.7 KiB
C#
161 lines
6.7 KiB
C#
// Copyright (C) 2024 Michael Becker <alcexhim@gmail.com>
|
|
//
|
|
// This file is part of Mocha.NET.
|
|
//
|
|
// Mocha.NET is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Mocha.NET is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
using Mocha.Core.Oop;
|
|
using Mocha.Core.Oop.Methods;
|
|
|
|
namespace Mocha.Core.Tests.MethodTests;
|
|
|
|
public class GetRelationshipTests : MethodTestsBase
|
|
{
|
|
|
|
[Test]
|
|
public void SingularGetRelationshipTestForSingularRelationship()
|
|
{
|
|
InstanceHandle c_TestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle c_TestClass2 = Oms.GetInstance(TEST_CLASS2_GUID);
|
|
|
|
InstanceHandle i_TestClass = Oms.CreateInstanceOf(c_TestClass);
|
|
InstanceHandle i_TestClass2 = Oms.CreateInstanceOf(c_TestClass2);
|
|
|
|
Oms.AssignRelationship(i_TestClass, r_Test_Class__has__Test_Class_2, i_TestClass2);
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
GetRelationshipMethod method = builder.CreateGetRelationshipMethod(c_TestClass, "get", "Test Class.has Test Class 2", AccessModifier.Public, true, r_Test_Class__has__Test_Class_2, true);
|
|
ReturnInstanceSetMethodBinding rsmb = builder.CreateReturnInstanceSetMethodBinding(method);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
context.SetWorkData(c_TestClass, i_TestClass); // set the `this` parm
|
|
|
|
InstanceHandle valueIH = Oms.Execute(context, rsmb);
|
|
|
|
object? value = context.GetWorkData(valueIH);
|
|
Assert.That(value, Is.EqualTo(i_TestClass2));
|
|
}
|
|
|
|
[Test]
|
|
public void SingularGetRelationshipTestForNonsingularRelationship()
|
|
{
|
|
InstanceHandle c_TestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle c_TestClass2 = Oms.GetInstance(TEST_CLASS2_GUID);
|
|
|
|
InstanceHandle i_TestClass = Oms.CreateInstanceOf(c_TestClass);
|
|
|
|
InstanceHandle i_TestClass2_1 = Oms.CreateInstanceOf(c_TestClass2);
|
|
InstanceHandle i_TestClass2_2 = Oms.CreateInstanceOf(c_TestClass2);
|
|
InstanceHandle i_TestClass2_3 = Oms.CreateInstanceOf(c_TestClass2);
|
|
|
|
InstanceHandle[] TEST_VALUES = new InstanceHandle[] { i_TestClass2_2, i_TestClass2_3, i_TestClass2_1 };
|
|
Oms.AssignRelationship(i_TestClass, r_Test_Class__has_multiple__Test_Class_2, TEST_VALUES);
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
GetRelationshipMethod method = builder.CreateGetRelationshipMethod(c_TestClass, "take one from", "Test Class.has multiple Test Class 2", AccessModifier.Public, true, r_Test_Class__has_multiple__Test_Class_2, true);
|
|
ReturnInstanceSetMethodBinding rsmb = builder.CreateReturnInstanceSetMethodBinding(method);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
context.SetWorkData(c_TestClass, i_TestClass); // set the `this` parm
|
|
|
|
InstanceHandle valueIH = Oms.Execute(context, rsmb);
|
|
|
|
object? value = context.GetWorkData(valueIH);
|
|
Assert.That(value, Is.EqualTo(TEST_VALUES[0]));
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void NonsingularGetRelationshipTest()
|
|
{
|
|
InstanceHandle c_TestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle c_TestClass2 = Oms.GetInstance(TEST_CLASS2_GUID);
|
|
|
|
InstanceHandle i_TestClass = Oms.CreateInstanceOf(c_TestClass);
|
|
|
|
InstanceHandle i_TestClass2_1 = Oms.CreateInstanceOf(c_TestClass2);
|
|
InstanceHandle i_TestClass2_2 = Oms.CreateInstanceOf(c_TestClass2);
|
|
InstanceHandle i_TestClass2_3 = Oms.CreateInstanceOf(c_TestClass2);
|
|
|
|
InstanceHandle[] TEST_VALUES = new InstanceHandle[] { i_TestClass2_2, i_TestClass2_3, i_TestClass2_1 };
|
|
Oms.AssignRelationship(i_TestClass, r_Test_Class__has_multiple__Test_Class_2, TEST_VALUES);
|
|
|
|
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
|
|
|
|
GetRelationshipMethod method = builder.CreateGetRelationshipMethod(c_TestClass, "get", "Test Class.has multiple Test Class 2", AccessModifier.Public, true, r_Test_Class__has_multiple__Test_Class_2, false);
|
|
ReturnInstanceSetMethodBinding rsmb = builder.CreateReturnInstanceSetMethodBinding(method);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
context.SetWorkData(c_TestClass, i_TestClass); // set the `this` parm
|
|
|
|
InstanceHandle valueIH = Oms.Execute(context, rsmb);
|
|
|
|
object? value_o = context.GetWorkData(valueIH);
|
|
InstanceHandle[] values = (InstanceHandle[])value_o;
|
|
for (int i = 0; i < TEST_VALUES.Length; i++)
|
|
{
|
|
Assert.That(values[i], Is.EqualTo(TEST_VALUES[i]));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void GetParentClassMatchesActualParentClass()
|
|
{
|
|
InstanceHandle c_Instance = Oms.GetInstance(KnownInstanceGuids.Classes.Instance);
|
|
InstanceHandle c_TestClass = Oms.GetInstance(TEST_CLASS_GUID);
|
|
InstanceHandle c_TestClassInstance = Oms.CreateInstanceOf(c_TestClass);
|
|
|
|
OmsMethodBuilder methodBuilder = new OmsMethodBuilder(Oms);
|
|
|
|
GetRelationshipMethod Instance__get__Parent_Class = methodBuilder.CreateGetRelationshipMethod(c_Instance, "get", "Parent Class", AccessModifier.Public, false, Oms.GetInstance(KnownRelationshipGuids.Instance__for__Class));
|
|
Oms.SetAttributeValue(Instance__get__Parent_Class.Handle, Oms.GetInstance(KnownAttributeGuids.Boolean.Singular), true);
|
|
|
|
ReturnInstanceSetMethodBinding Instance__get__Parent_Class_rsmb = Instance__get__Parent_Class.CreateMethodBinding(Oms);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
context.SetWorkData(c_Instance, c_TestClassInstance);
|
|
|
|
InstanceHandle workSet = Oms.Execute(context, Instance__get__Parent_Class_rsmb);
|
|
|
|
object? value = context.GetWorkData(workSet);
|
|
Assert.That(value, Is.EqualTo(c_TestClass));
|
|
}
|
|
|
|
[Test]
|
|
public void GetMethodType()
|
|
{
|
|
ConditionalSelectAttributeMethod sac = Oms.GetInstance<ConditionalSelectAttributeMethod>(KnownInstanceGuids.Methods.ConditionalSelectAttribute.Method__get__Abbreviation);
|
|
ReturnAttributeMethodBinding ramb = sac.CreateMethodBinding(Oms);
|
|
|
|
GetRelationshipMethod m_GetParentClass = Oms.GetInstance<GetRelationshipMethod>(KnownInstanceGuids.Methods.GetRelationship.Instance__get__Parent_Class);
|
|
ReturnInstanceSetMethodBinding rsmb = m_GetParentClass.CreateMethodBinding(Oms);
|
|
|
|
OmsContext context = Oms.CreateContext();
|
|
context.SetWorkData(Oms.GetInstance(KnownInstanceGuids.Classes.Instance), sac);
|
|
|
|
InstanceHandle ihResult = Oms.Execute(context, rsmb, sac);
|
|
|
|
object? oih = context.GetWorkData(ihResult);
|
|
if (Oms.TryGetSingularInstance(oih, out InstanceHandle ih))
|
|
{
|
|
InstanceHandle ihRealParent = Oms.GetParentClass(sac.Handle);
|
|
Assert.That(ih, Is.EqualTo(ihRealParent));
|
|
}
|
|
else
|
|
{
|
|
Assert.Fail("Method did not return at least one InstanceHandle");
|
|
}
|
|
}
|
|
} |