add test for passing a parm via a system routine

This commit is contained in:
Michael Becker 2025-09-19 19:17:12 -04:00
parent 80127b4399
commit 87ad532073
3 changed files with 63 additions and 3 deletions

View File

@ -1185,14 +1185,44 @@ public abstract class Oms
{ {
return Execute(context, methodBinding.GetHandle()); return Execute(context, methodBinding.GetHandle());
} }
public object? ExecuteReturningAttributeValue(OmsContext context, ReturnAttributeMethodBinding methodBinding) public object? ExecuteReturningAttributeValue(OmsContext context, ReturnAttributeMethodBinding methodBinding, KeyValuePair<InstanceHandle, object>[]? parms)
{ {
// override any specified parms we have
Dictionary<InstanceHandle, object>? oldParms = null;
if (parms != null)
{
oldParms = new Dictionary<InstanceHandle, object>();
foreach (KeyValuePair<InstanceHandle, object> kvp in parms)
{
if (context.HasWorkData(kvp.Key))
{
oldParms[kvp.Key] = context.GetWorkData(kvp.Key);
}
else
{
oldParms[kvp.Key] = null;
}
context.SetWorkData(kvp.Key, kvp.Value);
}
}
// call the internal MethodImplementation
InstanceHandle workData = Execute(context, methodBinding.GetHandle()); InstanceHandle workData = Execute(context, methodBinding.GetHandle());
// restore the original parms
if (oldParms != null)
{
foreach (KeyValuePair<InstanceHandle, object> kvp in oldParms)
{
context.SetWorkData(kvp.Key, kvp.Value);
}
}
return context.GetWorkData(workData); return context.GetWorkData(workData);
} }
public T ExecuteReturningAttributeValue<T>(OmsContext context, ReturnAttributeMethodBinding methodBinding, T defaultValue = default(T)) public T ExecuteReturningAttributeValue<T>(OmsContext context, ReturnAttributeMethodBinding methodBinding, T defaultValue = default(T), KeyValuePair<InstanceHandle, object>[]? parms = null)
{ {
object? value = ExecuteReturningAttributeValue(context, methodBinding); object? value = ExecuteReturningAttributeValue(context, methodBinding, parms);
if (value is T) if (value is T)
{ {
return (T)value; return (T)value;

View File

@ -61,6 +61,10 @@ public class OmsContext
private Dictionary<InstanceHandle, object?> _WorkData = new Dictionary<InstanceHandle, object?>(); private Dictionary<InstanceHandle, object?> _WorkData = new Dictionary<InstanceHandle, object?>();
private Dictionary<InstanceHandle, object?> _GlobalData = new Dictionary<InstanceHandle, object?>(); private Dictionary<InstanceHandle, object?> _GlobalData = new Dictionary<InstanceHandle, object?>();
public bool HasWorkData(IInstanceReference parm)
{
return _WorkData.ContainsKey(parm.GetHandle());
}
public object? GetWorkData(IInstanceReference parm) public object? GetWorkData(IInstanceReference parm)
{ {
if (_WorkData.ContainsKey(parm.GetHandle())) if (_WorkData.ContainsKey(parm.GetHandle()))

View File

@ -46,6 +46,32 @@ public class SystemRoutineTests : MethodTestsBase
Assert.That(value, Is.EqualTo(TEST_SYSTEM_ATTRIBUTE_ROUTINE_VALUE)); Assert.That(value, Is.EqualTo(TEST_SYSTEM_ATTRIBUTE_ROUTINE_VALUE));
} }
[Test]
public void PassParmViaSystemRoutineTest()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
OmsMethodBuilder methodBuilder = new OmsMethodBuilder(Oms);
OmsSystemRoutineBuilder systemRoutineBuilder = new OmsSystemRoutineBuilder(Oms);
SystemAttributeRoutine<string> testSystemAttributeRoutine = systemRoutineBuilder.CreateSystemAttributeRoutine(TEST_SYSTEM_ATTRIBUTE_ROUTINE_GUID, delegate (Oms oms, OmsContext context)
{
string value = context.GetWorkData<string>(oms.GetInstance(KnownAttributeGuids.Text.Token));
return TEST_SYSTEM_ATTRIBUTE_ROUTINE_VALUE + value;
});
GetAttributeBySystemRoutineMethod gasMethod = methodBuilder.CreateGetAttributeBySystemRoutineMethod(irTestClass, "get", "Test System Routine Text Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Text.Value), testSystemAttributeRoutine);
ReturnAttributeMethodBinding gasMethodRamb = methodBuilder.CreateReturnAttributeMethodBinding(gasMethod);
OmsContext context = Oms.CreateContext();
string value = Oms.ExecuteReturningAttributeValue<string>(context, gasMethodRamb, null, new KeyValuePair<InstanceHandle, object>[]
{
// !!! WARNING: do not use 'Value' here as it is the return type attribute of the GAS method and WILL be overwritten after execution !!!
new KeyValuePair<InstanceHandle, object>(Oms.GetInstance(KnownAttributeGuids.Text.Token), "pqdms")
});
Assert.That(value, Is.EqualTo(TEST_SYSTEM_ATTRIBUTE_ROUTINE_VALUE + "pqdms"));
}
[Test] [Test]
public void GetRandomNumberTest() public void GetRandomNumberTest()
{ {