Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/test_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Item 1
Item 2
Item 3
Item 4
Item 5
124 changes: 124 additions & 0 deletions test/test_script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
suppressPackageStartupMessages({
library(testthat)
})

# Run like:
#jh1889@mirovia:~/work/teaching/SEPwC_assessments/sediment_assessment/test$ Rscript test_script.R
# Test passed 🥇
# Test passed 🌈
# Test passed 🎊
# Test passed 🥳
# ── Warning: check main#
# ...
#

# load in the script you want to test
source("../todo.R", local=TRUE)
test_file <- "test_list.txt"


# tests --------------------
test_that("list_tasks works correctly", {
# Assuming test_file is defined and the necessary setup is done

TASK_FILE <<- test_file

task_list <- list_tasks()
expected_list <- "1. Item 1\n2. Item 2\n3. Item 3\n4. Item 4\n5. Item 5"

expect_equal(task_list, expected_list)
})

test_that("remove_task works correctly", {
# Assuming test_file is defined and the necessary setup is done

temp_list <- ".temp_test.txt"
file.copy(test_file, temp_list, overwrite=TRUE)
TASK_FILE <<- temp_list

# Test invalid index (should error)
expect_error(remove_task(0))

# Test invalid index (should error)
expect_error(remove_task(10))

# Remove the first task
remove_task(1)

task_list <- list_tasks()
new_expected <- "1. Item 2\n2. Item 3\n3. Item 4\n4. Item 5"

expect_equal(task_list, new_expected)

file.remove(temp_list)
})

test_that("add_task works correctly", {
# Assuming test_file is defined and the necessary setup is done

temp_list <- ".temp_test.txt"
file.copy(test_file, temp_list, overwrite=TRUE)
TASK_FILE <<- temp_list

add_task("Item 6")

task_list <- list_tasks()
expected_list <- "1. Item 1\n2. Item 2\n3. Item 3\n4. Item 4\n5. Item 5\n6. Item 6"

expect_equal(task_list, expected_list)

file.remove(temp_list)
})

test_that("check main", {

temp_list <- ".temp_test.txt"
file.copy(test_file, temp_list, overwrite=TRUE)
TASK_FILE <<- temp_list

# parse_args sets all arguments, so we need to create them all
args <- c("list" = TRUE, "add" = NULL, "remove" = NULL)
args <- data.frame(t(args))
output <- capture.output(main(args))
expect_equal(nchar(output), 59)

args <- c("add" = "Item 6", "list" = FALSE, "remove" = NULL)
args <- data.frame(t(args))
output <- capture.output(main(args))
expect_identical(output, character(0))

args <- c("list" = TRUE, "add" = NULL, "remove" = NULL)
args <- data.frame(t(args))
output <- capture.output(main(args))
expect_equal(nchar(output), 70)
expected_list <- "[1] \"1. Item 1\\n2. Item 2\\n3. Item 3\\n4. Item 4\\n5. Item 5\\n6. Item 6\""
expect_equal(output, expected_list)

args <- c("remove" = 6, "add" = NULL, "list" = FALSE)
args <- data.frame(t(args))
output <- capture.output(main(args))
expect_lt(nchar(output), 25)


file.remove(temp_list)

})


if (requireNamespace("lintr")) {
library(lintr)

context("linting script")
test_that("Coding style", {
output<-lintr::lint("../todo.R")
expect_lt(length(output),500)
expect_lt(length(output),400)
expect_lt(length(output),250)
expect_lt(length(output),100)
expect_lt(length(output),50)
expect_lt(length(output),10)
expect_equal(length(output),0)
})
}


82 changes: 82 additions & 0 deletions todo.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env Rscript
suppressPackageStartupMessages({
library(argparse)
})

TASK_FILE <- ".tasks.txt" # nolint

add_task <- function(task) {
# Code written with assistance of Google Gemini
write(task,
file = TASK_FILE,
append = TRUE)
}

list_tasks <- function() {
# Code written with assistance of Google Gemini
if (file.exists(TASK_FILE)) {
tasks <- readLines(TASK_FILE)
if (length(tasks) > 0) {
result <- character(0) # Initialize an empty character vector
for (i in seq_along(tasks)) {
result <- c(result, paste0(i, ". ", tasks[i]))
}
return(paste0(result, collapse = "\n")) # Return as single string
} else {
return("No tasks found in .tasks.txt\n")
}
} else {
stop("Task file .tasks.txt does not exist yet.\n")
}
}

remove_task <- function(index) {
# Code written with assistance of Google Gemini
if (file.exists(TASK_FILE)) {
tasks <- readLines(TASK_FILE)
index <- as.integer(index) # Ensure it's an integer

if (!is.na(index) && index >= 1 && index <= length(tasks)) {
removed_task <- tasks[index]
tasks <- tasks[-index]
writeLines(tasks, TASK_FILE)
cat(paste0("Task '", removed_task, "' (index ", index, ") removed from ", TASK_FILE, "\n"))
} else {
stop(paste0("Error: Invalid task index '", index, "' in ", TASK_FILE, "\n"))
}
} else {
stop("Error: Task file .tasks.txt does not exist.\n")
}
}

main <- function(args) {

if (!is.null(args$add)) {
add_task(args$add)
} else if (args$list) {
tasks <- list_tasks()
cat(tasks) # change print to cat
} else if (!is.null(args$remove)) {
remove_task(args$remove)
} else {
cat("Use --help to get help on using this program\n") # change print to cat and add a newline
}
}


if (sys.nframe() == 0) {

# main program, called via Rscript
parser <- ArgumentParser(description = "Command-line Todo List")
parser$add_argument("-a", "--add",
help = "Add a new task")
parser$add_argument("-l", "--list",
action = "store_true",
help = "List all tasks")
parser$add_argument("-r", "--remove",
help = "Remove a task by index")

args <- parser$parse_args()
main(args)
}