From 2223188dfffca4700eec491a600322fc1c0da002 Mon Sep 17 00:00:00 2001 From: Laurence Tratt Date: Tue, 30 Jan 2024 12:24:45 +0000 Subject: [PATCH] Ignore comment lines more generally. --- .../rust_lang_tester/lang_tests/not_ignore.rs | 5 +-- src/parser.rs | 31 ++++++++++++++----- src/tester.rs | 16 ++-------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/rust_lang_tester/lang_tests/not_ignore.rs b/examples/rust_lang_tester/lang_tests/not_ignore.rs index f660e0d..95e9576 100644 --- a/examples/rust_lang_tester/lang_tests/not_ignore.rs +++ b/examples/rust_lang_tester/lang_tests/not_ignore.rs @@ -2,8 +2,9 @@ // ignore-if: false // Run-time: // stdout: -// # check +// # an ignored comment +// check fn main() { - println!("# check"); + println!("check"); } diff --git a/src/parser.rs b/src/parser.rs index dff14a0..7863be1 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -18,7 +18,7 @@ pub(crate) fn parse_tests<'a>(comment_prefix: Option<&str>, test_str: &'a str) - continue; } if let Some(cp) = comment_prefix { - if lines[line_off].trim_start().starts_with(cp) { + if lines[line_off][indent..].starts_with(cp) { line_off += 1; continue; } @@ -52,7 +52,8 @@ pub(crate) fn parse_tests<'a>(comment_prefix: Option<&str>, test_str: &'a str) - if sub_indent == indent { break; } - let (end_line_off, key, val) = key_multiline_val(&lines, line_off, sub_indent); + let (end_line_off, key, val) = + key_multiline_val(comment_prefix, &lines, line_off, sub_indent); line_off = end_line_off; match key { "env-var" => { @@ -166,6 +167,7 @@ fn key_val<'a>(lines: &[&'a str], line_off: usize, indent: usize) -> (&'a str, & /// Turn one more lines of the format `key: val` (where `val` may spread over many lines) into its /// separate components. fn key_multiline_val<'a>( + comment_prefix: Option<&str>, lines: &[&'a str], mut line_off: usize, indent: usize, @@ -185,6 +187,12 @@ fn key_multiline_val<'a>( if cur_indent <= indent { break; } + if let Some(cp) = comment_prefix { + if lines[line_off][sub_indent..].starts_with(cp) { + line_off += 1; + continue; + } + } val.push(&lines[line_off][sub_indent..]); line_off += 1; } @@ -207,22 +215,31 @@ mod test { #[test] fn test_key_multiline() { - assert_eq!(key_multiline_val(&["x:", ""], 0, 0), (2, "x", vec![])); + assert_eq!(key_multiline_val(None, &["x:", ""], 0, 0), (2, "x", vec![])); assert_eq!( - key_multiline_val(&["x: y", " z", "a"], 0, 0), + key_multiline_val(None, &["x: y", " z", "a"], 0, 0), (2, "x", vec!["y", "z"]) ); assert_eq!( - key_multiline_val(&["x:", " z", "a"], 0, 0), + key_multiline_val(None, &["x:", " z", "a"], 0, 0), (2, "x", vec!["z"]) ); assert_eq!( - key_multiline_val(&["x:", " z ", " a ", " ", "b"], 0, 0), + key_multiline_val(None, &["x:", " z ", " a ", " ", "b"], 0, 0), (4, "x", vec!["z ", "a "]) ); assert_eq!( - key_multiline_val(&["x:", " z ", " a ", " ", " b"], 0, 0), + key_multiline_val(None, &["x:", " z ", " a ", " ", " b"], 0, 0), (5, "x", vec!["z ", " a ", "", "b"]) ); + assert_eq!( + key_multiline_val( + Some("#"), + &["x:", " z ", " a ", " # c2", " ", " b"], + 0, + 0 + ), + (6, "x", vec!["z ", " a ", "", "b"]) + ); } } diff --git a/src/tester.rs b/src/tester.rs index 35e1200..00a6772 100644 --- a/src/tester.rs +++ b/src/tester.rs @@ -130,17 +130,8 @@ impl LangTester { self } - /// If set, defines what top-level lines will be treated as comments if they begin with - /// `comment_prefix`. Note that non-top-level lines are not checked against this. - /// - /// For example, if we set `comment_prefix` to `#` then: - /// - /// ```text - /// # Considered a top-level comment by lang_tester - /// Compiler: - /// stdout: - /// # A literal string beginning with `#` - /// ``` + /// If set, defines what lines will be treated as comments if, ignoring the current level of + /// indentation, they begin with `comment_prefix`. /// /// This option defaults to `None`. pub fn comment_prefix>(&mut self, comment_prefix: S) -> &mut Self { @@ -664,8 +655,7 @@ fn test_file( return; } - let tests = - parse_tests(inner.comment_prefix.as_deref(), &test_str); + let tests = parse_tests(inner.comment_prefix.as_deref(), &test_str); let ignore = if let Some(ignore_if) = tests.ignore_if { Command::new(env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_owned())) .args(["-c", &ignore_if])