Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Покрытие через внешние тесты #1357

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Build_Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
<TestAssemblies Include="OneScript.DebugProtocol.Test" />
<TestAssemblies Include="OneScript.Dynamic.Tests" />
<TestAssemblies Include="OneScript.Language.Tests" />
<TestAssemblies Include="TestRunner" />
</ItemGroup>

<Exec
Expand Down
13 changes: 13 additions & 0 deletions src/1Script.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneScript.Dynamic.Tests", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneScript.Native", "OneScript.Native\OneScript.Native.csproj", "{90E896C1-FEAE-4C71-8E47-3E3F2D9C926C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestRunner", "Tests\TestRunner\TestRunner.csproj", "{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -253,6 +255,16 @@ Global
{90E896C1-FEAE-4C71-8E47-3E3F2D9C926C}.Release|x86.Build.0 = Release|Any CPU
{90E896C1-FEAE-4C71-8E47-3E3F2D9C926C}.LinuxDebug|Any CPU.ActiveCfg = LinuxDebug|Any CPU
{90E896C1-FEAE-4C71-8E47-3E3F2D9C926C}.LinuxDebug|Any CPU.Build.0 = LinuxDebug|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Debug|x86.ActiveCfg = Debug|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Debug|x86.Build.0 = Debug|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Release|Any CPU.Build.0 = Release|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Release|x86.ActiveCfg = Release|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.Release|x86.Build.0 = Release|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.LinuxDebug|Any CPU.ActiveCfg = Debug|Any CPU
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14}.LinuxDebug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -263,6 +275,7 @@ Global
{6D02017A-189F-45D7-B286-D67536AB4907} = {91059C5B-526C-4B81-B106-99DEFF542D1F}
{55890DF2-D13E-4C89-A01D-79CAD6726246} = {91059C5B-526C-4B81-B106-99DEFF542D1F}
{0F5E6099-39BA-41CF-B55F-357F7DF4DE00} = {91059C5B-526C-4B81-B106-99DEFF542D1F}
{8A3C7DAD-918F-4A4A-B36C-BCECA12D8A14} = {91059C5B-526C-4B81-B106-99DEFF542D1F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A4A871EF-C5A7-478F-907E-31C69A869973}
Expand Down
42 changes: 42 additions & 0 deletions src/Tests/TestRunner/ExternalTestsRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*----------------------------------------------------------
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 Xunit;

namespace TestRunner;

public class ExternalTestsRunner
{
[Fact]
public void TypeDescription()
{
TestRunnerHelper.Run(@"typedescription.os");
}

[Fact]
public void ValueTree()
{
TestRunnerHelper.Run(@"valuetree.os");
}

[Fact]
public void ValueTable()
{
TestRunnerHelper.Run(@"valuetable.os");
}

[Fact]
public void ValueList()
{
TestRunnerHelper.Run(@"value-list.os");
}

[Fact]
public void Math()
{
TestRunnerHelper.Run(@"math.os");
}
}
31 changes: 31 additions & 0 deletions src/Tests/TestRunner/TestRunner.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JunitXml.TestLogger" Version="3.0.124" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\OneScript.StandardLibrary\OneScript.StandardLibrary.csproj" />
<ProjectReference Include="..\..\ScriptEngine.HostedScript\ScriptEngine.HostedScript.csproj" />
<ProjectReference Include="..\..\ScriptEngine\ScriptEngine.csproj" />
</ItemGroup>

</Project>
110 changes: 110 additions & 0 deletions src/Tests/TestRunner/TestRunnerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*----------------------------------------------------------
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 Xunit;
using System.Reflection;
using OneScript.Commons;
using OneScript.Contexts;
using OneScript.StandardLibrary;
using OneScript.StandardLibrary.Collections;
using ScriptEngine;
using ScriptEngine.HostedScript;
using ScriptEngine.HostedScript.Extensions;
using ScriptEngine.Hosting;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;

namespace TestRunner;

[ContextClass("ПроверкиЗначений")]
public class TestRunnerHelper : AutoContext<TestRunnerHelper>
{

public const string GetTestSubName = "ПолучитьСписокТестов";

private static ScriptingEngine _instance;

private static ScriptingEngine Instance
{
get
{
if (_instance == null)
{
_instance = DefaultEngineBuilder
.Create()
.SetDefaultOptions()
.SetupEnvironment(e =>
{
e.AddStandardLibrary()
.UseTemplateFactory(new DefaultTemplatesFactory());
})
.Build();

Locale.SystemLanguageISOName = "RU";
}

return _instance;
}
}

public static void Run(string filename)
{
var engine = Instance;

var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
var filepath = Path.Combine(rootPath, "..", "..", "..", "..", "..", "..", "tests", filename);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

бррр

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я с ходу не нашёл, откуда вытащить корень исходника.


var compiler = engine.GetCompilerService();
compiler.FillSymbols(typeof(UserScriptContextInstance));

var testModule = compiler.Compile(engine.Loader.FromFile(filepath));
engine.Initialize();
var testInstance = (UserScriptContextInstance)engine.NewObject(testModule);
var testRunnerInstance = new TestRunnerHelper();

var getTestsIndex = testInstance.GetMethodNumber(GetTestSubName);
var tmp1 = testRunnerInstance.GetMethodNumber("ПроверитьРавенство");
dmpas marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotEqual(-1, getTestsIndex);

testInstance.CallAsFunction(getTestsIndex, new []{ testRunnerInstance }, out var result);
Assert.NotNull(result);

var arrayOfNames = (ArrayImpl)result;
Assert.NotNull(arrayOfNames);
foreach (var testName in arrayOfNames)
{
var testSubIndex = testInstance.GetMethodNumber(testName.AsString());
Assert.NotEqual(-1, testSubIndex);

testInstance.CallAsProcedure(testSubIndex, Array.Empty<IValue>());
}
}

[ContextMethod("ПроверитьРавенство")]
public void CheckEqual(IValue v1, IValue v2, string extendedInfo = null)
{
Assert.Equal(v1, v2);
}

[ContextMethod("ПроверитьНеРавенство")]
public void CheckNotEqual(IValue v1, IValue v2, string extendedInfo = null)
{
Assert.NotEqual(v1, v2);
}

[ContextMethod("ПроверитьИстину")]
public void CheckTrue(IValue v1, string extendedInfo = null)
{
Assert.True(v1?.AsBoolean() ?? false);
}

[ContextMethod("ПроверитьЛожь")]
public void CheckFalse(IValue v1, string extendedInfo = null)
{
Assert.False(v1?.AsBoolean() ?? true);
}

}