From aafd99ab94e9ae2c497758e4754a7be21af9cb17 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sun, 28 Sep 2025 14:30:46 -0400 Subject: [PATCH] implement GA - Get Attribute Method for static method calls --- .../GetAttributeMethodImplementation.cs | 24 +++++++++++--- .../MethodTests/GetAttributeMethodTests.cs | 33 ++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/GetAttributeMethodImplementation.cs b/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/GetAttributeMethodImplementation.cs index 4c0499c..d910766 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/GetAttributeMethodImplementation.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/GetAttributeMethodImplementation.cs @@ -24,21 +24,35 @@ public class GetAttributeMethodImplementation : MethodImplementation { InstanceHandle irForClass = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Method__for__Class)); InstanceHandle returnsAttribute = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Get_Attribute_Method__returns__Attribute)); - + bool isStatic = oms.GetAttributeValue(method, oms.GetInstance(KnownAttributeGuids.Boolean.Static)); + if (returnsAttribute == InstanceHandle.Empty) { throw new InvalidOperationException("no Work Set specified for method"); } - IInstanceReference? forInstance = (IInstanceReference?) context.GetWorkData(irForClass); - if (forInstance == null) + IInstanceReference? forInstance = null; + if (isStatic) { - throw new NullReferenceException(String.Format("non-static method call without instance reference {0}", oms.GetGlobalIdentifier(irForClass))); + forInstance = irForClass; + if (forInstance == null) + { + // we should never get here + // throw new NullReferenceException(String.Format("static method call without instance reference {0}", oms.GetGlobalIdentifier(irForClass))); + } + } + else + { + forInstance = (IInstanceReference?)context.GetWorkData(irForClass); + if (forInstance == null) + { + throw new NullReferenceException(String.Format("non-static method call without instance reference {0}", oms.GetGlobalIdentifier(irForClass))); + } } if (oms.IsInstanceOf(forInstance, oms.GetInstance(KnownInstanceGuids.Classes.WorkSet))) { - forInstance = (InstanceHandle?) context.GetWorkData(forInstance); + forInstance = (InstanceHandle?)context.GetWorkData(forInstance); } if (forInstance is InstanceHandle forInstanceHandle) diff --git a/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/GetAttributeMethodTests.cs b/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/GetAttributeMethodTests.cs index c16db26..90677c0 100644 --- a/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/GetAttributeMethodTests.cs +++ b/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/GetAttributeMethodTests.cs @@ -24,7 +24,7 @@ namespace Mocha.Core.Tests.MethodTests { [Test] - public void GetAttributeMethodTest() + public void GetInstanceAttributeMethodTest() { InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID); InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass); @@ -54,5 +54,36 @@ namespace Mocha.Core.Tests.MethodTests 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(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()); + Assert.That(testAttributeValue2, Is.EqualTo(testAttributeValue)); + } + } } \ No newline at end of file