JD2022-TU1/main/build/sharpmake/Tools/UnitTests.cs

85 lines
No EOL
2.7 KiB
C#

using System;
using System.Collections.Generic;
using JustDance.Tools.Extern;
using Sharpmake;
namespace JustDance.Tools
{
abstract class UnitTestsProject : FrameworkProject
{
public UnitTestsProject()
{
}
}
/// <summary>
/// A test project that tests the <typeparam name="TProject">source project</typeparam>
/// </summary>
/// <typeparam name="TProject"></typeparam>
abstract class UnitTestsProject<TProject> : UnitTestsProject
where TProject : BaseProject, new()
{
public UnitTestsProject()
{
var testedProject = new TProject();
ProjectType = testedProject.ProjectType;
SourceRootPath = testedProject.SourceRootPath.Replace("[project.Name]", testedProject.Name) + @"\..\Tests"; // Replaces \Project to \Tests
}
public override void ConfigureAll(Configuration conf, Target target)
{
base.ConfigureAll(conf, target);
conf.ReferencesByNuGetPackage.Add(Externs.NUnit);
conf.ReferencesByNuGetPackage.Add(Externs.NSubstitute);
conf.AddPublicDependency<TProject>(target);
conf.SolutionFolder = ProjectType + @"\" + new TProject().Name;
}
}
/// <summary>
/// The list of unit tested projects.
/// </summary>
public static class UnitTestedProjects
{
private static readonly List<Type> _unitTestedProjects = new List<Type>();
static UnitTestedProjects()
{
foreach (var assemblyType in typeof(UnitTestsProject).Assembly.GetTypes())
{
Type testedProjectType;
if (ExtractTestedProjectType(assemblyType, out testedProjectType))
{
_unitTestedProjects.Add(testedProjectType);
}
}
}
private static bool ExtractTestedProjectType(Type type, out Type testedProjectType)
{
if (type.IsSubclassOf(typeof(UnitTestsProject)))
{
do
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(UnitTestsProject<>))
{
testedProjectType = type.GetGenericArguments()[0];
return true;
}
type = type.BaseType;
} while (type != null);
}
testedProjectType = null;
return false;
}
/// <summary>
/// True if the specified project type is unit tested.
/// </summary>
public static bool Contains(Type projectType)
{
return _unitTestedProjects.Contains(projectType);
}
}
}