Skip to content

Commit

Permalink
better plot function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jtrecenti committed Mar 1, 2023
1 parent cd255cd commit bbdb7a1
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 15 deletions.
1 change: 0 additions & 1 deletion R/generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ captcha_generate <- function(write_disk = FALSE,
magick::image_trim() |>
magick::image_resize(stringr::str_glue("{n_cols}x{n_rows}"))


if (stats::runif(1) < p_implode) {
m_text <- magick::image_implode(m_text, factor = stats::runif(1, 0, .4))
}
Expand Down
16 changes: 8 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ plot.captcha <- function(x, y, ...) {
op <- graphics::par(no.readonly = TRUE)
graphics::par(mar = rep(0, 4L))
if (length(img) > 1) {
img |>
magick::image_border(color = "white", geometry = "5x5") |>
magick::image_montage(
tile = stringr::str_glue("{columns}x{rows}"),
geometry = stringr::str_glue(
"{getOption('captcha.print.height')}x0+0+0"
)
) |>
append <- function(ii, img) {
magick::image_append(
img[((ii-1)*columns+1):(min(ii*columns, length(img)))]
)
}
purrr::map(seq_len(rows), append, img) |>
magick::image_join() |>
magick::image_append(TRUE) |>
graphics::plot()
} else {
graphics::plot(img)
Expand Down
7 changes: 4 additions & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ utils::globalVariables(c("super"))
options(captcha.print.height = 150)
}

check_magick_ghostscript <- function(error = TRUE) {
check_magick_ghostscript <- function(error = TRUE,
test_error_message = FALSE) {
has_ghostscript <- magick::magick_config()$ghostscript
if (!has_ghostscript) {
if (!has_ghostscript || test_error_message) {
message <- paste(
"This package needs ImageMagick with ghostscript enabled",
"to run captcha_generate() and plot.captcha() functions.",
Expand All @@ -18,7 +19,7 @@ check_magick_ghostscript <- function(error = TRUE) {
"https://docs.ropensci.org/magick/#installation.",
sep = "\n"
)
if (error) stop(message)
if (error) stop(message) else message(message)

return(invisible(FALSE))
}
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/utils.new.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# print captcha as expected

Code
print(captcha)
Output
format width height colorspace matte filesize density
1 PNG 160 50 sRGB TRUE 2462 72x72

Binary file modified tests/testthat/_snaps/utils/plot_many.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions tests/testthat/test-generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test_that("generate rcaptcha, save", {
test_that("generate rcaptcha, don't save", {

if (check_magick_ghostscript(error = FALSE)) {
set.seed(1)
rcaptcha <- captcha_generate(write_disk = FALSE)
expect_s3_class(rcaptcha, "captcha")
expect_null(rcaptcha$path)
Expand All @@ -27,6 +28,26 @@ test_that("generate rcaptcha, don't save", {

})

test_that("random effects work", {

if (check_magick_ghostscript(error = FALSE)) {
set.seed(1)
rcaptcha <- captcha_generate(
write_disk = FALSE,
p_implode = 1,
p_oilpaint = 1,
p_noise = 1,
p_lat = 1
)
expect_s3_class(rcaptcha, "captcha")
expect_null(rcaptcha$path)
expect_type(rcaptcha$lab, "character")
} else {
expect_error(captcha_generate(write_disk = FALSE))
}

})

test_that("number of chars is correct", {

if (check_magick_ghostscript(error = FALSE)) {
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ test_that("ghostscript works", {

if (has_ghostscript) {
expect_true(check_magick_ghostscript())
} else {
expect_error(check_magick_ghostscript())
expect_false(check_magick_ghostscript(error = FALSE))
}
expect_error(check_magick_ghostscript(TRUE, TRUE))
expect_message(expect_false(
check_magick_ghostscript(FALSE, TRUE)
), "ImageMagick")

})

0 comments on commit bbdb7a1

Please sign in to comment.