forked from dmpas/oscript-ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngineHelpWrapper.cs
136 lines (111 loc) · 3.48 KB
/
EngineHelpWrapper.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
/*----------------------------------------------------------
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 System;
using System.IO;
using ScriptEngine.Machine.Contexts;
using ScriptEngine.HostedScript.Library;
using ScriptEngine.Machine;
using ScriptEngine.Environment;
using ScriptEngine.HostedScript;
namespace NUnitTests
{
public class EngineHelpWrapper : IHostApplication
{
private HostedScriptEngine engine;
public EngineHelpWrapper()
{
}
public HostedScriptEngine Engine
{
get
{
return engine;
}
}
public IValue TestRunner { get; private set; }
public HostedScriptEngine StartEngine()
{
engine = new HostedScriptEngine();
engine.Initialize();
// Тут можно указать любой класс из компоненты
engine.AttachAssembly(System.Reflection.Assembly.GetAssembly(typeof(oscriptFtp.FtpConnection)));
// Подключаем тестовую оболочку
engine.AttachAssembly(System.Reflection.Assembly.GetAssembly(typeof(EngineHelpWrapper)));
var testrunnerSource = LoadFromAssemblyResource("NUnitTests.Tests.testrunner.os");
var testrunnerModule = engine.GetCompilerService().CreateModule(testrunnerSource);
engine.LoadUserScript(new ScriptEngine.UserAddedScript()
{
Type = ScriptEngine.UserAddedScriptType.Class,
Module = testrunnerModule,
Symbol = "TestRunner"
});
var testRunner = AttachedScriptsFactory.ScriptFactory("TestRunner", new IValue[] { });
TestRunner = ValueFactory.Create(testRunner);
return engine;
}
public void RunTestScript(string resourceName)
{
var source = LoadFromAssemblyResource(resourceName);
var module = engine.GetCompilerService().CreateModule(source);
engine.LoadUserScript(new ScriptEngine.UserAddedScript()
{
Type = ScriptEngine.UserAddedScriptType.Class,
Module = module,
Symbol = resourceName
});
var test = AttachedScriptsFactory.ScriptFactory(resourceName, new IValue[] { });
ArrayImpl testArray;
{
int methodIndex = test.FindMethod("ПолучитьСписокТестов");
{
IValue ivTests;
test.CallAsFunction(methodIndex, new IValue[] { TestRunner }, out ivTests);
testArray = ivTests as ArrayImpl;
}
}
foreach (var ivTestName in testArray)
{
string testName = ivTestName.AsString();
int methodIndex = test.FindMethod(testName);
if (methodIndex == -1)
{
// Тест указан, но процедуры нет или она не экспортирована
continue;
}
test.CallAsProcedure(methodIndex, new IValue[] { });
}
}
public ICodeSource LoadFromAssemblyResource(string resourceName)
{
var asm = System.Reflection.Assembly.GetExecutingAssembly();
string codeSource;
using (Stream s = asm.GetManifestResourceStream(resourceName))
{
using (StreamReader r = new StreamReader(s))
{
codeSource = r.ReadToEnd();
}
}
return engine.Loader.FromString(codeSource);
}
public void Echo(string str, MessageStatusEnum status = MessageStatusEnum.Ordinary)
{
}
public string[] GetCommandLineArguments()
{
return new string[] { };
}
public bool InputString(out string result, int maxLen)
{
result = "";
return false;
}
public void ShowExceptionInfo(Exception exc)
{
}
}
}