test.rs 3.83 KB
Newer Older
MovingtoMars's avatar
MovingtoMars committed
1
use super::*;
David Campbell's avatar
David Campbell committed
2
use context;
MovingtoMars's avatar
MovingtoMars committed
3

4 5
use std::env;
use std::fs;
Sehny's avatar
Sehny committed
6
use std::io::{BufReader, BufRead, Write};
7

MovingtoMars's avatar
MovingtoMars committed
8 9
fn assert_cursor_pos(s: &str, cursor: usize, expected_pos: CursorPosition) {
    let buf = Buffer::from(s.to_owned());
David Campbell's avatar
David Campbell committed
10
    let words = context::get_buffer_words(&buf);
MovingtoMars's avatar
MovingtoMars committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    let pos = CursorPosition::get(cursor, &words[..]);
    assert!(expected_pos == pos,
            format!("buffer: {:?}, cursor: {}, expected pos: {:?}, pos: {:?}",
                    s,
                    cursor,
                    expected_pos,
                    pos));
}

#[test]
fn test_get_cursor_position() {
    use CursorPosition::*;

    let tests = &[("hi", 0, OnWordLeftEdge(0)),
                  ("hi", 1, InWord(0)),
                  ("hi", 2, OnWordRightEdge(0)),
                  ("abc  abc", 4, InSpace(Some(0), Some(1))),
                  ("abc  abc", 5, OnWordLeftEdge(1)),
                  ("abc  abc", 6, InWord(1)),
                  ("abc  abc", 8, OnWordRightEdge(1)),
                  (" a", 0, InSpace(None, Some(0))),
                  ("a ", 2, InSpace(Some(0), None))];

    for t in tests {
        assert_cursor_pos(t.0, t.1, t.2);
    }
}

fn assert_buffer_actions(start: &str, expected: &str, actions: &[Action]) {
    let mut buf = Buffer::from(start.to_owned());
    for a in actions {
        a.do_on(&mut buf);
    }

    assert_eq!(expected, String::from(buf));
}

#[test]
fn test_buffer_actions() {
    assert_buffer_actions("",
                          "h",
                          &[Action::Insert {
                                start: 0,
                                text: "hi".chars().collect(),
                            },
                            Action::Remove {
                                start: 1,
                                text: ".".chars().collect(),
                            }]);
}
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

#[test]
fn test_history_indexing() {
    let mut h = History::new();
    h.push(Buffer::from("a")).unwrap();
    h.push(Buffer::from("b")).unwrap();
    h.push(Buffer::from("c")).unwrap();
    assert_eq!(h.len(), 3);
    assert_eq!(String::from(h.buffers[0].clone()), "a".to_string());
    assert_eq!(String::from(h.buffers[1].clone()), "b".to_string());
    assert_eq!(String::from(h.buffers[2].clone()), "c".to_string());
}

#[test]
fn test_in_memory_history_truncating() {
    let mut h = History::new();
77
    h.set_max_buffers_size(2);
78 79
    for _ in 0..4 {
        h.push(Buffer::from("a")).unwrap();
Michael Aaron Murphy's avatar
Michael Aaron Murphy committed
80
        h.push(Buffer::from("b")).unwrap();
81
    }
82
    h.commit_to_file();
83 84 85 86 87 88
    assert_eq!(h.len(), 2);
}

#[test]
fn test_in_file_history_truncating() {
    let mut tmp_file = env::temp_dir();
Sehny's avatar
Sehny committed
89
    tmp_file.push("liner_test_file123.txt");
90 91 92

    {
        let mut h = History::new();
93
        let _ = h.set_file_name_and_load_history(&tmp_file).unwrap();
94
        h.set_max_file_size(5);
95 96
        for bytes in b'a'..b'z' {
            h.push(Buffer::from(format!("{}", bytes as char))).unwrap();
97
        }
98
        h.commit_to_file();
99 100
    }

101
    let f = fs::File::open(&tmp_file).unwrap();
102 103 104 105 106 107
    let r = BufReader::new(f);
    let count = r.lines().count();
    assert_eq!(count, 5);

    fs::remove_file(tmp_file).unwrap();
}
Sehny's avatar
Sehny committed
108

Sehny's avatar
Sehny committed
109
static TEXT: &'static str = "a
Sehny's avatar
Sehny committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
b
c
d
";

#[test]
fn test_reading_from_file() {
    let mut tmp_file = env::temp_dir();
    tmp_file.push("liner_test_file456.txt");
    {
        let mut f = fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .open(tmp_file.clone())
            .unwrap();
        f.write_all(TEXT.as_bytes()).unwrap();
    }
    let mut h = History::new();
129
    h.set_file_name_and_load_history(tmp_file);
Sehny's avatar
Sehny committed
130 131 132 133 134
    assert_eq!(String::from(h.buffers[0].clone()), "a".to_string());
    assert_eq!(String::from(h.buffers[1].clone()), "b".to_string());
    assert_eq!(String::from(h.buffers[2].clone()), "c".to_string());
    assert_eq!(String::from(h.buffers[3].clone()), "d".to_string());
}