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
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using NUnit.Framework;
using OpenTap.Engine.UnitTests.TestTestSteps;
using OpenTap.Plugins.BasicSteps;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class SerializeEnumTest
    {
 
        public class Step1 : TestStep
        {
            public enum SomeEnum { A, B };
            public Instr2 instr1 { get; set; }
            public SomeEnum Value { get; set; }
            public Step1()
            {
                Value = SomeEnum.B;
            }
 
            public override void Run()
            {
                throw new NotImplementedException();
            }
        }
 
        public class Step2 : TestStep
        {
            public enum SomeEnum { A, B };
 
            public SomeEnum Value { get; set; }
            public Step2()
            {
                Value = SomeEnum.B;
            }
 
            public override void Run()
            {
 
            }
        }
 
        public class Instr1 : Instrument
        {
            public enum SomeEnum { A, B };
            public IInstrument Instr { get; set; }
            public SomeEnum Value { get; set; }
            public Instr1()
            {
                Value = SomeEnum.B;
            }
        }
 
        public class Instr2 : Instrument
        {
            public enum SomeEnum { A, B };
            public SomeEnum Value { get; set; }
            public IInstrument Instr { get; set; }
            public IResultListener Result { get; set; }
            public Instr2()
            {
                Value = SomeEnum.B;
            }
        }
 
        [DisplayName("Test\\UnitTest Result2")]
        public class Result2 : ResultListener
        {
            public Result2()
            {
                Name = "Result2";
            }
 
            public IInstrument Instr { get; set; }
        }
 
 
 
        [Test]
        public void TestSerializePlan()
        {
            Step1 step1 = new Step1();
            Step2 step2 = new Step2();
            TestPlan plan1 = new TestPlan();
            plan1.ChildTestSteps.Add(step1);
            plan1.ChildTestSteps.Add(step2);
            string planString;
            using (var ms = new MemoryStream())
            {
                plan1.Save(ms);
                planString = Encoding.UTF8.GetString(ms.ToArray());
            }
 
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(planString)))
            {
                var plan = TestPlan.Load(ms, plan1.Path);
                Assert.AreEqual(((Step1)plan.ChildTestSteps[0]).Value, Step1.SomeEnum.B);
                Assert.AreEqual(((Step2)plan.ChildTestSteps[1]).Value, Step2.SomeEnum.B);
 
                // test some overloads.
                ms.Position = 0;
                Assert.IsNotNull(TestPlan.Load(ms, ""));
 
                ms.Position = 0;
                Assert.IsNotNull(TestPlan.Load(ms, null));
            }
        }
 
        [Test]
        public void SerializeAndRunSimplePlan()
        {
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(new Step2());
            Assert.AreEqual(Verdict.NotSet, plan.Execute().Verdict);
 
            using (var ms = new MemoryStream())
            {
                plan.Save(ms);
                ms.Position = 0;
 
                var plan2 = TestPlan.Load(ms, "");
                var planrun = plan2.Execute();
 
                Assert.AreEqual(Verdict.NotSet, planrun.Verdict);
            }
        }
 
 
        bool membersEquals(object o1, object o2)
        {
            var p1 = o1.GetType().GetPropertiesTap().Where(t => t.PropertyType.IsPrimitive).Select(prop => prop.GetValue(o1, null)).ToList();
            var p2 = o1.GetType().GetPropertiesTap().Where(t => t.PropertyType.IsPrimitive).Select(prop => prop.GetValue(o2, null)).ToList();
            return p1.Zip(p2, object.Equals).All(p => p);
        }
 
        [Test]
        public void SerializeResourcesWithResources()
        {
            var i1 = new Instr1();
            var i2 = new Instr1();
            InstrumentSettings.Current.Add(i1);
            InstrumentSettings.Current.Add(i2);
            try
            {
                var instruments = new InstrumentSettings();
                instruments.Add(new Instr1() { Value = Instr1.SomeEnum.A, Instr = i2 });
                instruments.Add(new Instr2() { Value = Instr2.SomeEnum.A, Instr = i2 });
 
                string stringdata;
                using (MemoryStream m = new MemoryStream())
                {
                    using (var x = XmlWriter.Create(m))
                        new TapSerializer().Serialize(x, instruments);
                    stringdata = Encoding.UTF8.GetString(m.ToArray());
                }
 
                InstrumentSettings instruments2;
                using (MemoryStream m = new MemoryStream(Encoding.UTF8.GetBytes(stringdata)))
                {
                    instruments2 = (InstrumentSettings)new TapSerializer().Deserialize(m);
                }
 
                Assert.AreEqual(((Instr1)instruments[0]).Value, ((Instr1)instruments2[0]).Value);
                Assert.AreEqual(((Instr2)instruments[1]).Value, ((Instr2)instruments2[1]).Value);
                Assert.AreEqual(((Instr2)instruments[1]).Instr, i2);
                Assert.AreEqual(((Instr1)instruments[0]).Instr, i2);
            }
            finally
            {
                InstrumentSettings.Current.Remove(i1);
                InstrumentSettings.Current.Remove(i2);
            }
        }
 
        [Test]
        public void SerializeSwitch()
        {
            var trace = new EngineUnitTestUtils.TestTraceListener();
            Log.AddListener(trace);
 
            // Lets hook up a switch to an RfConnection
            // and check that it can be serialized and deserialized.
            var sw = new DummySwitchIntrument();
            var rfCon = new RfConnection();
            rfCon.Via.Add(sw.Positions[0]);
            rfCon.Via.Add(sw.Position1);
 
            InstrumentSettings.Current.Clear();
            InstrumentSettings.Current.Add(sw);
            ConnectionSettings.Current.Clear();
            ConnectionSettings.Current.Add(rfCon);
 
            InstrumentSettings.Current.Save();
            ConnectionSettings.Current.Save();
 
            // Ensure that we are actually getting serialize/deserialized.
            InstrumentSettings.Current.Clear();
            ConnectionSettings.Current.Clear();
 
            // Ask to deserialize.
            InstrumentSettings.Current.Invalidate();
            ConnectionSettings.Current.Invalidate();
 
            sw = InstrumentSettings.Current[0] as DummySwitchIntrument;
            rfCon = ConnectionSettings.Current[0] as RfConnection;
            Assert.AreEqual(rfCon.Via[0], sw.Positions[0]);
            Assert.AreEqual(rfCon.Via[1], sw.Position1);
 
 
            Log.Flush();
            Log.RemoveListener(trace);
            trace.AssertErrors();
        }
 
        [Test]
        public void SerializeResourcesWithResourcesTough()
        {
            // Tough because instruments contains references between them
            var i1 = new Instr1();
            var i2 = new Instr2();
            i1.Instr = i2;
            i2.Instr = i1;
            var orig = InstrumentSettings.GetSettingsDirectory("Bench");
            InstrumentSettings.SetSettingsProfile("Bench", orig + "InstrumentTestDir");
            InstrumentSettings.Current.Add(i1);
            InstrumentSettings.Current.Add(i2);
            try
            {
                InstrumentSettings.Current.Save();
 
                InstrumentSettings.SetSettingsProfile("Bench", orig);
                InstrumentSettings.Current.ToString();
                InstrumentSettings.SetSettingsProfile("Bench", orig + "InstrumentTestDir");
                Assert.IsTrue(InstrumentSettings.Current.OfType<Instr1>().Any());
                var arr = InstrumentSettings.Current.Cast<dynamic>().ToArray();
                Assert.AreEqual(arr[0], arr[1].Instr);
                Assert.AreEqual(arr[1], arr[0].Instr);
            }
            finally
            {
                InstrumentSettings.SetSettingsProfile("Bench", orig);
                Directory.Delete(orig + "InstrumentTestDir", true);
            }
        }
 
        [Test]
        public void SerializePlatformSettings()
        {
            var toSerialize = EngineSettings.Current;
            var prevStationName = toSerialize.StationName;
            toSerialize.StationName = "hello";
            string stringdata;
            using (MemoryStream m = new MemoryStream())
            {
                using (var x = XmlWriter.Create(m))
                    new TapSerializer().Serialize(x, toSerialize);
                stringdata = Encoding.UTF8.GetString(m.ToArray());
            }
 
            EngineSettings platformSettings;
            using (MemoryStream m = new MemoryStream(Encoding.UTF8.GetBytes(stringdata)))
                platformSettings = (EngineSettings)new TapSerializer().Deserialize(m);
            Assert.IsTrue(membersEquals(toSerialize, platformSettings));
        }
 
        [Test]
        public void DeserializeLegacyPlatformSettings()
        {
            EngineSettings settings = (EngineSettings)new TapSerializer().DeserializeFromString(Resources.GetEmbedded("LegacyPlatformSettings.xml"), TypeData.FromType(typeof(EngineSettings)));
            Assert.AreEqual(settings.OperatorName, "SomeOperator");
        }
 
        [Test, Ignore("We are not backwards compatible.")]
        public void DeserializeLegacyResultSettings()
        {
            ResultSettings settings = (ResultSettings)new TapSerializer().DeserializeFromString(Resources.GetEmbedded("LegacyResultSettings.xml"), TypeData.FromType(typeof(ResultSettings)));
            Assert.IsTrue(settings[0] is LogResultListener);
            Assert.IsTrue((settings[0] as LogResultListener).FilePath == "SomePath.txt");
            Assert.IsTrue((settings[0] as LogResultListener).FilterOptions ==
                          (LogResultListener.FilterOptionsType.Errors | LogResultListener.FilterOptionsType.Verbose | LogResultListener.FilterOptionsType.Info));
            var serialized = new TapSerializer().SerializeToString(settings);
            var deserialized = (ResultSettings)new TapSerializer().DeserializeFromString(serialized);
            Assert.IsTrue((settings[0] as LogResultListener).FilePath == (deserialized[0] as LogResultListener).FilePath);
        }
 
        [Test]
        public void DeserializeLegacyTapPlan()
        {
            var stream = File.OpenRead("TestTestPlans/FiveDelays.TapPlan");
            TestPlan plan = (TestPlan)new TapSerializer().Deserialize(stream, type: TypeData.FromType(typeof(TestPlan)));
 
            var childSteps = plan.ChildTestSteps.OfType<DelayStep>().ToArray();
            Assert.AreEqual(childSteps.Length, 5);
        }
 
        [Test]
        [Ignore("support for 7.x test plans is dropped.")]
        public void LoadLegacyTestPlanReference()
        {
            var ser = new TapSerializer();
            TestPlan plan = (TestPlan)ser.DeserializeFromFile("TestTestPlans\\LegacyRefPlan.TapPlan");
            Assert.AreEqual(4, plan.ChildTestSteps.Count,"Unexpected number of childsteps.");
            Assert.IsTrue(plan.ChildTestSteps[0].ChildTestSteps[0] is VerdictStep);
            Assert.IsTrue(plan.ChildTestSteps[1].ChildTestSteps[0] is VerdictStep);
            Assert.IsTrue(plan.ChildTestSteps[2].ChildTestSteps[0] is VerdictStep);
            Assert.IsTrue(plan.ChildTestSteps[3].ChildTestSteps[0] is VerdictStep);
 
        }
 
    }
}