using System; using System.Collections.Generic; using JustDance.Tools.Extern; using Sharpmake; namespace JustDance.Tools { abstract class UnitTestsProject : FrameworkProject { public UnitTestsProject() { } } /// /// A test project that tests the source project /// /// abstract class UnitTestsProject : 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(target); conf.SolutionFolder = ProjectType + @"\" + new TProject().Name; } } /// /// The list of unit tested projects. /// public static class UnitTestedProjects { private static readonly List _unitTestedProjects = new List(); 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; } /// /// True if the specified project type is unit tested. /// public static bool Contains(Type projectType) { return _unitTestedProjects.Contains(projectType); } } }