Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify replay #184

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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: 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
Loading