Input<T> rule to check for valid selection

Is there a way to create a rule for an “Input” property, that verifies a selection has been made (property != None)?

2 Likes

@JMAnalog Welcome to the community! If you are specifically looking to check for != None that is already handled. If you want do something else, you can use the Rules system. For example:

       public Input<double> InputValue { get; set; }      

        public HandleInput()
        {
            InputValue = new Input<double>();
            Rules.Add(CheckForEmpty, "Value Can't be None", "InputValue");
        }

        private bool CheckForEmpty()
        {
            if (InputValue.Value <= 0.0)
            {
                return false;
            }
            else return true;               
        }

You can find general examples of creating Rules here:

Thanks for the response. I can see that a reasonable exception is thrown when attempting to reference the unassigned input (“Step input requires reference to a TestStep output.”), however I would prefer to have a rule violation flag displayed if the user forgets to make a selection.

To add more detail, I would like the following code to display a rule violation flag for the unspecified property, as it does when “Must Be True” = false.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTap;   // Use OpenTAP infrastructure/core components (log,TestStep definition, etc)
using System.Drawing;

namespace Experiment
{
    [Display("Output Values", Groups: new[] { "Experiment" }, Order: 1)]
    [AllowChildrenOfType(typeof(TestStep_Experiment2))]
    public class TestStep_Experiment1 : TestStep
    {
        [Display("int[] Output", Group: "Output", Order: 1)]
        [Output]
        public int[] IntArrayOutput { get; set; }

        public TestStep_Experiment1()
        {
        }

        public override void Run()
        {
        }
    }

    [Display("Input Values", Groups: new[] { "Experiment" }, Order: 2)]
    public class TestStep_Experiment2 : TestStep
    {
        [Display("Input", Group: "Input", Order: 1)]
        public Input intArrayInput { get; set; } = new Input();

        [Display("Must Be True", Group: "Input", Order: 2)]
        public bool mustBeTrue { get; set; }

        public bool CheckMustBeTrue()
        {
            return mustBeTrue;
        }

        public TestStep_Experiment2()
        {
            Rules.Add(CheckMustBeTrue, "\"Must Be True\" can't be false!", "mustBeTrue");
            intArrayInput = new Input();
            mustBeTrue = false;
        }

        public override void Run()
        {
        }
    }
}

It seems you may have identified a bug. If you create any Rule on the property, then you will get the visual warning, so you could use the above to force this. I will submit an issue on our side.

Note a few things:

  1. When you define an Input, you need to include the type, so in our case Input<Int>
  2. As of 9.10, you only need to use the Input<> type if the value can ONLY be set from an input and you want to restrict it:
    OpenTAP 9.10 – Release Highlights – OpenTAP News