Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow omitted lines like rustdoc tests do #21

Merged
merged 1 commit into from
Apr 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions src/skeptic/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ fn emit_tests(config: &Config, suite: DocTestSuite) -> Result<(), IoError> {
if let Some(ref t) = test.template {
let template = doc_test.templates.get(t)
.expect(&format!("template {} not found for {}", t, doc_test.path.display()));
try!(create_test_string(config, &Some(template.to_string()), test))
try!(create_test_runner(config, &Some(template.to_string()), test))
} else {
try!(create_test_string(config, &doc_test.old_template, test))
try!(create_test_runner(config, &doc_test.old_template, test))
}
};
out.push_str(&test_string);
Expand All @@ -312,13 +312,33 @@ fn emit_tests(config: &Config, suite: DocTestSuite) -> Result<(), IoError> {
write_if_contents_changed(&config.out_file, &out)
}

fn create_test_string(config: &Config,
/// Just like Rustdoc, ignore a "#" sign at the beginning of a line of code.
/// These are commonly an indication to omit the line from user-facing
/// documentation but include it for the purpose of playground links or skeptic
/// testing.
fn clean_omitted_line(line: &String) -> &str {
let trimmed = line.trim_left();
if trimmed == "#\n" {
&trimmed[1..]
} else if trimmed.starts_with("# ") {
&trimmed[2..]
} else {
line
}
}

/// Creates the Rust code that this test will be operating on.
fn create_test_input(lines: &[String]) -> String {
lines.iter().map(clean_omitted_line).collect()
}

fn create_test_runner(config: &Config,
template: &Option<String>,
test: &Test)
-> Result<String, IoError> {

let template = template.clone().unwrap_or_else(|| String::from("{}"));
let test_text = test.text.iter().fold(String::new(), |a, b| format!("{}{}", a, b));
let test_text = create_test_input(&test.text);

let mut s: Vec<u8> = Vec::new();
if test.ignore {
Expand Down Expand Up @@ -465,3 +485,30 @@ pub mod rt {
}
}
}

#[test]
fn test_omitted_lines() {
let lines = &[
"# use std::collections::BTreeMap as Map;\n".to_owned(),
"#\n".to_owned(),
"#[allow(dead_code)]\n".to_owned(),
"fn main() {\n".to_owned(),
" let map = Map::new();\n".to_owned(),
" #\n".to_owned(),
" # let _ = map;\n".to_owned(),
"}\n".to_owned(),
];

let expected = [
"use std::collections::BTreeMap as Map;\n",
"\n",
"#[allow(dead_code)]\n",
"fn main() {\n",
" let map = Map::new();\n",
"\n",
"let _ = map;\n",
"}\n",
].concat();

assert_eq!(create_test_input(lines), expected);
}