test.rs

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