#[cfg(test)]
macro_rules! test_list {
($setup:ident) => {
use query::ast::AST;
use query::Parser;
use task::{Note, Task};
#[test]
fn add_one_and_find_by_id() {
let mut list = $setup();
let task = Task::new("add one test");
let task_id = task.id.clone();
list.add(task).unwrap();
let res = list.find_by_id(task_id.clone()).unwrap();
assert_eq!(res.id, task_id);
}
#[test]
fn test_add_multiple() {
let mut list = $setup();
let tasks = vec![
Task::new("test 1"),
Task::new("test 2"),
Task::new("test 3"),
];
list.add_multiple(tasks.clone()).unwrap();
assert_eq!(list.search(AST::empty()).unwrap(), tasks);
}
#[test]
fn test_complete_a_task() {
let mut list = $setup();
let task = Task::new("task to complete");
let task_id = task.id.clone();
list.add(task).unwrap();
list.complete(task_id.clone()).unwrap();
let completed = list.find_by_id(task_id).unwrap();
assert!(completed.completed());
}
#[test]
fn test_return_correct_current_task() {
let mut list = $setup();
let tasks = vec![
Task::new("test 1"),
Task::new("test 2"),
Task::new("test 3"),
];
list.add_multiple(tasks.clone()).unwrap();
let current = list.current().unwrap();
assert_eq!(current.id, tasks[0].id);
list.complete(tasks[0].id.clone()).unwrap();
let current = list.current().unwrap();
assert_eq!(current.id, tasks[1].id);
}
#[test]
fn test_add_a_note_to_a_task() {
let mut list = $setup();
let task = Task::new("task to be noted");
list.add(task.clone()).unwrap();
let note = Note::new("a note");
list.add_note(task.id.clone(), note.clone()).unwrap();
let noted_task = list.find_by_id(task.id).unwrap();
assert_eq!(vec![note], noted_task.notes);
}
#[test]
fn test_update_a_task() {
let mut list = $setup();
let task = Task::new("task to be updated");
let task_id = task.id.clone();
list.add(task).unwrap();
let mut to_update = list.find_by_id(task_id.clone()).unwrap();
to_update.title = "task updated".to_string();
list.update(to_update).unwrap();
let updated = list.find_by_id(task_id.clone()).unwrap();
assert_eq!(task_id, updated.id);
assert_eq!("task updated".to_string(), updated.title)
}
#[test]
fn test_query_simple_title() {
let mut list = $setup();
let fixture = vec![Task::new("task 1"), Task::new("task 2")];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from("title = \"task 1\"").parse().unwrap();
let res = list.search(ast).unwrap();
assert_eq!(res, vec![fixture[0].clone()]);
}
#[test]
fn test_query_other_context() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2"),
Task::new("other task").with_context("other"),
Task::new("task 3"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from("context = \"other\"").parse().unwrap();
let res = list.search(ast).unwrap();
assert_eq!(res, vec![fixture[2].clone()]);
}
#[test]
fn test_query_multiple_ors() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2"),
Task::new("other task").with_context("other"),
Task::new("task 3"),
Task::new("task 4"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast =
Parser::from("title = \"task 4\" or title = \"task 1\" or title = \"other task\"")
.parse()
.unwrap();
let mut res = list.search(ast).unwrap();
assert_eq!(
res.sort(),
vec![fixture[0].clone(), fixture[4].clone(), fixture[2].clone()].sort()
)
}
#[test]
fn test_query_grouped_expressions() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2"),
Task::new("other task").with_context("other"),
Task::new("task 3"),
Task::new("task 4"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from(
"(title = \"task 1\" and context = \"default\") or (context = \"other\")",
).parse()
.unwrap();
let mut res = list.search(ast).unwrap();
assert_eq!(
res.sort(),
vec![fixture[0].clone(), fixture[2].clone()].sort()
)
}
#[test]
fn test_query_string_literal_only() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2"),
Task::new("other task").with_context("other"),
Task::new("task 3"),
Task::new("task 4"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from("task").parse().unwrap();
let res = list.search(ast).unwrap();
assert_eq!(
res,
vec![
fixture[0].clone(),
fixture[1].clone(),
fixture[2].clone(),
fixture[3].clone(),
fixture[4].clone()
]
)
}
#[test]
fn test_query_priority_equals_1() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2"),
Task::new("other task").with_context("other"),
Task::new("task 3"),
Task::new("task 4"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from("priority = 1").parse().unwrap();
let res = list.search(ast).unwrap();
assert_eq!(
res,
vec![
fixture[0].clone(),
fixture[1].clone(),
fixture[2].clone(),
fixture[3].clone(),
fixture[4].clone()
]
)
}
#[test]
fn test_query_priority_gt_1() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2"),
Task::new("other task")
.with_context("other")
.with_priority(2),
Task::new("task 3"),
Task::new("task 4"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from("priority > 1").parse().unwrap();
let res = list.search(ast).unwrap();
assert_eq!(res, vec![fixture[2].clone()])
}
#[test]
fn test_completed_false() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2").precomplete(),
Task::new("other task")
.with_context("other")
.with_priority(2),
Task::new("task 3").precomplete(),
Task::new("task 4"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from("completed = false").parse().unwrap();
let res = list.search(ast).unwrap();
assert_eq!(
res,
vec![fixture[0].clone(), fixture[2].clone(), fixture[4].clone()]
)
}
#[test]
fn test_completed_true() {
let mut list = $setup();
let fixture = vec![
Task::new("task 1"),
Task::new("task 2").precomplete(),
Task::new("other task")
.with_context("other")
.with_priority(2),
Task::new("task 3").precomplete(),
Task::new("task 4"),
];
list.add_multiple(fixture.clone()).unwrap();
let ast = Parser::from("completed = true").parse().unwrap();
let res = list.search(ast).unwrap();
assert_eq!(res, vec![fixture[1].clone(), fixture[3].clone()])
}
};
}