| 1 |
use pretty_assertions::assert_eq; |
| 2 |
|
| 3 |
use crate::{Arena, Options, format_commonmark, parse_document}; |
| 4 |
|
| 5 |
use super::*; |
| 6 |
|
| 7 |
#[test] |
| 8 |
fn round_trip_one_field() { |
| 9 |
let mut options = Options::default(); |
| 10 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 11 |
let arena = Arena::new(); |
| 12 |
let input = "---\nlayout: post\n---\nText\n"; |
| 13 |
let root = parse_document(&arena, input, &options); |
| 14 |
let mut buf = String::new(); |
| 15 |
format_commonmark(root, &options, &mut buf).unwrap(); |
| 16 |
assert_eq!(buf, input); |
| 17 |
} |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn round_trip_wide_delimiter() { |
| 21 |
let mut options = Options::default(); |
| 22 |
options.extension.front_matter_delimiter = Some("\u{04fc}".to_owned()); |
| 23 |
let arena = Arena::new(); |
| 24 |
let input = "\u{04fc}\nlayout: post\n\u{04fc}\nText\n"; |
| 25 |
let root = parse_document(&arena, input, &options); |
| 26 |
let mut buf = String::new(); |
| 27 |
format_commonmark(root, &options, &mut buf).unwrap(); |
| 28 |
assert_eq!(buf, input); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn ast() { |
| 33 |
assert_ast_match!( |
| 34 |
[extension.front_matter_delimiter = Some("q".to_owned())], |
| 35 |
"q\nlayout: post\nq\nText\n", |
| 36 |
(document (1:1-4:4) [ |
| 37 |
(frontmatter (1:1-3:1) "q\nlayout: post\nq\n") |
| 38 |
(paragraph (4:1-4:4) [ |
| 39 |
(text (4:1-4:4) "Text") |
| 40 |
]) |
| 41 |
]) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn ast_blank_line() { |
| 47 |
assert_ast_match!( |
| 48 |
[extension.front_matter_delimiter = Some("---".to_owned())], |
| 49 |
r#"--- |
| 50 |
a: b |
| 51 |
--- |
| 52 |
|
| 53 |
hello world |
| 54 |
"#, |
| 55 |
(document (1:1-5:11) [ |
| 56 |
(frontmatter (1:1-3:3) "---\na: b\n---\n\n") |
| 57 |
(paragraph (5:1-5:11) [ |
| 58 |
(text (5:1-5:11) "hello world") |
| 59 |
]) |
| 60 |
]) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn ast_carriage_return() { |
| 66 |
assert_ast_match!( |
| 67 |
[extension.front_matter_delimiter = Some("q".to_owned())], |
| 68 |
"q\r\nlayout: post\r\nq\r\nText\r\n", |
| 69 |
(document (1:1-4:4) [ |
| 70 |
(frontmatter (1:1-3:1) "q\r\nlayout: post\r\nq\r\n") |
| 71 |
(paragraph (4:1-4:4) [ |
| 72 |
(text (4:1-4:4) "Text") |
| 73 |
]) |
| 74 |
]) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn ast_wide_delimiter() { |
| 80 |
assert_ast_match!( |
| 81 |
[extension.front_matter_delimiter = Some("\u{04fc}".to_owned())], |
| 82 |
"\u{04fc}\nlayout: post\n\u{04fc}\nText\n", |
| 83 |
(document (1:1-4:4) [ |
| 84 |
(frontmatter (1:1-3:2) "\u{04fc}\nlayout: post\n\u{04fc}\n") |
| 85 |
(paragraph (4:1-4:4) [ |
| 86 |
(text (4:1-4:4) "Text") |
| 87 |
]) |
| 88 |
]) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn trailing_space_open() { |
| 94 |
let input = "--- \nlayout: post\n---\nText\n"; |
| 95 |
|
| 96 |
let mut options = Options::default(); |
| 97 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 98 |
let arena = Arena::new(); |
| 99 |
let root = parse_document(&arena, input, &options); |
| 100 |
|
| 101 |
let found = root |
| 102 |
.descendants() |
| 103 |
.find(|n| node_matches!(n, NodeValue::FrontMatter(..))); |
| 104 |
|
| 105 |
assert!(found.is_none(), "no FrontMatter expected"); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn leading_space_open() { |
| 110 |
let input = " ---\nlayout: post\n---\nText\n"; |
| 111 |
|
| 112 |
let mut options = Options::default(); |
| 113 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 114 |
let arena = Arena::new(); |
| 115 |
let root = parse_document(&arena, input, &options); |
| 116 |
|
| 117 |
let found = root |
| 118 |
.descendants() |
| 119 |
.find(|n| node_matches!(n, NodeValue::FrontMatter(..))); |
| 120 |
|
| 121 |
assert!(found.is_none(), "no FrontMatter expected"); |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn leading_space_close() { |
| 126 |
let input = "---\nlayout: post\n ---\nText\n"; |
| 127 |
|
| 128 |
let mut options = Options::default(); |
| 129 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 130 |
let arena = Arena::new(); |
| 131 |
let root = parse_document(&arena, input, &options); |
| 132 |
|
| 133 |
let found = root |
| 134 |
.descendants() |
| 135 |
.find(|n| node_matches!(n, NodeValue::FrontMatter(..))); |
| 136 |
|
| 137 |
assert!(found.is_none(), "no FrontMatter expected"); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn trailing_space_close() { |
| 142 |
let input = "---\nlayout: post\n--- \nText\n"; |
| 143 |
|
| 144 |
let mut options = Options::default(); |
| 145 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 146 |
let arena = Arena::new(); |
| 147 |
let root = parse_document(&arena, input, &options); |
| 148 |
|
| 149 |
let found = root |
| 150 |
.descendants() |
| 151 |
.find(|n| node_matches!(n, NodeValue::FrontMatter(..))); |
| 152 |
|
| 153 |
assert!(found.is_none(), "no FrontMatter expected"); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn second_line() { |
| 158 |
let input = "\n---\nlayout: post\n ---\nText\n"; |
| 159 |
|
| 160 |
let mut options = Options::default(); |
| 161 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 162 |
let arena = Arena::new(); |
| 163 |
let root = parse_document(&arena, input, &options); |
| 164 |
|
| 165 |
let found = root |
| 166 |
.descendants() |
| 167 |
.find(|n| node_matches!(n, NodeValue::FrontMatter(..))); |
| 168 |
|
| 169 |
assert!(found.is_none(), "no FrontMatter expected"); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn fm_only_with_trailing_newline() { |
| 174 |
let input = "---\nfoo: bar\n---\n"; |
| 175 |
|
| 176 |
let mut options = Options::default(); |
| 177 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 178 |
let arena = Arena::new(); |
| 179 |
let root = parse_document(&arena, input, &options); |
| 180 |
|
| 181 |
let found = root |
| 182 |
.descendants() |
| 183 |
.find(|n| node_matches!(n, NodeValue::FrontMatter(..))); |
| 184 |
|
| 185 |
assert!(found.is_some(), "front matter expected"); |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn fm_only_without_trailing_newline() { |
| 190 |
let input = "---\nfoo: bar\n---"; |
| 191 |
|
| 192 |
let mut options = Options::default(); |
| 193 |
options.extension.front_matter_delimiter = Some("---".to_owned()); |
| 194 |
let arena = Arena::new(); |
| 195 |
let root = parse_document(&arena, input, &options); |
| 196 |
|
| 197 |
let found = root |
| 198 |
.descendants() |
| 199 |
.find(|n| node_matches!(n, NodeValue::FrontMatter(..))); |
| 200 |
|
| 201 |
assert!(found.is_some(), "front matter expected"); |
| 202 |
} |