From c2fdd8bd5db4727baad50bbdb39a4d2224dc8bac Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 25 Jun 2019 17:02:49 +0200 Subject: [PATCH] document and test first-code-block #1 #3 --- README.md | 24 ++++++++++++++++++++++++ lib/Pod/Utilities.pm6 | 4 +++- t/03-first-code-block.t | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 t/03-first-code-block.t diff --git a/README.md b/README.md index 122de59..e6fee18 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,30 @@ use Pod::Utilities; Pod::Utilities is a set of tools to deal with Pod elements. It lets you create several Pod objects, obtain gists and modify headings. +### sub first-code-block + +```perl6 +sub first-code-block ( + Array @pod +) returns Str; +``` + +Returns the first Pod::Block::Code found in an array, concatenating all lines in it. +If any is found, it will return an empty string. + +Example: + +```perl6 +=being pod + say "some code"; + say "more code"; +=end pod + +first-code-block($=pod[0].contents) + +# OUTPUT «say "some code";␤say "more code";␤» +``` + # AUTHOR Alexander Mouquin <@Mouq> diff --git a/lib/Pod/Utilities.pm6 b/lib/Pod/Utilities.pm6 index bc6ff98..070a501 100644 --- a/lib/Pod/Utilities.pm6 +++ b/lib/Pod/Utilities.pm6 @@ -32,8 +32,10 @@ sub pod-gist(Pod::Block $pod, $level = 0) is export { @chunks.join; } +#| Returns the first Pod::Block::Code found in an array, concatenating +#| all lines in it. If any is found, it will return an empty string. sub first-code-block(@pod) is export { - @pod.first(* ~~ Pod::Block::Code).contents.grep(Str).join; + @pod.first(* ~~ Pod::Block::Code).contents.grep(Str).join || ""; } sub pod-with-title($title, *@blocks) is export { diff --git a/t/03-first-code-block.t b/t/03-first-code-block.t new file mode 100644 index 0000000..8a20eb2 --- /dev/null +++ b/t/03-first-code-block.t @@ -0,0 +1,19 @@ +use v6.c; +use Test; + +plan *; + +use Pod::Utilities; + + +=begin pod + say "this code rocks."; + say "Perl6" ~ "is cool." +=end pod + +my $code = $=pod[0].contents[0].contents[0]; + +is first-code-block($=pod.first.contents), $code.join, "Basic test"; +is first-code-block([]) , "", "Returns empty string"; + +done-testing; \ No newline at end of file