test.rs

 1use std::path::{Path, PathBuf};
 2use tempdir::TempDir;
 3
 4pub fn temp_tree(tree: serde_json::Value) -> TempDir {
 5    let dir = TempDir::new("").unwrap();
 6    write_tree(dir.path(), tree);
 7    dir
 8}
 9
10fn write_tree(path: &Path, tree: serde_json::Value) {
11    use serde_json::Value;
12    use std::fs;
13
14    if let Value::Object(map) = tree {
15        for (name, contents) in map {
16            let mut path = PathBuf::from(path);
17            path.push(name);
18            match contents {
19                Value::Object(_) => {
20                    fs::create_dir(&path).unwrap();
21                    write_tree(&path, contents);
22                }
23                Value::Null => {
24                    fs::create_dir(&path).unwrap();
25                }
26                Value::String(contents) => {
27                    fs::write(&path, contents).unwrap();
28                }
29                _ => {
30                    panic!("JSON object must contain only objects, strings, or null");
31                }
32            }
33        }
34    } else {
35        panic!("You must pass a JSON object to this helper")
36    }
37}
38
39pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
40    let mut text = String::new();
41    for row in 0..rows {
42        let c: char = (start_char as u32 + row as u32) as u8 as char;
43        let mut line = c.to_string().repeat(cols);
44        if row < rows - 1 {
45            line.push('\n');
46        }
47        text += &line;
48    }
49    text
50}