Skip to content

Commit 0e478a6

Browse files
committed
Adjust class, typeof, colnames checks & messaging, gcd messaging
- S3 class vectors are ordered, so use `identical` - Improve class vector formatting - Tweak other `class` and `typeof` message text - Improve duplicate colnames message - Improve vector interpolation formatting - Fix typo in GCD error messaging
1 parent 6735810 commit 0e478a6

7 files changed

+31
-27
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ importFrom(checkmate,test_subset)
9898
importFrom(checkmate,vname)
9999
importFrom(cli,cli_abort)
100100
importFrom(cli,cli_inform)
101+
importFrom(cli,cli_vec)
101102
importFrom(cli,cli_warn)
102103
importFrom(data.table,":=")
103104
importFrom(data.table,address)

R/archive.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ validate_version_bound <- function(version_bound, x, na_ok = FALSE,
4646
)
4747
}
4848
} else {
49-
if (!test_set_equal(class(version_bound), class(x[["version"]]))) {
49+
if (!identical(class(version_bound), class(x[["version"]]))) {
5050
cli_abort(
51-
"{version_bound_arg} must have the same classes as x$version,
52-
which is {class(x$version)}",
51+
"{version_bound_arg} must have the same `class` vector as x$version,
52+
which has a `class` of {paste(collapse = ' ', deparse(class(x$version)))}",
5353
class = "epiprocess__version_bound_mismatched_class"
5454
)
5555
}
5656
if (!identical(typeof(version_bound), typeof(x[["version"]]))) {
5757
cli_abort(
58-
"{version_bound_arg} must have the same type as x$version,
59-
which is {typeof(x$version)}",
58+
"{version_bound_arg} must have the same `typeof` as x$version,
59+
which has a `typeof` of {typeof(x$version)}",
6060
class = "epiprocess__version_bound_mismatched_typeof"
6161
)
6262
}

R/methods-epi_archive.R

+8-8
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ epix_as_of <- function(x, max_version, min_time_value = -Inf, all_versions = FAL
6868
if (length(other_keys) == 0) other_keys <- NULL
6969

7070
# Check a few things on max_version
71-
if (!test_set_equal(class(max_version), class(x$DT$version))) {
71+
if (!identical(class(max_version), class(x$DT$version))) {
7272
cli_abort(
73-
"`max_version` must have the same classes as `epi_archive$DT$version`."
73+
"`max_version` must have the same `class` vector as `epi_archive$DT$version`."
7474
)
7575
}
76-
if (!test_set_equal(typeof(max_version), typeof(x$DT$version))) {
76+
if (!identical(typeof(max_version), typeof(x$DT$version))) {
7777
cli_abort(
78-
"`max_version` must have the same types as `epi_archive$DT$version`."
78+
"`max_version` must have the same `typeof` as `epi_archive$DT$version`."
7979
)
8080
}
8181
assert_scalar(max_version, na.ok = FALSE)
@@ -859,11 +859,11 @@ epix_truncate_versions_after <- function(x, max_version) {
859859
#' @rdname epix_truncate_versions_after
860860
#' @export
861861
epix_truncate_versions_after.epi_archive <- function(x, max_version) {
862-
if (!test_set_equal(class(max_version), class(x$DT$version))) {
863-
cli_abort("`max_version` must have the same classes as `epi_archive$DT$version`.")
862+
if (!identical(class(max_version), class(x$DT$version))) {
863+
cli_abort("`max_version` must have the same `class` as `epi_archive$DT$version`.")
864864
}
865-
if (!test_set_equal(typeof(max_version), typeof(x$DT$version))) {
866-
cli_abort("`max_version` must have the same types as `epi_archive$DT$version`.")
865+
if (!identical(typeof(max_version), typeof(x$DT$version))) {
866+
cli_abort("`max_version` must have the same `typeof` as `epi_archive$DT$version`.")
867867
}
868868
assert_scalar(max_version, na.ok = FALSE)
869869
if (max_version > x$versions_end) {

R/methods-epi_df.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ decay_epi_df <- function(x) {
123123
#' @param template `epi_df` template to use to restore
124124
#' @return `epi_df` or degrade into `tbl_df`
125125
#' @importFrom dplyr dplyr_reconstruct
126+
#' @importFrom cli cli_vec
126127
#' @export
127128
#' @noRd
128129
dplyr_reconstruct.epi_df <- function(data, template) {
@@ -135,9 +136,11 @@ dplyr_reconstruct.epi_df <- function(data, template) {
135136
# Duplicate columns, cli_abort
136137
dup_col_names <- cn[duplicated(cn)]
137138
if (length(dup_col_names) != 0) {
138-
cli_abort(paste0(
139-
"Column name(s) {unique(dup_col_names)}",
140-
"must not be duplicated."
139+
cli_abort(c(
140+
"Duplicate column names are not allowed",
141+
"i" = "Duplicated column name{?s}:
142+
{cli_vec(unique(dup_col_names),
143+
style = list('vec-sep2' = ', ', 'vec-last' = ', '))}"
141144
))
142145
}
143146

R/utils.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ gcd2num <- function(a, b, rrtol = 1e-6, pqlim = 1e6, irtol = 1e-6) {
609609
# `b_curr` is the candidate GCD / iterand; check first if it seems too small:
610610
if (abs(b_curr) <= iatol) {
611611
cli_abort(
612-
"No GCD found; remaining potential Gads are all too small relative
612+
"No GCD found; remaining potential GCDs are all too small relative
613613
to one/both of the original inputs; see `irtol` setting."
614614
)
615615
}

tests/testthat/test-archive-version-bounds.R

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,30 @@ test_that("`validate_version_bound` validate and class checks together allow and
5151
my_version_bound1 <- `class<-`(24, "c1")
5252
expect_error(
5353
validate_version_bound(my_version_bound1, x_int, na_ok = FALSE),
54-
regexp = "must have the same classes as"
54+
regexp = "must have the same `class` vector as"
5555
)
5656
my_version_bound2 <- `class<-`(list(12), c("c2a", "c2b", "c2c"))
57-
expect_error(validate_version_bound(my_version_bound2, x_list, na_ok = FALSE), regexp = "must have the same classes")
57+
expect_error(validate_version_bound(my_version_bound2, x_list, na_ok = FALSE), regexp = "must have the same `class`")
5858
# Want no error matching date to date or datetime to datetime, but no interop due to tz issues:
5959
validate_version_bound(my_date, x_date, version_bound_arg = "vb")
6060
validate_version_bound(my_datetime, x_datetime, version_bound_arg = "vb")
6161
expect_error(
6262
validate_version_bound(my_datetime, x_date, na_ok = TRUE, version_bound_arg = "vb"),
63-
regexp = "must have the same classes",
63+
regexp = "must have the same `class`",
6464
class = "epiprocess__version_bound_mismatched_class"
6565
)
6666
expect_error(
6767
validate_version_bound(my_date, x_datetime, na_ok = TRUE, version_bound_arg = "vb"),
68-
regexp = "must have the same classes",
68+
regexp = "must have the same `class`",
6969
class = "epiprocess__version_bound_mismatched_class"
7070
)
7171
# Bad:
72-
expect_error(validate_version_bound(3.5, x_int, TRUE, "vb"), regexp = "must have the same classes")
73-
expect_error(validate_version_bound(.Machine$integer.max, x_dbl, TRUE, "vb"), regexp = "must have the same classes")
72+
expect_error(validate_version_bound(3.5, x_int, TRUE, "vb"), regexp = "must have the same `class`")
73+
expect_error(validate_version_bound(.Machine$integer.max, x_dbl, TRUE, "vb"), regexp = "must have the same `class`")
7474
expect_error(validate_version_bound(
7575
`class<-`(list(2), "clazz"),
7676
tibble::tibble(version = `class<-`(5L, "clazz")), TRUE, "vb"
77-
), regexp = "must have the same type", class = "epiprocess__version_bound_mismatched_typeof")
77+
), regexp = "must have the same `typeof`", class = "epiprocess__version_bound_mismatched_typeof")
7878
# Maybe questionable:
7979
expect_error(validate_version_bound(3, x_int, TRUE, "vb"))
8080
expect_error(validate_version_bound(3L, x_dbl, TRUE, "vb"))
@@ -99,7 +99,7 @@ test_that("archive version bounds args work as intended", {
9999
clobberable_versions_start = 1241,
100100
versions_end = measurement_date
101101
),
102-
regexp = "must have the same classes"
102+
regexp = "must have the same `class`"
103103
)
104104
expect_error(
105105
as_epi_archive(update_tbl[integer(0L), ]),

tests/testthat/test-methods-epi_df.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ test_that("Subsetting drops & does not drop the epi_df class appropriately", {
8484

8585
test_that("When duplicate cols in subset should abort", {
8686
expect_error(toy_epi_df[, c(2, 2:3, 4, 4, 4)],
87-
"Column name(s) time_value, y must not be duplicated.",
87+
"Duplicated column names: time_value, y",
8888
fixed = TRUE
8989
)
9090
expect_error(toy_epi_df[1:4, c(1, 2:4, 1)],
91-
"Column name(s) geo_value must not be duplicated.",
91+
"Duplicated column name: geo_value",
9292
fixed = TRUE
9393
)
9494
})

0 commit comments

Comments
 (0)