From 3bdee294bc7d6f92055783cd20d212b948188bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Sun, 11 Dec 2022 23:02:01 +0100 Subject: [PATCH] Order `BaseProblem` types by `Asembly.FullName` (#141) Stop relying on default ordering returned by `Assembly.GetTypes()` This tackles one of the issues raised in https://github.com/eduherminio/AoCHelper/issues/140. --- src/AoCHelper/Solver.cs | 3 ++- tests/AoCHelper.Test/SolverTest.cs | 32 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/AoCHelper/Solver.cs b/src/AoCHelper/Solver.cs index 22dc978..4c5a158 100644 --- a/src/AoCHelper/Solver.cs +++ b/src/AoCHelper/Solver.cs @@ -495,7 +495,8 @@ await AnsiConsole.Live(table) internal static IEnumerable 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? options) diff --git a/tests/AoCHelper.Test/SolverTest.cs b/tests/AoCHelper.Test/SolverTest.cs index 6daf1e4..769df42 100644 --- a/tests/AoCHelper.Test/SolverTest.cs +++ b/tests/AoCHelper.Test/SolverTest.cs @@ -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 Solve_1() => Solve(); @@ -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); + } + } } }