Skip to content

Commit

Permalink
add can_undo and can_redo; fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
daattali committed Nov 19, 2022
1 parent afdefa9 commit 5fd71c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/undomanager.R
Expand Up @@ -5,6 +5,8 @@
#' using undo and redo operations.
#'
#' @field value Get the value
#' @field can_undo Whether there are any undo operations available
#' @field can_redo Whether there are any redo operations available
#' @field undo_size Get the number of undo operations
#' @field redo_size Get the number of redo operations
#' @examples
Expand Down Expand Up @@ -33,6 +35,14 @@ UndoManager <- R6::R6Class(

redo_size = function() {
length(private$.redo_stack)
},

can_undo = function() {
self$undo_size > 0
},

can_redo = function() {
self$redo_size > 0
}

),
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-undomanager.R
Expand Up @@ -34,6 +34,20 @@ test_that("UndoManager with multiple types", {
expect_error(UndoManager$new(c("character", "integer", "numeric"))$do("a")$do(1)$do(1L), NA)
})

test_that("UndoManager can_undo and can_redo", {
expect_false(UndoManager$new()$can_undo)
expect_false(UndoManager$new()$do(1)$can_undo)
expect_true(UndoManager$new()$do(1)$do(2)$can_undo)
expect_false(UndoManager$new()$do(1)$do(2)$undo()$can_undo)
expect_true(UndoManager$new()$do(1)$do(2)$undo()$redo()$can_undo)

expect_false(UndoManager$new()$can_redo)
expect_false(UndoManager$new()$do(1)$can_redo)
expect_false(UndoManager$new()$do(1)$do(2)$can_redo)
expect_true(UndoManager$new()$do(1)$do(2)$undo()$can_redo)
expect_false(UndoManager$new()$do(1)$do(2)$undo()$redo()$can_redo)
})

test_that("UndoManager do works", {
expect_identical(UndoManager$new()$do(5)$value, 5)
expect_identical(UndoManager$new()$do(5)$undo_size, 0L)
Expand Down

0 comments on commit 5fd71c7

Please sign in to comment.