Test the CSV Record Parser
Hard About 35 min Free, no account needed
You're given a correct CSV record parser. Write tests that pin quoting, empty fields, whitespace, and malformed input.
The brief
This parser reads one CSV record and returns its fields. The implementation is already written and correct. Your job is to write the tests that prove it. A happy-path row with two plain fields does not exercise the transitions where CSV parsers usually go wrong.
Here is the public surface:
public class CsvRecordParser
{
public string[] Parse(string line) { ... }
}The contract is deliberately small and exact:
- A comma separates unquoted fields. Empty input is one empty field, and leading, adjacent, and trailing empty fields are preserved.
- Whitespace is data. The parser never trims an unquoted field or the contents of a quoted field.
- A field is quoted only when its first character is a quote. Inside that field, commas are data and two consecutive quotes decode to one literal quote.
- After a quoted field closes, only a comma or the end of the record may follow.
- An unterminated quoted field, a quote inside an unquoted field, or any junk after a closing quote throws System.FormatException.
These examples show the important shapes:
alpha,beta -> ["alpha", "beta"]
,alpha,, -> ["", "alpha", "", ""]
"north,west",ok -> ["north,west", "ok"]
"She said ""hello""",done -> ["She said \"hello\"", "done"]Write a Tests class. Each public, parameterless method is one test: it passes if it runs without throwing and fails when an assertion throws. A tiny assertion library is in scope with no using needed:
Assert.True(condition); Assert.False(condition);
Assert.Equal(expected, actual); Assert.NotEqual(a, b);
Assert.Null(x); Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);Assert.Equal compares arrays by identity, not by their elements. For each parsed record, assert its Length and the fields at the indexes that matter. Reference exceptions by their full name:
Assert.Throws<System.FormatException>(() => new CsvRecordParser().Parse("\"unfinished"));Your suite is run twice over:
- Against the correct CsvRecordParser above, every one of your tests must pass.
- Against a set of planted-bug mutants, broken parsers you never see. A mutant is caught when at least one of your tests fails against it.
You pass when your suite is green on the correct code and catches at least 5 of the 6 mutants. The mutants split quoted data, discard or normalize fields, mishandle doubled quotes, or accept malformed quoting. Choose inputs that isolate each rule, and use Assert.Throws for invalid records. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the CsvRecordParser class. Each public, parameterless method is one test; it
// passes if it does not throw. Make your suite pass on the correct code AND catch the hidden buggy
// versions. Assert a returned array through its Length and indexed fields, not by array identity.
public class Tests
{
public void Parses_two_plain_fields()
{
var fields = new CsvRecordParser().Parse("alpha,beta");
Assert.Equal(2, fields.Length);
Assert.Equal("alpha", fields[0]);
Assert.Equal("beta", fields[1]);
}
// TODO: cover empty fields, whitespace, quoted commas, doubled quotes, and malformed quoting.
}How it is graded
You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.
What it practices
- Equivalence partitioning
- Cover each class of input and every branch with one deliberate case.
- Error path testing
- Prove exceptions, retries and rejected inputs behave as specified.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Related katas
- Test the Bowling Scorer Hard Free You're given a correct ten-pin bowling scorer. Write the tests that exercise spares, strikes, and the bonus rolls.
- Test the Progressive Tax Hard Free You're given a correct marginal income-tax calculator. Write the tests that prove each band is taxed marginally, not flatly.
- Test the Interval Merger Hard Free You're given a correct interval merger. Write the tests that pin overlap, touching, containment, and sorting.
- Test the Luhn Checksum Hard Free You're given a correct Luhn card-number validator. Write the tests that pin the doubling, the digit reduction, and the rejects.