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

feat: test file generator #11

Merged
merged 7 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: integrate test file generation
  • Loading branch information
S-ecki committed Dec 2, 2023
commit 99dea9dc888262109d4fd1ea23df0386d24e75f4
72 changes: 72 additions & 0 deletions day_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ void main(List<String?> args) async {
File('solutions/$dayFileName').writeAsString(dayTemplate(dayNumber)),
);

// Create test file
final testFileName = 'day${dayNumber}_test.dart';
unawaited(
File('test/$testFileName').writeAsString(_testTemplate(dayNumber)),
);

final exportFile = File('solutions/index.dart');
final exports = exportFile.readAsLinesSync();
final content = "export 'day$dayNumber.dart';\n";
Expand Down Expand Up @@ -105,3 +111,69 @@ class Day$dayNumber extends GenericDay {

''';
}

String _testTemplate(String day) {
return '''
import 'package:test/test.dart';

import '../solutions/day$day.dart';

// *******************************************************************
// Fill out the variables below according to the puzzle description!
// The test code should usually not need to be changed.
// *******************************************************************

/// Paste in the small example that is given for the FIRST PART of the puzzle.
/// It will be evaluated again the `_exampleSolutionPart1` below.
const _exampleInput1 = \'''
\''';

/// Paste in the small example that is given for the SECOND PART of the puzzle.
/// It will be evaluated against the `_exampleSolutionPart2` below.
///
/// In case the second part uses the same example, uncomment below line instead:
// const _exampleInput2 = _exampleInput;
const _exampleInput2 = \'''
\''';

/// The solution for the FIRST PART's example, which is given by the puzzle.
const _exampleSolutionPart1 = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a different value than 0, Like -1 or null? The tests would pass by default since the default return value is 0 as well. Not sure if this is the desired behaviour

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would indeed pass at the start, which might not be great from a purist POV.
But as this will not pass as soon as the user adds the actual solution from the example (or adds his solution) - and I expect them to do this when they use the test - I think it is ok.
An even bigger case for me to leave it as is: The examplePart2 will not be distracting/annoying (red 😅) until you acutally start to work at it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍


/// The solution for the SECOND PART's example, which is given by the puzzle.
const _exampleSolutionPart2 = 0;

/// The actual solution for the FIRST PART of the puzzle, based on your input.
/// This can only be filled out after you have solved the puzzle and is used
/// for regression testing when refactoring.
const _puzzleSolutionPart1 = 0;

/// The actual solution for the SECOND PART of the puzzle, based on your input.
/// This can only be filled out after you have solved the puzzle and is used
/// for regression testing when refactoring.
const _puzzleSolutionPart2 = 0;

void main() {
group(
'Day $day - Example Input',
() {
test('Part 1', () {
final day = Day$day()..inputForTesting = _exampleInput1;
expect(day.solvePart1(), _exampleSolutionPart1);
});
test('Part 2', () {
final day = Day$day()..inputForTesting = _exampleInput2;
expect(day.solvePart2(), _exampleSolutionPart2);
});
},
);
group(
'Day $day - Puzzle Input',
() {
final day = Day$day();
test('Part 1', () => expect(day.solvePart1(), _puzzleSolutionPart1));
test('Part 2', () => expect(day.solvePart2(), _puzzleSolutionPart2));
},
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep the Puzzle Input group commented out by default to prevent the tests from failing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Else the test suite would still fail until you provide the _puzzleSolutionPart1 and _puzzleSolutionPart2 variables and make it less clear that your solution worked.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point! Pushed the changes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could be a bit more "smart" about it and skip the test if the solution hasn't been filled in:

const _puzzleSolutionPart1 = null;
const _puzzleSolutionPart2 = null;

group(
    'Day 01 - Puzzle Input',
    () {
      final day = Day01();
      test(
        'Part 1',
        skip: _puzzleSolutionPart1 == null
            ? 'Skipped because _puzzleSolutionPart1 is null'
            : false,
        () => expect(day.solvePart1(), _puzzleSolutionPart1),
      );
      test(
        'Part 1',
        skip: _puzzleSolutionPart2 == null
            ? 'Skipped because _puzzleSolutionPart2 is null'
            : false,
        () => expect(day.solvePart1(), _puzzleSolutionPart2),
      );
    },
  );

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is perfect! I will add and commit it! 🚀
Was also thinking about skipping the tests without touching the actual test code, but stopped at the thought of a shouldTest boolean set by the user 😅
Thanks a lot! 💙

}
''';
}
10 changes: 9 additions & 1 deletion utils/generic_day.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:meta/meta.dart';
import 'package:timing/timing.dart';

import 'input_util.dart';
Expand All @@ -10,7 +11,14 @@ typedef SolutionWithDuration = (int, Duration);
abstract class GenericDay {
GenericDay(this.day) : input = InputUtil(day);
final int day;
final InputUtil input;
InputUtil input;

/// This setter must only be used to mutate the input of an existing day
/// implementation for testing purposes.
@visibleForTesting
// ignore: avoid_setters_without_getters
set inputForTesting(String example) =>
input = InputUtil.fromMultiLineString(example);

dynamic parseInput();
int solvePart1();
Expand Down