Skip to content

Commit

Permalink
create tests for paleta_create_brewer functions; fix #105
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestguevarra committed Jan 1, 2025
1 parent 2671e54 commit a411dac
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
58 changes: 43 additions & 15 deletions R/paleta_brewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
#'
#' @examples
#' paleta_create_sequential(n = 5, org = "acdc", name = "blues")
#' paleta_create_divergent(n = 5, org = "acdc", name = "rdylgn")
#'
#' @rdname create_paleta
#' @export
#'

paleta_create_sequential <- function(n, org, name) {
paleta_create_sequential <- function(n,
org = c("acdc", "nhs"),
name) {
## Get org ----
org <- match.arg(org)

## Check if specified palette is found in specified org palette ----
paleta_check_colour(name = name, org = org)

Expand All @@ -34,17 +40,21 @@ paleta_create_sequential <- function(n, org, name) {
## Check if number of colours is compatible with sequential ----
if (n < 3) {
cli::cli_bullets(
"!" = "Sequential palettes have minimum 3 colours",
"i" = "Returning 3 colours"
c(
"!" = "Sequential palettes have minimum 3 colours",
"i" = "Returning 3 colours"
)
)

n <- 3
}

if (n > 9) {
cli::cli_bullets(
"!" = "Sequential palettes have maximum 9 colours",
"i" = "Returning 9 colours"
c(
"!" = "Sequential palettes have maximum 9 colours",
"i" = "Returning 9 colours"
)
)

n <- 9
Expand All @@ -56,6 +66,15 @@ paleta_create_sequential <- function(n, org, name) {
## Update palette to n ----
pal <- grDevices::colorRampPalette(pal)(n)

cli::cli_bullets(
c(
c(
"v" = "Sequential colour palette successfully created",
"i" = "Sequential palette: {pal}"
)
)
)

## Create palette class ----
class(pal) <- "palette"

Expand All @@ -78,17 +97,21 @@ paleta_create_divergent <- function(n, name, org) {
## Check if number of colours is compatible with divergent ----
if (n < 3) {
cli::cli_bullets(
"!" = "Divergent palettes have minimum 3 colours",
"i" = "Returning 3 colours"
c(
"!" = "Divergent palettes have minimum 3 colours",
"i" = "Returning 3 colours"
)
)

n <- 3
}

if (n > 11) {
cli::cli_bullets(
"!" = "Divergent palettes have maximum 11 colours",
"i" = "Returning 11 colours"
c(
"!" = "Divergent palettes have maximum 11 colours",
"i" = "Returning 11 colours"
)
)

n <- 11
Expand All @@ -100,6 +123,13 @@ paleta_create_divergent <- function(n, name, org) {
## Update palette to n ----
pal <- grDevices::colorRampPalette(pal)(n)

cli::cli_bullets(
c(
"v" = "Divergent colour palette successfully created",
"i" = "Divergent palette: {pal}"
)
)

## Create palette class ----
class(pal) <- "palette"

Expand All @@ -124,8 +154,10 @@ paleta_create_qualitative <- function(n, name, org) {
## Check that n is not more than length(pal) ----
if (n > length(pal)) {
cli::cli_bullets(
"!" = "{.code n = {n}} is greater than available colours in {name} palette",
"i" = "Returning all colours in {name} colour palette"
c(
"!" = "{.code n = {n}} is greater than available colours in {name} palette",
"i" = "Returning all colours in {name} colour palette"
)
)

n <- length(pal)
Expand Down Expand Up @@ -222,13 +254,9 @@ paleta_check_type <- function(name,
cli::cli_abort(
"{name} is not a {pal_type} colour palette"
)

FALSE
} else {
cli::cli_alert_success(
"{name} is a {pal_type} colour palette"
)

TRUE
}
}
3 changes: 2 additions & 1 deletion man/create_paleta.Rd

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

2 changes: 2 additions & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ reference:
- title: Africa CDC
contents:
- acdc_palettes
- acdc_brewer_palettes
- acdc_fonts
- acdc_green
- acdc_red
Expand Down Expand Up @@ -127,6 +128,7 @@ reference:
- title: NHS
contents:
- nhs_palettes
- nhs_brewer_palettes
- nhs_fonts
- nhs_blue
- nhs_white
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-paleta_brewer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Tests for paleta_create functions --------------------------------------------

test_that("paleta_create functions work as expected", {
expect_vector(
paleta_create_sequential(n = 5, org = "acdc", name = "blues"),
size = 5
)
expect_vector(
paleta_create_divergent(n = 5, org = "acdc", name = "rdylgn"),
size = 5
)
expect_vector(
paleta_create_qualitative(n = 5, "pastel1", "acdc"),
size = 5
)
expect_message(paleta_create_sequential(n = 2, org = "acdc", name = "blues"))
expect_message(paleta_create_sequential(n = 10, org = "acdc", name = "blues"))
expect_message(paleta_create_divergent(n = 2, org = "acdc", name = "rdylgn"))
expect_message(paleta_create_divergent(n = 12, org = "acdc", name = "rdylgn"))
expect_message(paleta_create_qualitative(n = 12, "pastel1", "acdc"))

expect_vector(
paleta_create_brewer(n = 5, name = "blues", org = "nhs")
)

expect_error(paleta_create_brewer(n = 5, name = "ylorbr", org = "nhs"))
expect_error(
paleta_create_brewer(n = 5, name = "blues", org = "nhs", type = "divergent")
)
})

0 comments on commit a411dac

Please sign in to comment.