Skip to content

Commit

Permalink
numeric and integer validators error with factors (#102)
Browse files Browse the repository at this point in the history
* numeric and integer validators error with factors

Closes #94

* fix?

* fixes #94

https://cran.r-project.org/bin/windows/base/old/4.1.0/NEWS.R-4.1.0.html

> Using c() to combine a factor with other factors now gives a factor, an ordered factor when combining ordered factors with identical levels.

* tests need to be run only on R > 4.1
  • Loading branch information
IndrajeetPatil authored Apr 7, 2022
1 parent 3f4f560 commit 516c80d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/check-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ jobs:
#- {os: macOS-latest, r: 'devel'}
- {os: macOS-latest, r: 'release'}
- {os: macOS-latest, r: 'oldrel-1'}
#- {os: macOS-latest, r: 'oldrel-2'}

- {os: windows-latest, r: 'devel'}
- {os: windows-latest, r: 'release'}
- {os: windows-latest, r: 'oldrel-1'}
- {os: windows-latest, r: 'oldrel-2'}

# Use older ubuntu to maximise backward compatibility
- {os: ubuntu-18.04, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-18.04, r: 'release'}
- {os: ubuntu-18.04, r: 'oldrel-1'}
- {os: ubuntu-18.04, r: 'oldrel-2'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 0 additions & 2 deletions R/validation-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ isOfType <- function(object, type, nullAllowed = FALSE) {
#' validateIsLogical(TRUE)
#' @export
validateIsOfType <- function(object, type, nullAllowed = FALSE) {
type <- c(type)

# special case for integer to ensure that we call the special method
if (length(type) == 1 && type[1] == "integer") {
return(validateIsInteger(object, nullAllowed = nullAllowed))
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-validation-inclusion.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ test_that("isIncluded returns `TRUE` when base type values are included", {
})

test_that("isIncluded returns `TRUE` when compound type values are included", {
skip_if_not(getRversion() > "4.1")
skip_if_not(getRversion() > "4.1") # for factors

expect_true(isIncluded(as.factor("a"), c("a", "b")))
expect_true(isIncluded(c("a", "b"), as.factor(c("a", "b"))))
expect_true(isIncluded(as.factor("a"), list("a", "b")))
expect_true(isIncluded(c("a", "b"), as.factor(c("a", "b"))))
expect_true(isIncluded(list("a", "b"), as.factor(c("a", "b"))))
expect_true(isIncluded(as.Date("1970-02-01"), c(as.Date("1970-02-01"), as.Date("1980-12-21"))))
expect_true(isIncluded(as.Date("1970-02-01"), list(as.Date("1970-02-01"), as.Date("1980-12-21"))))
Expand All @@ -54,11 +54,11 @@ test_that("isIncluded returns `FALSE` when base type values are not included", {
})

test_that("isIncluded returns `FALSE` when compound type values are not included", {
skip_if_not(getRversion() > "4.1")
skip_if_not(getRversion() > "4.1") # for factors

expect_false(isIncluded(as.factor("a"), c("d", "b")))
expect_false(isIncluded(c("a", "b"), as.factor(c("d", "b"))))
expect_false(isIncluded(as.factor("a"), list("c", "b")))
expect_false(isIncluded(c("a", "b"), as.factor(c("d", "b"))))
expect_false(isIncluded(list("a", "b"), as.factor(c("c", "b"))))
expect_false(isIncluded(as.Date("1970-02-01"), c(as.Date("1972-02-01"), as.Date("1980-12-21"))))
expect_false(isIncluded(as.Date("1970-02-01"), list(as.Date("1980-02-01"), as.Date("1980-12-21"))))
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-validation-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ test_that("validateIsInteger produces expected error message when validating tha
)
})

test_that("validateIsInteger produces errors if a factor is provided", {
df <- data.frame(numCol = c(1, 2, 3))
df$numCol <- as.factor(df$numCol)

expect_error(
validateIsInteger(df$numCol),
"argument 'df$numCol' is of type 'factor', but expected 'integer'!",
fixed = TRUE
)
})

# validateIsCharacter ------------------------------

test_that("validateIsCharacter returns `NULL` for characters", {
Expand All @@ -131,6 +142,19 @@ test_that("validateIsNumeric produces errors if not numeric", {
expect_error(validateIsNumeric(list("1.2", "2.3")))
})

test_that("validateIsNumeric produces errors if a factor is provided", {
skip_if_not(getRversion() > "4.1") # for factors

df <- data.frame(numCol = c(1, 2, 3))
df$numCol <- as.factor(df$numCol)

expect_error(
validateIsNumeric(df$numCol),
"argument 'df$numCol' is of type 'factor', but expected 'numeric, or integer'!",
fixed = TRUE
)
})

# validateIsLogical ------------------------------

test_that("validateIsLogical returns `NULL` for logicals", {
Expand Down

0 comments on commit 516c80d

Please sign in to comment.