From f33d51f9315a86b8d8b94925473aece8f33b938a Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Fri, 14 Nov 2025 20:14:47 -0400 Subject: [PATCH] Implement OpenFile command --- core/src/editor_buffer/mod.rs | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/core/src/editor_buffer/mod.rs b/core/src/editor_buffer/mod.rs index 9849aab..9f5b45f 100644 --- a/core/src/editor_buffer/mod.rs +++ b/core/src/editor_buffer/mod.rs @@ -2,8 +2,8 @@ use std::path::PathBuf; use crate::{Point, TextBuffer}; -mod io; mod command; +mod io; pub use command::{Command, Movement, Unit}; #[derive(Default)] @@ -64,3 +64,47 @@ impl EditorBuffer { 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); + } +}