Skip to content

Commit

Permalink
Simplify replay (#184)
Browse files Browse the repository at this point in the history
Add get test coverage to 100%
  • Loading branch information
hadley authored Jun 26, 2024
1 parent 8d2e7ad commit 23aa1f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
5 changes: 1 addition & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ S3method(parse_all,connection)
S3method(parse_all,default)
S3method(print,evaluate_evaluation)
S3method(replay,character)
S3method(replay,condition)
S3method(replay,default)
S3method(replay,error)
S3method(replay,list)
S3method(replay,message)
S3method(replay,recordedplot)
S3method(replay,source)
S3method(replay,value)
S3method(replay,warning)
export(create_traceback)
export(evaluate)
export(flush_console)
Expand Down
26 changes: 4 additions & 22 deletions R/replay.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#' message("Goodbye")
#' }
#' replay(evaluate("f2()"))
replay <- function(x) UseMethod("replay", x)
replay <- function(x) {
UseMethod("replay", x)
}

#' @export
replay.list <- function(x) {
Expand All @@ -45,25 +47,10 @@ replay.source <- function(x) {
}

#' @export
replay.warning <- function(x) {
cat_line(format_condition(x))
}

#' @export
replay.message <- function(x) {
cat_line(format_condition(x))
}

#' @export
replay.error <- function(x) {
replay.condition <- function(x) {
cat_line(format_condition(x))
}

#' @export
replay.value <- function(x) {
if (x$visible) print(x$value)
}

#' @export
replay.recordedplot <- function(x) {
print(x)
Expand All @@ -88,14 +75,9 @@ format_condition <- function(x) {
}

body <- conditionMessage(x)
if (inherits(x, "message")) {

}

paste0(header, "\n", body)
}


#' Line prompt.
#'
#' Format a single expression as if it had been entered at the command prompt.
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/replay.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
Error in f():
4

# replace nicely formats multiple lines

Code
replay(ev)
Output
> 1 +
+ 2
[1] 3

# format_condition handles different types of warning

Code
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-replay.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ test_that("replay handles rlang conditions", {
expect_snapshot(replay(ev))
})

test_that("replace nicely formats multiple lines", {
ev <- evaluate("1 + \n 2")
expect_snapshot(replay(ev))
})

test_that("can replay plots", {
ev <- evaluate("plot(1)")

path <- withr::local_tempfile()
pdf(path)
expect_output(replay(ev))
dev.off()

expect_true(file.exists(path))
})

test_that("format_condition handles different types of warning", {
expect_snapshot({
w1 <- simpleWarning("This is a warning")
Expand Down

0 comments on commit 23aa1f5

Please sign in to comment.