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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Linq;
using System.Threading;
using NUnit.Framework;
using OpenTap.Engine.UnitTests.TestTestSteps;
using OpenTap.Plugins.BasicSteps;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class BreakConditionsTest
    {
        public class TestStepFailsTimes : TestStep
        {
            public int fails = 0;
            public int timesRun = 0;
 
            public override void PrePlanRun()
            {
                fails = 5;
                timesRun = 0;
                base.PrePlanRun();
            }
 
            public override void Run()
            {
                timesRun += 1;
                if (fails > 0)
                {
                    fails -= 1;
                    throw new Exception("Intended failure!");
                }
 
                UpgradeVerdict(Verdict.Pass);
            }
        }
 
        // The new abort condition system gives many possibilities.
        // 1. everything inherits (same as without)
        // 2. step not allowed to fail
        // 3. and or step not allowed to error
        // 4. step allowed to fail
        // 5. and or step allowed to error
        // Retry:
        // 2. Single step retry until not fail.
        //    - Step passes
        //    - Step creates an error
        //    - Step never passes -> break?
        // 3. Parent step retry until child steps pass
        // what about inconclusive??
 
 
        [TestCase(Verdict.Error, BreakCondition.BreakOnError)]
        [TestCase(Verdict.Fail, BreakCondition.BreakOnFail)]
        [TestCase(Verdict.Inconclusive, BreakCondition.BreakOnInconclusive)]
        [TestCase(Verdict.Pass, BreakCondition.BreakOnPass)]
        [TestCase(Verdict.Inconclusive,
            BreakCondition.BreakOnInconclusive | BreakCondition.BreakOnError)]
 
        public void TestStepBreakOnError(Verdict verdictOutput, object _condition)
        {
            // _condition arg cannot be a BreakCondition as BreakCondition is not public.
            BreakCondition condition = (BreakCondition) _condition;
            var l = new PlanRunCollectorListener();
            TestPlan plan = new TestPlan();
            var verdict = new VerdictStep
            {
                VerdictOutput = verdictOutput
            };
            BreakConditionProperty.SetBreakCondition(plan, condition);
            var verdict2 = new VerdictStep
            {
                VerdictOutput = Verdict.Pass
            };
            
            plan.Steps.Add(verdict);
            plan.Steps.Add(verdict2);
            var run = plan.Execute(new[] {l});
            Assert.AreEqual(verdictOutput, run.Verdict);
            Assert.AreEqual(1, l.StepRuns.Count);
            Assert.AreEqual(BreakCondition.Inherit, BreakConditionProperty.GetBreakCondition(verdict2));
            var log = l.LogString;
            Assert.IsTrue(log.Contains("Break issued from"));
        }
 
        [TestCase(Verdict.Pass, EngineSettings.AbortTestPlanType.Step_Error, 2)]
        [TestCase(Verdict.Fail, EngineSettings.AbortTestPlanType.Step_Error, 2)]
        [TestCase(Verdict.Error, EngineSettings.AbortTestPlanType.Step_Error, 1)]
        [TestCase(Verdict.Fail, EngineSettings.AbortTestPlanType.Step_Fail, 1)]
        [TestCase(Verdict.Pass, EngineSettings.AbortTestPlanType.Step_Pass, 1)]
        [TestCase(Verdict.Inconclusive, EngineSettings.AbortTestPlanType.Step_Inconclusive, 1)]
        [TestCase(Verdict.Fail,
            EngineSettings.AbortTestPlanType.Step_Error | EngineSettings.AbortTestPlanType.Step_Fail, 1)]
        [TestCase(Verdict.Error,
            EngineSettings.AbortTestPlanType.Step_Error | EngineSettings.AbortTestPlanType.Step_Fail, 1)]
        [TestCase(Verdict.Pass,
            EngineSettings.AbortTestPlanType.Step_Error | EngineSettings.AbortTestPlanType.Step_Fail, 2)]
        public void EngineInheritedConditions(Verdict verdictOutput, EngineSettings.AbortTestPlanType abortTestPlanType,
            int runCount)
        {
            Verdict finalVerdict = verdictOutput;
            var prev = EngineSettings.Current.AbortTestPlan;
            try
            {
                EngineSettings.Current.AbortTestPlan = abortTestPlanType;
                var l = new PlanRunCollectorListener();
                TestPlan plan = new TestPlan();
                var verdict = new VerdictStep
                {
                    VerdictOutput = verdictOutput
                };
                BreakConditionProperty.SetBreakCondition(verdict, BreakCondition.Inherit);
                var verdict2 = new VerdictStep
                {
                    VerdictOutput = Verdict.Pass
                };
                plan.Steps.Add(verdict);
                plan.Steps.Add(verdict2);
                var run = plan.Execute(new[] {l});
                Assert.AreEqual(finalVerdict, run.Verdict);
                Assert.AreEqual(runCount, l.StepRuns.Count);
                Assert.AreEqual(BreakCondition.Inherit, BreakConditionProperty.GetBreakCondition(verdict2));
            }
            finally
            {
                EngineSettings.Current.AbortTestPlan = prev;
            }
        }
 
        [Test]
        public void EngineInheritedConditions2()
        {
            Verdict verdictOutput = Verdict.Fail;
            EngineSettings.AbortTestPlanType abortTestPlanType = EngineSettings.AbortTestPlanType.Step_Fail;
            int runCount = 1;
            Verdict finalVerdict = verdictOutput;
            var prev = EngineSettings.Current.AbortTestPlan;
            try
            {
                EngineSettings.Current.AbortTestPlan = abortTestPlanType;
                var l = new PlanRunCollectorListener();
                TestPlan plan = new TestPlan();
                var verdict = new VerdictStep
                {
                    VerdictOutput = verdictOutput,
                };
                BreakConditionProperty.SetBreakCondition(verdict, BreakCondition.Inherit | BreakCondition.BreakOnError);
                var verdict2 = new VerdictStep
                {
                    VerdictOutput = Verdict.Pass
                };
                plan.Steps.Add(verdict);
                plan.Steps.Add(verdict2);
                var run = plan.Execute(new[] {l});
                Assert.AreEqual(finalVerdict, run.Verdict);
                Assert.AreEqual(runCount, l.StepRuns.Count);
                Assert.AreEqual(BreakCondition.Inherit, BreakConditionProperty.GetBreakCondition(verdict2));
            }
            finally
            {
                EngineSettings.Current.AbortTestPlan = prev;
            }
        }
 
        /// <summary>  This step overrides the verdict of the child steps. </summary>
        [AllowAnyChild]
        class VerdictOverrideStep : TestStep
        {
            public Verdict OutputVerdict { get; set; } = Verdict.Pass;
            public override void Run()
            {
                foreach (var step in EnabledChildSteps)
                    RunChildStep(step, throwOnBreak: false);
                this.Verdict = Verdict.Pass;
            }
        }
 
        [Test]
        public void StepsCanOverrideVerdicts()
        {
            var plan = new TestPlan();
            var stepCatch = new VerdictOverrideStep { OutputVerdict = Verdict.Pass };
            var verdict = new VerdictStep { VerdictOutput = Verdict.Error };
            plan.ChildTestSteps.Add(stepCatch);
            stepCatch.ChildTestSteps.Add(verdict);
 
            var run = plan.Execute();
            Assert.AreEqual(Verdict.Pass, run.Verdict);
        }
 
        public class TestStepWaitInfinite : TestStep
        {
            public Semaphore Sem = new Semaphore(0,1);
        
            public override void Run()
            {
                Sem.Release();
                TapThread.Sleep(TimeSpan.MaxValue);
            }
        }
 
        public class TestStepAbortPlan : TestStep
        {
            public BreakConditionsTest.TestStepWaitInfinite WaitFor { get; set; }
            public override void Run()
            {
                WaitFor?.Sem.WaitOne();
                PlanRun.MainThread.Abort();
            }
        }
 
        [Test]
        public void TestStepWaitInfiniteAndAbortTest()
        {
            var plan = new TestPlan();
            var parallel = new ParallelStep();
            var abort = new TestStepAbortPlan(){WaitFor =  new TestStepWaitInfinite()};
            parallel.ChildTestSteps.Add(abort);
            parallel.ChildTestSteps.Add(abort.WaitFor);
            plan.ChildTestSteps.Add(parallel);
            var run = plan.Execute();
            Assert.AreEqual(Verdict.Aborted, run.Verdict);
            foreach (var step in parallel.ChildTestSteps)
            {
                Assert.AreEqual(Verdict.Aborted, step.Verdict);    
            }
        }
 
        [Test]
        public void TestPlanBreakConditions()
        {
            var plan = new TestPlan();
            
            var errorStep = new VerdictStep() {VerdictOutput = Verdict.Error};
            var failStep = new VerdictStep() {VerdictOutput = Verdict.Fail};
            var inconclusiveStep = new VerdictStep() {VerdictOutput = Verdict.Inconclusive};
            var passStep = new VerdictStep() {VerdictOutput = Verdict.Pass};
            var nopStep = new SequenceStep();
            
            plan.Steps.Add(passStep);
            plan.Steps.Add(inconclusiveStep);
            plan.Steps.Add(failStep);
            plan.Steps.Add(errorStep);
            plan.Steps.Add(nopStep);
 
            var defaultValue = BreakConditionProperty.GetBreakCondition(plan);
            Assert.AreEqual(BreakCondition.Inherit, defaultValue);
            
            // break on fail, this means that 'passStep' will not get executed 
            BreakConditionProperty.SetBreakCondition(plan, BreakCondition.BreakOnPass);
            var col = new PlanRunCollectorListener();
            Assert.AreEqual(Verdict.Pass, plan.Execute(new []{col}).Verdict);
            Assert.AreEqual(1, col.StepRuns.Count);
            
            BreakConditionProperty.SetBreakCondition(plan, BreakCondition.BreakOnInconclusive);
            col = new PlanRunCollectorListener();
            Assert.AreEqual(Verdict.Inconclusive, plan.Execute(new []{col}).Verdict);
            Assert.AreEqual(2, col.StepRuns.Count);
            
            BreakConditionProperty.SetBreakCondition(plan, BreakCondition.BreakOnFail);
            col = new PlanRunCollectorListener();
            Assert.AreEqual(Verdict.Fail, plan.Execute(new []{col}).Verdict);
            Assert.AreEqual(3, col.StepRuns.Count);
            
            BreakConditionProperty.SetBreakCondition(plan, BreakCondition.BreakOnError);
            col = new PlanRunCollectorListener();
            Assert.AreEqual(Verdict.Error, plan.Execute(new []{col}).Verdict);
            Assert.AreEqual(4, col.StepRuns.Count);
            
            BreakConditionProperty.SetBreakCondition(plan, 0);
            col = new PlanRunCollectorListener();
            Assert.AreEqual(Verdict.Error, plan.Execute(new []{col}).Verdict);
            Assert.AreEqual(5, col.StepRuns.Count);
            
        }
 
        /// <summary>
        /// Testing that TestPlan.Locked causes BreakConditions to be locked.
        /// </summary>
        [Test]
        public void TestBreakConditionsLocked()
        {
            var plan = new TestPlan();
            var a = AnnotationCollection.Annotate(plan);
            var mem = a.GetMember("BreakConditions");
            Assert.IsFalse(mem.Get<IAccessAnnotation>().IsReadOnly);
            plan.Locked = true;
            a.Read();
            Assert.IsTrue(mem.Get<IAccessAnnotation>().IsReadOnly);
            plan.Locked = false;
            a.Read();
            Assert.IsFalse(mem.Get<IAccessAnnotation>().IsReadOnly);
        }
 
        class TestStepThatThrows : TestStep
        {
            public override void Run()
            {
                throw new Exception("!");
            }
        }
        
        [AllowAnyChild]
        class TestStepThatCatches : TestStep
        {
            public bool AvoidCatch { get; set; }
            public TestStepRun[] Runs;
            public override void Run()
            {
                if (AvoidCatch)
                {
                    Runs = RunChildSteps(throwOnBreak: false).ToArray();
                    var run = Runs.Last();
                    if(run.Verdict == Verdict.Error)
                        Assert.AreEqual("!", run.Exception.Message);
                    else 
                        Assert.IsNull(run.Exception);
                    Verdict = Verdict.Pass;
                }
                else
                {
                    try
                    {
                        RunChildSteps(throwOnBreak: true);
                        Assert.Fail("The child steps should have thrown an exception");
                    }
                    catch (TestStepBreakException)
                    {
                        Verdict = Verdict.Pass;
                    }
                    catch (Exception e)
                    {
                        Assert.AreEqual(e.Message, "!");
                        Verdict = Verdict.Pass;
                    }
                }
            }
        }
 
        [Test]
        public void TestStepThrowsException()
        {
            var a = new TestStepThatCatches();
            var b = new TestStepThatThrows();
            // add a 'nop' to verify
            a.ChildTestSteps.Add(new SequenceStep());
            a.ChildTestSteps.Add(b);
            a.ChildTestSteps.Add(new SequenceStep());
            var plan = new TestPlan();
            plan.Steps.Add(a);
            var r = plan.Execute();
            Assert.AreEqual(Verdict.Pass, r.Verdict);
            a.AvoidCatch = true;
            var r2 = plan.Execute();
            Assert.AreEqual(Verdict.Pass, r2.Verdict);
            Assert.AreEqual(2, a.Runs.Length);
        }
        
        [Test]
        public void TestStepThrowsBreakException()
        {
            var a = new TestStepThatCatches();
            var b = new VerdictStep { VerdictOutput = Verdict.Fail };
            BreakConditionProperty.SetBreakCondition(b, BreakCondition.BreakOnFail);
            a.ChildTestSteps.Add(new SequenceStep());
            a.ChildTestSteps.Add(b);
            a.ChildTestSteps.Add(new SequenceStep());
            var plan = new TestPlan();
            plan.Steps.Add(a);
            var r = plan.Execute();
            Assert.AreEqual(Verdict.Pass, r.Verdict);
            a.AvoidCatch = true;
            var r2 = plan.Execute();
            Assert.AreEqual(Verdict.Pass , r2.Verdict);
            Assert.AreEqual(2, a.Runs.Length);
        }
    }
}