Skip to content

Commit

Permalink
Wrote some tests for file dependencies
Browse files Browse the repository at this point in the history
Added done and undone string
Removed dependencies is now an option
  • Loading branch information
nimaaskarian committed Mar 21, 2024
1 parent eba726b commit d0d412c
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 53 deletions.
12 changes: 6 additions & 6 deletions src/cli_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl <'a>CliApp <'a>{
}
if self.todo_app.is_tree() {
let mut print_todo = PrintTodoTree::new(self.todo_app.args.show_done, self.todo_app.args.minimal_tree);
print_todo.print_list(&self.todo_app.todo_list);
print_todo.print_list(&self.todo_app.todo_list, self.todo_app.args.done_string.as_str(), self.todo_app.args.undone_string.as_str());
} else {
self.print_list()
}
Expand Down Expand Up @@ -88,7 +88,7 @@ impl PrintTodoTree {
}

#[inline]
pub fn print_list(&mut self, todo_list: &TodoList) {
pub fn print_list(&mut self, todo_list: &TodoList, done_str: &str, undone_str: &str) {
let mut todos = todo_list.undone.todos.clone();
if self.show_done {
todos.extend(todo_list.done.todos.clone())
Expand All @@ -99,11 +99,11 @@ impl PrintTodoTree {
if self.depth > 0 {
self.print_indention();
}
self.print_todo(todo);
self.print_todo(todo, done_str, undone_str);

if let Some(todo_list) = todo.dependency.todo_list() {
let mut tree_child = self.tree_child();
tree_child.print_list(todo_list);
tree_child.print_list(todo_list, done_str, undone_str);
}

if let Some(note) = todo.dependency.note() {
Expand All @@ -113,8 +113,8 @@ impl PrintTodoTree {
}

#[inline]
fn print_todo(&self, todo: &Todo) {
println!("{}", todo.display(Some(self.show_done)));
fn print_todo(&self, todo: &Todo,done_str: &str, undone_str: &str) {
println!("{}", todo.display(Some(self.show_done), done_str, undone_str));
}

#[inline]
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ pub struct Args {
#[arg(long)]
done_selected: bool,

/// String before done todos
#[arg(long, default_value_t=String::from("[x] "))]
done_string: String,

/// String before undone todos
#[arg(long, default_value_t=String::from("[ ] "))]
undone_string: String,

/// Append todo
#[arg(short='a', long)]
append_todo: Vec<String>,
Expand Down
56 changes: 45 additions & 11 deletions src/todo_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ impl App {

#[inline]
pub fn display(&self) -> Vec<String> {
self.current_list().display(self.args.show_done)
self.current_list().display(self.args.show_done, self.args.done_string.as_str(), self.args.undone_string.as_str())
}

#[inline]
Expand Down Expand Up @@ -613,7 +613,7 @@ impl App {
#[inline]
pub fn print_selected(&self) {
for index in self.selected.clone() {
println!("{}", self.todo_list[index].display(Some(self.args.show_done)));
println!("{}", self.todo_list[index].display(Some(self.args.show_done), self.args.done_string.as_str(), self.args.undone_string.as_str()));
}
}
}
Expand All @@ -627,10 +627,14 @@ mod tests {

use super::*;

#[test]
fn test_delete_todo() -> io::Result<()> {
fn dir() -> PathBuf {
PathBuf::from("test-results")
}

fn write_test_todos() -> io::Result<App>{
let mut args = Args::parse();
let dir = PathBuf::from("test-results");
let dir = dir();
fs::create_dir_all(dir.join("notes"))?;
args.todo_path = dir.join("todo");
let mut app = App::new(args);
app.append(String::from("Hello"));
Expand All @@ -640,29 +644,59 @@ mod tests {
app.append(String::from(dependency));
}
app.write()?;
for _ in 0..3 {
app.traverse_up();
}
Ok(app)
}

#[test]
fn test_write() -> io::Result<()> {
let dir = dir();
write_test_todos()?;
let mut names = fs::read_dir(dir.join("notes"))?
.map(|res| res.map(|e| e.file_name().to_str().unwrap().to_string()))
.collect::<Result<Vec<_>, io::Error>>()?;


let expected_names = vec!["275549796be6d9a9c6b45d71df4714bfd934c0ba.todo", "560b05afe5e03eae9f8ad475b0b8b73ea6911272.todo", "b3942ad1c555625b7f60649fe50853830b6cdb04.todo"];
let mut expected_names : Vec<String> = expected_names.iter()
.map(|s|s.to_string()).collect();
names.sort();
expected_names.sort();

assert_eq!(names, expected_names);
for _ in 0..3 {
app.traverse_up();
}
fs::remove_dir_all(&dir)?;
Ok(())
}

#[test]
fn test_delete_todo() -> io::Result<()> {
let mut app = write_test_todos()?;
app.delete_todo();
app.write();
app.write()?;

let dir = dir();
let names : io::Result<Vec<PathBuf>> = fs::read_dir(dir.join("notes"))?
.map(|res| res.map(|e|e.path())).collect();
assert!(names?.is_empty());
fs::remove_dir_all(&dir)?;
Ok(())
}

#[test]
fn test_remove_current_dependency() -> io::Result<()> {
let mut app = write_test_todos()?;
app.remove_current_dependent();
app.write()?;

let dir = dir();
let names : io::Result<Vec<PathBuf>> = fs::read_dir(dir.join("notes"))?
.map(|res| res.map(|e|e.path())).collect();
assert!(names?.is_empty());
let string = fs::read_to_string(&dir.join("todo"))?;
let expected_string = String::from("[0] Hello\n");
assert_eq!(string, expected_string);
fs::remove_dir_all(&dir)?;
Ok(())
// assert_eq!(contents, expected);
}
}
37 changes: 19 additions & 18 deletions src/todo_app/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Todo {
pub message: String,
priority: i8,
pub dependency: Dependency,
removed_dependencies: Vec<Dependency>,
removed_dependency: Option<Dependency>,
done:bool,
pub schedule: Schedule,
}
Expand Down Expand Up @@ -89,7 +89,7 @@ impl TryFrom<&str> for Todo {
}
Ok(Todo {
dependency,
removed_dependencies: vec![],
removed_dependency: None,
schedule,
message,
priority,
Expand All @@ -109,7 +109,7 @@ impl Todo {
Todo {
schedule: Schedule::new(),
dependency: Dependency::default(),
removed_dependencies: vec![],
removed_dependency: None,
message,
priority: Todo::fixed_priority(priority),
done,
Expand Down Expand Up @@ -157,7 +157,7 @@ impl Todo {

#[inline]
pub fn delete_removed_dependent_files(&mut self, path: &PathBuf) -> io::Result<()>{
for dependency in &mut self.removed_dependencies {
if let Some(dependency) = &mut self.removed_dependency {
let _ = dependency.todo_list.remove_dependency_files(path);
let _ = remove_file(path.join(dependency.get_name()));
}
Expand All @@ -170,20 +170,19 @@ impl Todo {
}

#[inline]
pub fn display(&self, show_done: Option<bool>) -> String {
pub fn display(&self, show_done: Option<bool>, done_str: &str, undone_str: &str) -> String {
let show_done = match show_done {
None => true,
Some(value) => value,
};
let done_string = if show_done {
let inside_str = if self.done() {
"x"
if self.done() {
done_str
} else {
" "
};
format!("[{inside_str}] ")
undone_str
}
} else {
String::new()
""
};
let note_string = self.dependency.display();
let daily_str = self.schedule.display();
Expand All @@ -192,7 +191,9 @@ impl Todo {

#[inline]
pub fn remove_dependency(&mut self) {
self.removed_dependencies.push(self.dependency.clone());
if self.dependency.is_written() {
self.removed_dependency = Some(self.dependency.clone());
}
self.dependency.remove();
}

Expand Down Expand Up @@ -331,7 +332,7 @@ mod tests {
fn test_try_from_string() {
let input = "[1]>2c924e3088204ee77ba681f72be3444357932fca Test";
let expected = Ok(Todo {
removed_dependencies: vec![],
removed_dependency: None,
schedule: Schedule::new(),
dependency:Dependency::new_note("2c924e3088204ee77ba681f72be3444357932fca".to_string(), "".to_string()),
message: "Test".to_string(),
Expand Down Expand Up @@ -445,7 +446,7 @@ mod tests {
let todo = Todo::try_from(input1).unwrap();

let expected = Todo {
removed_dependencies: vec![],
removed_dependency: None,
schedule: Schedule::new(),
dependency: Dependency::new_todo_list( "1BE348656D84993A6DF0DB0DECF2E95EF2CF461c".to_string()),
message: "Read for exams".to_string(),
Expand All @@ -460,7 +461,7 @@ mod tests {
let input = "[-2] this one should be daily [D1(2023-09-05)]";
let todo = Todo::try_from(input).unwrap();
let expected = Todo {
removed_dependencies: vec![],
removed_dependency: None,
schedule: Schedule::from("D1(2023-09-05)"),
dependency: Dependency::default(),
message: "this one should be daily".to_string(),
Expand All @@ -476,7 +477,7 @@ mod tests {
#[test]
fn test_daily_display() {
let test = Todo {
removed_dependencies: vec![],
removed_dependency: None,
dependency: Dependency::default(),
schedule: Schedule::from("D1()"),
message: "this one should be daily".to_string(),
Expand All @@ -485,15 +486,15 @@ mod tests {
};
let expected = "2. this one should be daily (Daily)";

assert_eq!(test.display(Some(false)), expected)
assert_eq!(test.display(Some(false), "[x]", "[ ]"), expected)
}

#[test]
fn test_weekly() {
let input = "[-2] this one should be daily [D7(2023-09-05)]";
let todo = Todo::try_from(input).unwrap();
let expected = Todo {
removed_dependencies: vec![],
removed_dependency: None,
dependency: Dependency::default(),
schedule: Schedule::from("D7(2023-09-05)"),
message: "this one should be daily".to_string(),
Expand Down
10 changes: 10 additions & 0 deletions src/todo_app/todo/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ pub struct Dependency {
name: String,
mode: DependencyMode,
note: String,
written: bool,
pub(crate) todo_list: TodoList,
}

impl Dependency {
#[inline]
pub fn default() -> Self {
Dependency {
written: false,
mode: DependencyMode::None,
name: String::new(),
note: String::new(),
Expand All @@ -33,13 +35,19 @@ impl Dependency {
#[inline]
fn new(name: String, mode: DependencyMode) -> Self {
Dependency {
written: false,
mode,
name,
note: String::new(),
todo_list: TodoList::new(),
}
}

#[inline]
pub fn is_written(&self) -> bool {
self.written
}

#[inline]
pub fn note(&self) -> Option<&String> {
if self.is_note() {
Expand Down Expand Up @@ -93,6 +101,7 @@ impl Dependency {
}
_ => {}
};
self.written = true;

Ok(())
}
Expand All @@ -119,6 +128,7 @@ impl Dependency {
}
_ => {}
};
self.written = true;
Ok(())
}

Expand Down
Loading

0 comments on commit d0d412c

Please sign in to comment.