Skip to content

Commit 6b29cb9

Browse files
unique_check() -> check_unique() (#426)
* `unique_check()` -> `check_unique()` + improvements * Update R/misc.R Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent ad33275 commit 6b29cb9

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

R/misc.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,21 @@ check_frac_range <- function(x, ..., arg = caller_arg(x), call = caller_env()) {
297297
call = call
298298
)
299299
}
300+
301+
check_unique <- function(x, ..., arg = caller_arg(x), call = caller_env()) {
302+
check_dots_empty()
303+
x2 <- x[!is.na(x)]
304+
is_dup <- duplicated(x2)
305+
if (!any(is_dup)) {
306+
return(invisible(NULL))
307+
}
308+
309+
dup_list <- x2[is_dup]
310+
cli::cli_abort(
311+
c(
312+
x = "{.arg {arg}} must have unique values.",
313+
i = "Duplicates: {.val {dup_list}}"
314+
),
315+
call = call
316+
)
317+
}

R/parameters.R

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,6 @@ parameters.list <- function(x, ...) {
6666
)
6767
}
6868

69-
unique_check <- function(x, ..., call = caller_env()) {
70-
check_dots_empty()
71-
x2 <- x[!is.na(x)]
72-
is_dup <- duplicated(x2)
73-
if (any(is_dup)) {
74-
dup_list <- x2[is_dup]
75-
cl <- match.call()
76-
77-
cli::cli_abort(
78-
c(
79-
x = "Element {.field {deparse(cl$x)}} should have unique values.",
80-
i = "Duplicates exist for {cli::qty(dup_list)} item{?s}: {dup_list}"
81-
),
82-
call = call
83-
)
84-
}
85-
invisible(TRUE)
86-
}
87-
8869
param_or_na <- function(x) {
8970
inherits(x, "param") | all(is.na(x))
9071
}
@@ -135,7 +116,7 @@ parameters_constr <- function(
135116

136117
check_character(name, call = call)
137118
check_character(id, call = call)
138-
unique_check(id, call = call)
119+
check_unique(id, call = call)
139120
check_character(source, call = call)
140121
check_character(component, call = call)
141122
check_character(component_id, call = call)

tests/testthat/_snaps/misc.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@
174174
Error:
175175
! `c(0.1, NA)` must be a numeric vector of length 2 with values between 0 and 1, not a double vector.
176176

177+
# check_unique() errors on duplicates
178+
179+
Code
180+
check_unique(c("a", "a"))
181+
Condition
182+
Error:
183+
x `c("a", "a")` must have unique values.
184+
i Duplicates: "a"
185+
186+
---
187+
188+
Code
189+
check_unique(c("a", "b", "a", "b"))
190+
Condition
191+
Error:
192+
x `c("a", "b", "a", "b")` must have unique values.
193+
i Duplicates: "a" and "b"
194+
195+
---
196+
197+
Code
198+
my_ids <- c("x", "x")
199+
check_unique(my_ids)
200+
Condition
201+
Error:
202+
x `my_ids` must have unique values.
203+
i Duplicates: "x"
204+
177205
# vctrs-helpers-parameters
178206

179207
Code

tests/testthat/_snaps/parameters.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
parameters_constr(ab, id = c("a", "a"), ab, ab, ab)
1414
Condition
1515
Error:
16-
x Element id should have unique values.
17-
i Duplicates exist for item: a
16+
x `id` must have unique values.
17+
i Duplicates: "a"
1818

1919
---
2020

@@ -58,8 +58,8 @@
5858
parameters(list(a = mtry(), a = penalty()))
5959
Condition
6060
Error in `parameters()`:
61-
x Element id should have unique values.
62-
i Duplicates exist for item: a
61+
x `id` must have unique values.
62+
i Duplicates: "a"
6363

6464
---
6565

tests/testthat/test-misc.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ test_that("check_frac_range()", {
9191
expect_snapshot(error = TRUE, check_frac_range(c(0.1, NA)))
9292
})
9393

94+
test_that("check_unique() passes with unique values", {
95+
expect_null(check_unique(c("a", "b", "c")))
96+
expect_null(check_unique(c(1, 2, 3)))
97+
expect_null(check_unique(character()))
98+
})
99+
100+
test_that("check_unique() ignores NA values", {
101+
expect_null(check_unique(c("a", NA, "b")))
102+
expect_null(check_unique(c(NA, NA, NA)))
103+
expect_null(check_unique(c("a", NA, NA, "b")))
104+
})
105+
106+
test_that("check_unique() errors on duplicates", {
107+
expect_snapshot(error = TRUE, check_unique(c("a", "a")))
108+
expect_snapshot(error = TRUE, check_unique(c("a", "b", "a", "b")))
109+
expect_snapshot(error = TRUE, {
110+
my_ids <- c("x", "x")
111+
check_unique(my_ids)
112+
})
113+
})
114+
94115
test_that("vctrs-helpers-parameters", {
95116
expect_false(dials:::is_parameters(2))
96117
expect_snapshot(

0 commit comments

Comments
 (0)