Refactor test cases of the Markua Parser in Perl 5
Earlier I've mentined that I don't like the fact that we used copy-paste in the test script and duplicate code. Before we go on and add more test, let's refactor the test code.
I've created an array called @cases listing all the (2) test cases. We'll have more.
- my @cases = ('heading1', 'headers');
I've also changed the code processing the test cases to be in a loop and use the values from the @cases array:
- for my $case (@cases) {
- my $result = $m->parse_file("t/input/$case.md");
- is_deeply $result, decode_json( path("t/dom/$case.json")->slurp_utf8 );
- }
Finally, though in the code it is earlier, I've updated the plan to be based on the number of entries in the @cases array.
The previous test file is here:
examples/markua-parser/c580d63/t/01-test.t
- use strict;
- use warnings;
- use Test::More;
- use JSON::MaybeXS qw(decode_json);
- use Path::Tiny qw(path);
- use Markua::Parser;
- plan tests => 3;
- my $m = Markua::Parser->new;
- isa_ok $m, 'Markua::Parser';
- my $result = $m->parse_file('t/input/heading1.md');
- is_deeply $result, decode_json( path('t/dom/heading1.json')->slurp_utf8 );
- $result = $m->parse_file('t/input/headers.md');
- is_deeply $result, decode_json( path('t/dom/headers.json')->slurp_utf8 );
The new test file is here:
examples/markua-parser/79d9bfe/t/01-test.t
- use strict;
- use warnings;
- use Test::More;
- use JSON::MaybeXS qw(decode_json);
- use Path::Tiny qw(path);
- use Markua::Parser;
- my @cases = ('heading1', 'headers');
- plan tests => 1 + scalar @cases;
- my $m = Markua::Parser->new;
- isa_ok $m, 'Markua::Parser';
- for my $case (@cases) {
- my $result = $m->parse_file("t/input/$case.md");
- is_deeply $result, decode_json( path("t/dom/$case.json")->slurp_utf8 );
- }
We can then record our changes in git:
$ git add . $ git commit -m "refactor test code" $ git push

Published on 2020-01-27