forked from DanielAbalde/Tenrec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.cs
446 lines (401 loc) · 19.3 KB
/
Runner.cs
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Tenrec
{
[TestClass]
public static class Runner
{
private static Rhino.Runtime.InProcess.RhinoCore _rhinoCore;
public static List<Assembly> MissingLibraries { get; private set; }
static Runner()
{
MissingLibraries = new List<Assembly>();
RhinoInside.Resolver.Initialize();
//System.Windows.Forms.MessageBox.Show("Runner Resolver");
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name).Name;
string path = System.IO.Path.Combine(RhinoInside.Resolver.RhinoSystemDirectory, assemblyName + ".dll");
if (System.IO.File.Exists(path))
return Assembly.LoadFrom(path);
if (assemblyName.Equals("Grasshopper") || assemblyName.Contains("GH_IO"))
{
path = System.IO.Path.Combine(System.IO.Directory.GetParent(RhinoInside.Resolver.RhinoSystemDirectory).FullName, "Plug-ins", "Grasshopper", assemblyName + ".dll");
if (System.IO.File.Exists(path))
return Assembly.LoadFrom(path);
}
if (assemblyName.Contains(".resources"))
return null;
MissingLibraries.Add(args.RequestingAssembly);
return null;
}
[STAThread]
public static void Initialize(TestContext context = null)
{
if(_rhinoCore == null)
{
//System.Windows.Forms.MessageBox.Show("Runner INITIALIZE");
_rhinoCore = new Rhino.Runtime.InProcess.RhinoCore();
//LoadGrasshopper(context);
}
}
private static void LoadGrasshopper(TestContext context)
{
//foreach(var error in Grasshopper.Instances.ComponentServer.LoadingExceptions)
//{
// context.WriteLine($"{error.Name}: {error.Type}, {error.Message}");
//}
//context.WriteLine("");
//foreach(var ef in Grasshopper.Kernel.GH_ComponentServer.ExternalFiles(true,true))
//{
// context.WriteLine($"ex... {ef.FileName} | {ef.FilePath}");
//}
//context.WriteLine("");
//var server = new Grasshopper.Kernel.GH_ComponentServer();
//server.GHAFileLoaded += (s, e) => {
// context.WriteLine($"loading... {e.Name} | {e.FileName}");
//};
//server.LoadExternalFiles(false);
//context.WriteLine("");
//foreach (var lib in Grasshopper.Instances.ComponentServer.Libraries.OrderBy(l => l.Name))
//{
// context.WriteLine($"{lib.Name} | {lib.Location}");
//}
//context.WriteLine("");
}
[AssemblyCleanup]
public static void Dispose()
{
//System.Windows.Forms.MessageBox.Show("Runner DISPOSE");
if(_rhinoCore != null)
{
_rhinoCore.Dispose();
_rhinoCore = null;
}
}
/// <summary>
/// Compute a <see cref="Group_UnitTest"/> and finds all errors or messages for each of the objects it contains.
/// </summary>
/// <param name="group">The <see cref="Group_UnitTest"/> containing the objects to evaluate.</param>
/// <param name="log">The output messages.</param>
/// <returns>Resulting unit test state.</returns>
private static UnitTestState EvaluateGroup(Grasshopper.Kernel.Special.GH_Group group, TestContext context, out List<ObjectMessage> log)
{
var state = UnitTestState.Untested;
log = new List<ObjectMessage>();
if (context != null)
{
foreach (var obj in group.Objects())
{
var type = obj.GetType().ToString();
if (type == "Grasshopper.Kernel.Components.GH_PlaceholderParameter" || type == "Grasshopper.Kernel.Components.GH_PlaceholderComponent")
{
context.WriteLine($"Placeholder found! {obj.Name} could not be loaded into the document.");
}
}
}
foreach (var obj in group.Objects())
{
if (obj is Grasshopper.Kernel.IGH_ActiveObject aobj)
{
aobj.CollectData();
aobj.ComputeData();
}
}
foreach (var obj in group.Objects())
{
if (obj is Grasshopper.Kernel.IGH_ActiveObject aobj)
{
var level = aobj.RuntimeMessageLevel;
var messages = aobj.RuntimeMessages(level);
if (messages != null && messages.Count > 0)
{
var name = obj.NickName.Equals(obj.Name, StringComparison.OrdinalIgnoreCase) ? obj.Name : $"{obj.Name}({obj.NickName})";
log.Add(new ObjectMessage(name, level, messages, obj));
if ((state == UnitTestState.Untested || state == UnitTestState.Valid) &&
level == Grasshopper.Kernel.GH_RuntimeMessageLevel.Error)
state = UnitTestState.Failure;
}
}
}
if (state == UnitTestState.Untested)
state = UnitTestState.Valid;
return state;
}
/// <summary>
/// Evaluate a specific <see cref="Group_UnitTest"/> in a Grasshopper document.
/// </summary>
/// <param name="doc">The Grasshopper document.</param>
/// <param name="tenrecGroupInstance">The <see cref="Group_UnitTest"/> instance ID.</param>
/// <param name="context">The context to send messages to the unit test output.</param>
/// <returns>True if the test was executed, false otherwise.</returns>
public static bool RunTenrecGroup(Grasshopper.Kernel.GH_Document doc, Guid tenrecGroupInstance, TestContext context)
{
var obj = doc.FindObject(tenrecGroupInstance, false);
if (obj == null)
throw new Exception($"Group with id: {tenrecGroupInstance} not found");
if (obj is Grasshopper.Kernel.Special.GH_Group group)
{
var state = EvaluateGroup(group, context, out List<ObjectMessage> log);
switch (state)
{
case UnitTestState.Valid:
foreach (var l in log)
{
if (context != null)
context.WriteLine(l.ToString());
}
break;
case UnitTestState.Failure:
foreach (var l in log)
{
throw new AssertFailedException(l.ToString());
}
break;
case UnitTestState.Untested:
throw new Exception($"Group {group.NickName} could not be tested");
}
return true;
}
return false;
}
/// <summary>
/// Evaluate all <see cref="Group_UnitTest"/> that contains a Grasshopper document.
/// </summary>
/// <param name="doc">The Grasshopper document.</param>
/// <param name="context">The context to send messages to the unit test output.</param>
/// <returns>True if the test was executed, false otherwise.</returns>
public static bool RunTenrecGroups(Grasshopper.Kernel.GH_Document doc, TestContext context)
{
return doc.Objects.OfType<Grasshopper.Kernel.Special.GH_Group>()
.All(obj => RunTenrecGroup(doc, obj.InstanceGuid, context));
}
/// <summary>
/// Evaluate a specific <see cref="Group_UnitTest"/> in a Grasshopper file.
/// </summary>
/// <param name="filePath">The Grasshopper file path.</param>
/// <param name="tenrecGroupInstance">The <see cref="Group_UnitTest"/> instance ID.</param>
/// <param name="context">The context to send messages to the unit test output.</param>
/// <returns>True if the test was executed, false otherwise.</returns>
public static bool RunTenrecGroup(string filePath, Guid tenrecGroupInstance, TestContext context)
{
return OpenDocument(filePath, context, out Grasshopper.Kernel.GH_Document doc) && RunTenrecGroup(doc, tenrecGroupInstance, context);
}
/// <summary>
/// Evaluate all <see cref="Group_UnitTest"/> that contains a Grasshopper file.
/// </summary>
/// <param name="filePath">The Grasshopper file path.</param>
/// <param name="context">The context to send messages to the unit test output.</param>
/// <returns>True if the test was executed, false otherwise.</returns>
public static bool RunTenrecGroups(string filePath, TestContext context)
{
return OpenDocument(filePath, context, out Grasshopper.Kernel.GH_Document doc) && RunTenrecGroups(doc, context);
}
/// <summary>
/// Get a Grasshopper document from a Grasshopper file path.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <param name="doc">The resulting Grasshopper document.</param>
/// <returns>True if the file could be opened, false otherwise.</returns>
private static bool OpenDocument(string filePath, TestContext context, out Grasshopper.Kernel.GH_Document doc)
{
if (string.IsNullOrEmpty(filePath))
throw new ArgumentNullException(nameof(filePath));
if (!System.IO.File.Exists(filePath))
throw new Exception($"File not found: {filePath}");
doc = new Grasshopper.Kernel.GH_Document();
var arc = new GH_IO.Serialization.GH_Archive();
if (!arc.ReadFromFile(filePath))
{
OutputMessages(context, arc, true);
return false;
}
OutputMessages(context, arc, false);
arc.ClearMessages();
var extracted = arc.ExtractObject(doc, "Definition");
OutputMessages(context, arc, false);
doc.DestroyProxySources();
doc.Enabled = true;
return extracted;
void OutputMessages(TestContext c, GH_IO.Serialization.GH_Archive a, bool includeInfo)
{
if (c != null && a.MessageCount(includeInfo, true, true) > 0)
foreach (var m in a.Messages)
c.WriteLine($"{m.Type}: {m.Message}");
}
}
}
///// <summary>
///// Evaluator to test if Grasshopper files contains errors or runtime messages.
///// </summary>
//public static class RunnerOBSOLETE
//{
// /// <summary>
// /// Compute a <see cref="Group_UnitTest"/> and finds all errors or messages for each of the objects it contains.
// /// </summary>
// /// <param name="group">The <see cref="Group_UnitTest"/> containing the objects to evaluate.</param>
// /// <param name="log">The output messages.</param>
// /// <returns>Resulting unit test state.</returns>
// public static UnitTestState EvaluateGroup(GH_Group group, out List<ObjectMessage> log)
// {
// var state = UnitTestState.Untested;
// log = new List<ObjectMessage>();
// foreach (var obj in group.Objects())
// {
// if (obj is IGH_ActiveObject aobj)
// {
// aobj.CollectData();
// aobj.ComputeData();
// }
// }
// foreach (var obj in group.Objects())
// {
// if (obj is IGH_ActiveObject aobj)
// {
// var level = aobj.RuntimeMessageLevel;
// var messages = aobj.RuntimeMessages(level);
// if (messages != null && messages.Count > 0)
// {
// var name = obj.NickName.Equals(obj.Name, StringComparison.OrdinalIgnoreCase) ? obj.Name : $"{obj.Name}({obj.NickName})";
// log.Add(new ObjectMessage(name, level, messages, obj));
// if ((state == UnitTestState.Untested || state == UnitTestState.Valid) &&
// level == GH_RuntimeMessageLevel.Error)
// state = UnitTestState.Failure;
// }
// }
// }
// if (state == UnitTestState.Untested)
// state = UnitTestState.Valid;
// return state;
// }
// /// <summary>
// /// Evaluate a specific <see cref="Group_UnitTest"/> in a Grasshopper document.
// /// </summary>
// /// <param name="doc">The Grasshopper document.</param>
// /// <param name="tenrecGroupInstance">The <see cref="Group_UnitTest"/> instance ID.</param>
// /// <param name="context">The context to send messages to the unit test output.</param>
// /// <returns>True if the test was executed, false otherwise.</returns>
// public static bool RunTenrecGroup(GH_Document doc, Guid tenrecGroupInstance, TestContext context)
// {
// RhinoHeadless.Initialize();
// var obj = doc.FindObject(tenrecGroupInstance, false);
// if (obj == null)
// throw new Exception($"Group with id: {tenrecGroupInstance} not found");
// if (obj is GH_Group group)
// {
// var state = EvaluateGroup(group, out List<ObjectMessage> log);
// switch (state)
// {
// case UnitTestState.Valid:
// foreach (var l in log)
// {
// context.WriteLine(l.ToString());
// }
// break;
// case UnitTestState.Failure:
// foreach (var l in log)
// {
// throw new AssertFailedException(l.ToString());
// }
// break;
// case UnitTestState.Untested:
// throw new Exception($"Group {group.NickName} could not be tested");
// }
// return true;
// }
// return false;
// }
// /// <summary>
// /// Evaluate all <see cref="Group_UnitTest"/> that contains a Grasshopper document.
// /// </summary>
// /// <param name="doc">The Grasshopper document.</param>
// /// <param name="context">The context to send messages to the unit test output.</param>
// /// <returns>True if the test was executed, false otherwise.</returns>
// public static bool RunTenrecGroups(GH_Document doc, TestContext context)
// {
// RhinoHeadless.Initialize();
// return doc.Objects.OfType<GH_Group>()
// .All(obj => RunTenrecGroup(doc, obj.InstanceGuid, context));
// }
// /// <summary>
// /// Evaluate a specific <see cref="Group_UnitTest"/> in a Grasshopper file.
// /// </summary>
// /// <param name="filePath">The Grasshopper file path.</param>
// /// <param name="tenrecGroupInstance">The <see cref="Group_UnitTest"/> instance ID.</param>
// /// <param name="context">The context to send messages to the unit test output.</param>
// /// <returns>True if the test was executed, false otherwise.</returns>
// public static bool RunTenrecGroup(string filePath, Guid tenrecGroupInstance, TestContext context)
// {
// RhinoHeadless.Initialize();
// return OpenDocument(filePath, context, out GH_Document doc) && RunTenrecGroup(doc, tenrecGroupInstance, context);
// }
// /// <summary>
// /// Evaluate all <see cref="Group_UnitTest"/> that contains a Grasshopper file.
// /// </summary>
// /// <param name="filePath">The Grasshopper file path.</param>
// /// <param name="context">The context to send messages to the unit test output.</param>
// /// <returns>True if the test was executed, false otherwise.</returns>
// public static bool RunTenrecGroups(string filePath, TestContext context)
// {
// RhinoHeadless.Initialize();
// return OpenDocument(filePath, context, out GH_Document doc) && RunTenrecGroups(doc, context);
// }
// /// <summary>
// /// Get a Grasshopper document from a Grasshopper file path.
// /// </summary>
// /// <param name="filePath">The file path.</param>
// /// <param name="doc">The resulting Grasshopper document.</param>
// /// <returns>True if the file could be opened, false otherwise.</returns>
// public static bool OpenDocument(string filePath, TestContext context, out GH_Document doc)
// {
// //var io = new GH_DocumentIO();
// //if (!io.Open(filePath))
// //{
// // doc = null;
// // throw new Exception($"Failed to open file: {filePath}");
// //}
// //doc = io.Document;
// //doc.Enabled = true;
// if (string.IsNullOrEmpty(filePath))
// throw new ArgumentNullException(nameof(filePath));
// if (!System.IO.File.Exists(filePath))
// throw new Exception($"File not found: {filePath}");
// doc = new GH_Document();
// var arc = new GH_IO.Serialization.GH_Archive();
// if (!arc.ReadFromFile(filePath))
// {
// OutputMessages(context, arc, true);
// return false;
// }
// OutputMessages(context, arc, false);
// arc.ClearMessages();
// var extracted = arc.ExtractObject(doc, "Definition");
// OutputMessages(context, arc, false);
// if(context != null)
// {
// foreach (var obj in doc.Objects)
// {
// var type = obj.GetType().ToString();
// if (type == "Grasshopper.Kernel.Components.GH_PlaceholderParameter" || type == "Grasshopper.Kernel.Components.GH_PlaceholderComponent")
// {
// context.WriteLine($"Placeholder found! {obj.Name} could not be loaded into the document.");
// }
// }
// }
// doc.DestroyProxySources();
// doc.Enabled = true;
// return extracted;
// void OutputMessages(TestContext c, GH_IO.Serialization.GH_Archive a, bool includeInfo)
// {
// if (c != null && a.MessageCount(includeInfo, true, true) > 0)
// foreach (var m in a.Messages)
// c.WriteLine($"{m.Type}: {m.Message}");
// }
// }
//}
}