Skip to content

Commit

Permalink
Allow specifying test generators to run through command-line (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Jun 28, 2017
1 parent 7d4f40c commit f1c637d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
3 changes: 1 addition & 2 deletions generators/Exercise.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Generators.Input;
using Generators.Output;
using Humanizer;

namespace Generators
{
public abstract class Exercise
{
protected Exercise()
{
Name = GetType().Name.Kebaberize();
Name = GetType().ToExerciseName();
CanonicalData = CanonicalDataParser.Parse(Name);
Configuration = new ExerciseConfiguration();
}
Expand Down
33 changes: 33 additions & 0 deletions generators/ExerciseCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Generators.Output;

namespace Generators
{
public class ExerciseCollection : IEnumerable<Exercise>
{
private readonly string[] _exerciseNames;

public ExerciseCollection() => _exerciseNames = null;
public ExerciseCollection(string[] exerciseNames) => _exerciseNames = exerciseNames;

public IEnumerator<Exercise> GetEnumerator() => GetDefinedGenerators().GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

private IEnumerable<Exercise> GetDefinedGenerators() =>
from type in Assembly.GetEntryAssembly().GetTypes()
where IsConcreteExercise(type) && ShouldBeIncluded(type)
select (Exercise)Activator.CreateInstance(type);

private static bool IsConcreteExercise(Type type) => typeof(Exercise).IsAssignableFrom(type) && !type.GetTypeInfo().IsAbstract;

private bool ShouldBeIncluded(Type type)
=> _exerciseNames == null ||
_exerciseNames.Contains(type.ToExerciseName(), StringComparer.OrdinalIgnoreCase) ||
_exerciseNames.Contains(type.Name, StringComparer.OrdinalIgnoreCase);
}
}
22 changes: 0 additions & 22 deletions generators/Input/ExerciseCollection.cs

This file was deleted.

3 changes: 3 additions & 0 deletions generators/Output/NameExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Text.RegularExpressions;
using Humanizer;

namespace Generators.Output
{
public static class NameExtensions
{
public static string ToExerciseName(this Type exerciseType) => exerciseType.Name.Kebaberize();

public static string ToTestClassName(this string input) => $"{input.Dehumanize()}Test";

public static string ToTestedClassName(this string input) => input.Dehumanize();
Expand Down
11 changes: 7 additions & 4 deletions generators/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace Generators
{
public static class Program
{
public static void Main()
public static void Main(string[] args)
{
SetupLogger();
GenerateAll();
Generate(args);
}

private static void SetupLogger()
Expand All @@ -18,14 +18,17 @@ private static void SetupLogger()
.CreateLogger();
}

private static void GenerateAll()
private static void Generate(string[] exerciseNames)
{
Log.Information("Generating tests...");

foreach (var exercise in new ExerciseCollection())
foreach (var exercise in GetExercises(exerciseNames))
exercise.Generate();

Log.Information("Generated tests.");
}

private static ExerciseCollection GetExercises(string[] exerciseNames)
=> exerciseNames.Length == 0 ? new ExerciseCollection() : new ExerciseCollection(exerciseNames);
}
}

0 comments on commit f1c637d

Please sign in to comment.