Skip to content

Commit d92abb3

Browse files
Add more test for rustdoc --test
1 parent b0803d4 commit d92abb3

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
// check-test-line-numbers-match
13+
14+
/// This looks like another awesome test!
15+
///
16+
/// ```
17+
/// println!("foo?");
18+
/// ```
19+
pub fn foooo() {}

src/test/rustdoc/test_option_check/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// compile-flags: --test
1212
// check-test-line-numbers-match
1313

14+
pub mod bar;
15+
1416
/// This is a Foo;
1517
///
1618
/// ```

src/tools/compiletest/src/runtest.rs

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::io::{self, BufReader};
3030
use std::path::{Path, PathBuf};
3131
use std::process::{Command, Output, ExitStatus};
3232
use std::str;
33+
use std::collections::HashMap;
3334

3435
use extract_gdb_version;
3536

@@ -1902,17 +1903,28 @@ actual:\n\
19021903
}
19031904
}
19041905

1905-
fn check_rustdoc_test_option(&self, res: ProcRes) {
1906-
let mut file = fs::File::open(&self.testpaths.file)
1906+
fn get_lines<P: AsRef<Path>>(&self, path: &P,
1907+
mut other_files: Option<&mut Vec<String>>) -> Vec<usize> {
1908+
let mut file = fs::File::open(path)
19071909
.expect("markdown_test_output_check_entry File::open failed");
19081910
let mut content = String::new();
19091911
file.read_to_string(&mut content)
19101912
.expect("markdown_test_output_check_entry read_to_string failed");
19111913
let mut ignore = false;
1912-
let mut v: Vec<usize> =
1913-
content.lines()
1914-
.enumerate()
1915-
.filter_map(|(line_nb, line)| {
1914+
content.lines()
1915+
.enumerate()
1916+
.filter_map(|(line_nb, line)| {
1917+
if (line.trim_left().starts_with("pub mod ") ||
1918+
line.trim_left().starts_with("mod ")) &&
1919+
line.ends_with(";") {
1920+
if let Some(ref mut other_files) = other_files {
1921+
other_files.push(line.rsplit("mod ")
1922+
.next()
1923+
.unwrap()
1924+
.replace(";", ""));
1925+
}
1926+
None
1927+
} else {
19161928
let sline = line.split("///").last().unwrap_or("");
19171929
let line = sline.trim_left();
19181930
if line.starts_with("```") {
@@ -1926,36 +1938,58 @@ actual:\n\
19261938
} else {
19271939
None
19281940
}
1929-
})
1930-
.collect();
1941+
}
1942+
})
1943+
.collect()
1944+
}
1945+
1946+
fn check_rustdoc_test_option(&self, res: ProcRes) {
1947+
let mut other_files = Vec::new();
1948+
let mut files: HashMap<String, Vec<usize>> = HashMap::new();
1949+
files.insert(self.testpaths.file.to_str().unwrap().to_owned(),
1950+
self.get_lines(&self.testpaths.file, Some(&mut other_files)));
1951+
for other_file in other_files {
1952+
let mut path = self.testpaths.file.clone();
1953+
path.set_file_name(&format!("{}.rs", other_file));
1954+
files.insert(path.to_str().unwrap().to_owned(), self.get_lines(&path, None));
1955+
}
19311956

19321957
let mut tested = 0;
19331958
for _ in res.stdout.split("\n")
19341959
.filter(|s| s.starts_with("test "))
19351960
.inspect(|s| {
19361961
let tmp: Vec<&str> = s.split(" - line ").collect();
19371962
if tmp.len() == 2 {
1938-
tested += 1;
1939-
let line = tmp[1].split(" ...")
1940-
.next()
1941-
.unwrap_or("0")
1942-
.parse()
1943-
.unwrap_or(0);
1944-
if let Ok(pos) = v.binary_search(&line) {
1945-
v.remove(pos);
1946-
} else {
1947-
self.fatal_proc_rec(
1948-
&format!("Not found doc test: \"{}\" in {:?}", s, v),
1949-
&res);
1963+
let path = tmp[0].rsplit("test ").next().unwrap();
1964+
println!("{:?}", path);
1965+
if let Some(ref mut v) = files.get_mut(path) {
1966+
tested += 1;
1967+
let line = tmp[1].split(" ...")
1968+
.next()
1969+
.unwrap_or("0")
1970+
.parse()
1971+
.unwrap_or(0);
1972+
if let Ok(pos) = v.binary_search(&line) {
1973+
v.remove(pos);
1974+
} else {
1975+
self.fatal_proc_rec(
1976+
&format!("Not found doc test: \"{}\" in \"{}\":{:?}",
1977+
s, path, v),
1978+
&res);
1979+
}
19501980
}
19511981
}
19521982
}) {}
19531983
if tested == 0 {
1954-
self.fatal_proc_rec("No test has been found", &res);
1955-
} else if v.len() != 0 {
1956-
self.fatal_proc_rec(&format!("Not found test at line{} {:?}",
1957-
if v.len() > 1 { "s" } else { "" }, v),
1958-
&res);
1984+
self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
1985+
} else {
1986+
for (entry, v) in &files {
1987+
if v.len() != 0 {
1988+
self.fatal_proc_rec(&format!("Not found test at line{} \"{}\":{:?}",
1989+
if v.len() > 1 { "s" } else { "" }, entry, v),
1990+
&res);
1991+
}
1992+
}
19591993
}
19601994
}
19611995

0 commit comments

Comments
 (0)