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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
using OpenTap.Addin.Annotation;
using OpenTap.Addin.Entity;
using System;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Xml.Serialization;
 
namespace OpenTap.Addin
{
    public enum TestVariableType
    {
        String = 0,
        Int = 1,
        Double = 2,
        Bool = 3,
        Container = 8,
        Refrence = 9
    }
 
    public class NullVariable : DynamicObject
    {
        private string _____name_____;
 
        public NullVariable(string name)
        {
            _____name_____ = name;
        }
 
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            throw new ArgumentException($"参数 {_____name_____} 不存在");
        }
 
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            throw new ArgumentException($"参数 {_____name_____} 不存在");
        }
 
        public object this[int index]
        {
            get
            {
                throw new ArgumentException($"参数 {_____name_____} 不存在");
            }
            set
            {
                throw new ArgumentException($"参数 {_____name_____} 不存在");
            }
        }
 
        public static bool operator ==(NullVariable v1, object v2)
        {
            if (v2 is NullVariable) return true;
            return v2 == null;
        }
 
        public static bool operator !=(NullVariable v1, object v2)
        {
            if (v2 is NullVariable vv2) return v1 == vv2;
            return v2 != null;
        }
 
        public override string ToString()
        {
            return "NULL";
        }
 
        public override bool Equals(object obj)
        {
            return obj is NullVariable variable;
        }
 
        public override int GetHashCode()
        {
            throw new NullReferenceException();
        }
    }
 
 
    public class TestVariable : DynamicObject, INotifyPropertyChanged
    {
        #region ...
        public event PropertyChangedEventHandler PropertyChanged;
 
        protected void Set([CallerMemberName] string name = null)
        {
 
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
 
        protected void Set<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null)
            {
                Set(propertyName);
            }
        }
        #endregion
 
        private bool nameCanEdit = true;
        [XmlIgnore]
        public bool NameCanEdit
        {
            get => nameCanEdit;
            set
            {
                Set(ref nameCanEdit, value);
            }
        }
 
        private string name;
        [DataGridCol("名称", "*", true)]
        public string Name
        {
            get => name;
            set
            {
                Set(ref name, value);
                Set(nameof(Title));
            }
        }
 
        private string contextName;
        [XmlIgnore]
        public string ContextName
        {
            get => contextName;
            set
            {
                Set(ref contextName, value);
                Set(nameof(Title));
            }
        }
 
        [XmlIgnore]
        public string Title
        {
            get
            {
                if (string.IsNullOrEmpty(ContextName))
                    return Name;
                return $"{Name}  ({ContextName})";
            }
            set
            {
                Set(ref name, value);
                Set(nameof(Title));
            }
        }
 
        public bool IsArray { get; set; }
 
        private TestVariableType type;
        [DataGridCol("类型", "*", true)]
 
        public TestVariableType Type
        {
            get => type;
            set
            {
                Set(ref type, value);
            }
        }
 
        [DataGridCol("值", "*", false, true)]
        public string Value { get; set; }
 
        private ObservableCollection<TestVariable> children = new ObservableCollection<TestVariable>();
        public ObservableCollection<TestVariable> Children
        {
            get => children;
            set
            {
                Set(ref children, value);
            }
        }
 
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;
            string name = binder.Name;
            var pInfo = typeof(TestVariable).GetProperty(name);
            if (pInfo == null)
            {
                var child = Children.FirstOrDefault(c => c.Name == name);
                if (child == null)
                {
                    result = new NullVariable($"{this.Name}.{name}");
                    return true;
                }
                if (child.IsArray || child.Type == TestVariableType.Container)
                {
                    result = child;
                }
                else
                {
                    result = child.GetValue();
                }
                return true;
            }
            else
            {
                result = pInfo.GetValue(this);
                return true;
            }
        }
 
        public object this[int index]
        {
            get
            {
                if (!IsArray)
                {
                    throw new Exception();
                }
                return Children[index].GetValue();
            }
            set
            {
                if (!IsArray)
                {
                    throw new Exception();
                }
                Children[index].Value = value.ToString();
            }
        }
 
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            string name = binder.Name;
            var pInfo = typeof(TestVariable).GetProperty(name);
            if (pInfo == null)
            {
                var child = Children.FirstOrDefault(c => c.Name == name);
                if (child == null)
                {
                    if (Type != TestVariableType.Container)
                    {
                        return false;
                    }
                    child = new TestVariable
                    {
                        Name = name
                    };
                    Children.Add(child);
                }
 
                child.Type = FormatType(value.GetType());
                child.Value = value.ToString();
                return true;
            }
            else
            {
                pInfo.SetValue(this, value);
                return true;
            }
        }
 
        public object GetValue()
        {
            if (Type == TestVariableType.Int)
            {
                return new NumberData(Value);
            }
            else if (Type == TestVariableType.Double)
            {
                return new NumberData(Value);
            }
            else if (Type == TestVariableType.Bool)
            {
                if (bool.TryParse(Value, out bool boolValue))
                    return boolValue;
            }
            else if (Type == TestVariableType.String)
            {
                return Value;
            }
            return null;
        }
 
 
        public RuntimeVariable ToRuntime(TestPlanRun run)
        {
            if (IsArray)
            {
                switch (Type)
                {
                    case TestVariableType.String:
                        return new RuntimeVariable(run, Name, Children.Select(e => e.GetValue()).ToArray());
                    case TestVariableType.Int:
                        return new RuntimeVariable(run, Name, Children.Select(e => e.GetValue()).ToArray());
                    case TestVariableType.Double:
                        return new RuntimeVariable(run, Name, Children.Select(e => e.GetValue()).ToArray());
                    case TestVariableType.Bool:
                        return new RuntimeVariable(run, Name, Children.Select(e => e.GetValue()).ToArray());
                    case TestVariableType.Container:
                        return new RuntimeVariable(run, Name, Children.Select(e => e.ToRuntime(run)).ToArray());
                    case TestVariableType.Refrence:
                        return new RuntimeVariable(run, Name, Children.Select(e => e.GetValue()).ToArray());
                }
            }
            else
            {
                switch (Type)
                {
                    case TestVariableType.String:
                        return new RuntimeVariable(run, Name, Value);
                    case TestVariableType.Int:
                        return new RuntimeVariable(run, Name, GetValue());
                    case TestVariableType.Double:
                        return new RuntimeVariable(run, Name, GetValue());
                    case TestVariableType.Bool:
                        return new RuntimeVariable(run, Name, GetValue());
                    case TestVariableType.Container:
                        return new RuntimeVariable(run, Name, new ConcurrentDictionary<string, RuntimeVariable>
                               (Children.Select(e => e.ToRuntime(run)).ToDictionary(v => v.__name___, v => v)));
                    case TestVariableType.Refrence:
                        return new RuntimeVariable(run, Name, null);
                }
            }
            return null;
        }
 
        public object GetValue__()
        {
            if (Type == TestVariableType.Int)
            {
                if (int.TryParse(Value, out int intValue))
                    return intValue;
            }
            else if (Type == TestVariableType.Double)
            {
                if (double.TryParse(Value, out double doubleValue))
                    return doubleValue;
            }
            else if (Type == TestVariableType.Bool)
            {
                if (bool.TryParse(Value, out bool boolValue))
                    return boolValue;
            }
            else if (Type == TestVariableType.String)
            {
                return Value;
            }
            return null;
        }
 
        //public RuntimeVariable ToRuntime___()
        //{
        //    if (IsArray)
        //    {
        //        switch (Type)
        //        {
        //            case TestVariableType.String:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(string[]),
        //                    TypeName = "string[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Int:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(int[]),
        //                    TypeName = "int[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Double:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(double[]),
        //                    TypeName = "double[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Bool:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(bool[]),
        //                    TypeName = "bool[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Container:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(object[]),
        //                    TypeName = "Container[]",
        //                    data = new VariableContext(Name, new ConcurrentDictionary<string, RuntimeVariable>
        //                        (Children.Select(e => e.ToRuntime()).ToDictionary(v => v.Name, v => v)))
        //                };
        //        }
        //    }
        //    else
        //    {
        //        switch (Type)
        //        {
        //            case TestVariableType.String:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(string),
        //                    TypeName = "string",
        //                    data = Value
        //                };
        //            case TestVariableType.Int:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(int),
        //                    TypeName = "int",
        //                    data = int.Parse(Value)
        //                };
        //            case TestVariableType.Double:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(double),
        //                    TypeName = "double",
        //                    data = double.Parse(Value)
        //                };
        //            case TestVariableType.Bool:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(bool),
        //                    TypeName = "bool",
        //                    data = bool.Parse(Value)
        //                };
        //            case TestVariableType.Container:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(object[]),
        //                    TypeName = "object",
        //                    data = new VariableContext(Name, new ConcurrentDictionary<string, RuntimeVariable>
        //                       (Children.Select(e => e.ToRuntime()).ToDictionary(v => v.Name, v => v)))
        //                };
        //        }
        //    }
        //    return null;
        //}
 
        //public RuntimeContext ToRuntime2()
        //{
        //    if (IsArray)
        //    {
        //        var  
        //        switch (Type)
        //        {
        //            case TestVariableType.String:
        //                return new RuntimeContext
        //                {
        //                    Name = Name,
        //                    Type = typeof(string[]),
        //                    TypeName = "string[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Int:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(int[]),
        //                    TypeName = "int[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Double:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(double[]),
        //                    TypeName = "double[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Bool:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(bool[]),
        //                    TypeName = "bool[]",
        //                    data = Children.Select(e => e.GetValue()).ToArray()
        //                };
        //            case TestVariableType.Container:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(object[]),
        //                    TypeName = "Container[]",
        //                    data = new VariableContext(new ConcurrentDictionary<string, RuntimeVariable>
        //                        (Children.Select(e => e.ToRuntime()).ToDictionary(v => v.Name, v => v)))
        //                };
        //        }
        //    }
        //    else
        //    {
        //        switch (Type)
        //        {
        //            case TestVariableType.String:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(string),
        //                    TypeName = "string",
        //                    data = Value
        //                };
        //            case TestVariableType.Int:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(int),
        //                    TypeName = "int",
        //                    data = int.Parse(Value)
        //                };
        //            case TestVariableType.Double:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(double),
        //                    TypeName = "double",
        //                    data = double.Parse(Value)
        //                };
        //            case TestVariableType.Bool:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(bool),
        //                    TypeName = "bool",
        //                    data = bool.Parse(Value)
        //                };
        //            case TestVariableType.Container:
        //                return new RuntimeVariable
        //                {
        //                    Name = Name,
        //                    Type = typeof(object[]),
        //                    TypeName = "object",
        //                    data = new VariableContext(new ConcurrentDictionary<string, RuntimeVariable>
        //                       (Children.Select(e => e.ToRuntime()).ToDictionary(v => v.Name, v => v)))
        //                };
        //        }
        //    }
        //    return null;
        //}
 
        public static TestVariableType FormatType(Type type)
        {
            if (type == typeof(string))
            {
                return TestVariableType.String;
            }
            if (type == typeof(int))
            {
                return TestVariableType.Int;
            }
            if (type == typeof(double))
            {
                return TestVariableType.Double;
            }
            if (type == typeof(bool))
            {
                return TestVariableType.Bool;
            }
            return TestVariableType.Refrence;
        }
 
        public static string DefaultValue(TestVariableType type)
        {
            switch (type)
            {
                case TestVariableType.String:
                    return string.Empty;
                case TestVariableType.Int:
                    return "0";
                case TestVariableType.Double:
                    return "0";
                case TestVariableType.Bool:
                    return "False";
                default:
                    return null;
            }
        }
    }
}