Skip to content
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
cli progress bars. It makes cli redraw the progress bar for every
progress update.

* `hash_raw_animal()` and `hash_raw_emoji()` now validate `n_adj` and `size`,
respectively, consistently with their character-vector variants. This also
affects `hash_obj_animal()` and `hash_obj_emoji()` (#834, @fly1d).

* Fix issues with `ansi_strwrap()` having `\r` in the string (#667)

# cli 3.6.6
Expand Down
17 changes: 15 additions & 2 deletions R/hash.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,13 @@ hash_emoji1_transform <- function(md5, size) {
#' * `names`: names of the emojis, in a character vector.

hash_raw_emoji <- function(x, size = 3) {
stopifnot(is.raw(x))
stopifnot(
is.raw(x),
is_count(size),
# Match hash_emoji(): at least one emoji, and at most four to keep
# nrow(emojis)^size within the exact integer range of an R double.
size >= 1 && size <= 4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment where these values are from?

)
md5 <- hash_raw_md5(x)
emo <- hash_emoji1_transform(md5, size)

Expand Down Expand Up @@ -492,7 +498,14 @@ hash_animal1_transform <- function(md5, n_adj) {
#' * `words: the adjectives and the animal name in a character vector.

hash_raw_animal <- function(x, n_adj = 2) {
stopifnot(is.raw(x))
stopifnot(
is.raw(x),
is_count(n_adj),
# Match hash_animal(): zero adjectives leaves just the animal name;
# at most three keeps length(gfycat_adjectives)^n_adj *
# length(gfycat_animals) within the exact integer range of an R double.
n_adj >= 0 && n_adj <= 3

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

)
md5 <- hash_raw_md5(x)
hash <- hash_animal1_transform(md5, n_adj)

Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-hash.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ test_that("hash_obj_emoji", {
})
})

test_that("emoji hash size is validated for raw vectors and objects", {
expect_no_error(hash_raw_emoji(raw(), size = 1))
expect_no_error(hash_raw_emoji(raw(), size = 4))
expect_error(hash_raw_emoji(raw(), size = 0), class = "simpleError")
expect_error(hash_raw_emoji(raw(), size = 1.5), class = "simpleError")
expect_error(hash_raw_emoji(raw(), size = 5), class = "simpleError")
expect_error(hash_obj_emoji(NULL, size = 0), class = "simpleError")
expect_error(hash_obj_emoji(NULL, size = 5), class = "simpleError")
})

test_that("hash_animal", {
expect_snapshot({
hash_animal(character())$words
Expand Down Expand Up @@ -193,6 +203,16 @@ test_that("hash_obj_animal", {
})
})

test_that("animal hash adjective count is validated for raw vectors and objects", {
expect_no_error(hash_raw_animal(raw(), n_adj = 0))
expect_no_error(hash_raw_animal(raw(), n_adj = 3))
expect_error(hash_raw_animal(raw(), n_adj = -1), class = "simpleError")
expect_error(hash_raw_animal(raw(), n_adj = 1.5), class = "simpleError")
expect_error(hash_raw_animal(raw(), n_adj = 4), class = "simpleError")
expect_error(hash_obj_animal(NULL, n_adj = -1), class = "simpleError")
expect_error(hash_obj_animal(NULL, n_adj = 4), class = "simpleError")
})

test_that("hash_xxhash", {
expect_snapshot({
hash_xxhash(letters[1:5])
Expand Down
Loading