Skip to content

Commit 89e3510

Browse files
Add more tests to check extracted doctests feature
1 parent eb61be3 commit 89e3510

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

src/librustdoc/doctest/tests.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use std::path::PathBuf;
22

3-
use super::{BuildDocTestBuilder, GlobalTestOptions};
3+
use rustc_span::edition::Edition;
4+
use rustc_span::{DUMMY_SP, FileName};
5+
6+
use super::extracted::ExtractedDocTests;
7+
use super::{BuildDocTestBuilder, GlobalTestOptions, ScrapedDocTest};
8+
use crate::html::markdown::LangString;
49

510
fn make_test(
611
test_code: &str,
@@ -19,9 +24,9 @@ fn make_test(
1924
builder = builder.test_id(test_id.to_string());
2025
}
2126
let doctest = builder.build(None);
22-
let (wrapper, line_offset) =
27+
let (wrapped, line_offset) =
2328
doctest.generate_unique_doctest(test_code, dont_insert_main, opts, crate_name);
24-
(wrapper.to_string(), line_offset)
29+
(wrapped.to_string(), line_offset)
2530
}
2631

2732
/// Default [`GlobalTestOptions`] for these unit tests.
@@ -461,3 +466,50 @@ pub mod outer_module {
461466
let (output, len) = make_test(input, None, false, &opts, Vec::new(), None);
462467
assert_eq!((output, len), (expected, 2));
463468
}
469+
470+
fn get_extracted_doctests(code: &str) -> ExtractedDocTests {
471+
let opts = default_global_opts("");
472+
let mut extractor = ExtractedDocTests::new();
473+
extractor.add_test_with_edition(
474+
ScrapedDocTest::new(
475+
FileName::Custom(String::new()),
476+
0,
477+
Vec::new(),
478+
LangString::default(),
479+
code.to_string(),
480+
DUMMY_SP,
481+
),
482+
&opts,
483+
Edition::Edition2018,
484+
);
485+
extractor
486+
}
487+
488+
// Test that `extracted::DocTest::wrapper` is `None` if the doctest has a `main` function.
489+
#[test]
490+
fn test_extracted_doctest_wrapper_field() {
491+
let extractor = get_extracted_doctests("fn main() {}");
492+
493+
assert_eq!(extractor.doctests().len(), 1);
494+
let doctest_code = extractor.doctests()[0].doctest_code.as_ref().unwrap();
495+
assert!(doctest_code.wrapper.is_none());
496+
}
497+
498+
// Test that `ExtractedDocTest::doctest_code` is `None` if the doctest has syntax error.
499+
#[test]
500+
fn test_extracted_doctest_doctest_code_field() {
501+
let extractor = get_extracted_doctests("let x +=");
502+
503+
assert_eq!(extractor.doctests().len(), 1);
504+
assert!(extractor.doctests()[0].doctest_code.is_none());
505+
}
506+
507+
// Test that `extracted::DocTest::wrapper` is `Some` if the doctest needs wrapping.
508+
#[test]
509+
fn test_extracted_doctest_wrapper_field_with_info() {
510+
let extractor = get_extracted_doctests("let x = 12;");
511+
512+
assert_eq!(extractor.doctests().len(), 1);
513+
let doctest_code = extractor.doctests()[0].doctest_code.as_ref().unwrap();
514+
assert!(doctest_code.wrapper.is_some());
515+
}

0 commit comments

Comments
 (0)