tests: Add cvb tests for covar_sanitize
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Also fix a cvb related bug in covar_sanitize when cvb is used on its own.
This commit is contained in:
parent
563db88189
commit
b103cc50e9
|
@ -53,6 +53,7 @@ cvar_vec_to_numeric <- function(cvar) {
|
|||
all_char <- is.character(cvar) & length(names(cvar)) == 0
|
||||
mix_char <- is.character(cvar) & length(names(cvar)) != 0
|
||||
is_empty <- length(cvar) == 0
|
||||
is_cvb <- is(cvar, "cvb")
|
||||
# No need to do conversion on fully named numeric vectors, or if they are
|
||||
# cbvecs
|
||||
outvec <- cvar
|
||||
|
@ -74,6 +75,8 @@ cvar_vec_to_numeric <- function(cvar) {
|
|||
# And covert to numeric
|
||||
outvec <- as.numeric(outvec)
|
||||
names(outvec) <- ncvar
|
||||
} else if (is_cvb) {
|
||||
outvec <- new("cbvec", values = list(outvec))
|
||||
}
|
||||
|
||||
outvec
|
||||
|
@ -182,9 +185,12 @@ covar_sanitize <- function(covars = list(), ftypes = list()) {
|
|||
} else {
|
||||
cv <- vector(mode = "numeric", length = 0)
|
||||
}
|
||||
# Make all stand-alone cvb's into cbvecs
|
||||
if (is(cv, "cvb")) cv <- new("cbvec", values = list(cv))
|
||||
# There are 3 ways one can specify covars, handle all of them
|
||||
if (is.list(cv)) {
|
||||
out <- cvar_list_to_list(cv, ft, ftypes)
|
||||
} else if (is(cv, "cbvec") | is(cv, "cvb") | is.vector(cv)) {
|
||||
} else if (is(cv, "cbvec") | is.vector(cv)) {
|
||||
out <- cvar_vec_to_list(cv, ft, ftypes)
|
||||
}
|
||||
out
|
||||
|
|
|
@ -99,6 +99,74 @@ test_that("cvb cannot be combined with non-character/numeric elements", {
|
|||
expect_error(c(cv1, cv2, data.frame(one = 1)))
|
||||
})
|
||||
|
||||
test_that("covar_sanitize works with cvb", {
|
||||
set_parser(
|
||||
ftype = "test",
|
||||
fname = "t1",
|
||||
transforms = c("none", "none"),
|
||||
def_pars = c(0, 1),
|
||||
par_names = c("t1p1", "t1p2"),
|
||||
description = "Test function 1"
|
||||
)
|
||||
|
||||
set_parser(
|
||||
ftype = "test",
|
||||
fname = "t2",
|
||||
transforms = c("none", "none", "none"),
|
||||
def_pars = c(0, 1, 2),
|
||||
par_names = c("t2p1", "t2p2", "t2p3"),
|
||||
description = "Test function 2"
|
||||
)
|
||||
|
||||
ftypes <- list(test = "t1")
|
||||
covars <- list(test = list(
|
||||
t1p1 = c(
|
||||
cvb("d", "dem1"),
|
||||
cvb("p", "dem2", 2),
|
||||
"dem2"
|
||||
),
|
||||
t1p2 = c()
|
||||
))
|
||||
|
||||
# Test if c combines multiple items correctly and sanitizes correctly
|
||||
cbtest <- list(cvb("d", "dem1"), cvb("p", "dem2", 2), cvb("identity", "dem2" = 0))
|
||||
cbtest <- new("cbvec", values = cbtest)
|
||||
|
||||
covars <- covar_sanitize(covars, ftypes = ftypes)
|
||||
cv_expect <- list(test = list(t1p1 = cbtest, t1p2 = NULL))
|
||||
expect_equal(covars, cv_expect)
|
||||
|
||||
# Test if cvb can be used on its own
|
||||
covars <- list(test = cvb("d", "dem1"))
|
||||
|
||||
cbtest2 <- list(cvb("d", "dem1"))
|
||||
cbtest2 <- new("cbvec", values = cbtest2)
|
||||
|
||||
covars <- covar_sanitize(covars, ftypes = ftypes)
|
||||
cv_expect <- list(test = list(t1p1 = cbtest2, t1p2 = cbtest2))
|
||||
|
||||
expect_equal(covars, cv_expect)
|
||||
|
||||
# Test if cvb can be used on its own in a list of pars
|
||||
covars <- list(test = list(
|
||||
t1p1 = c(
|
||||
cvb("d", "dem1"),
|
||||
cvb("p", "dem2", 2),
|
||||
"dem2"
|
||||
),
|
||||
t1p2 = cvb("d", "dem1")
|
||||
))
|
||||
|
||||
cbtest2 <- list(cvb("d", "dem1"))
|
||||
cbtest2 <- new("cbvec", values = cbtest2)
|
||||
|
||||
covars <- covar_sanitize(covars, ftypes = ftypes)
|
||||
cv_expect <- list(test = list(t1p1 = cbtest, t1p2 = cbtest2))
|
||||
|
||||
expect_equal(covars, cv_expect)
|
||||
|
||||
})
|
||||
|
||||
test_that("correct covars are adusted correctly", {
|
||||
set_parser(
|
||||
ftype = "test",
|
||||
|
|
Loading…
Reference in New Issue