Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support for "test mode" #211

Open
codemonkey85 opened this issue Dec 3, 2024 · 4 comments
Open

Feature Request: Support for "test mode" #211

codemonkey85 opened this issue Dec 3, 2024 · 4 comments
Assignees
Labels
Feature New features

Comments

@codemonkey85
Copy link
Contributor

codemonkey85 commented Dec 3, 2024

Every day's puzzle has some test input data (see https://adventofcode.com/2024/day/1 for instance). What I like to do (and what I assume everyone else does) is build my code around the test, checking that when I'm done, the output matches the provided test solution. However, what this library doesn't have is a way to directly support this pattern. What if we could hardcode the test data as a string in our Day code, and pass in a bool or something from the console to tell the app to use the test data instead of the input data?

For example:

namespace Advent2024.Days;

public sealed class Day01 : BaseDay
{
    private readonly string input = """
                                     3   4
                                     4   3
                                     2   5
                                     1   3
                                     3   9
                                     3   3
                                     """;

    public Day01(bool TestMode)
    {
        if (!TestMode)
        {
            input = File.ReadAllText(InputFilePath);
        }
    }

    public override ValueTask<string> Solve_1()
    {
        // Steps to solve part 1
        return new(string.Empty);
    }

    public override ValueTask<string> Solve_2()
    {
        // Steps to solve part 2
        return new(string.Empty);
    }
}
@eduherminio
Copy link
Owner

Let me give it a thought, because I rather provide facilitate doing this in a simple way using proper tests projects, instead of a boolean.

Meanwhile, right now you can already do something like this (note the two variants):

using NUnit.Framework;

namespace AoC_2024.Test;

public class Day_01 : AoC_2024.Day_01
{
    protected override string InputFileDirPath => "TestInputs";
}

public class AnyName : Day_02
{
    public override string InputFilePath => Path.Combine(
        "TestInputs",
        "02_Example.txt");
}

public static class ExampleTests
{
    [TestCase(typeof(Day_01), "11", "31")]
    [TestCase(typeof(AnyName), "2", "4")]
    public static async Task Test(Type type, string sol1, string sol2)
    {
        if (Activator.CreateInstance(type) is BaseDay instance)
        {
            await Assert.ThatAsync(async () => await instance.Solve_1(), Is.EqualTo(sol1));
            await Assert.ThatAsync(async () => await instance.Solve_2(), Is.EqualTo(sol2));
        }
        else
        {
            Assert.Fail($"{type} is not a BaseDay");
        }
    }
}

I'm aware that having to create another class per day just to override the input file dir path isn't ideal, of course.

@eduherminio eduherminio self-assigned this Dec 4, 2024
@eduherminio eduherminio added the Feature New features label Dec 4, 2024
@codemonkey85
Copy link
Contributor Author

codemonkey85 commented Dec 4, 2024

Worth noting: there can be different test data for part 1 vs part 2. See for instance: https://adventofcode.com/2024/day/3

Part 1 test data: xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
Part 2 test data: xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

EDIT: also, I am implementing your unit test suggestion in this year's Advent, so thanks for the tip! https://github.com/codemonkey85/Advent-of-Code-2024

@codemonkey85
Copy link
Contributor Author

codemonkey85 commented Dec 4, 2024

I'm sure you considered this already, but this could be done in a simple way, if there were a way to override the BaseDay instances' InputFileDirPath like this. No need for separate test classes then.

using Advent2024.Days;
using AoCHelper;

namespace Advent2024.Tests;

public static class Tests
{
    [TestCase(typeof(Day01), "TestInputs", "11", "31")]
    [TestCase(typeof(Day02), "TestInputs", "2", "4")]
    [TestCase(typeof(Day03), "TestInputs", "161", "")] // TODO: Need to account for different test data in part 2
    [TestCase(typeof(Day04), "TestInputs", "", "")]
    public static async Task Test(Type type, string inputFileDirPath, string sol1, string sol2)
    {
        if (Activator.CreateInstance(type) is BaseDay instance)
        {
            instance.InputFileDirPath = inputFileDirPath; // Set the input file directory path to the test input directory
            await Assert.ThatAsync(async () => await instance.Solve_1(), Is.EqualTo(sol1));
            await Assert.ThatAsync(async () => await instance.Solve_2(), Is.EqualTo(sol2));
        }
        else
        {
            Assert.Fail($"{type} is not a BaseDay");
        }
    }
}

@eduherminio
Copy link
Owner

Exactly, my code works in the current version, I provided it as a temporary alternative until this is solved properly.
As you noticed, as of now, InputFilePath only exposes a getter and InputFileDirPath only exposes a getter and is internal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New features
Projects
None yet
Development

No branches or pull requests

2 participants