Skip to content

Commit 4b3262f

Browse files
committed
Add a test module to comments
1 parent 71d24e9 commit 4b3262f

File tree

1 file changed

+74
-66
lines changed

1 file changed

+74
-66
lines changed

src/comment.rs

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,6 @@ fn left_trim_comment_line<'a>(line: &'a str) -> &'a str {
9393
}
9494
}
9595

96-
#[test]
97-
fn format_comments() {
98-
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, 100));
99-
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10, 0));
100-
101-
assert_eq!("// A multi line comment\n // between args.",
102-
rewrite_comment("// A multi line comment\n // between args.",
103-
false,
104-
60,
105-
12));
106-
107-
let input = "// comment";
108-
let expected = "/* com\n \
109-
* men\n \
110-
* t */";
111-
assert_eq!(expected, rewrite_comment(input, true, 9, 69));
112-
113-
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100, 100));
114-
}
115-
11696
pub trait FindUncommented {
11797
fn find_uncommented(&self, pat: &str) -> Option<usize>;
11898
}
@@ -142,31 +122,6 @@ impl FindUncommented for str {
142122
}
143123
}
144124

145-
#[test]
146-
fn test_find_uncommented() {
147-
fn check(haystack: &str, needle: &str, expected: Option<usize>) {
148-
println!("haystack {:?}, needle: {:?}", haystack, needle);
149-
assert_eq!(expected, haystack.find_uncommented(needle));
150-
}
151-
152-
check("/*/ */test", "test", Some(6));
153-
check("//test\ntest", "test", Some(7));
154-
check("/* comment only */", "whatever", None);
155-
check("/* comment */ some text /* more commentary */ result", "result", Some(46));
156-
check("sup // sup", "p", Some(2));
157-
check("sup", "x", None);
158-
check("π? /**/ π is nice!", "π is nice", Some(9));
159-
check("/*sup yo? \n sup*/ sup", "p", Some(20));
160-
check("hel/*lohello*/lo", "hello", None);
161-
check("acb", "ab", None);
162-
check(",/*A*/ ", ",", Some(0));
163-
check("abc", "abc", Some(0));
164-
check("/* abc */", "abc", None);
165-
check("/**/abc/* */", "abc", Some(4));
166-
check("\"/* abc */\"", "abc", Some(4));
167-
check("\"/* abc", "abc", Some(4));
168-
}
169-
170125
// Returns the first byte position after the first comment. The given string
171126
// is expected to be prefixed by a comment, including delimiters.
172127
// Good: "/* /* inner */ outer */ code();"
@@ -204,27 +159,6 @@ pub fn contains_comment(text: &str) -> bool {
204159
CharClasses::new(text.chars()).any(|(kind, _)| kind == CodeCharKind::Comment )
205160
}
206161

207-
pub fn uncommented(text: &str) -> String {
208-
CharClasses::new(text.chars()).filter_map(|(s, c)| match s {
209-
CodeCharKind::Normal => Some(c),
210-
CodeCharKind::Comment => None
211-
}).collect()
212-
}
213-
214-
#[test]
215-
fn test_uncommented() {
216-
assert_eq!(&uncommented("abc/*...*/"), "abc");
217-
assert_eq!(&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"), "..ac\n");
218-
assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
219-
}
220-
221-
#[test]
222-
fn test_contains_comment() {
223-
assert_eq!(contains_comment("abc"), false);
224-
assert_eq!(contains_comment("abc // qsdf"), true);
225-
assert_eq!(contains_comment("abc /* kqsdf"), true);
226-
assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
227-
}
228162

229163
struct CharClasses<T>
230164
where T: Iterator,
@@ -358,3 +292,77 @@ impl<T> Iterator for CharClasses<T> where T: Iterator, T::Item: RichChar {
358292
return Some((CodeCharKind::Normal, item));
359293
}
360294
}
295+
296+
#[cfg(test)]
297+
mod test {
298+
use super::{CharClasses, CodeCharKind, contains_comment, rewrite_comment, FindUncommented};
299+
300+
#[test]
301+
fn format_comments() {
302+
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, 100));
303+
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10, 0));
304+
305+
assert_eq!("// A multi line comment\n // between args.",
306+
rewrite_comment("// A multi line comment\n // between args.",
307+
false,
308+
60,
309+
12));
310+
311+
let input = "// comment";
312+
let expected = "/* com\n \
313+
* men\n \
314+
* t */";
315+
assert_eq!(expected, rewrite_comment(input, true, 9, 69));
316+
317+
assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100, 100));
318+
}
319+
320+
// This is probably intended to be a non-test fn, but it is not used. I'm
321+
// keeping it around unless it helps us test stuff.
322+
fn uncommented(text: &str) -> String {
323+
CharClasses::new(text.chars()).filter_map(|(s, c)| match s {
324+
CodeCharKind::Normal => Some(c),
325+
CodeCharKind::Comment => None
326+
}).collect()
327+
}
328+
329+
#[test]
330+
fn test_uncommented() {
331+
assert_eq!(&uncommented("abc/*...*/"), "abc");
332+
assert_eq!(&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"), "..ac\n");
333+
assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
334+
}
335+
336+
#[test]
337+
fn test_contains_comment() {
338+
assert_eq!(contains_comment("abc"), false);
339+
assert_eq!(contains_comment("abc // qsdf"), true);
340+
assert_eq!(contains_comment("abc /* kqsdf"), true);
341+
assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
342+
}
343+
344+
#[test]
345+
fn test_find_uncommented() {
346+
fn check(haystack: &str, needle: &str, expected: Option<usize>) {
347+
println!("haystack {:?}, needle: {:?}", haystack, needle);
348+
assert_eq!(expected, haystack.find_uncommented(needle));
349+
}
350+
351+
check("/*/ */test", "test", Some(6));
352+
check("//test\ntest", "test", Some(7));
353+
check("/* comment only */", "whatever", None);
354+
check("/* comment */ some text /* more commentary */ result", "result", Some(46));
355+
check("sup // sup", "p", Some(2));
356+
check("sup", "x", None);
357+
check("π? /**/ π is nice!", "π is nice", Some(9));
358+
check("/*sup yo? \n sup*/ sup", "p", Some(20));
359+
check("hel/*lohello*/lo", "hello", None);
360+
check("acb", "ab", None);
361+
check(",/*A*/ ", ",", Some(0));
362+
check("abc", "abc", Some(0));
363+
check("/* abc */", "abc", None);
364+
check("/**/abc/* */", "abc", Some(4));
365+
check("\"/* abc */\"", "abc", Some(4));
366+
check("\"/* abc", "abc", Some(4));
367+
}
368+
}

0 commit comments

Comments
 (0)