test.rs

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