1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
use test;
use markdown;
use lang:bs:macro;
class CountVisitor extends Visitor {
Nat count;
core:lang:Type type;
init(core:lang:Type t) {
init { type = t; }
}
ElementSeq visit(ElementSeq seq) {
if (core:lang:typeOf(seq) is type)
count++;
seq;
}
Element visit(Element element) {
if (core:lang:typeOf(element) is type)
count++;
element;
}
FormattedText visit(FormattedText text) {
if (core:lang:typeOf(text) is type)
count++;
text;
}
TextSpan visit(TextSpan span) {
if (core:lang:typeOf(span) is type)
count++;
span;
}
}
Nat countType(Document d, core:lang:Type t) {
CountVisitor x(t);
d.visit(x);
x.count;
}
test BasicMarkdown {
var simple = str {
this is a
paragraph
this is the
next *paragraph*
with four
lines
single line
this is a heading
=================
### deep heading
this is another paragraph
};
Document result = parse(simple);
check result.elements.elements.count == 6;
check result.countType(named{Paragraph}) == 4;
check result.countType(named{Heading}) == 2;
// print("Result:");
// print(result.toS);
// print("\nHTML:");
// print(result.toHtml.toS);
}
test MarkdownLists {
var nested = str {
- First
- Second
- Third
- Fourth
};
{
Document result = parse(nested);
check result.countType(named{List}) == 2;
// print("Result:");
// print(result.toHtml.toS);
}
var lists = str {
Heading
=======
This is a short list:
- First element
end of list
This is a list:
- First element
- Second element
with additional text
- Third element
- Nested list
- Second element
with additional text
- Fourth element
1. Number first
2. number second
};
{
Document result = parse(lists);
check result.countType(named{List}) == 4;
// print("Result:");
// print(result.toHtml.toS);
}
}
test MarkdownCode {
var code = str {
Paragraph
```c++
inline code in C++
has two lines
```
another with `inline code`!
- In a list:
```java
test code
second line
```
- Next element
};
Document result = parse(code);
check result.countType(named{CodeBlock}) == 2;
check result.countType(named{Paragraph}) == 4;
check result.countType(named{InlineCode}) == 1;
check result.countType(named{List}) == 1;
// print(result.toHtml.toS);
}
test MarkdownCodeNoSpace {
var code = str {
Paragraph
```c++
inline code
```
END
};
Document result = parse(code);
check result.countType(named{CodeBlock}) == 1;
check result.countType(named{Paragraph}) == 2;
}
test MarkdownMultilineMarkup {
var code = str {
test *multiline
bold* text and `multiline
code` in paragraphs
- list item
test [multiline
link text](linktarget)
in the paragraph and `multiline
code` in a list paragraph
};
Document result = parse(code);
check result.countType(named{Paragraph}) == 3;
check result.countType(named{ItalicText}) == 1;
check result.countType(named{InlineCode}) == 2;
check result.countType(named{Link}) == 1;
}
|