Skip to content

Commit 43b7747

Browse files
committed
Add markdown rendering tests for named ranges
The named repeat range feature adds new rendering paths in the markdown renderer. We add three tests: - `RepeatRange` with a name renders as `<sup>n:1..=255</sup>`, verifying the `name:` prefix appears before the range. - `RepeatRange` without a name renders as `<sup>2..5</sup>` with no spurious colon. - `RepeatRangeNamed` renders as `<sup>n</sup>`, verifying that a named reference produces the expected superscript.
1 parent 6e9836e commit 43b7747

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

tools/mdbook-spec/src/grammar/render_markdown.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ fn markdown_escape(s: &str) -> Cow<'_, str> {
251251
#[cfg(test)]
252252
mod tests {
253253
use super::*;
254+
use grammar::RangeLimit;
254255
use std::collections::HashMap;
255256

256257
/// Creates a minimal `RenderCtx` for testing.
@@ -424,4 +425,56 @@ mod tests {
424425
fn markdown_escape_plain() {
425426
assert_eq!(markdown_escape("abc"), "abc");
426427
}
428+
429+
// -- Named repeat range tests --
430+
431+
#[test]
432+
fn repeat_range_with_name() {
433+
// A RepeatRange with a name renders as `<sup>n:1..=255</sup>`.
434+
let result = render(ExpressionKind::RepeatRange {
435+
expr: Box::new(Expression::new_kind(ExpressionKind::Nt("x".to_string()))),
436+
name: Some("n".to_string()),
437+
min: Some(1),
438+
max: Some(255),
439+
limit: RangeLimit::Closed,
440+
});
441+
assert!(
442+
result.contains("<sup>n:1..=255</sup>"),
443+
"expected <sup>n:1..=255</sup>, got: {result}"
444+
);
445+
}
446+
447+
#[test]
448+
fn repeat_range_without_name() {
449+
// A RepeatRange without a name renders with no spurious
450+
// colon — just `<sup>2..5</sup>`.
451+
let result = render(ExpressionKind::RepeatRange {
452+
expr: Box::new(Expression::new_kind(ExpressionKind::Nt("x".to_string()))),
453+
name: None,
454+
min: Some(2),
455+
max: Some(5),
456+
limit: RangeLimit::HalfOpen,
457+
});
458+
assert!(
459+
result.contains("<sup>2..5</sup>"),
460+
"expected <sup>2..5</sup>, got: {result}"
461+
);
462+
assert!(
463+
!result.contains(":"),
464+
"unnamed range should not contain a colon"
465+
);
466+
}
467+
468+
#[test]
469+
fn repeat_range_named_reference() {
470+
// A RepeatRangeNamed renders as `<sup>n</sup>`.
471+
let result = render(ExpressionKind::RepeatRangeNamed(
472+
Box::new(Expression::new_kind(ExpressionKind::Nt("x".to_string()))),
473+
"n".to_string(),
474+
));
475+
assert!(
476+
result.contains("<sup>n</sup>"),
477+
"expected <sup>n</sup>, got: {result}"
478+
);
479+
}
427480
}

0 commit comments

Comments
 (0)