-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 5 commits
99dea9d
a4654af
7eb1ced
e4a36ea
24381db
c8189ab
33bd36e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,17 @@ void main(List<String?> args) async { | |
// Create lib file | ||
final dayFileName = 'day$dayNumber.dart'; | ||
unawaited( | ||
File('solutions/$dayFileName').writeAsString(dayTemplate(dayNumber)), | ||
File('solutions/$dayFileName') | ||
.create(recursive: true) | ||
.then((file) => file.writeAsString(_dayTemplate(dayNumber!))), | ||
); | ||
|
||
// Create test file | ||
final testFileName = 'day${dayNumber}_test.dart'; | ||
unawaited( | ||
File('test/$testFileName') | ||
.create(recursive: true) | ||
.then((file) => file.writeAsString(_testTemplate(dayNumber!))), | ||
); | ||
|
||
final exportFile = File('solutions/index.dart'); | ||
|
@@ -78,7 +88,7 @@ void main(List<String?> args) async { | |
print('All set, Good luck!'); | ||
} | ||
|
||
String dayTemplate(String dayNumber) { | ||
String _dayTemplate(String dayNumber) { | ||
return ''' | ||
import '../utils/index.dart'; | ||
|
||
|
@@ -105,3 +115,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; | ||
|
||
/// 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)); | ||
}, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we keep the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Else the test suite would still fail until you provide the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point! Pushed the changes There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is perfect! I will add and commit it! 🚀 |
||
} | ||
'''; | ||
} |
There was a problem hiding this comment.
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
ornull
? The tests would pass by default since the default return value is0
as well. Not sure if this is the desired behaviourThere was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point 👍