Skip to content
Browse files

[t/text.t] added tests

And made appropriate changes in the module.
  • Loading branch information...
1 parent f269131 commit 5d41601916ca1a9b3233e2f8090ef3838c4fa8d6 @masak committed Apr 21, 2012
Showing with 103 additions and 4 deletions.
  1. +8 −4 lib/Text/Markdown.pm
  2. +95 −0 t/text.t
View
12 lib/Text/Markdown.pm
@@ -1,6 +1,10 @@
module Text::Markdown;
-class Text {
+class Document {
+ has @.children;
+}
+
+class Para {
has $.text;
has @.children;
}
@@ -131,7 +135,7 @@ sub extract_tspans($text) {
grammar Markdown {
token TOP {
^ <paragraph>* % [\n\n+] $
- { make $<paragraph>».ast }
+ { make Document.new(:children($<paragraph>».ast)) }
}
token paragraph {
@@ -144,11 +148,11 @@ grammar Markdown {
my @children = extract_tspans($text);
- make Text.new(:$text, :@children);
+ make Para.new(:$text, :@children);
}
}
}
sub parse-markdown(Cool $text) is export {
- Markdown.parse($text).ast.list;
+ Markdown.parse($text).ast;
}
View
95 t/text.t
@@ -0,0 +1,95 @@
+use v6;
+use Test;
+use Text::Markdown;
+
+{
+ my $doc = parse-markdown "One paragraph";
+
+ is +$doc.children, 1, "Only one paragraph";
+}
+
+{
+ my $doc = parse-markdown "One slide.\n\nTwo paragraphs.";
+
+ is +$doc.children, 2, "The slide has two elements";
+}
+
+{
+ my $doc = parse-markdown "One slide with *italics* in it.";
+ my $para = $doc.children[0];
+
+ isa_ok $para, Text::Markdown::Para;
+ is +$para.children, 3, "The text has three elements";
+ is $para.children[0].text, "One slide with ", 'correct 1/3 tspan';
+ is $para.children[1].text, "italics", 'correct 2/3 tspan';
+ is $para.children[2].text, " in it.", 'correct 3/3 tspan';
+ is $para.children[0].font-style, "", 'correct 1/3 font-style';
+ is $para.children[1].font-style, "italics", 'correct 2/3 font-style';
+ is $para.children[2].font-style, "", 'correct 3/3 font-style';
+}
+
+{
+ my $doc = parse-markdown "One slide with **bold** in it.";
+ my $para = $doc.children[0];
+
+ isa_ok $para, Text::Markdown::Para;
+ is +$para.children, 3, "The text has three elements";
+ is $para.children[0].text, "One slide with ", 'correct 1/3 tspan';
+ is $para.children[1].text, "bold", 'correct 2/3 tspan';
+ is $para.children[2].text, " in it.", 'correct 3/3 tspan';
+ is $para.children[0].font-weight, "", 'correct 1/3 font-weight';
+ is $para.children[1].font-weight, "bold", 'correct 2/3 font-weight';
+ is $para.children[2].font-weight, "", 'correct 3/3 font-weight';
+}
+
+{
+ my $doc = parse-markdown "Both *beer* and **cheesecake**!?";
+ my $para = $doc.children[0];
+
+ is +$para.children, 5, "handles italic and bold combined";
+}
+
+{
+ my $doc = parse-markdown "Now we use both *italics* and **bold** ***and*** combine them.";
+ my $para = $doc.children[0];
+
+ isa_ok $para, Text::Markdown::Para;
+ is +$para.children, 7, "The text has seven elements";
+ is $para.children[0].text, "Now we use both ", 'correct 1/7 tspan';
+ is $para.children[1].text, "italics", 'correct 2/7 tspan';
+ is $para.children[2].text, " and ", 'correct 3/7 tspan';
+ is $para.children[3].text, "bold", 'correct 4/7 tspan';
+ is $para.children[4].text, " ", 'correct 5/7 tspan';
+ is $para.children[5].text, "and", 'correct 6/7 tspan';
+ is $para.children[6].text, " combine them.", 'correct 7/7 tspan';
+ is $para.children[0].font-style, "", 'correct 1/7 font-style';
+ is $para.children[1].font-style, "italics", 'correct 2/7 font-style';
+ is $para.children[2].font-style, "", 'correct 3/7 font-style';
+ is $para.children[3].font-style, "", 'correct 4/7 font-style';
+ is $para.children[4].font-style, "", 'correct 5/7 font-style';
+ is $para.children[5].font-style, "italics", 'correct 6/7 font-style';
+ is $para.children[6].font-style, "", 'correct 7/7 font-style';
+ is $para.children[0].font-weight, "", 'correct 1/7 font-weight';
+ is $para.children[1].font-weight, "", 'correct 2/7 font-weight';
+ is $para.children[2].font-weight, "", 'correct 3/7 font-weight';
+ is $para.children[3].font-weight, "bold", 'correct 4/7 font-weight';
+ is $para.children[4].font-weight, "", 'correct 5/7 font-weight';
+ is $para.children[5].font-weight, "bold", 'correct 6/7 font-weight';
+ is $para.children[6].font-weight, "", 'correct 7/7 font-weight';
+}
+
+{
+ my $doc = parse-markdown "This text contains `code` written in Perl 6.";
+ my $para = $doc.children[0];
+
+ isa_ok $para, Text::Markdown::Para;
+ is +$para.children, 3, "The text has 3 elements.";
+ is $para.children[0].text, "This text contains ", 'correct 1/3 tspan';
+ is $para.children[1].text, "code", 'correct 2/3 tspan';
+ is $para.children[2].text, " written in Perl 6.", 'correct 3/3 tspan';
+ is $para.children[0].font-family, '', 'correct 1/3 font-family';
+ is $para.children[1].font-family, 'monospace', 'correct 2/3 font-family';
+ is $para.children[2].font-family, '', 'correct 3/3 font-family';
+}
+
+done;

0 comments on commit 5d41601

Please sign in to comment.
Something went wrong with that request. Please try again.