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
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace OpenTap
{
    /// <summary>
    /// Mixins can be used with EmbedPropertiesAttribute to give extra functionality to a test step.
    /// </summary>
    public interface IMixin : ITapPlugin
    {
        
    }
 
    abstract class MixinEvent<T2> where T2: IMixin
    {
        static readonly TraceSource log = Log.CreateSource("Mixin");
        protected static T1 Invoke<T1>(object target, Action<T2, T1> f, T1 arg) => Invoke(target, f, null, arg,  out _);
        protected static T1 Invoke<T1>(object target, Action<T2, T1> f, Func<T2, double> ordering, T1 arg, out bool anyInvoked)
        {
            anyInvoked = false;
            var emb = TypeData.GetTypeData(target).GetBaseType<EmbeddedTypeData>();
            if (emb == null) return arg;
            var embeddingMembers = emb.GetEmbeddingMembers();
 
            List<T2> objects = null;
 
            foreach (var mem in embeddingMembers)
            {
                if (!mem.TypeDescriptor.DescendsTo(typeof(T2))) continue;
                if (mem.Readable == false) continue;
 
                if (mem.GetValue(target) is T2 mixin)
                {
                    (objects ??= []).Add(mixin);
                }
            }
 
            if (objects != null)
            {
 
                IEnumerable<T2> orderedObjects = ordering != null ? objects.OrderBy(ordering) : objects;
 
                foreach (var mixin in orderedObjects)
                {
                    try
                    {
                        f(mixin, arg);
                    }
                    catch (Exception e) when (e is not OperationCanceledException)
                    {
                        log.Error("Caught error in mixin: {0}", e.Message);
                        log.Debug(e);
                    }
                }
                anyInvoked = true;
            }
 
 
            return arg;
        }
        
    }
    
    class TestStepPreRunEvent : MixinEvent<ITestStepPreRunMixin>
    {
        public static TestStepPreRunEventArgs Invoke(ITestStep step)
        {
            var eventArg = new TestStepPreRunEventArgs(step);
            Invoke(step, static (v, arg) => v.OnPreRun(arg), static v => (v as ITestStepPreRunMixinOrder)?.GetPreRunOrder() ?? 0.0, eventArg, out bool anyInvoked);
            eventArg.AnyPrerunsInvoked = anyInvoked;
            return eventArg;
        }
    }
 
    /// <summary> Event args for ITestStepPreRun mixin. </summary>
    public sealed class TestStepPreRunEventArgs
    {
        /// <summary> The step for which the event happens. </summary>
        public ITestStep TestStep { get; }
        /// <summary> Can be set to true to skip the step</summary>
        public bool SkipStep { get; set; }
        
        /// <summary> Indicates whether any prerun mixins were invoked. </summary>
        internal bool AnyPrerunsInvoked { get; set; }
 
        internal TestStepPreRunEventArgs(ITestStep step) => TestStep = step;
    }
    
    class TestPlanPreRunEvent : MixinEvent<ITestPlanPreRunMixin>
    {
        public static TestPlanPreRunEventArgs Invoke(TestPlan plan) => 
            Invoke(plan, (v, arg) => v.OnPreRun(arg), new TestPlanPreRunEventArgs(plan));
    }
    
    /// <summary> Event args for ITestStepPreRun mixin. </summary>
    public sealed class TestPlanPreRunEventArgs
    {
        /// <summary> The step for which the event happens. </summary>
        public TestPlan TestPlan { get; }
 
        internal TestPlanPreRunEventArgs(TestPlan step) => TestPlan = step;
    }
 
    class TestStepPostRunEvent : MixinEvent<ITestStepPostRunMixin>
    {
        public static void Invoke(ITestStep step) => 
            Invoke(step, static (mixin, args) => mixin.OnPostRun(args), static v => (v as ITestStepPostRunMixinOrder)?.GetPostRunOrder() ?? 0, new TestStepPostRunEventArgs(step), out _);
    }
    
    /// <summary> Event args for ITestStepPostRun mixin. </summary>
    public sealed class TestStepPostRunEventArgs
    {
        /// <summary> The step for which the event happens. </summary>
        public ITestStep TestStep { get; }
        
        internal TestStepPostRunEventArgs(ITestStep step)  => TestStep = step;
    }
 
    class ResourcePreOpenEvent: MixinEvent<IResourcePreOpenMixin>
    {
        public static void Invoke(IResource resource) => 
            Invoke(resource, static (mixin, args) => mixin.OnPreOpen(args), new ResourcePreOpenEventArgs(resource));
    }
 
    /// <summary> Event args for IResourcePreOpenMixin mixin. </summary>
    public class ResourcePreOpenEventArgs
    {
        /// <summary> The resource for which the event happens. </summary>
        public IResource Resource { get; }
        
        internal ResourcePreOpenEventArgs(IResource resource) => Resource = resource;
    }
    
    /// <summary> This mixin is activated just after a step has been executed. It allows modifying the test step run. </summary>
    public interface ITestStepPostRunMixin : IMixin
    {
        /// <summary> Invoked after test step run.</summary>
        void OnPostRun(TestStepPostRunEventArgs eventArgs);
    }
 
    /// <summary> Defines an ordering for test step post run mixins. </summary>
    public interface ITestStepPostRunMixinOrder : ITestStepPostRunMixin
    {
 
        /// <summary>
        /// Gets the order. Default value is 0. The ordering is ascending.
        /// </summary>
        double GetPostRunOrder();
    }
 
 
    /// <summary> This mixin is activated just before a step is executed. It allows modifying the test step run. </summary>
    public interface ITestStepPreRunMixin : IMixin
    {
        /// <summary> Invoked before test step run.</summary>
        void OnPreRun(TestStepPreRunEventArgs eventArgs);
    }
 
    /// <summary> Defines an ordering for test step pre run mixins. </summary>
    public interface ITestStepPreRunMixinOrder : ITestStepPreRunMixin
    {
        /// <summary> Gets the order. Default value is 0. The ordering is ascending. </summary>
        double GetPreRunOrder();
    }
 
    /// <summary> This mixin is activated just before a resource opens. </summary>
    public interface IResourcePreOpenMixin : IMixin
    {
        /// <summary> Invoked just before IResource.Open is called.</summary>
        void OnPreOpen(ResourcePreOpenEventArgs eventArgs);
    }
 
    /// <summary> This mixin is activated just before a step is executed. It allows modifying the test step run. </summary>
    public interface ITestPlanPreRunMixin : IMixin
    {
        /// <summary> Invoked before test step run.</summary>
        void OnPreRun(TestPlanPreRunEventArgs eventArgs);
    }
}