chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using OpenTap.Engine.UnitTests;
using OpenTap.Plugins.BasicSteps;
 
namespace OpenTap.UnitTests
{
    public class BasicStepsTest
    {
        [TestCase(true, Verdict.Aborted, null)]
        [TestCase(false, Verdict.Error, null)]
        [TestCase(false, Verdict.Pass, Verdict.Pass)]
        public void TimeGuardStepTest(bool stopOnError, Verdict expectedVerdict, Verdict? verdictOnAbort)
        {
            var plan = new TestPlan();
            var guard = new TimeGuardStep {StopOnTimeout = stopOnError, Timeout = 0.05};
            if (verdictOnAbort != null)
                guard.TimeoutVerdict = verdictOnAbort.Value;
            
            // if this delay step runs to completion, the verdict of the test plan will be NotSet, failing the final assertion.
            var delay = new DelayStep {DelaySecs = 120};
            plan.ChildTestSteps.Add(guard);
            guard.ChildTestSteps.Add(delay);
            var run = plan.Execute();
            
            Assert.AreEqual(expectedVerdict, run.Verdict);
        }
 
 
        class PassThirdTime : TestStep
        {
            public int Iterations = 0;
            public override void PrePlanRun()
            {
                Iterations = 0;
                base.PrePlanRun();
            }
 
            public override void Run()
            {
                Iterations += 1;
                if (Iterations < 3)
                {
                    UpgradeVerdict(Verdict.Fail);
                }
                UpgradeVerdict(Verdict.Pass);
            }
        }
        
        [Test]
        [Pairwise]
        public void RepeatUntilPass([Values(true, false)] bool retry)
        {
            var step = new PassThirdTime();
            BreakConditionProperty.SetBreakCondition(step, BreakCondition.BreakOnFail);
            
            var rpt = new RepeatStep()
            {
                Action =  RepeatStep.RepeatStepAction.Until,
                TargetStep = step,
                TargetVerdict = Verdict.Pass,
                Retry = retry
            };
            rpt.ChildTestSteps.Add(step);
 
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(rpt);
 
            var run = plan.Execute();
 
            if (retry)
            {
                Assert.AreEqual(Verdict.Pass, run.Verdict);
                Assert.AreEqual(3, step.Iterations);
            }
            else
            {
                // break condition reached -> Error verdict.
                Assert.AreEqual(Verdict.Fail, run.Verdict);
                Assert.AreEqual(1, step.Iterations);
            }
        }
        
        [Test]
        public void RepeatUntilPass2()
        {
            var step = new PassThirdTime();
            var rpt = new RepeatStep
            {
                Action =  RepeatStep.RepeatStepAction.Until,
                TargetStep = step,
                TargetVerdict = Verdict.Pass,
                ClearVerdict = true,
                MaxCount = new Enabled<uint>{IsEnabled = true, Value = 5}
            };
            rpt.ChildTestSteps.Add(step);
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(rpt);
            
            var run = plan.Execute();
 
            Assert.AreEqual(Verdict.Pass, run.Verdict);
            Assert.AreEqual(3, step.Iterations);
        }
        
        
        // These two cases are technically equivalent.
        [Test]
        [TestCase(Verdict.Fail, RepeatStep.RepeatStepAction.While)]
        [TestCase(Verdict.Pass, RepeatStep.RepeatStepAction.Until)]
        public void RepeatWhileError(Verdict targetVerdict, RepeatStep.RepeatStepAction action)
        {
            var step = new PassThirdTime();
            BreakConditionProperty.SetBreakCondition(step, BreakCondition.BreakOnFail);
            
            var rpt = new RepeatStep()
            {
                Action =  action,
                TargetVerdict = targetVerdict,
                Retry = true
            };
            rpt.TargetStep = rpt; // target self. The Repeat Loop will inherit the verdict.
            rpt.ChildTestSteps.Add(step);
 
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(rpt);
 
            var run = plan.Execute();
 
            Assert.AreEqual(Verdict.Pass, run.Verdict); 
            Assert.AreEqual(3, step.Iterations);
        }
 
        [Test]
        public void ScpiRegexStepValidation()
        {
            ScpiInstrument instr = new ScpiDummyInstrument();
            instr.Rules.Clear(); // skip validation on the instrument itself.
            SCPIRegexStep scpiRegex = new SCPIRegexStep
            {
                Instrument = instr,
                Query = "SYST:CHAN:MOD? (@1,2)", // query with arguments.
                Action = SCPIAction.Query
            };
            Assert.IsTrue(string.IsNullOrEmpty(scpiRegex.Error));
            scpiRegex.Query = "SYST:CHAN:MOD"; // Not a valid query!
            Assert.IsFalse(string.IsNullOrEmpty(scpiRegex.Error));
            scpiRegex.Action = SCPIAction.Command; // it is a valid command though.
            Assert.IsTrue(string.IsNullOrEmpty(scpiRegex.Error));
        }
 
        class SimpleResultTest2 : TestStep 
        {
            public override void Run()
            {
                Results.Publish("Test", new List<string> { "X", "Y" }, 1.0, 2.0);
            }
        }
        
        [Test]
        public void RepeatCheckResultsHasIterations()
        {
            var pushResult1 = new SimpleResultTest2();
            var pushResult2 = new SimpleResultTest2();
            
            var repeatStep = new RepeatStep
            {
                Action =  RepeatStep.RepeatStepAction.Fixed_Count,
                Count = 100
            };
            
            var collectEverythingListener = new RecordAllResultListener();
            
            repeatStep.ChildTestSteps.Add(pushResult1);
            repeatStep.ChildTestSteps.Add(pushResult2);
 
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(repeatStep);
 
            plan.Execute(new IResultListener[]{collectEverythingListener});
            
            // verify that there are 200 distinct result tables (from 200 different test plan runs)
            // 200 = repeatStep.Count * 2 (pushResult1 and pushResult2).
            Assert.AreEqual(200, collectEverythingListener.Results.Count);
            
            // verify that each result table came from a different step run
            Assert.AreEqual(200, collectEverythingListener.ResultTableGuids.Distinct().Count());
        }
        
        [Test]
        public void TestSweepLoopAcrossRunsReferencedResources()
        {
            var plan = new TestPlan();
            var sweep = new SweepLoop();
            var delay = new DelayStep();
            plan.ChildTestSteps.Add(sweep);
            sweep.ChildTestSteps.Add(delay);
 
            var b = AnnotationCollection.Annotate(sweep);
            var members = b.GetMember("SweepMembers");
            var avail = members.Get<IAvailableValuesAnnotationProxy>();
            var multi = members.Get<IMultiSelectAnnotationProxy>();
            multi.SelectedValues = avail.AvailableValues;
            b.Write();
            sweep.CrossPlan = SweepLoop.SweepBehaviour.Across_Runs;
            Assert.IsTrue(sweep.SweepParameters.Any());
            // if SweepParameters.Count > 0 && across-runs mode was enabled. This could cause an exception.
            Assert.AreEqual(0, sweep.ReferencedResources.Count());
        }
 
        [Test]
        public void TestScpiQueryNullValidation()
        {
            var scpiStep = new SCPIRegexStep();
            scpiStep.Query = null;
            Assert.DoesNotThrow(() =>
            {
                foreach (var rule in scpiStep.Rules)
                {
                    rule.IsValid().ToString();
                    rule.ErrorMessage.ToString();
                }
            });
        }
    }
}