From 4b661f9dc440b38c1fd16035c91ae0be60350031 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Wed, 25 May 2022 20:44:03 +0100 Subject: [PATCH] (prost-build): Add tests for codeblock cleanup Signed-off-by: Daniel Silverstone --- prost-build/src/ast.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/prost-build/src/ast.rs b/prost-build/src/ast.rs index 1bb4a3dbe..9b9988411 100644 --- a/prost-build/src/ast.rs +++ b/prost-build/src/ast.rs @@ -289,4 +289,48 @@ mod tests { assert_eq!(t.expected, actual, "failed {}", t.name); } } + + #[test] + fn test_codeblocks() { + struct TestCase { + name: &'static str, + input: &'static str, + #[allow(unused)] + cleanedup_expected: Vec<&'static str>, + } + + let tests = vec![ + TestCase { + name: "unlabelled_block", + input: " thingy\n", + cleanedup_expected: vec!["", "```text", "thingy", "```"], + }, + TestCase { + name: "rust_block", + input: "```rust\nfoo.bar()\n```\n", + cleanedup_expected: vec!["", "```compile_fail", "foo.bar()", "```"], + }, + TestCase { + name: "js_block", + input: "```javascript\nfoo.bar()\n```\n", + cleanedup_expected: vec!["", "```text,javascript", "foo.bar()", "```"], + }, + ]; + + for t in tests { + let loc = Location { + path: vec![], + span: vec![], + leading_comments: Some(t.input.into()), + trailing_comments: None, + leading_detached_comments: vec![], + }; + let comments = Comments::from_location(&loc); + #[cfg(feature = "cleanup-markdown")] + let expected = t.cleanedup_expected; + #[cfg(not(feature = "cleanup-markdown"))] + let expected: Vec<&str> = t.input.lines().collect(); + assert_eq!(expected, comments.leading, "failed {}", t.name); + } + } }