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 e8c0ef0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 14 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
101 changes: 87 additions & 14 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 @@ -1379,6 +1381,7 @@ impl TTApp {
}
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 Down Expand Up @@ -1742,20 +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>>();
let tags = vec!["finance", "UNBLOCKED", "PENDING", "TAGGED", "UDA"]
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>();
dbg!(task.tags());
for tag in tags {
assert!(task.tags().unwrap().contains(&tag));
Expand Down Expand Up @@ -1831,4 +1824,84 @@ mod tests {
assert_eq!(app.tasks.lock().unwrap().len(), 26);
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());
dbg!(now.year());
dbg!(now.month());
dbg!(now.day());

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);
dbg!(&s);
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", "OVERDUE", "PENDING", "QUARTER", "TODAY", "UDA"] {
assert!(task.tags().unwrap().contains(&s.to_string()));
}

// Name Value
// ID 27
// Description new task for testing earlier today
// Status Pending
// Entered 2021-02-17 23:50:54
// Due 2021-02-17 00:00:00
// Last modified 2021-02-17 23:50:54
// Virtual tags DUE DUETODAY LATEST MONTH OVERDUE PENDING QUARTER READY TODAY UDA
// UNBLOCKED WEEK YEAR
// UUID d94af0e3-799a-43c2-9d26-c361f0a96c04
// Urgency 11.05
// Est 30

// due 0.771 * 12 = 9.25
// UDA priority. 1 * 1.8 = 1.8
// ------
// 11.05

let output = Command::new("task")
.arg("rc.confirmation=off")
.arg("undo")
.output()
.unwrap();
dbg!(String::from_utf8_lossy(&output.stdout));

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 e8c0ef0

Please sign in to comment.