Commit 4d8dd37c authored by Sehny's avatar Sehny

Test history loading

parent ca809cc8
......@@ -61,9 +61,8 @@ impl History {
}
/// Set history file name. At the same time enable history.
pub fn set_file_name(&mut self, name: String) -> io::Result<()> {
pub fn set_file_name(&mut self, name: String) {
self.file_name = Some(name);
self.read_history_from_file()
}
/// Set maximal number of buffers stored in memory
......@@ -76,10 +75,11 @@ impl History {
self.max_file_size = size;
}
fn read_history_from_file(&mut self) -> io::Result<()> {
/// Load history from given file name
pub fn load_history(&mut self) -> io::Result<()> {
let file_name = match self.file_name.clone() {
Some(name) => name,
None => return Err(Error::new(ErrorKind::Other, "Liner: internal error")),
None => return Err(Error::new(ErrorKind::Other, "Liner: file name not specified")),
};
let file = try!(OpenOptions::new().read(true).open(file_name));
let reader = BufReader::new(file);
......
......@@ -25,6 +25,7 @@ fn main() {
}
println!("History file: {}", file_name);
con.history.set_file_name(file_name);
con.history.load_history();
loop {
let res = con.read_line("[prompt]$ ",
......
......@@ -3,7 +3,7 @@ use context;
use std::env;
use std::fs;
use std::io::{BufReader, BufRead};
use std::io::{BufReader, BufRead, Write};
fn assert_cursor_pos(s: &str, cursor: usize, expected_pos: CursorPosition) {
let buf = Buffer::from(s.to_owned());
......@@ -84,7 +84,7 @@ fn test_in_memory_history_truncating() {
#[test]
fn test_in_file_history_truncating() {
let mut tmp_file = env::temp_dir();
tmp_file.push("liner_test_file123456.txt");
tmp_file.push("liner_test_file123.txt");
{
let mut h = History::new();
......@@ -102,3 +102,32 @@ fn test_in_file_history_truncating() {
fs::remove_file(tmp_file).unwrap();
}
static TEXT: &'static str =
"a
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();
h.set_file_name(String::from(tmp_file.to_string_lossy().into_owned()));
let _ = h.load_history();
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());
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment