Skip to content

Commit

Permalink
constant() now returns integer vector always
Browse files Browse the repository at this point in the history
closes #280
  • Loading branch information
wibeasley committed Dec 4, 2019
1 parent 03379ec commit 8c1bafa
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 58 deletions.
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ In the future:
Version 0.11 (Released ?)
==========================================================

### Breaking Changes
### Breaking Changes (possible, but unlikely)

* A possible, but unlikely, breaking change is that `kernel_api()` defaults to "text/csv" and UTF-8 encoding. Formerly, the function would decide on the content-type and encoding. More details are below in the 'Stability Features' subsection.
* [`kernel_api()`](https://ouhscbbmc.github.io/REDCapR/reference/kernel_api.html) defaults to "text/csv" and UTF-8 encoding. Formerly, the function would decide on the content-type and encoding. More details are below in the 'Stability Features' subsection.

*
* [`constant()`](https://ouhscbbmc.github.io/REDCapR/reference/constant.html) no longer accepts `simplify` as an options. An integer vector is always returned. (#280)

### New Features

Expand Down
9 changes: 3 additions & 6 deletions R/constant.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#' @description Collection of constants defined by the REDCap developers.
#'
#' @param name Name of constant. Required character.
#' @param simplify Simplifies the vector of values to a common data-type,
#' if possible. Passed to the `simplify` parameter of [base::sapply()].
#'
#' @return The constant's value. Currently all are single integers,
#' but that could be expanded in the future.
Expand Down Expand Up @@ -101,16 +99,15 @@
#' }

#' @export
constant <- function(name, simplify=TRUE) {
constant <- function(name) {
checkmate::assert_character(name, any.missing = FALSE, min.chars = 1L)
checkmate::assert_subset(name, names(constant_list), empty.ok = FALSE)
checkmate::assert_logical(simplify, any.missing = FALSE, len = 1L)

sapply(
vapply(
X = name,
FUN = function(x) constant_list[[x]],
USE.NAMES = FALSE,
simplify = simplify
FUN.VALUE = integer(1)
)
}

Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

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

9 changes: 2 additions & 7 deletions docs/reference/constant.html

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

5 changes: 1 addition & 4 deletions man/constant.Rd

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

40 changes: 3 additions & 37 deletions tests/testthat/test-constant.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
library(testthat)

test_that("scalar w/ simplify", {
test_that("scalar", {
expect_equal(object= constant("form_incomplete" ), expected=0L)
expect_equal(object= constant("form_unverified" ), expected=1L)
expect_equal(object= constant("form_complete" ), expected=2L)
})

test_that("scalar w/o simplify", {
expect_equal(object= constant("form_incomplete" , simplify=TRUE), expected=0L)
expect_equal(object= constant("form_unverified" , simplify=TRUE), expected=1L)
expect_equal(object= constant("form_complete" , simplify=TRUE), expected=2L)
})

test_that("vector w/ simplify", {
test_that("vector", {
expected <- c(2L, 2L, 0L)
observed <- constant(c("form_complete", "form_complete", "form_incomplete"), simplify=TRUE)
expect_equal(observed, expected)
})

test_that("vector w/o simplify", {
expected <- list(2L, 2L, 0L)
observed <- constant(c("form_complete", "form_complete", "form_incomplete"), simplify=FALSE)
observed <- constant(c("form_complete", "form_complete", "form_incomplete"))
expect_equal(observed, expected)
})

Expand Down Expand Up @@ -52,28 +40,6 @@ test_that("missing name", {
)
})

test_that("bad simplify", {
# expected_error_message <- "Assertion on 'simplify' failed: Must be of type 'logical', not 'character'."
expect_error(
constant("form_complete", simplify="aa"),
"^Assertion on 'simplify' failed: Must be of type 'logical', not 'character'\\.$"
)
})
test_that("missing simplify", {
# expected_error_message <- "Assertion on 'simplify' failed: Must be of type 'logical', not 'NULL'. "
expect_error(
constant("form_complete", simplify=NULL),
"^Assertion on 'simplify' failed: Must be of type 'logical', not 'NULL'\\.$"
)
})
test_that("NA simplify", {
# newer version of checkmate: "Assertion on 'simplify' failed: Contains missing values (element 1)."
# older version of checkmate: "Assertion on 'simplify' failed: Contains missing values."
expect_error(
constant("form_complete", simplify=NA_character_),
"^Assertion on 'simplify' failed: Contains missing values.*$"
)
})


# ---- constant-to-* -----------------------------------------------------------
Expand Down

0 comments on commit 8c1bafa

Please sign in to comment.