Skip to content
This repository has been archived by the owner on Mar 20, 2020. It is now read-only.

Commit

Permalink
Dotnet Core Testrunner für die Konsole.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown authored and Unknown committed Jan 25, 2018
1 parent e51a9df commit 8a0f06e
Show file tree
Hide file tree
Showing 26 changed files with 304 additions and 3,620 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,4 @@ __pycache__/
0penCover/

binaries/
Releases/
16 changes: 8 additions & 8 deletions CodeRunner/CodeRunner.sln
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2002
VisualStudioVersion = 15.0.27130.2020
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Startup", "Startup\Startup.csproj", "{CFF7A867-F291-4350-A490-C5CBE1D3D710}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfFrontend", "WpfFrontend\WpfFrontend.csproj", "{8D405DFD-01CC-49F9-A5AA-79F12B7F92CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestRunner", "TestRunner\TestRunner.csproj", "{A65D7694-245A-4CD3-A976-45A1946F0F30}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestRunner", "TestRunner\TestRunner.csproj", "{A65D7694-245A-4CD3-A976-45A1946F0F30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleFrontend", "ConsoleFrontend\ConsoleFrontend.csproj", "{0C5B88E5-D40B-4336-A7C8-802A4D46B1C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CFF7A867-F291-4350-A490-C5CBE1D3D710}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CFF7A867-F291-4350-A490-C5CBE1D3D710}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFF7A867-F291-4350-A490-C5CBE1D3D710}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFF7A867-F291-4350-A490-C5CBE1D3D710}.Release|Any CPU.Build.0 = Release|Any CPU
{8D405DFD-01CC-49F9-A5AA-79F12B7F92CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D405DFD-01CC-49F9-A5AA-79F12B7F92CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D405DFD-01CC-49F9-A5AA-79F12B7F92CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -27,6 +23,10 @@ Global
{A65D7694-245A-4CD3-A976-45A1946F0F30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A65D7694-245A-4CD3-A976-45A1946F0F30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A65D7694-245A-4CD3-A976-45A1946F0F30}.Release|Any CPU.Build.0 = Release|Any CPU
{0C5B88E5-D40B-4336-A7C8-802A4D46B1C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C5B88E5-D40B-4336-A7C8-802A4D46B1C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C5B88E5-D40B-4336-A7C8-802A4D46B1C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C5B88E5-D40B-4336-A7C8-802A4D46B1C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
27 changes: 27 additions & 0 deletions CodeRunner/ConsoleFrontend/ConsoleFrontend.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>consolerunner</AssemblyName>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Ninject" Version="3.3.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TestRunner\TestRunner.csproj" />
</ItemGroup>

</Project>
Binary file not shown.
57 changes: 57 additions & 0 deletions CodeRunner/ConsoleFrontend/DummyTools/dummy.jsonTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[
{
"Input": ["2x^10+2x^3"],
"ExpectedOutput": ["20x^9+6x^2"],
"TrimOutput": true
},
{
"Input": ["2x^10 +2x^3"],
"ExpectedOutput": ["20x^9+6x^2"],
"TrimOutput": true
},
{
"Input": ["2x^10 + 2x^3"],
"ExpectedOutput": ["20x^9+6x^2"],
"TrimOutput": true
},
{
"Input": ["2x^10+ x^3"],
"ExpectedOutput": ["20x^9+6x^2"],
"TrimOutput": true
},
{
"Input": ["2x^10 +2x ^3"],
"ExpectedOutput": ["20x^9+6x^2"],
"TrimOutput": true
},
{
"Input": ["2x^10+2x^3+10"],
"ExpectedOutput": ["20x^9+6x^2"],
"TrimOutput": true
},
{
"Input": ["2x^10+2x^3+sin(x)"],
"ExpectedOutput": ["20x^9+6x^2-cos(x)"],
"TrimOutput": true
},
{
"Input": ["2x^10(2x^3+10)"],
"ExpectedOutput": ["20x^9*6x^2*(2x^3+10)"],
"TrimOutput": true
},
{
"Input": ["2x^10(2x^3+10)"],
"ExpectedOutput": ["20x^9*6x^2(2x^3+10)"],
"TrimOutput": true
},
{
"Input": ["2x^10(2x^3+10)"],
"ExpectedOutput": ["20x^9*6x^2/(2x^3+10)"],
"TrimOutput": true
},
{
"Input": ["2x^-10"],
"ExpectedOutput": ["-20x^-11"],
"TrimOutput": true
}
]
5 changes: 5 additions & 0 deletions CodeRunner/ConsoleFrontend/DummyTools/runner.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Command": "./DummyTools/DummyOutput.exe",
"Argument": "-q -f test.txt",
"Test": "./DummyTools/dummy.jsonTest"
}
39 changes: 39 additions & 0 deletions CodeRunner/ConsoleFrontend/Helpers/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using TestRunner.Models;

namespace ConsoleFrontend.Helpers
{
public static class ExtensionMethods
{
public static string ToConsoleOutput(this TestResult result)
{
var builder = new StringBuilder();
builder.Append($"Duration: {result.Duration}ms");
builder.Append(Environment.NewLine);
builder.Append($"Started?: {result.RunSuccessful}");
builder.Append(Environment.NewLine);
builder.Append($"Success: {result.OutputMatches}");
builder.Append(Environment.NewLine);

if (result.Exception != null)
{
builder.Append($"Exception:{Environment.NewLine}{result.Exception}");
builder.Append(Environment.NewLine);
}

if (result.OutputMatches == false)
{
builder.AppendLine($"Reason: {result.FailReason}");
builder.AppendLine($"Expected:");
builder.AppendLine(string.Join(Environment.NewLine, result.ExpectedOutput));
builder.AppendLine("Received:");
builder.Append(string.Join(Environment.NewLine, result.Output));
}

return builder.ToString();
}
}
}
25 changes: 25 additions & 0 deletions CodeRunner/ConsoleFrontend/Helpers/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleFrontend.Helpers
{
internal class ConfigOptions
{
[Option('c', "config", Required = true, HelpText = "Configuration file containing command, argument and test reference.")]
public string ConfigFile { get; set; }
}

internal class StartupOptions
{
[Option('a', "argument", Required = false, HelpText = "Arguments to pass to the command.")]
public string Argument { get; set; }
[Option('c', "command", Required = true, HelpText = "Command to run. Should be double quoted.")]
public string Command { get; set; }
[Option('t', "test", Required = true, HelpText = "Name of the jsonTest file.")]
public string Test { get; set; }

public bool IsValid => string.IsNullOrWhiteSpace(Command) == false && string.IsNullOrWhiteSpace(Test) == false;
}
}
90 changes: 90 additions & 0 deletions CodeRunner/ConsoleFrontend/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using CommandLine;
using CommandLine.Text;
using ConsoleFrontend.Helpers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TestRunner.Models;

namespace ConsoleFrontend
{
class Program
{
private static CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private static CancellationToken _cancellationToken = _cancellationTokenSource.Token;
private const int DefaultExecutionTimeout = 5000;

static async Task Main(string[] args)
{
var parser = new Parser(config => config.HelpWriter = null);
var results = parser.ParseArguments<StartupOptions>(args)
.WithParsed(async options => await StartFromStartupOptions(options))
.WithNotParsed(errors =>
Parser.Default.ParseArguments<ConfigOptions>(args)
.WithParsed(async options => await StartFromConfigOptions(options))
.WithNotParsed(errpors => OnStartupParameterError())
);

while (_cancellationToken.IsCancellationRequested == false)
await Task.Delay(250);
}

private static async Task StartFromStartupOptions(StartupOptions options)
{
var tests = LoadTestCasesFromFile(options.Test);
await Run(options.Command, options.Argument, tests);
}

private static async Task StartFromConfigOptions(ConfigOptions options)
{
var content = File.ReadAllText(options.ConfigFile);
var config = JsonConvert.DeserializeObject<StartupOptions>(content);
var tests = LoadTestCasesFromFile(config.Test);
await Run(config.Command, config.Argument, tests);
}

private static async Task Run(string command, string argument, IEnumerable<TestCase> tests)
{
var runner = new ProcessTestRunner();

int counter = 0;
foreach(var test in tests)
{
Console.WriteLine($"Starting test {counter++} of {tests.Count()}");
var result = await runner.Run(command, argument, test.Input, test.ExpectedOutput, DefaultExecutionTimeout);
Console.WriteLine(result.ToConsoleOutput());
}

_cancellationTokenSource.Cancel();
}

private static IEnumerable<TestCase> LoadTestCasesFromFile(string filename)
{
// Kein Error Handling, die Exceptions werden automatisch geworfen, wenn die Datei nicht existiert.
var content = File.ReadAllText(filename);
return JsonConvert.DeserializeObject<List<TestCase>>(content);
}

private static void OnStartupParameterError()
{
Console.WriteLine("You have not supplied correct startup parameters. You can either use a configuration file written in the following format:");
Console.WriteLine();
Console.WriteLine("{");
Console.WriteLine(" \"command\": \"$Command_To_Run\"");
Console.WriteLine(" \"argument\": \"$Argument_To_Add_To_Command\" (optional)");
Console.WriteLine(" \"test\": \"$Filename_of_jsonTest-File\"");
Console.WriteLine("}");
Console.WriteLine();
Console.WriteLine("And supply the path to the file with '-c'");
Console.WriteLine();
Console.WriteLine($"Alternatively, use {Environment.NewLine}\t-c to supply a command (double quoted){Environment.NewLine}\t-a to specify arguments added to the command (double quoted){Environment.NewLine}\t-t path to the jsonTest-file");

_cancellationTokenSource.Cancel();
}
}
}
6 changes: 0 additions & 6 deletions CodeRunner/Startup/App.config

This file was deleted.

9 changes: 0 additions & 9 deletions CodeRunner/Startup/App.xaml

This file was deleted.

46 changes: 0 additions & 46 deletions CodeRunner/Startup/App.xaml.cs

This file was deleted.

Loading

0 comments on commit 8a0f06e

Please sign in to comment.