Skip to content

Commit

Permalink
Add whole test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Feb 28, 2020
0 parents commit aab9558
Show file tree
Hide file tree
Showing 25 changed files with 1,104 additions and 0 deletions.
433 changes: 433 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions Intent.Assertions/Assert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Intent.Assertions
{
public class Assert
{
public static void AreEqual<T>(T expected, T actual)
{
if (expected.Equals(actual))
{
throw new TestPassedException();
}
else
{
throw new TestFailedException($"Expected {expected}, but got {actual}.");
}
}
}
}
11 changes: 11 additions & 0 deletions Intent.Assertions/Intent.Assertions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

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

</Project>
42 changes: 42 additions & 0 deletions Intent.Console/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Reflection;
using static System.Console;
using static System.ConsoleColor;

namespace Intent.Console
{
internal class ConsoleLogger : IRunLogger
{
public void WriteTestInconclusive(MethodInfo m)
{
var currentColor = ForegroundColor;
ForegroundColor = Yellow;
WriteLine($"[?] {m.Name} inconclusive");
ForegroundColor = currentColor;
}

public void WriteTestPassed(MethodInfo m)
{
var currentColor = ForegroundColor;
ForegroundColor = Green;
WriteLine($"[+] {m.Name} passed");
ForegroundColor = currentColor;
}

public void WriteTestFailure(MethodInfo m, Exception ex)
{
var currentColor = ForegroundColor;
ForegroundColor = Red;
WriteLine($"[-] {m.Name} failed{Environment.NewLine}{ex}");
ForegroundColor = currentColor;
}

public void WriteFrameworkError(Exception ex)
{
var currentColor = ForegroundColor;
ForegroundColor = DarkRed;
WriteLine($"[-] framework failed{Environment.NewLine}{ex}{Environment.NewLine}{Environment.NewLine}");
ForegroundColor = currentColor;
}
}
}
16 changes: 16 additions & 0 deletions Intent.Console/Intent.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.3.0-alpha.20104.2" />
</ItemGroup>

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

</Project>
10 changes: 10 additions & 0 deletions Intent.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Intent.Console
{
class Program
{
static void Main(string[] path)
{
new Runner().Run(path, new ConsoleLogger());
}
}
}
8 changes: 8 additions & 0 deletions Intent.Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Intent.Console": {
"commandName": "Project",
"commandLineArgs": "--path \"C:\\Projects\\temp\\Intent\\Some.Tests\\bin\\Debug\\netstandard2.0\\Some.Tests.dll\""
}
}
}
9 changes: 9 additions & 0 deletions Intent.Primitives/ExcludeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Intent
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method)]
public class ExcludeAttribute : Attribute
{
}
}
13 changes: 13 additions & 0 deletions Intent.Primitives/IRunLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Reflection;

namespace Intent
{
public interface IRunLogger
{
void WriteTestPassed(MethodInfo m);
void WriteTestInconclusive(MethodInfo m);
void WriteTestFailure(MethodInfo m, Exception ex);
void WriteFrameworkError(Exception ex);
}
}
7 changes: 7 additions & 0 deletions Intent.Primitives/Intent.Primitives.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
11 changes: 11 additions & 0 deletions Intent.Primitives/TestFailedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Intent
{
public class TestFailedException : Exception
{
public TestFailedException(string message) : base(message)
{
}
}
}
11 changes: 11 additions & 0 deletions Intent.Primitives/TestPassedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Intent
{
public class TestPassedException : Exception
{
public TestPassedException()
{
}
}
}
14 changes: 14 additions & 0 deletions Intent.Primitives/TestResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Intent
{
public enum TestResult
{
None = 0,
Passed,
Inconclusive,
Skipped,
Failed,
Error,
}
}
15 changes: 15 additions & 0 deletions Intent.TestAdapter/Intent.TestAdapter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.ObjectModel" Version="16.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\Projects\temp\Intent\Intent\Intent.csproj" />
</ItemGroup>

</Project>
61 changes: 61 additions & 0 deletions Intent.TestAdapter/VsLoggerAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using VsTestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult;
using System;
using System.Reflection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

namespace Intent.TestAdapter
{
internal class VsLoggerAdapter : IRunLogger
{
private IFrameworkHandle _handle;
private IMessageLogger _logger;
private Uri _uri;

public VsLoggerAdapter(IFrameworkHandle handle, Uri uri)
{
_handle = handle;
_logger = handle;
_uri = uri;
}

public void WriteFrameworkError(Exception ex)
{
_logger.SendMessage(TestMessageLevel.Error, ex.ToString());
}

public void WriteTestFailure(MethodInfo m, Exception ex)
{

var tc = new TestCase(m.Name, _uri, m.DeclaringType.Assembly.Location);
_handle.RecordResult(new VsTestResult(tc)
{
DisplayName = m.Name.Replace("_", " "),
Outcome = TestOutcome.Failed,
ErrorMessage = ex.Message,
ErrorStackTrace = ex.StackTrace
});
}

public void WriteTestInconclusive(MethodInfo m)
{
var tc = new TestCase(m.Name, _uri, m.DeclaringType.Assembly.Location);
_handle.RecordResult(new VsTestResult(tc)
{
DisplayName = m.Name.Replace("_", " "),
Outcome = TestOutcome.Skipped
});
}

public void WriteTestPassed(MethodInfo m)
{
var tc = new TestCase(m.Name, _uri, m.DeclaringType.Assembly.Location);
_handle.RecordResult(new VsTestResult(tc)
{
DisplayName = m.Name.Replace("_", " "),
Outcome = TestOutcome.Passed
});
}
}
}
56 changes: 56 additions & 0 deletions Intent.TestAdapter/VsTestDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Intent.TestAdapter
{
[DefaultExecutorUri(VsTestExecutor.Id)]
//[FileExtension(".exe")]
//[FileExtension(".dll")]
public class VsTestDiscoverer : ITestDiscoverer
{
public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
{
logger.SendMessage(TestMessageLevel.Informational, "Intent 1.0");
foreach (var p in sources)
{
try
{
var asm = Assembly.LoadFrom(p);
if (asm.IsExcluded())
continue;

var ts = asm.GetTypes().SkipExcluded();
foreach (var t in ts)
{
var ms = t.GetMethods().SkipExcluded();
foreach (var m in ms)
{
try
{
var discoveredTest = new TestCase(m.Name, VsTestExecutor.Uri, p)
{
DisplayName = m.Name.Replace("_", " ")
};

logger.SendMessage(TestMessageLevel.Informational, $"Found test '{discoveredTest.DisplayName}'.");
discoverySink.SendTestCase(discoveredTest);
}
catch (Exception ex)
{
logger.SendMessage(TestMessageLevel.Error, ex.ToString());
}
}
}
}
catch (Exception ex)
{
logger.SendMessage(TestMessageLevel.Error, ex.ToString());
}
}
}
}
}
38 changes: 38 additions & 0 deletions Intent.TestAdapter/VsTestExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Intent.TestAdapter
{
[ExtensionUri(Id)]
public class VsTestExecutor : ITestExecutor
{
public const string Id = "executor://intent.testadapter";
public static readonly Uri Uri = new Uri(Id);

public void Cancel()
{
// noop
}

public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
{
var sources = tests.Select(t => t.Source).Distinct().ToList();
RunTests(sources, runContext, frameworkHandle);
}

public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
{
IMessageLogger logger = frameworkHandle;
logger.SendMessage(TestMessageLevel.Informational, "Intent 1.0");

if (runContext.KeepAlive)
frameworkHandle.EnableShutdownAfterTestRun = true;

new Runner().Run(sources, new VsLoggerAdapter(frameworkHandle, Uri));
}
}
}
21 changes: 21 additions & 0 deletions Intent.Tests/Intent.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Intent.Console\Intent.Console.csproj" />
<ProjectReference Include="..\Some.Tests\Some.Tests.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions Intent.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Intent.Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}
Loading

0 comments on commit aab9558

Please sign in to comment.