use std::collections::HashMap;
use add_ed::{
buffer::{
Buffer,
},
ui::DummyUI,
Ed,
error_consts::*,
};
mod mock_io;
use mock_io::{MockIO, ShellCommand};
fn prepare_write_test(
command_input: Vec<&'static str>,
) -> (MockIO, Buffer, DummyUI) {
let io = MockIO{
mock_fs: HashMap::from([
("existing_file".to_owned(), "Preexisting\ndata\n".to_owned()),
("other_file".to_owned(), "other\ndata\n".to_owned()),
]),
mock_shell: HashMap::from([
(ShellCommand{
command: "sudo tee examplefile".to_owned(),
input: "data\n".to_owned()
},
String::new()),
(ShellCommand{
command: "sudo tee otherfile".to_owned(),
input: "write\ndata\n".to_owned()
},
String::new()),
])
};
let mut buffer = Buffer::new();
buffer.insert(vec!["write\n", "data\n"], 0).unwrap();
let ui = DummyUI{
input: command_input.iter()
.map(|s|s.to_owned().to_owned())
.collect(),
print_ui: None,
};
(io, buffer, ui)
}
#[test]
fn file_write() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1", "w",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection incorrectly modified when writing without selection or path."
);
}
assert_eq!(
io.mock_fs.get("default_file").map(|x|&x[..]),
Some("write\ndata\n"),
"Correct data not put into file."
);
assert!(
buffer.saved(),
"Buffer wasn't flagged as saved after running 'w'"
);
}
#[test]
fn file_append() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1", "W",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"existing_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection incorrectly modified when writing without selection or path."
);
}
assert_eq!(
io.mock_fs.get("existing_file").map(|x|&x[..]),
Some("Preexisting\ndata\nwrite\ndata\n"),
"Correct data not put into file."
);
assert!(
!buffer.saved(),
"Buffer was flagged as saved after running 'W'"
);
}
#[test]
fn file_write_to_path() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1", "w write_file",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection incorrectly modified when writing without selection or path."
);
assert_eq!(
&ed.see_state().file,
&"write_file",
"state.file not updated after writing whole buffer to it."
);
}
assert_eq!(
io.mock_fs.get("write_file").map(|x|&x[..]),
Some("write\ndata\n"),
"Correct data not put into file."
);
assert!(
buffer.saved(),
"Buffer wasn't flagged as saved after running 'w'"
);
}
#[test]
fn file_append_to_path() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1", "W other_file",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection incorrectly modified when writing without selection or path."
);
assert_eq!(
&ed.see_state().file,
&"default_file",
"state.file changed when its contents aren't exactly all of buffer."
);
}
assert_eq!(
io.mock_fs.get("other_file").map(|x|&x[..]),
Some("other\ndata\nwrite\ndata\n"),
"Correct data not put into file."
);
assert!(
!buffer.saved(),
"Buffer was flagged as saved after running 'W'"
);
}
#[test]
fn file_partial_write_to_path() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1,2", "1w partial_file",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection not correctly updated to specific selection given to 'w'."
);
assert_eq!(
&ed.see_state().file,
&"default_file",
"State file changed when not whole buffer was saved."
);
}
assert_eq!(
io.mock_fs.get("partial_file").map(|x|&x[..]),
Some("write\n"),
"Correct data not put into file."
);
assert!(
!buffer.saved(),
"Buffer was flagged as saved after running 'w' with partial buffer selected."
);
}
#[test]
fn file_partial_append_to_path() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1,2", "1W existing_file",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection not correctly updated to specific selection given to 'W'."
);
assert_eq!(
&ed.see_state().file,
&"default_file",
"State file changed when not whole buffer was saved."
);
}
assert_eq!(
io.mock_fs.get("existing_file").map(|x|&x[..]),
Some("Preexisting\ndata\nwrite\n"),
"Correct data not put into file."
);
assert!(
!buffer.saved(),
"Buffer was flagged as saved after running 'W'"
);
}
#[test]
fn file_partial_write_to_command() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1,2", "2w !sudo tee examplefile",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(2,2),
"Selection not correctly updated to specific selection given to 'w'."
);
assert_eq!(
&ed.see_state().file,
&"default_file",
"State file changed when not whole buffer was written, and to a command."
);
}
assert!(
!buffer.saved(),
"Buffer was flagged as saved after running 'w' with partial buffer selected."
);
}
#[test]
fn file_write_to_command() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1", "w !sudo tee otherfile",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
ed.run_macro(&mut ui).expect("Error running test.");
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection not correctly updated to specific selection given to 'w'."
);
assert_eq!(
&ed.see_state().file,
&"default_file",
"State file changed when not whole buffer was written, and to a command."
);
}
assert!(
!buffer.saved(),
"Buffer was flagged as saved after running 'w' into a command."
);
}
#[test]
fn file_write_to_invalid_command() {
let (mut io, mut buffer, mut ui) = prepare_write_test(vec![
"1", "w !not existing",
]);
{
let mut ed = Ed::new(
&mut buffer,
&mut io,
"default_file".to_string(),
HashMap::new(),
false,
false,
)
;
let res = ed.run_macro(&mut ui);
assert_eq!(
Err(CHILD_EXIT_ERROR),
res,
"Running non-existing command didn't return the expected error."
);
assert_eq!(
ed.see_state().selection,
(1,1),
"Selection not correctly updated to specific selection given to 'w'."
);
assert_eq!(
&ed.see_state().file,
&"default_file",
"State file changed when not whole buffer was written, and to a command."
);
}
}