Skip to content

Commit 040d51e

Browse files
committed
bq
tests accept para after bq
1 parent 9c76da8 commit 040d51e

File tree

2 files changed

+187
-2
lines changed

2 files changed

+187
-2
lines changed

src/commitmsgfmt.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ impl CommitMsgFmt {
2828
let mut buf = String::new();
2929
for tok in msg {
3030
match *tok {
31-
Comment(ref s) | Literal(ref s) | Scissored(ref s) | Trailer(ref s) => {
31+
BlockQuote(ref s) | Comment(ref s) | Literal(ref s) | Scissored(ref s)
32+
| Trailer(ref s) => {
3233
buf.push_str(s);
3334
}
3435
ListItem(ref indent, ref li, ref s) => {
@@ -207,6 +208,70 @@ foo
207208
assert_eq!(filter(72, &input), expected);
208209
}
209210

211+
#[test]
212+
fn preserves_block_quote() {
213+
let input = "
214+
foo
215+
216+
> block quote
217+
paragraph
218+
";
219+
220+
let expected = input;
221+
222+
assert_eq!(filter(72, &input), expected);
223+
}
224+
225+
#[test]
226+
fn preserves_block_quote_with_attribution() {
227+
let input = "
228+
foo
229+
230+
author wrote:
231+
> block quote
232+
paragraph
233+
";
234+
235+
let expected = input;
236+
237+
assert_eq!(filter(72, &input), expected);
238+
}
239+
240+
#[test]
241+
fn parses_block_quote_with_preceding_attribution_as_unspaced_paragraph_and_literal() {
242+
let input = "
243+
xxx xxxx xxxxx xxxxxxxx xxx xxxx xxxxxxxxxxxxxxxx xxxxx
244+
245+
xxxxxxx xxxxxxxxx xxxxxxxx xxx xxxxxxxxxx
246+
> xxxxxxx xxx xxxx xxxxxx xx xxx xxxxxxxxx xxxxx xxx xxxx xxxx
247+
> xxxx xxxxxxx xxxx xxx xxxxxxxxx
248+
>
249+
> xx xxxxx xxxxxxx xxxxxxx xxxxxxxx xxxxxxx xxxxxx xx
250+
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx xx
251+
> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxx xxxxx
252+
> xxxxxxxxxxxxxxxxxxxxxxxx xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx xxx
253+
>
254+
> xxxx xxxx xxxxxxxx xxxx xxx xxxxxxxx x xxxxxx xx xxxxx
255+
>
256+
> xxxxxxxxx xxx xxxx xxxx xxxxxxxxxxxxx
257+
> xxx xxxxxxxx xxxx xxxx xx xxx xxxx xx xxx xxxx xxxx xxxxxxxxxxx
258+
> xxx xxxx xxxx xxxx
259+
> x xxxxxxx xxx xxxxx xxxx xx xxxx xxxxx
260+
> xx xxxxxxxxxx xx xxx xxxxxxxx xxxx xx xxx xxxx
261+
> xx xxxxxxx xxxx
262+
> xx xxxxxx xx xxxx
263+
> x xxxx xxxxxxxx x xxxxxx
264+
265+
xxxxx xxxx xxx xxxxxxxx xxx xxxx x xxxxxxx xx xx xxxxxxxxxxx
266+
xxxxx xxxxxxxx xx xxx xxx xx xxxxxxxx xxx xxxx xxxx xxx
267+
xxxxxxxxx xxxxxxxxxxx xx xxxxxx
268+
";
269+
270+
let expected = input;
271+
272+
assert_eq!(filter(60, &input), expected);
273+
}
274+
210275
#[test]
211276
fn formats_footnotes() {
212277
let msg = "

src/parser.rs

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub enum Token<'input> {
1717
Subject(String),
1818
Scissored(&'input str),
1919
Trailer(&'input str),
20+
BlockQuote(&'input str),
2021
VerticalSpace,
2122
}
2223

@@ -83,7 +84,13 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
8384
toks.push(Token::Trailer(line));
8485
} else if let Some(y) = match toks.last_mut() {
8586
Some(&mut Token::Footnote(_, ref mut b)) => extend_prose_buffer_with_line(b, line),
86-
Some(&mut Token::Paragraph(ref mut b)) => extend_prose_buffer_with_line(b, line),
87+
Some(&mut Token::Paragraph(ref mut b)) => {
88+
if is_line_block_quote(line) {
89+
Some(Token::BlockQuote(line))
90+
} else {
91+
extend_prose_buffer_with_line(b, line)
92+
}
93+
}
8794
Some(&mut Token::ListItem(_, _, ref mut b)) => {
8895
if list_item.is_match(line) {
8996
Some(list_item_from_line(&list_item, line))
@@ -96,6 +103,8 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
96103
Some(list_item_from_line(&list_item, line))
97104
} else if is_line_indented(line) {
98105
Some(Token::Literal(line))
106+
} else if is_line_block_quote(line) {
107+
Some(Token::BlockQuote(line))
99108
} else {
100109
px = false;
101110
Some(Token::Paragraph(line.trim().to_owned()))
@@ -209,6 +218,10 @@ fn is_line_footnote(line: &str) -> bool {
209218
false
210219
}
211220

221+
fn is_line_block_quote(line: &str) -> bool {
222+
line.starts_with('>')
223+
}
224+
212225
#[cfg(test)]
213226
mod tests {
214227
use super::Token::*;
@@ -513,6 +526,113 @@ some other paragraph
513526
);
514527
}
515528

529+
#[test]
530+
fn parses_block_quote_verbatim() {
531+
assert_eq!(
532+
parse(
533+
"
534+
some subject
535+
536+
some paragraph
537+
538+
> some block quote
539+
540+
some other paragraph
541+
"
542+
),
543+
[
544+
VerticalSpace,
545+
Subject("some subject".to_owned()),
546+
VerticalSpace,
547+
Paragraph("some paragraph".to_owned()),
548+
VerticalSpace,
549+
BlockQuote("> some block quote"),
550+
VerticalSpace,
551+
Paragraph("some other paragraph".to_owned()),
552+
],
553+
);
554+
}
555+
556+
#[test]
557+
fn parses_nested_block_quotes_verbatim() {
558+
assert_eq!(
559+
parse(
560+
"
561+
some subject
562+
563+
some paragraph
564+
565+
> > some block quote
566+
567+
some other paragraph
568+
"
569+
),
570+
[
571+
VerticalSpace,
572+
Subject("some subject".to_owned()),
573+
VerticalSpace,
574+
Paragraph("some paragraph".to_owned()),
575+
VerticalSpace,
576+
BlockQuote("> > some block quote"),
577+
VerticalSpace,
578+
Paragraph("some other paragraph".to_owned()),
579+
],
580+
);
581+
}
582+
583+
#[test]
584+
fn parses_nested_block_quotes_ignoring_quote_marker_spacing_and_quote_levels() {
585+
assert_eq!(
586+
parse(
587+
"
588+
some subject
589+
590+
some paragraph
591+
592+
>>>> >>> >> some block quote
593+
594+
some other paragraph
595+
"
596+
),
597+
[
598+
VerticalSpace,
599+
Subject("some subject".to_owned()),
600+
VerticalSpace,
601+
Paragraph("some paragraph".to_owned()),
602+
VerticalSpace,
603+
BlockQuote(">>>> >>> >> some block quote"),
604+
VerticalSpace,
605+
Paragraph("some other paragraph".to_owned()),
606+
],
607+
);
608+
}
609+
610+
#[test]
611+
fn parses_block_quote_with_immediately_preceding_paragraph_as_attribution_leaving_no_vertical_space(
612+
) {
613+
assert_eq!(
614+
parse(
615+
"
616+
some subject
617+
618+
some attribution paragraph
619+
> some block quote
620+
621+
some other paragraph
622+
"
623+
),
624+
[
625+
VerticalSpace,
626+
Subject("some subject".to_owned()),
627+
VerticalSpace,
628+
Paragraph("some attribution paragraph".to_owned()),
629+
BlockQuote("> some block quote"),
630+
VerticalSpace,
631+
Paragraph("some other paragraph".to_owned()),
632+
],
633+
);
634+
}
635+
516636
#[test]
517637
fn parses_trailers() {
518638
// Trailers look like HTTP or email headers but are not formally

0 commit comments

Comments
 (0)