Skip to content

Commit

Permalink
include current test contents in prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Nov 18, 2024
1 parent f1ddebc commit f51fc37
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
34 changes: 25 additions & 9 deletions R/test_this.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
#'
#' @export
test_this <- function() {
context <- rstudioapi::getActiveDocumentContext()
context <- rstudioapi::getSourceEditorContext()
test_helper <- retrieve_test_helper()

# TODO: ensure that the file is a .R file in an `R/` directory
turn <- assemble_turn(context)
test_lines <- retrieve_test(context$path)

test_file <- open_test(context$path)
# TODO: ensure that the file is a .R file in an `R/` directory
turn <- assemble_turn(context, test_lines)

tryCatch(
stream_inline(
Expand All @@ -41,10 +41,11 @@ test_this <- function() {
invisible(TRUE)
}

assemble_turn <- function(context) {
assemble_turn <- function(context, test_lines) {
# TODO: handle case where there's nothing there, in which case test
# the whole document
paste0(

res <-
c(
"## Context",
"",
Expand All @@ -55,9 +56,24 @@ assemble_turn <- function(context) {
"## Selection",
"",
rstudioapi::primary_selection(context)$text
),
collapse = "\n"
)
)

# TODO: if there are no tests for the current file, it may still be
# nice to include tests from an adjacent file.
if (!is.null(test_lines)) {
res <- c(
res,
"",
"The current tests look like this:",
"",
test_lines,
"",
"Do your best to pattern-match how the existing tests create objects to test against.",
""
)
}

paste0(res, collapse = "\n")
}

# TODO: more variables can be replaced with constants here, and logic
Expand Down
18 changes: 14 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ retrieve_test_helper <- function() {
test_helper()
}

open_test <- function(path) {
retrieve_test <- function(path) {
name <- basename(path)
res <- file.path("tests", "testthat", paste0("test-", name))
usethis::edit_file(res)
file <- file.path("tests", "testthat", paste0("test-", name))

res
if (file.exists(file)) {
lines <- readLines(file)
if (identical(lines, "")) {
lines <- NULL
}
} else {
lines <- NULL
}

usethis::edit_file(file)

lines
}

0 comments on commit f51fc37

Please sign in to comment.