diff options
| author | Suyashtnt <suyashtnt@gmail.com> | 2024-07-08 18:11:06 +0200 |
|---|---|---|
| committer | Ivan Molodetskikh <yalterz@gmail.com> | 2024-07-08 19:43:07 +0300 |
| commit | 1af9f9bd95283544b068dbbe33e923ab9e836243 (patch) | |
| tree | 66b4143a9eafa351bc5a6ac84a6ba01f1a680191 | |
| parent | 926451c8bee7126b047e8fcf8c7e3f87c43755f7 (diff) | |
| download | niri-1af9f9bd95283544b068dbbe33e923ab9e836243.tar.gz niri-1af9f9bd95283544b068dbbe33e923ab9e836243.tar.bz2 niri-1af9f9bd95283544b068dbbe33e923ab9e836243.zip | |
niri-config: update wiki parses test to test all codeblocks
This makes sure the failing codeblocks do fail. This also optimizes the algorithm a bit by removing a `.collect()`
Signed-off-by: Suyashtnt <suyashtnt@gmail.com>
| -rw-r--r-- | niri-config/tests/wiki-parses.rs | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/niri-config/tests/wiki-parses.rs b/niri-config/tests/wiki-parses.rs index 9c03b471..ca7e9b75 100644 --- a/niri-config/tests/wiki-parses.rs +++ b/niri-config/tests/wiki-parses.rs @@ -1,19 +1,18 @@ use std::fs; use std::path::PathBuf; -const NO_TEST_COMMENT: &str = "<!-- no-test -->"; - struct KdlCodeBlock { filename: String, code: String, line_number: usize, + must_fail: bool, } fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBlock> { - // Removes the > from callouts that might contain ```kdl``` - let lines: Vec<_> = file_contents + let mut lines = file_contents .lines() .map(|line| { + // Removes the > from callouts that might contain ```kdl``` let line = line.trim(); if line.starts_with(">") { if line.len() == 1 { @@ -25,22 +24,18 @@ fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBloc line } }) - .enumerate() - .collect(); + .enumerate(); - let mut lines_iter = lines.iter(); let mut kdl_code_blocks = vec![]; - while let Some((line_number, line)) = lines_iter.next() { - let start_snippet = - line.starts_with("```kdl") && lines[line_number - 1].1 != NO_TEST_COMMENT; - - if !start_snippet { + while let Some((line_number, line)) = lines.next() { + if !line.starts_with("```kdl") { continue; } let mut snippet = String::new(); - for (_, line) in lines_iter + + for (_, line) in lines .by_ref() .take_while(|(_, line)| !line.starts_with("```")) { @@ -50,8 +45,9 @@ fn extract_kdl_from_file(file_contents: &str, filename: &str) -> Vec<KdlCodeBloc kdl_code_blocks.push(KdlCodeBlock { code: snippet, - line_number: *line_number, + line_number, filename: filename.to_string(), + must_fail: line.contains("must-fail"), }); } @@ -86,15 +82,23 @@ fn wiki_docs_parses() { code, line_number, filename, + must_fail, } in code_blocks { if let Err(error) = niri_config::Config::parse(&filename, &code) { + if !must_fail { + errors.push(format!( + "Error parsing wiki KDL code block at {}:{}: {:?}", + filename, + line_number, + miette::Report::new(error) + )); + } + } else if must_fail { errors.push(format!( - "Error parsing wiki KDL code block at {}:{}: {:?}", - filename, - line_number, - miette::Report::new(error) - )) + "Expected error parsing wiki KDL code block at {}:{}", + filename, line_number + )); } } |
