Skip to content

Commit 21dbe45

Browse files
committed
check comments, add test, remove unnecessry .to_string()
1. Check comments for accuracy 2. Add basic unit test. Test checks output for string replacement and use of key html tags. Not sensitive to whitespace handling. 3. Remove unnecessary to_string() calls from main() and test function.
1 parent a99c47e commit 21dbe45

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ fn replace_text(document: &str, orig_string: &str, replacement: &str) -> String
187187
}
188188

189189
fn main() {
190-
let doc = "This is my input.\n\n1. Also [my](#) input.\n2. Certainly *my* input.\n".to_string();
191-
let orig = "my".to_string();
192-
let repl = "your".to_string();
190+
let doc = "This is my input.\n\n1. Also [my](#) input.\n2. Certainly *my* input.\n";
191+
let orig = "my";
192+
let repl = "your";
193193
let html = replace_text(&doc, &orig, &repl);
194194

195195
println!("{}", html);

Diff for: examples/iterator_replace.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extern crate comrak;
22
use comrak::nodes::NodeValue;
33
use comrak::{format_html, parse_document, Arena, Options};
4+
use ntest::{assert_false, assert_true};
45

56
fn replace_text(document: &str, orig_string: &str, replacement: &str) -> String {
67
// The returned nodes are created in the supplied Arena, and are bound by its lifetime.
@@ -12,7 +13,7 @@ fn replace_text(document: &str, orig_string: &str, replacement: &str) -> String
1213
// Iterate over all the descendants of root.
1314
for node in root.descendants() {
1415
if let NodeValue::Text(ref mut text) = node.data.borrow_mut().value {
15-
// If the node is a text node, append its text to `output_text`.
16+
// If the node is a text node, replace `orig_string` with `replacement`.
1617
*text = text.replace(orig_string, replacement)
1718
}
1819
}
@@ -24,10 +25,28 @@ fn replace_text(document: &str, orig_string: &str, replacement: &str) -> String
2425
}
2526

2627
fn main() {
27-
let doc = "This is my input.\n\n1. Also [my](#) input.\n2. Certainly *my* input.\n".to_string();
28-
let orig = "my".to_string();
29-
let repl = "your".to_string();
28+
let doc = "This is my input.\n\n1. Also [my](#) input.\n2. Certainly *my* input.\n";
29+
let orig = "my";
30+
let repl = "your";
3031
let html = replace_text(&doc, &orig, &repl);
3132

3233
println!("{}", html);
3334
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
#[test]
41+
fn sample_replace() {
42+
let doc = "Replace deeply nested *[foo](https://example.com)* with bar.\n\nReplace shallow foo with bar.";
43+
let orig = "foo";
44+
let repl = "bar";
45+
let html = replace_text(&doc, &orig, &repl);
46+
println!("{:?}", html);
47+
assert_false!(html.contains("foo"));
48+
assert_true!(html.contains("bar"));
49+
assert_true!(html.contains("<a"));
50+
assert_true!(html.contains("<p"));
51+
}
52+
}

0 commit comments

Comments
 (0)