Permalink
Browse files

add UniqueForRuntime

  • Loading branch information...
SimonCropp committed Sep 2, 2018
1 parent 5b16c70 commit 7864a2f9b9f6e1c08d11bdf6fc1028d9ab9fc4e5
@@ -0,0 +1,4 @@
runtimes
.NET Core 4.6.26725.06 => NetCore_2.1
.NET Framework 4.7.3132.0 => Net_4.7
@@ -14,6 +14,14 @@ public void ExitsBeforeNamerIsCalled()
}
}
[Test]
public void UniqueForRuntime()
{
var runtimes = new[] {".NET Core 4.6.26725.06", ".NET Framework 4.7.3132.0"};
Approvals.VerifyAll("runtimes", runtimes,
s => $"{s} => {ApprovalResults.GetDotNetRuntime(true, s)}");
}
[Test]
public void WithoutExtraInfo()
{
@@ -1,6 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using ApprovalUtilities.Utilities;
namespace ApprovalTests.Namers
@@ -17,6 +22,56 @@ public static string GetDotNetVersion()
return "Net_v" + Environment.Version;
}
public static IDisposable UniqueForRuntime(bool throwOnError = true)
{
return NamerFactory.AsEnvironmentSpecificTest(() => GetDotNetRuntime(throwOnError));
}
public static string GetDotNetRuntime(bool throwOnError)
{
var frameworkDescription = RuntimeInformation.FrameworkDescription;
return GetDotNetRuntime(throwOnError, frameworkDescription);
}
public static string GetDotNetRuntime(bool throwOnError, string frameworkDescription)
{
if (frameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase))
{
var version = Version.Parse(frameworkDescription.Replace(".NET Framework ", ""));
return $"Net_{version.Major}.{version.Minor}";
}
if (frameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
{
var version = Version.Parse(frameworkDescription.Replace(".NET Core ", ""));
var map = new Dictionary<string, string>
{
{
"4.6","2.1"
}
};
if (map.TryGetValue($"{version.Major}.{version.Minor}", out var result))
{
return "NetCore_" + result;
}
}
if (throwOnError)
{
throw new NotImplementedException($@"Your current framework is not properly handled by ApprovalTests
Framework: {frameworkDescription}.
To suppress this error and make the test pass using the full FrameworkDescription use:
using (Namers.ApprovalResults.UniqueForRuntime(throwOnError: true)){{
//The code being tested
}}
To help ApprovalTest please submit a new issue using the following link:
https://github.com/approvals/ApprovalTests.Net/issues/new?title=Unknown%3A+%27Runtime%27&body={frameworkDescription}
");
}
return frameworkDescription;
}
public static IDisposable UniqueForMachineName()
{
return NamerFactory.AsEnvironmentSpecificTest(GetMachineName);

0 comments on commit 7864a2f

Please sign in to comment.