-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement yaml metadata hook * Add conditional parsing * Carping our way out of errors. * Spacing * Small improvement on the way we return from the yaml metadata hook * Use Test2::Tools::Exception for catching die * Small update to Markdown::Perl::Options pod documentation
- Loading branch information
1 parent
bd9aba3
commit 724851e
Showing
5 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use strict; | ||
use warnings; | ||
use utf8; | ||
|
||
use Markdown::Perl 'convert', 'set_hooks'; | ||
use Test::More; | ||
use Test2::Tools::Warnings; | ||
use Test2::Tools::Exception; | ||
|
||
my $p = Markdown::Perl->new(); | ||
my $page = <<EOF; | ||
--- | ||
name: Mark is down | ||
draft: false | ||
number: 42 | ||
--- | ||
# Mark is down! | ||
I repeat: "Mark is down!" | ||
EOF | ||
|
||
my $invalid_page = <<EOF; | ||
--- | ||
name: Mark is down | ||
draft: false | ||
number: 42 | ||
--- | ||
# Mark is down! | ||
I repeat: "Mark is down!" | ||
EOF | ||
|
||
# Test 1: Check if we can get a string value | ||
{ | ||
sub hook_is_name_mark { | ||
my $x = shift; | ||
ok(exists($x->{name}) && $x->{name} eq 'Mark is down', "key 'name' was retrieved and validated as being 'Mark is down'"); | ||
} | ||
$p->set_hooks(yaml_metadata => \&hook_is_name_mark); | ||
$p->convert($page); | ||
} | ||
|
||
# Test 2: Validate that hook is not called if yaml is invalid | ||
{ | ||
my $hook_called = 0; | ||
sub hook_called { | ||
$hook_called = 1; | ||
} | ||
$p->set_hooks(yaml_metadata => \&hook_called); | ||
ok(!$hook_called, "Hook was not called because metadata was invalid."); | ||
$p->convert($invalid_page); | ||
} | ||
|
||
# Test 3: Validate that invalid yaml causes a carp() | ||
{ | ||
sub hook { | ||
} | ||
$p->set_hooks(yaml_metadata => \&hook); | ||
like(warning { $p->convert($invalid_page) }, qr/invalid/, "Got expected warning"); | ||
} | ||
|
||
# Test 4: What happens if inside the hook we die() | ||
{ | ||
sub hook_die { | ||
die "last words"; | ||
} | ||
$p->set_hooks(yaml_metadata => \&hook_die); | ||
like( dies { $p->convert($page) }, qr/last words/, "The hook correctly died."); | ||
} | ||
|
||
done_testing; |