Implement OpenFile command

This commit is contained in:
Matthew Gordon 2025-11-14 20:14:47 -04:00
parent 69d87ef680
commit f33d51f931
1 changed files with 45 additions and 1 deletions

View File

@ -2,8 +2,8 @@ use std::path::PathBuf;
use crate::{Point, TextBuffer}; use crate::{Point, TextBuffer};
mod io;
mod command; mod command;
mod io;
pub use command::{Command, Movement, Unit}; pub use command::{Command, Movement, Unit};
#[derive(Default)] #[derive(Default)]
@ -64,3 +64,47 @@ impl EditorBuffer {
todo!() todo!()
} }
} }
#[cfg(test)]
mod tests {
use super::*;
use std::io::{Read, Seek, SeekFrom, Write};
use tempfile::NamedTempFile;
fn create_simple_test_file() -> NamedTempFile {
let inner = || {
let mut file = NamedTempFile::new()?;
writeln!(file, "Messe ocus Pangur Bán,")?;
writeln!(file, "cechtar nathar fria saindan")?;
writeln!(file, "bíth a menmasam fri seilgg")?;
writeln!(file, "mu menma céin im saincheirdd.")?;
writeln!(file)?;
writeln!(file, "Caraimse fos ferr cach clú")?;
writeln!(file, "oc mu lebran leir ingn")?;
writeln!(file, "ni foirmtech frimm Pangur Bá")?;
writeln!(file, "caraid cesin a maccdán.")?;
writeln!(file)?;
writeln!(file, "Orubiam scél cen scís")?;
writeln!(file, "innar tegdais ar noendís")?;
writeln!(file, "taithiunn dichrichide clius")?;
writeln!(file, "ni fristarddam arnáthius.")?;
file.seek(SeekFrom::Start(0))?;
Ok::<_, std::io::Error>(file)
};
inner().expect("Creating temporary file")
}
#[test]
fn open_file_loads_file_contents() {
let mut test_file = create_simple_test_file();
let mut expected_contents = String::new();
test_file
.read_to_string(&mut expected_contents)
.expect("Reading text file");
let mut target = EditorBuffer::new();
target.execute(Command::OpenFile(test_file.path().into()));
let found_contents: String = target.buffer.iter_chars().collect();
assert_eq!(expected_contents, found_contents);
}
}