Skip to content

Commit bedc79f

Browse files
rustdoc: Add tests for foreign code highlighting
Test cases: - Highlighting enabled: Python, JavaScript, JSON produce arborium tags - Language aliases work (py -> python, js -> javascript) - Highlighting disabled: no arborium tags produced - Unsupported languages fall back to plain escaped text
1 parent 89da870 commit bedc79f

File tree

2 files changed

+140
-4
lines changed

2 files changed

+140
-4
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,12 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
270270
// Try to highlight with arborium if enabled and we have a language
271271
let code_html = if self.highlight_foreign_code {
272272
lang.and_then(|l| {
273-
highlight::highlight_foreign_code(l, original_text.trim_suffix('\n'))
274-
})
275-
.unwrap_or_else(|| {
276-
Escape(original_text.trim_suffix('\n')).to_string()
273+
highlight::highlight_foreign_code(
274+
l,
275+
original_text.trim_suffix('\n'),
276+
)
277277
})
278+
.unwrap_or_else(|| Escape(original_text.trim_suffix('\n')).to_string())
278279
} else {
279280
Escape(original_text.trim_suffix('\n')).to_string()
280281
};

src/librustdoc/html/markdown/tests.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,138 @@ fn test_ascii_with_prepending_hashtag() {
544544
# hello</code></pre></div>",
545545
);
546546
}
547+
548+
#[test]
549+
fn test_foreign_code_highlighting_enabled() {
550+
fn t(input: &str, expected_contains: &str) {
551+
let mut map = IdMap::new();
552+
let mut output = String::new();
553+
Markdown {
554+
content: input,
555+
links: &[],
556+
ids: &mut map,
557+
error_codes: ErrorCodes::Yes,
558+
edition: DEFAULT_EDITION,
559+
playground: &None,
560+
heading_offset: HeadingOffset::H2,
561+
highlight_foreign_code: true,
562+
}
563+
.write_into(&mut output)
564+
.unwrap();
565+
assert!(
566+
output.contains(expected_contains),
567+
"expected output to contain {:?}, got: {}",
568+
expected_contains,
569+
output
570+
);
571+
}
572+
573+
// Python: keywords should be wrapped in <a-k>
574+
t(
575+
r#"```python
576+
def hello():
577+
pass
578+
```"#,
579+
"<a-k>def</a-k>",
580+
);
581+
582+
// JavaScript: keywords and numbers
583+
t(
584+
r#"```javascript
585+
let x = 42;
586+
```"#,
587+
"<a-k>let</a-k>",
588+
);
589+
590+
// JSON: strings should be highlighted
591+
t(
592+
r#"```json
593+
{"key": "value"}
594+
```"#,
595+
"<a-s>",
596+
);
597+
598+
// Language aliases should work
599+
t(
600+
r#"```py
601+
def foo():
602+
pass
603+
```"#,
604+
"<a-k>def</a-k>",
605+
);
606+
607+
t(
608+
r#"```js
609+
const x = 1;
610+
```"#,
611+
"<a-k>const</a-k>",
612+
);
613+
}
614+
615+
#[test]
616+
fn test_foreign_code_highlighting_disabled() {
617+
fn t(input: &str, should_not_contain: &str) {
618+
let mut map = IdMap::new();
619+
let mut output = String::new();
620+
Markdown {
621+
content: input,
622+
links: &[],
623+
ids: &mut map,
624+
error_codes: ErrorCodes::Yes,
625+
edition: DEFAULT_EDITION,
626+
playground: &None,
627+
heading_offset: HeadingOffset::H2,
628+
highlight_foreign_code: false,
629+
}
630+
.write_into(&mut output)
631+
.unwrap();
632+
assert!(
633+
!output.contains(should_not_contain),
634+
"expected output NOT to contain {:?}, got: {}",
635+
should_not_contain,
636+
output
637+
);
638+
}
639+
640+
// With highlighting disabled, no arborium tags should appear
641+
t(
642+
r#"```python
643+
def hello():
644+
pass
645+
```"#,
646+
"<a-k>",
647+
);
648+
649+
t(
650+
r#"```javascript
651+
let x = 42;
652+
```"#,
653+
"<a-k>",
654+
);
655+
}
656+
657+
#[test]
658+
fn test_foreign_code_unsupported_language_fallback() {
659+
// Unsupported languages should fall back to plain text even with highlighting enabled
660+
let mut map = IdMap::new();
661+
let mut output = String::new();
662+
Markdown {
663+
content: r#"```someunknownlang
664+
hello world
665+
```"#,
666+
links: &[],
667+
ids: &mut map,
668+
error_codes: ErrorCodes::Yes,
669+
edition: DEFAULT_EDITION,
670+
playground: &None,
671+
heading_offset: HeadingOffset::H2,
672+
highlight_foreign_code: true,
673+
}
674+
.write_into(&mut output)
675+
.unwrap();
676+
677+
// Should have the language class but no arborium tags
678+
assert!(output.contains("language-someunknownlang"));
679+
assert!(!output.contains("<a-"));
680+
assert!(output.contains("hello world"));
681+
}

0 commit comments

Comments
 (0)