Skip to content

Commit

Permalink
Test that labels are unique (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
larmarange authored and hadley committed Jan 24, 2019
1 parent 4b3d096 commit ac15324
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# haven (development version)

* `labelled()` tests that value labels are unique (@larmarange, #364).

* `as_factor(x)` is now faster when `x` does not contain any missing values
(@hughparsonage).

Expand Down
3 changes: 3 additions & 0 deletions R/labelled.R
Expand Up @@ -48,6 +48,9 @@ labelled <- function(x, labels, label = NULL) {
if (is.null(names(labels))) {
stop("`labels` must have names", call. = FALSE)
}
if (any(duplicated(stats::na.omit(labels)))) {
stop("`labels` must be unique", call. = FALSE)
}
if (!is.null(label) && (!is.character(label) || length(label) != 1)) {
stop("`label` must be a character vector of length one", call. = FALSE)
}
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-labelled.R
Expand Up @@ -23,6 +23,10 @@ test_that("label must be length 1 character or missing", {
"character vector of length one")
})

test_that("labels must be unique", {
expect_error(labelled(1, c(female = 1, male = 1)), "must be unique")
})

# methods -----------------------------------------------------------------

test_that("printed output is stable", {
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-labelled_spss.R
Expand Up @@ -32,6 +32,11 @@ test_that("subsetting preserves attributes", {
expect_identical(x, x[])
})

test_that("labels must be unique", {
expect_error(
labelled_spss(1, c(female = 1, male = 1), na_values = 9),
"must be unique")
})

# is.na -------------------------------------------------------------------

Expand Down

0 comments on commit ac15324

Please sign in to comment.