Skip to content

Commit

Permalink
Order BaseProblem types by Asembly.FullName (#141)
Browse files Browse the repository at this point in the history
Stop relying on default ordering returned by `Assembly.GetTypes()`
This tackles one of the issues raised in #140.
  • Loading branch information
eduherminio authored Dec 11, 2022
1 parent b2022b0 commit 3bdee29
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/AoCHelper/Solver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ await AnsiConsole.Live(table)
internal static IEnumerable<Type> LoadAllProblems(Assembly assembly)
{
return assembly.GetTypes()
.Where(type => typeof(BaseProblem).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract);
.Where(type => typeof(BaseProblem).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
.OrderBy(t => t.FullName);
}

private static SolverConfiguration PopulateConfiguration(Action<SolverConfiguration>? options)
Expand Down
32 changes: 31 additions & 1 deletion tests/AoCHelper.Test/SolverTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
using System.Reflection;
using Xunit;

namespace AdventOfCode.Days._01
{
class Day01 : AoCHelper.Test.SolverTest.ProblemFixture { }

}

namespace AdventOfCode.Days._02
{
class Day02 : AoCHelper.Test.SolverTest.ProblemFixture { }

}

namespace AdventOfCode.Days._10
{
class Day10 : AoCHelper.Test.SolverTest.ProblemFixture { }

}

namespace AoCHelper.Test
{
[Collection("Sequential")]
public class SolverTest
{
private abstract class ProblemFixture : BaseProblem
internal abstract class ProblemFixture : BaseProblem
{
public override ValueTask<string> Solve_1() => Solve();

Expand Down Expand Up @@ -103,5 +121,17 @@ public void LoadAllProblems()
Assembly.GetExecutingAssembly()!.GetTypes().Count(type => typeof(BaseProblem).IsAssignableFrom(type) && !type.IsAbstract),
Solver.LoadAllProblems(Assembly.GetExecutingAssembly()).Count());
}

[Fact]
public void LoadAllProblems_OrderedByFullName()
{
var orderedTypes = Solver.LoadAllProblems(Assembly.GetExecutingAssembly()).OrderBy(t => t.FullName);
var types = Solver.LoadAllProblems(Assembly.GetExecutingAssembly());

foreach (var (First, Second) in orderedTypes.Zip(types))
{
Assert.Equal(First, Second);
}
}
}
}

0 comments on commit 3bdee29

Please sign in to comment.