89 lines
3.6 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 GetAttributeMethodTests : MethodTestsBase
{
[Test]
public void GetInstanceAttributeMethodTest()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
InstanceHandle irTestAttribute = Oms.GetInstance(TEST_ATTR_GUID);
Assert.That(irTestAttribute, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.AddAttribute(irTestClass, irTestAttribute);
Oms.SetAttributeValue(irTestClassInstance, irTestAttribute, TEST_ATTR_VALUE);
string testAttributeValue = Oms.GetAttributeValue<string>(irTestClassInstance, irTestAttribute);
Assert.That(testAttributeValue, Is.EqualTo(TEST_ATTR_VALUE));
// here, the test attribute has a particular value.
// let's make sure that calling a GA- Get Attribute method on TestClass returns the same
OmsMethodBuilder methodBuilder = new OmsMethodBuilder(Oms);
GetAttributeMethod gaMethod = methodBuilder.CreateGetAttributeMethod(irTestClass, "get", "Test Attribute", irTestAttribute);
// Method is: `Test Class@get Test Attribute(GA)`
ReturnAttributeMethodBinding gaMethodRamb = methodBuilder.CreateReturnAttributeMethodBinding(gaMethod);
OmsContext context = Oms.CreateContext();
object? testAttributeValue2 = Oms.ExecuteInstanceMethodReturningAttribute(context, irTestClassInstance, gaMethodRamb);
Assert.That(testAttributeValue2, Is.TypeOf<String>());
Assert.That(testAttributeValue2, Is.EqualTo(testAttributeValue));
}
[Test]
public void GetStaticAttributeMethodTest()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestAttribute = Oms.GetInstance(TEST_ATTR_GUID);
Assert.That(irTestAttribute, Is.Not.EqualTo(InstanceHandle.Empty));
Oms.AddAttribute(irTestClass, irTestAttribute);
Oms.SetAttributeValue(irTestClass, irTestAttribute, TEST_ATTR_VALUE);
string testAttributeValue = Oms.GetAttributeValue<string>(irTestClass, irTestAttribute);
Assert.That(testAttributeValue, Is.EqualTo(TEST_ATTR_VALUE));
// here, the test attribute has a particular value.
// let's make sure that calling a GA- Get Attribute method on TestClass returns the same
OmsMethodBuilder methodBuilder = new OmsMethodBuilder(Oms);
GetAttributeMethod gaMethod = methodBuilder.CreateGetAttributeMethod(irTestClass, "get", "Test Attribute", AccessModifier.Public, true, irTestAttribute);
// Method is: `Test Class@get Test Attribute(GA)*S`
ReturnAttributeMethodBinding gaMethodRamb = methodBuilder.CreateReturnAttributeMethodBinding(gaMethod);
OmsContext context = Oms.CreateContext();
object? testAttributeValue2 = Oms.ExecuteReturningAttributeValue(context, gaMethodRamb, null);
Assert.That(testAttributeValue2, Is.TypeOf<String>());
Assert.That(testAttributeValue2, Is.EqualTo(testAttributeValue));
}
}
}