Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed Feb 18, 2021
1 parent 4da96f4 commit bff4be7
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Expand Up @@ -41,6 +41,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --all -- --test-threads=1

fmt:
name: Rustfmt
Expand Down
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -18,6 +18,7 @@ default = ["crossterm-backend"]
crossterm-backend = ["tui/crossterm", "crossterm"]

[dependencies]
regex = "1"
itertools = "0.9"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
184 changes: 154 additions & 30 deletions src/app.rs
Expand Up @@ -45,6 +45,8 @@ use rustyline::Word;
use std::io;
use tui::{backend::CrosstermBackend, Terminal};

use regex::Regex;

const MAX_LINE: usize = 4096;

pub fn cmp(t1: &Task, t2: &Task) -> Ordering {
Expand Down Expand Up @@ -1377,8 +1379,12 @@ impl TTApp {
if (reference - chrono::Duration::nanoseconds(1)).month() % 4 == now.month() % 4 {
add_tag(&mut task, "QUARTER".to_string());
}
if reference.year() == now.year() {
add_tag(&mut task, "YEAR".to_string());
}
match get_date_state(&d, self.config.due) {
DateState::EarlierToday | DateState::LaterToday => {
add_tag(&mut task, "DUE".to_string());
add_tag(&mut task, "TODAY".to_string());
add_tag(&mut task, "DUETODAY".to_string());
}
Expand All @@ -1387,9 +1393,6 @@ impl TTApp {
if reference.day() == now.day() + 1 {
add_tag(&mut task, "TOMORROW".to_string());
}
if reference.year() == now.year() {
add_tag(&mut task, "YEAR".to_string());
}
}
_ => (),
}
Expand Down Expand Up @@ -1742,21 +1745,10 @@ mod tests {

let app = TTApp::new().unwrap();
let task = app.task_by_id(11).unwrap();
let tags = vec![
"COLOR",
"PENDING",
"ANNOTATED",
"TAGGED",
// "MONTH",
// "QUARTER",
// "DUE",
// "TOMORROW",
// "YEAR",
]
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
dbg!(task.tags());
let tags = vec!["finance", "UNBLOCKED", "PENDING", "TAGGED", "UDA"]
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
for tag in tags {
assert!(task.tags().unwrap().contains(&tag));
}
Expand All @@ -1767,33 +1759,30 @@ mod tests {
let app = TTApp::new().unwrap();
let task = app.task_by_id(1).unwrap();
for r in vec![
"deleted",
"completed",
"active",
"keyword.",
"tag.",
"project.",
"overdue",
"scheduled",
"due.today",
"due",
"blocked",
"blocking",
"completed",
"deleted",
"due",
"due.today",
"keyword.",
"overdue",
"project.",
"recurring",
"scheduled",
"tag.",
"tagged",
"uda.",
] {
assert!(app.config.rule_precedence_color.contains(&r.to_string()));
}
let style = app.style_for_task(&task);

dbg!(style);
assert_eq!(style, Style::default().fg(Color::Indexed(2)));

let task = app.task_by_id(11).unwrap();
dbg!(task.tags().unwrap());
let style = app.style_for_task(&task);
dbg!(style);
}

#[test]
Expand Down Expand Up @@ -1831,4 +1820,139 @@ mod tests {
assert_eq!(app.tasks.lock().unwrap().len(), 26);
assert_eq!(app.current_context_filter, "");
}

#[test]
fn test_task_tomorrow() {
let total_tasks: u64 = 26;

let mut app = TTApp::new().unwrap();
assert!(app.get_context().is_ok());
assert!(app.update().is_ok());
assert_eq!(app.tasks.lock().unwrap().len(), total_tasks as usize);
assert_eq!(app.current_context_filter, "");

let now = Local::now();
let now = TimeZone::from_utc_datetime(now.offset(), &now.naive_utc());

let mut command = Command::new("task");
command.arg("add");
let message = format!(
"'new task for testing tomorrow' due:{:04}-{:02}-{:02}",
now.year(),
now.month(),
now.day() + 1
);

let shell = message.as_str().replace("'", "\\'");
let cmd = shlex::split(&shell).unwrap();
for s in cmd {
command.arg(&s);
}
let output = command.output().unwrap();
let s = String::from_utf8_lossy(&output.stdout);
let re = Regex::new(r"^Created task (?P<task_id>\d+).\n$").unwrap();
let caps = re.captures(&s).unwrap();
let task_id = caps["task_id"].parse::<u64>().unwrap();
assert_eq!(task_id, total_tasks + 1);

assert!(app.get_context().is_ok());
assert!(app.update().is_ok());
assert_eq!(app.tasks.lock().unwrap().len(), (total_tasks + 1) as usize);
assert_eq!(app.current_context_filter, "");

let task = app.task_by_id(task_id).unwrap();

for s in &[
"DUE",
"MONTH",
"PENDING",
"QUARTER",
"TOMORROW",
"UDA",
"UNBLOCKED",
"YEAR",
] {
assert!(task.tags().unwrap().contains(&s.to_string()));
}

let output = Command::new("task")
.arg("rc.confirmation=off")
.arg("undo")
.output()
.unwrap();

let mut app = TTApp::new().unwrap();
assert!(app.get_context().is_ok());
assert!(app.update().is_ok());
assert_eq!(app.tasks.lock().unwrap().len(), total_tasks as usize);
assert_eq!(app.current_context_filter, "");
}

#[test]
fn test_task_earlier_today() {
let total_tasks: u64 = 26;

let mut app = TTApp::new().unwrap();
assert!(app.get_context().is_ok());
assert!(app.update().is_ok());
assert_eq!(app.tasks.lock().unwrap().len(), total_tasks as usize);
assert_eq!(app.current_context_filter, "");

let now = Local::now();
let now = TimeZone::from_utc_datetime(now.offset(), &now.naive_utc());

let mut command = Command::new("task");
command.arg("add");
let message = format!(
"'new task for testing earlier today' due:{:04}-{:02}-{:02}",
now.year(),
now.month(),
now.day()
);

let shell = message.as_str().replace("'", "\\'");
let cmd = shlex::split(&shell).unwrap();
for s in cmd {
command.arg(&s);
}
let output = command.output().unwrap();
let s = String::from_utf8_lossy(&output.stdout);
let re = Regex::new(r"^Created task (?P<task_id>\d+).\n$").unwrap();
let caps = re.captures(&s).unwrap();
let task_id = caps["task_id"].parse::<u64>().unwrap();
assert_eq!(task_id, total_tasks + 1);

assert!(app.get_context().is_ok());
assert!(app.update().is_ok());
assert_eq!(app.tasks.lock().unwrap().len(), (total_tasks + 1) as usize);
assert_eq!(app.current_context_filter, "");

let task = app.task_by_id(task_id).unwrap();
for s in &[
"DUE",
"DUETODAY",
"MONTH",
"OVERDUE",
"PENDING",
"QUARTER",
"TODAY",
"UDA",
"UNBLOCKED",
"YEAR",
] {
assert!(task.tags().unwrap().contains(&s.to_string()));
}

let output = Command::new("task")
.arg("rc.confirmation=off")
.arg("undo")
.output()
.unwrap();

let mut app = TTApp::new().unwrap();
assert!(app.get_context().is_ok());
assert!(app.update().is_ok());
assert_eq!(app.tasks.lock().unwrap().len(), total_tasks as usize);
assert_eq!(app.current_context_filter, "");
}
}

0 comments on commit bff4be7

Please sign in to comment.