The planner has its own set of checks. They use the same Planner.cs as the application with a different starting file, Checks.cs, because a program can have only one file of top-level statements. They never touch standard input. That's a benefit of keeping the rules in Planner: they can be tested without any input or output.
A healthy run prints Planner checks passed. A failure prints a named FAILED line and ends with exit code 1.
Records make checks easy
Because StudyTask and Summary compare by value, a single == checks every field at once. Compare one line of the check with what it would otherwise take. Predict these two results.
var actual = new Summary(2, 35, 10, 25);
Console.WriteLine(actual == new Summary(2, 35, 10, 25));
Console.WriteLine(actual == new Summary(2, 35, 10, 24));
record Summary(int Tasks, int Planned, int Completed, int Remaining);Check your prediction
True
FalseWhat the checks cover
Read Checks.cs and match each group to the brief.
- Parsing: valid lines, each boundary from both sides (0, 1, 180 and 181 minutes; 40 and 41 characters), and each kind of malformed line.
- Adding: a refused task isn't stored, and the 100-task limit holds on exactly task 101.
- Completing: a repeat changes nothing, and indexes -1 and "one past the end" are both refused.
- Summarizing: the empty plan, then a plan that mixes done and todo tasks.
Do the checks really work?
A check that can't fail is worse than none, since it gives false confidence. So test the tests: make a realistic mistake, and see whether anything notices. This is called mutation testing. Work through these in the Planner.cs tab above, restoring the code after each one.
- In
IsValid, changetask.Minutes >= 1totask.Minutes >= 0. - In
Add, changetasks.Count >= MaxTaskstotasks.Count > MaxTasks. - In
Complete, changeDone = truetoDone = !tasks[index].Done. - In
Summarize, changeif (task.Done)toif (true).
Each one should produce at least one FAILED line. This site's own build runs these and several more automatically, so a check that stops catching one of them is itself reported as a failure. Mutation 3 is worth a second look: it passes every check that completes a task only once. It takes the "completing twice" check to catch it.
Your turn
Checks aren't only for code that exists. Here, you write the check before the code, so the failure tells you exactly what to build.
Check a pending count
Write PendingCount(List<StudyTask> tasks), which returns the number of tasks not yet done. Write checks first, using the Check helper: an empty list gives 0, a mix of two todo and one done gives 2, and all done gives 0. Run them and see them fail against a method that always returns -1. Then write the real method. A correct run prints Pending checks passed.
Console.WriteLine(PendingCount(new List<StudyTask>()));
static int PendingCount(List<StudyTask> tasks)
{
return -1;
}
record StudyTask(string Title, int Minutes, bool Done);Hint 1: the idea
Only tasks with Done false add to the count. Think of the empty list as well as the normal case.
Hint 2: a first step
Copy the Check helper with its failures variable, and write the empty-list check first. It should fail against the supplied method.
Show one solution
int failures = 0;
Check(PendingCount(new List<StudyTask>()) == 0, "an empty list has none pending");
Check(PendingCount(new List<StudyTask>
{
new StudyTask("Read", 25, false),
new StudyTask("Review", 10, true),
new StudyTask("Practice", 40, false),
}) == 2, "counts only todo tasks");
Check(PendingCount(new List<StudyTask> { new StudyTask("Read", 25, true) }) == 0, "done tasks aren't pending");
if (failures == 0)
{
Console.WriteLine("Pending checks passed");
}
void Check(bool passed, string name)
{
if (!passed)
{
Console.WriteLine($"FAILED: {name}");
failures++;
}
}
static int PendingCount(List<StudyTask> tasks)
{
int pending = 0;
foreach (StudyTask task in tasks)
{
if (!task.Done)
{
pending++;
}
}
return pending;
}
record StudyTask(string Title, int Minutes, bool Done);Pending checks passedThe checks cover an empty list, a mixture, and a list where every task is done, so a method that counted all tasks, or the done ones, would fail at least one. Check prints only failures, so the single line at the end appears only when all three pass.
Mutate your own solution: change !task.Done to task.Done. How many of your checks fail? Would one check have been enough to catch it?
Completion is saved in this browser only. It does not measure mastery.