Skip to content

Commit

Permalink
updates to clip testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbarbone committed Dec 3, 2024
1 parent e541aec commit e084199
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
20 changes: 18 additions & 2 deletions R/clipboard.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ write_clipboard <- function(x, ...) {
#' @export
#' @rdname clipboard
write_clipboard.default <- function(x, ...) {
x <- as.character(x)
clipr::write_clip(x, allow_non_interactive = TRUE)
}

Expand Down Expand Up @@ -88,7 +89,7 @@ read_clipboard <- function(method = read_clipboard_methods(), ...) {
fuj::require_namespace("clipr")
switch(
match_param(method),
default = type_convert2(clipr::read_clip(TRUE)),
default = type_convert2(clipr_read_clip(TRUE)),
tibble = ,
excel = ,
calc = ,
Expand All @@ -113,6 +114,21 @@ read_clipboard <- function(method = read_clipboard_methods(), ...) {
)
}

clipr_read_clip <- function(...) {
withCallingHandlers(
clipr::read_clip(...),
simpleWarning = function(e) {
if (grepl(
"System clipboard contained no readable text",

Check warning on line 122 in R/clipboard.R

View workflow job for this annotation

GitHub Actions / lint

file=R/clipboard.R,line=122,col=55,[trailing_whitespace_linter] Trailing whitespace is superfluous.
conditionMessage(e),
fixed = TRUE
)) {
tryInvokeRestart("muffleWarning")

Check warning on line 126 in R/clipboard.R

View workflow job for this annotation

GitHub Actions / lint

file=R/clipboard.R,line=126,col=41,[trailing_whitespace_linter] Trailing whitespace is superfluous.
}
}
)
}

#' @export
#' @rdname clipboard
read_clipboard_methods <- function() {
Expand Down Expand Up @@ -163,7 +179,7 @@ do_read_table_clipboard <- function(
...
) {
res <- utils::read.table(
file = textConnection(clipr::read_clip(TRUE)),
file = textConnection(clipr_read_clip(TRUE)),
header = header,
sep = sep,
row.names = row.names,
Expand Down
2 changes: 1 addition & 1 deletion man/reexports.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions tests/testthat/test-clipboard.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
need_clipr <- function() {
testthat::skip_if_not_installed("clipr")
testthat::skip_if_not(clipr::clipr_available(allow_non_interactive = TRUE))

Check warning on line 3 in tests/testthat/test-clipboard.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-clipboard.R,line=3,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
# not sure if this is needed
# testthat::skip_if_not(clipr::clipr_available(allow_non_interactive = TRUE))

Check warning on line 5 in tests/testthat/test-clipboard.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-clipboard.R,line=5,col=5,[commented_code_linter] Commented code should be removed.
}

test_that("clipboard", {
Expand All @@ -16,8 +18,11 @@ test_that("clipboard", {
test_clipboard(c(0.1234, -0.1586, 0.0001200))
test_clipboard(-1:4)
test_clipboard(as.Date("2020-01-02") + 0:4)
test_clipboard(runif(1e6))
test_clipboard(runif(1e4))

expect_error(clear_clipboard(), NA)
expect_equal(read_clipboard(), NULL) # previously ""

Check warning on line 25 in tests/testthat/test-clipboard.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-clipboard.R,line=25,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
x <- quick_dfl(
var1 = 1:3,
var2 = letters[1:3],
Expand All @@ -26,13 +31,14 @@ test_that("clipboard", {
)

expect_error(write_clipboard(x), NA)
expect_equal(read_clipboard("data.frame"), x)

expect_error(clear_clipboard(), NA)
expect_equal(read_clipboard(), NA) # previously ""
res <- read_clipboard("data.frame")
expect_s3_class(res, "data.frame")
if (package_available("tibble"))

Check warning on line 36 in tests/testthat/test-clipboard.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-clipboard.R,line=36,col=35,[trailing_whitespace_linter] Trailing whitespace is superfluous.
expect_equal(as.data.frame(res), x)

# finally test tibble
skip_if_not_installed("tibble")
expect_s3_class(res, "tbl_df")
expect_equal(read_clipboard("tibble"), tibble::as_tibble(x))
})

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-strings.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ test_that("Extract dates", {
})

test_that("print_c()", {
skip_if_not(is_windows(), "mark::write_clipboard() needs to be updated")
expect_equal(
utils::capture.output(print_c(1:3)),
c("c(", "1,", "2,", "3,", "NULL", ")")
Expand Down

0 comments on commit e084199

Please sign in to comment.