diff --git a/R/as_color_vctr.R b/R/as_color_vctr.R index 0463cdd..b98a70d 100644 --- a/R/as_color_vctr.R +++ b/R/as_color_vctr.R @@ -1,6 +1,8 @@ #' Coerce object to a color_vctr #' -#' Coersion function for generation of a color_vctr from an existing vector +#' Coercion function for generation of a color_vctr from an existing vector +#' +#' @rdname as_color_vctr #' #' @param x object to coerce #' @param text_color A vector of length 1 or same length as vect. Details the @@ -18,12 +20,14 @@ as_color_vctr <- function(x, text_color = NA, background = NA, style = NA){ UseMethod("as_color_vctr",x) } +#' @rdname as_color_vctr #' @export as_color_vctr.default <- function(x, text_color = NA, background = NA, style = NA){ stop(gettextf("cannot coerce class %s to a color_vctr", sQuote(deparse(class(x))[1L])), domain = NA) } +#' @rdname as_color_vctr #' @export as_color_vctr.numeric <- function(x,text_color = NA, background = NA, style = NA){ new_color_vctr(x, @@ -32,17 +36,20 @@ as_color_vctr.numeric <- function(x,text_color = NA, background = NA, style = NA style = style) } +#' @rdname as_color_vctr #' @export as_color_vctr.color_vctr<- function(x,text_color = NA, background = NA, style = NA){ x } +#' @rdname as_color_vctr #' @export as_color_vctr.character <- as_color_vctr.numeric #' @export as_color_vctr.integer <- as_color_vctr.numeric +#' @rdname as_color_vctr #' @export as_color_vctr.logical <- as_color_vctr.numeric diff --git a/man/as_color_vctr.Rd b/man/as_color_vctr.Rd index fa826a2..0c76035 100644 --- a/man/as_color_vctr.Rd +++ b/man/as_color_vctr.Rd @@ -2,9 +2,24 @@ % Please edit documentation in R/as_color_vctr.R \name{as_color_vctr} \alias{as_color_vctr} +\alias{as_color_vctr.default} +\alias{as_color_vctr.numeric} +\alias{as_color_vctr.color_vctr} +\alias{as_color_vctr.character} +\alias{as_color_vctr.logical} \title{Coerce object to a color_vctr} \usage{ as_color_vctr(x, text_color = NA, background = NA, style = NA) + +\method{as_color_vctr}{default}(x, text_color = NA, background = NA, style = NA) + +\method{as_color_vctr}{numeric}(x, text_color = NA, background = NA, style = NA) + +\method{as_color_vctr}{color_vctr}(x, text_color = NA, background = NA, style = NA) + +\method{as_color_vctr}{character}(x, text_color = NA, background = NA, style = NA) + +\method{as_color_vctr}{logical}(x, text_color = NA, background = NA, style = NA) } \arguments{ \item{x}{object to coerce} @@ -22,5 +37,5 @@ style of the text Valid values can be found from the `valid_style()` function. NA means no styling.} } \description{ -Coersion function for generation of a color_vctr from an existing vector +Coercion function for generation of a color_vctr from an existing vector } diff --git a/tests/testthat/test-color_vctr_utils.R b/tests/testthat/test-color_vctr_utils.R index 5a709dc..47692dd 100644 --- a/tests/testthat/test-color_vctr_utils.R +++ b/tests/testthat/test-color_vctr_utils.R @@ -20,96 +20,3 @@ test_that("Subsetting the vector results in a smaller color_vctr", { ) }) - - -test_that("Assignment of a color_vctr preserves the styling", { - example_color_vctr <- color_vctr( - 1, - style = "underline", - text_color = "blue" - ) - - example_color_vctr[2] <- color_vctr( - 2, - style = "strikethrough", - text_color = "magenta" - ) - - expect_equal( - example_color_vctr, - color_vctr( - c(1,2), - style = c("underline", "strikethrough"), - text_color = c("blue", "magenta") - ) - ) -}) - -test_that("Assignment of a color_vctr fills with NA when necessary", { - example_color_vctr <- color_vctr( - 1, - style = "underline", - text_color = "blue" - ) - - example_color_vctr[4] <- color_vctr( - 2, - style = "strikethrough", - text_color = "magenta" - ) - - expect_equal( - example_color_vctr, - color_vctr( - c(1,NA, NA, 2), - style = c("underline", NA, NA, "strikethrough"), - text_color = c("blue", NA, NA, "magenta") - ) - ) -}) - -test_that("Assignment of a color_vctr can be with matching atomics as well", { - example_color_vctr <- color_vctr( - 1, - style = "underline", - text_color = "blue" - ) - - example_color_vctr[2] <- 2 - - expect_equal( - example_color_vctr, - color_vctr( - c(1, 2), - style = c("underline", NA ), - text_color = c("blue", NA ) - ) - ) - -}) - - -test_that("If assignment of a color_vctr is larger then vector being added, it will use a subset of the input data", { - example_color_vctr <- color_vctr( - 1, - style = "underline", - text_color = "blue" - ) - - expect_warning({ - example_color_vctr[2] <- color_vctr( - c(2,3), - style = "strikethrough", - text_color = "magenta" - )}, - "number of items to replace is not a multiple of replacement length") - - expect_equal( - example_color_vctr, - color_vctr( - c(1,2), - style = c("underline", "strikethrough"), - text_color = c("blue", "magenta") - ) - ) -})