Skip to content

Commit

Permalink
Merge pull request #16 from TestCentric/issue-15
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
CharliePoole authored Mar 5, 2023
2 parents 54c3dd9 + d07ec56 commit ddd5e14
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 2 deletions.
7 changes: 7 additions & 0 deletions TestCentric.Metadata.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
LICENSE = LICENSE
NOTICE.md = NOTICE.md
README.md = README.md
versioning.cake = versioning.cake
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nuget", "nuget", "{A6EB94CC-A287-4D4E-9D5C-FBA337E29008}"
ProjectSection(SolutionItems) = preProject
nuget\TestCentric.Metadata.nuspec = nuget\TestCentric.Metadata.nuspec
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "testcentric.engine.metadata.tests", "src\testcentric.engine.metadata.tests\testcentric.engine.metadata.tests.csproj", "{89449F96-70D5-46CF-80F0-2577DB0BAEC2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,6 +33,10 @@ Global
{DE3BDBAD-F893-4E17-9B24-FF12FA191A7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE3BDBAD-F893-4E17-9B24-FF12FA191A7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE3BDBAD-F893-4E17-9B24-FF12FA191A7A}.Release|Any CPU.Build.0 = Release|Any CPU
{89449F96-70D5-46CF-80F0-2577DB0BAEC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89449F96-70D5-46CF-80F0-2577DB0BAEC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89449F96-70D5-46CF-80F0-2577DB0BAEC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89449F96-70D5-46CF-80F0-2577DB0BAEC2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
43 changes: 41 additions & 2 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ Setup((context) =>
// Directories
var PROJECT_DIR = Context.Environment.WorkingDirectory.FullPath + "/";
var BIN_DIR = PROJECT_DIR + "bin/" + Configuration + "/";
var TEST_DIR = BIN_DIR + "tests/";
var PACKAGE_DIR = PROJECT_DIR + "package/";
var NUGET_DIR = PROJECT_DIR + "nuget/";

// Testing
var NETFX_TEST_ASSEMBLY = "testcentric.engine.metadata.tests.exe";
var NETCORE_TEST_ASSEMBLY = "testcentric.engine.metadata.tests.dll";
var TEST_RUNTIMES = new [] {"net35", "net40", "net45", "netcoreapp2.1", "netcoreapp3.1", "net5.0", "net6.0", "net7.0"};

// Publishing
var PackageName = NUGET_ID + "." + PackageVersion + ".nupkg";

Expand Down Expand Up @@ -129,6 +135,35 @@ Task("Build")
);
});

//////////////////////////////////////////////////////////////////////
// TESTING
//////////////////////////////////////////////////////////////////////

Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
var errors = new List<string>();

foreach (var runtime in TEST_RUNTIMES)
{
Console.WriteLine("\n----------------------------------------");
Console.WriteLine("TESTING UNDER " + runtime.ToUpper());
Console.WriteLine("----------------------------------------\r\n");

bool isNetFX = runtime.StartsWith("net2") || runtime.StartsWith("net3") || runtime.StartsWith("net4");
int rc = isNetFX
? StartProcess($"{TEST_DIR}{runtime}/{NETFX_TEST_ASSEMBLY}")
: StartProcess("dotnet", $"{TEST_DIR}{runtime}/{NETCORE_TEST_ASSEMBLY}");

if (rc != 0)
errors.Add($"Testing under {runtime}, rc={rc}");
}

if (errors.Count > 0)
throw new Exception("\r\n" + string.Join("\r\n", errors));
});

//////////////////////////////////////////////////////////////////////
// PACKAGE
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -198,11 +233,15 @@ Task("CreateProductionRelease")
//////////////////////////////////////////////////////////////////////

Task("AppVeyor")
.IsDependentOn("Build")
.IsDependentOn("Package")
.IsDependentOn("BuildTestAndPackage")
.IsDependentOn("PublishToMyGet")
.IsDependentOn("CreateProductionRelease");

Task("BuildTestAndPackage")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("Package");

Task("Default")
.IsDependentOn("Build");

Expand Down
91 changes: 91 additions & 0 deletions src/testcentric.engine.metadata.tests/AssemblyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using NUnit.Framework;
using System;
using System.Reflection;

namespace TestCentric.Metadata
{
public class AssemblyTests
{
static string THIS_ASSEMBLY = Assembly.GetExecutingAssembly().Location;

AssemblyDefinition _assemblyDef;


[OneTimeSetUp]
public void CreateAssemblyDefinition()
{
_assemblyDef = AssemblyDefinition.ReadAssembly(THIS_ASSEMBLY);
}

[Test]
public void CheckAttribute()
{
var titleAttr = GetCustomAttribute("System.Reflection.AssemblyTitleAttribute");

Assert.NotNull(titleAttr, "Title Attribute not found");

var args = titleAttr.ConstructorArguments;
Assert.That(args.Count, Is.EqualTo(1));
Assert.That(args[0].Value, Is.EqualTo("TestCentric.Metadata Tests"));
}

[TestCase("nunit.framework")]
[TestCase("testcentric.engine.metadata")]
public void HasAssemblyReference(string name)
{
Assert.That(HasReferenceTo(name));
}

[Test]
public void GetFrameworkName()
{
#if NET35
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(null));
#elif NET40
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(".NETFramework,Version=v4.0"));
#elif NET45
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(".NETFramework,Version=v4.5"));
#elif NETCOREAPP2_1
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(".NETCoreApp,Version=v2.1"));
#elif NETCOREAPP3_1
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(".NETCoreApp,Version=v3.1"));
#elif NET5_0
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(".NETCoreApp,Version=v5.0"));
#elif NET6_0
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(".NETCoreApp,Version=v6.0"));
#elif NET7_0
Assert.That(_assemblyDef.GetFrameworkName(), Is.EqualTo(".NETCoreApp,Version=v7.0"));
#else
Assert.Fail($"Untested target runtime: {_assemblyDef.GetFrameworkName()}");
#endif
}

[Test]
public void GetRuntimeVersion()
{
#if NET35
Assert.That(_assemblyDef.GetRuntimeVersion(), Is.EqualTo(new Version(2, 0, 50727)));
#else
Assert.That(_assemblyDef.GetRuntimeVersion(), Is.EqualTo(new Version(4, 0, 30319)));
#endif
}

private CustomAttribute GetCustomAttribute(string fullName)
{
foreach (var attr in _assemblyDef.CustomAttributes)
if (attr.AttributeType.FullName == fullName)
return attr;

return null;
}

private bool HasReferenceTo(string name)
{
foreach (var reference in _assemblyDef.MainModule.AssemblyReferences)
if (reference.Name == name)
return true;

return false;
}
}
}
17 changes: 17 additions & 0 deletions src/testcentric.engine.metadata.tests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;
using NUnitLite;

namespace TestCentric.Metadata
{
class Program
{
static int Main(string[] args)
{
#if NETFRAMEWORK
return new AutoRun().Execute(args);
#else
return new TextRunner(typeof(Program).GetTypeInfo().Assembly).Execute(args);
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net35;net40;net45;netcoreapp2.1;netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<RootNamespace>TestCentric.Metadata</RootNamespace>
<OutputType>Exe</OutputType>
<OutputPath>..\..\bin\$(Configuration)\tests\</OutputPath>
<AssemblyTitle>TestCentric.Metadata Tests</AssemblyTitle>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnitLite" Version="3.13.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\testcentric.engine.metadata\testcentric.engine.metadata.csproj" />
</ItemGroup>

</Project>

0 comments on commit ddd5e14

Please sign in to comment.