Repository navigation
Expand file tree
/
Copy pathPythonTest.cs
More file actions
208 lines (184 loc) · 8.03 KB
/
Copy pathPythonTest.cs
File metadata and controls
208 lines (184 loc) · 8.03 KB
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
using System;
using System.IO;
using System.Linq;
using System.Threading;
using OpenTap.Cli;
using Python.Runtime;
namespace OpenTap.Python.UnitTests
{
[Display("test", Group:"python")]
public class PythonTest : ICliAction
{
public int Execute(CancellationToken cancellationToken)
{
PythonInitializer.LoadPython();
if (!PythonEngine.IsInitialized)
{
foreach (var env in Environment.GetEnvironmentVariables().Keys)
{
log.Info(" {0} = {1}", env, Environment.GetEnvironmentVariable(env.ToString()));
}
}
Assert.IsTrue(PythonEngine.IsInitialized);
PythonEngine.DebugGIL = true;
var b = new TestCls();
var bt = TypeData.GetTypeData(b);
var bt_m = bt.GetMembers();
var stepInterface = TypeData.FromType(typeof(ITestStep));
var allSteps = stepInterface.DerivedTypes;
var pyStep = TypeData.GetTypeData("TestModule.BasicStepTest");
var pyDutType = TypeData.GetTypeData("TestModule.DutTest");
Assert.IsTrue(pyDutType.DescendsTo(typeof(IDut)));
var dut = (IDut) pyDutType.CreateInstance();
DutSettings.Current.Add(dut);
var isStep = pyStep.DescendsTo(typeof(ITestStep));
Assert.IsTrue(isStep);
var step = (ITestStep)pyStep.CreateInstance();
//step.PrePlanRun();
//step.PostPlanRun();
pyStep.GetMember("Dut").SetValue(step, dut);
var plan = new TestPlan();
plan.ChildTestSteps.Add(step);
step.GetType().GetMethod("MethodTest").Invoke(step, Array.Empty<object>());
step.GetType().GetProperty("Frequency").SetValue(step, 100.0);
step.GetType().GetProperty("Frequency").GetValue(step);
Log.Flush();
SessionLogs.Flush();
if (true)
{
try
{
var run = plan.Execute();
var postPlanRunExecuted = (bool) pyStep.GetMember("PostPlanRunExecuted").GetValue(step);
var prePlanRunExecuted = (bool) pyStep.GetMember("PrePlanRunExecuted").GetValue(step);
Assert.IsTrue(postPlanRunExecuted);
Assert.IsTrue(prePlanRunExecuted);
Assert.IsTrue(run.Verdict == Verdict.NotSet);
}
catch (Exception)
{
}
}
var a = AnnotationCollection.Annotate(step);
//var members = step.GetType().GetMembers();
var freq = step.GetType().GetProperty("Frequency").GetValue(step);
var callable = a.GetMember("MethodTest");
var method = callable.Get<IMethodAnnotation>();
method.Invoke();
Test2();
TestEmbeddedProperties();
TestValidationErrors();
return 0;
}
void TestValidationErrors()
{
InstrumentSettings.Current.Clear();
DutSettings.Current.Clear();
var td =TypeData.GetTypeData("PythonExamples.BasicFunctionality.BasicFunctionality");
var step = (TestStep)td.CreateInstance();
var err = step.Error;
if (string.IsNullOrWhiteSpace(err) == false)
{
throw new Exception("Error should be null");
}
var frequencyMember = TypeData.GetTypeData(step).GetMember("Frequency");
frequencyMember.SetValue(step, -1000.0);
err = step.Error;
if (string.IsNullOrWhiteSpace(err))
{
throw new Exception("Error should be set.");
}
}
void TestEmbeddedProperties()
{
var td =TypeData.GetTypeData("Test.TestStep3");
var step = (TestStep)td.CreateInstance();
var a = TypeData.GetTypeData(step).GetMember("B.A");
var c = TypeData.GetTypeData(step).GetMember("C");
var b = td.GetMember("B");
var test = a.GetValue(step);
a.SetValue(step, 10.0);
c.SetValue(step, 10.0);
Assert.IsTrue(object.Equals(a.GetValue(step), c.GetValue(step)));
var plan = new TestPlan();
plan.ChildTestSteps.Add(step);
var run = plan.Execute();
Assert.AreEqual(Verdict.NotSet, run.Verdict);
}
private static TraceSource log = Log.CreateSource("test");
void Test2()
{
foreach (var pluginName in pluginTypes)
{
log.Info("plugin: {0}", pluginName);
var lckManagerType2 = TypeData.GetTypeData(pluginName);
var ins2 = lckManagerType2.CreateInstance();
Assert.IsNotNull(ins2);
}
foreach (var (name, type) in pluginTypes2)
{
log.Info("plugin: {0} is {1}.", name, type);
var lckManagerType2 = TypeData.GetTypeData(name);
var ins2 = lckManagerType2.CreateInstance();
Assert.IsNotNull(ins2);
Assert.Assignable(type, ins2);
}
var pyStep3 = TypeData.GetTypeData("TestModule.BasicStepTest.StepWithNoNamespace");
var teststep2 = (TestStep)pyStep3.CreateInstance();
Assert.IsTrue(teststep2.ChildTestSteps != null);
var td2 = TypeData.GetTypeData(teststep2);
var freq = td2.GetMember("Frequency");
freq.SetValue(teststep2, 10.0);
var val = (double)freq.GetValue(teststep2);
Assert.AreEqual(val, 10.0);
var instr1Type = TypeData.GetTypeData("Test.TestScpiInstrument");
var instr1 = instr1Type.CreateInstance();
Assert.IsTrue(instr1 is ScpiInstrument);
var instr2Type = TypeData.GetTypeData("TestModule.BasicStepTest.TestScpiInstrument2");
Assert.IsTrue(instr2Type != null);
var instr2 = instr2Type.CreateInstance();
Assert.IsTrue(instr2 is ScpiInstrument);
{
var cs= TypeData.GetDerivedTypes<ComponentSettings>().ToArray();
var instr= TypeData.GetDerivedTypes<ITestStep>().ToArray();
}
{
try
{
File.Delete("Settings/Settings Test.xml");
}
catch
{
}
var compsetType = TypeData.GetTypeData("TestModule.BasicStepTest.SettingsTest");
var set2 = ComponentSettings.GetCurrent(compsetType);
var set4 = compsetType.CreateInstance();
var cv = compsetType.GetMember("A").GetValue(set2);
var settings = TypeData.GetDerivedTypes<ComponentSettings>();
compsetType.GetMember("A").SetValue(set2, 10.0);
set2.Invalidate();
set2 = ComponentSettings.GetCurrent(compsetType);
cv = compsetType.GetMember("A").GetValue(set2);
Assert.AreEqual((double)cv, 1.0);
compsetType.GetMember("A").SetValue(set2, 10.0);
set2.Save();
set2.Invalidate();
set2 = ComponentSettings.GetCurrent(compsetType);
cv = compsetType.GetMember("A").GetValue(set2);
Assert.AreEqual((double)cv, 10.0);
}
}
readonly string[] pluginTypes = {
"PythonExamples.CsvResultListener.CsvPythonResultListener",
"TestModule.StepWithNoCtor",
"TestModule.BasicStepTest.TestStep2",
"TestModule.BasicStepTest.LockManager",
"Test.LockManager2",
"TestModule.BasicStepTest.TestScpiInstrument2",
"Test.TestScpiInstrument"
};
readonly (string, Type)[] pluginTypes2 = {
("Test.LockManager2", typeof(ILockManager))
};
}
}