Skip to content

Commit e198cb6

Browse files
committed
started tests
1 parent a06678c commit e198cb6

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

.vscode/settings.json

Whitespace-only changes.

src/doc_structure.rs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ pub mod docs {
2929
returns: HashMap<String, String>,
3030
}
3131

32+
impl PartialEq for Doc {
33+
fn eq(&self, other: &Doc) -> bool {
34+
self.short_description == other.short_description
35+
&& self.long_description == other.long_description
36+
&& self.descriptors == other.descriptors
37+
&& self.params == other.params
38+
&& self.returns == other.returns
39+
}
40+
}
41+
3242
impl Doc {
3343
/// # Build a `Doc` from an array of strings
3444
/// Parse `Doc` fields.
@@ -232,7 +242,7 @@ pub mod docs {
232242
}
233243
}
234244

235-
pub fn export_json(docstrings: &[DocFile], file_name: &str) {
245+
pub fn to_json(docstrings: &[DocFile], file_name: &str) {
236246
let json = serde_json::to_string_pretty(&docstrings).expect("Could not convert to JSON");
237247
let path_as_str = file_name.replace("~", home_dir().unwrap().to_str().unwrap());
238248
let path = Path::new(&path_as_str);
@@ -288,4 +298,95 @@ pub mod docs {
288298
}
289299
}
290300
}
301+
302+
#[cfg(test)]
303+
mod tests {
304+
use super::*;
305+
306+
macro_rules! map(
307+
{ $($key:expr => $value:expr),+ } => {
308+
{
309+
let mut m = ::std::collections::HashMap::new();
310+
$(
311+
m.insert($key, $value);
312+
)+
313+
m
314+
}
315+
};
316+
);
317+
318+
#[test]
319+
fn make_doc() {
320+
let input: &[String] = &vec![
321+
"runner()".to_string(),
322+
"This is the beginning".to_string(),
323+
"@params filename: don\'t test me".to_string(),
324+
"@params location: where to put it".to_string(),
325+
"@returns nothing:".to_string(),
326+
];
327+
let result = Doc::make_doc(&input);
328+
let mut expected = Doc::default();
329+
expected.short_description = "runner()".to_string();
330+
expected.long_description = "This is the beginning".to_string();
331+
expected.descriptors = HashMap::new();
332+
expected.params = map!(
333+
"location".to_string() => "where to put it".to_string(),
334+
"filename".to_string() => "don\'t test me".to_string()
335+
);
336+
expected.returns = map!("nothing".to_string() => String::new());
337+
assert_eq!(result, expected);
338+
}
339+
340+
#[test]
341+
fn docfile_add() {
342+
let mut docfile = DocFile::default();
343+
let mut expected = Doc::default();
344+
expected.short_description = "runner()".to_string();
345+
expected.long_description = "This is the beginning".to_string();
346+
expected.descriptors = HashMap::new();
347+
expected.params = map!(
348+
"location".to_string() => "where to put it".to_string(),
349+
"filename".to_string() => "don\'t test me".to_string()
350+
);
351+
expected.returns = map!("nothing".to_string() => String::new());
352+
353+
let mut result = Doc::default();
354+
result.short_description = "runner()".to_string();
355+
result.long_description = "This is the beginning".to_string();
356+
result.descriptors = HashMap::new();
357+
result.params = map!(
358+
"location".to_string() => "where to put it".to_string(),
359+
"filename".to_string() => "don\'t test me".to_string()
360+
);
361+
result.returns = map!("nothing".to_string() => String::new());
362+
assert_eq!(0, docfile.thedocs.len());
363+
docfile.add(expected);
364+
assert_eq!(1, docfile.thedocs.len());
365+
assert_eq!(result, docfile.thedocs[0]);
366+
}
367+
368+
#[test]
369+
fn test_get_info() {
370+
let p = Path::new("example.sh");
371+
let result = get_info(&p);
372+
let expected: Vec<Vec<String>> = vec![
373+
[
374+
"runner()".to_string(),
375+
"This is the beginning".to_string(),
376+
"# - CTRL-O pushs the boundaries".to_string(),
377+
]
378+
.to_vec(),
379+
[
380+
"runner()".to_string(),
381+
"This is the beginning".to_string(),
382+
"@params filename: don\'t test me".to_string(),
383+
"@params location: where to put it".to_string(),
384+
"@returns nothing:".to_string(),
385+
]
386+
.to_vec(),
387+
[].to_vec(),
388+
];
389+
assert_eq!(expected, result);
390+
}
391+
}
291392
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
start(matches.value_of("INPUT").expect("no file found."), false)
6969
};
7070
if matches.is_present("json") {
71-
export_json(&all_em, matches.value_of("json").unwrap());
71+
to_json(&all_em, matches.value_of("json").unwrap());
7272
} else {
7373
for doc in &all_em {
7474
if matches.is_present("color") {

0 commit comments

Comments
 (0)