Skip to content

Commit fe556d1

Browse files
committed
improve and test function validate_unique_names()
1 parent 45c2f20 commit fe556d1

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

R/guide.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ read_guide <- function(path, verify_hash = FALSE) {
5757
#' @noRd
5858
#'
5959
validate_unique_names <- function(names_vector, name_type) {
60-
if (anyDuplicated(names_vector)) {
61-
duplicates <- unique(names_vector[duplicated(names_vector)])
60+
duplications <- duplicated(names_vector)
61+
if (any(duplications)) {
62+
duplicates <- glue::glue_collapse(
63+
glue::double_quote(unique(names_vector[duplications])),
64+
sep = ', ',
65+
last = " and "
66+
)
6267
rlang::abort(glue::glue(
63-
"Duplicate keys in {name_type} names of the translations: {paste0(duplicates, collapse = ', ')}"
68+
"Duplicate keys in {name_type} names of the translations: {duplicates}"
6469
))
6570
}
6671
}

tests/testthat/test-guide.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ test_that("check_guide: guide without a .template location yields an error", {
4949
)
5050
})
5151

52+
test_that("Function validate_unique_names works correctly", {
53+
expect_error(
54+
validate_unique_names(c("a", "b", "a"), "short"),
55+
regexp = "Duplicate keys in short names"
56+
)
57+
expect_error(
58+
validate_unique_names(c("a", "b", "a"), "long"),
59+
regexp = "Duplicate keys in long names"
60+
)
61+
})
62+
5263
test_that("read_guide: duplicate long names in translations yield an error", {
5364
expect_error(
5465
read_guide(test_path(

0 commit comments

Comments
 (0)