alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
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
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using OpenTap.Addin;
using System;
using System.Xml.Linq;
 
namespace OpenTap.Plugins
{
    /// <summary> Serializer implementation for TestStepList. </summary>
    internal class TestStepListSerializer : TapSerializerPlugin
    {
        static XName testStepName = "TestStep";
        static XName SequenceName = "SequenceName";
        static XName varName = "Variable";
        static XName paramName = "Parameters";
        static XName DebugIds = "DebugIds";
        public override string TargetName { get; } = "TestStepList";
 
        /// <summary> The order of this serializer. </summary>
        public override double Order
        {
            get { return 2; }
        }
        /// <summary> Deserialization implementation. </summary>
        public override bool Deserialize(XElement elem, ITypeData t, Action<object> setResult)
        {
            if (t.IsA(typeof(TestStepList)) == false) return false;
            var steps = new TestStepList();
            var sequenceNameAttr = elem.Attribute("SequenceName");
            steps.SequenceName = sequenceNameAttr?.Value ?? string.Empty;
            foreach (var subnode in elem.Elements())
            {
                if (subnode.Name == varName)
                {
                    TestVariable v = null;
                    try
                    {
                        if (!Serializer.Deserialize(subnode, x => v = (TestVariable)x))
                        {
                            Serializer.PushError(subnode, "Unable to deserialize test step.");
                            continue; // skip to next step.
                        }
                    }
                    catch (Exception ex)
                    {
                        Serializer.PushError(subnode, "Unable to deserialize test step.", ex);
                        continue;
                    }
 
                    if (v != null)
                        steps.Variables.Add(v);
                }
                else if (subnode.Name == paramName)
                {
                    TestVariable v = null;
                    try
                    {
                        if (!Serializer.Deserialize(subnode, x => v = (TestVariable)x))
                        {
                            Serializer.PushError(subnode, "Unable to deserialize test step.");
                            continue; // skip to next step.
                        }
                    }
                    catch (Exception ex)
                    {
                        Serializer.PushError(subnode, "Unable to deserialize test step.", ex);
                        continue;
                    }
 
                    if (v != null)
                        steps.Parameters.Add(v);
                }
                else if (subnode.Name == DebugIds)
                {
                    Guid id = Guid.Empty;
                    try
                    {
                        if (!Serializer.Deserialize(subnode, x => id = (Guid)x))
                        {
                            Serializer.PushError(subnode, "Unable to deserialize DebugId.");
                            continue; // skip to next step.
                        }
                    }
                    catch (Exception ex)
                    {
                        Serializer.PushError(subnode, "Unable to deserialize DebugId.", ex);
                        continue;
                    }
 
                    if (id != Guid.Empty)
                        steps.DebugIds.Add(id);
                }
                else
                {
                    ITestStep result = null;
                    try
                    {
                        if (!Serializer.Deserialize(subnode, x => result = (ITestStep)x))
                        {
                            Serializer.PushError(subnode, "Unable to deserialize test step.");
                            continue; // skip to next step.
                        }
                    }
                    catch (Exception ex)
                    {
                        Serializer.PushError(subnode, "Unable to deserialize test step.", ex);
                        continue;
                    }
 
                    if (result != null)
                        steps.Add(result);
                }
            }
            setResult(steps);
            return true;
        }
 
        
        /// <summary> Serialization implementation. </summary>
        public override bool Serialize(XElement elem, object target, ITypeData expectedType)
        {
            if (target is TestStepList)
            {
                TestStepList list = (TestStepList)target;
                if (!string.IsNullOrEmpty(list.SequenceName))
                {
                    elem.Add(new XAttribute(SequenceName, list.SequenceName));
                }
                for (int i = 0; i < list.Count; i++)
                {
                    var item = list[i];
                    var newelem = new XElement(testStepName);
                    Serializer.Serialize(newelem, item, null, true);
                    elem.Add(newelem);
                }
 
                {
                    var vars = list.Variables;
                    if (vars != null && vars.Count > 0)
                    {
                        for (int i = 0; i < vars.Count; i++)
                        {
                            var item = vars[i];
                            var newelem = new XElement(varName);
                            Serializer.Serialize(newelem, item, null, true);
                            elem.Add(newelem);
                        }
                    }
                }
 
                {
                    var parameters = list.Parameters;
                    if (parameters != null && parameters.Count > 0)
                    {
                        for (int i = 0; i < parameters.Count; i++)
                        {
                            var item = parameters[i];
                            var newelem = new XElement(paramName);
                            Serializer.Serialize(newelem, item, null, true);
                            elem.Add(newelem);
                        }
                    }
                }
 
                {
                    var debugIds = list.DebugIds;
                    if (debugIds != null && debugIds.Count > 0)
                    {
                        for (int i = 0; i < debugIds.Count; i++)
                        {
                            var item = debugIds[i];
                            var newelem = new XElement(DebugIds);
                            Serializer.Serialize(newelem, item, null, true);
                            elem.Add(newelem);
                        }
                    }
                }
 
 
                return true;
            }
            return false;
        }
    }
 
}