Skip to content

Commit

Permalink
Tweak: better separation between bullets and problems
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Jul 21, 2021
1 parent 42f03a4 commit 5322173
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 44 deletions.
2 changes: 1 addition & 1 deletion R/add.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ error_add_rows_to_grouped_df <- function() {

error_incompatible_new_rows <- function(names) {
tibble_error(
bullets(
problems(
"New rows can't add columns:",
cnd_message(error_unknown_column_names(names))
),
Expand Down
6 changes: 2 additions & 4 deletions R/tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ error_tibble_row_size_one <- function(j, name, size) {
desc <- paste0("at position ", j)
}

tibble_error(bullets(
tibble_error(problems(
"All vectors must be size one, use `list()` to wrap.",
paste0("Column ", desc, " is of size ", size, ".")
))
Expand All @@ -350,9 +350,7 @@ error_incompatible_size <- function(.rows, vars, vars_len, rows_source) {
paste0("Size ", x, ": ", pluralise_commas(text, y))
})

problems <- set_default_name(problems, "")

tibble_error(problems(
tibble_error(bullets(
"Tibble columns must have compatible sizes:",
if (!is.null(.rows)) paste0("Size ", .rows, ": ", rows_source),
problems,
Expand Down
6 changes: 3 additions & 3 deletions R/tribble.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ error_tribble_named_after_tilde <- function() {
}

error_tribble_lhs_column_syntax <- function(lhs) {
tibble_error(bullets(
tibble_error(problems(
"All column specifications must use the `~name` syntax.",
paste0("Found ", expr_label(lhs), " on the left-hand side of `~`.")
))
}

error_tribble_rhs_column_syntax <- function(rhs) {
tibble_error(bullets(
tibble_error(problems(
'All column specifications must use the `~name` or `~"name"` syntax.',
paste0("Found ", expr_label(rhs), " on the right-hand side of `~`.")
))
Expand All @@ -232,7 +232,7 @@ error_tribble_non_rectangular <- function(cols, cells) {
}

error_frame_matrix_list <- function(pos) {
tibble_error(bullets(
tibble_error(problems(
"All values must be atomic:",
pluralise_commas("Found list-valued element(s) at position(s) ", pos, ".")
))
Expand Down
9 changes: 7 additions & 2 deletions R/utils-msg-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pluralise_n <- function(message, n) {

bullets <- function(header, ...) {
bullets <- vec_c(..., .name_spec = "{outer}")
bullets <- set_default_name(bullets, "x")
if (packageVersion("rlang") >= "0.4.11.9001") {
bullets <- set_default_name(bullets, "*")
}

paste0(
ensure_full_stop(header), "\n",
Expand All @@ -59,6 +61,7 @@ bullets <- function(header, ...) {

problems <- function(header, ..., .problem = " problem(s)") {
problems <- vec_c(..., .name_spec = "{outer}")
problems <- set_default_name(problems, "x")
MAX_BULLETS <- 6L
if (length(problems) >= MAX_BULLETS) {
n_more <- length(problems) - MAX_BULLETS + 1L
Expand Down Expand Up @@ -93,12 +96,14 @@ commas <- function(problems) {
}

ensure_full_stop <- function(x) {
gsub("(?::|([^.?]))$", "\\1.", x)
set_names(gsub("(?::|([^.?]))$", "\\1.", x), names(x))
}

set_default_name <- function(x, name) {
if (is.null(names(x))) {
names(x) <- rep_along(x, name)
} else {
names(x)[names(x) == ""] <- name
}

x
Expand Down
36 changes: 18 additions & 18 deletions tests/testthat/_snaps/msg.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,54 +316,54 @@
Output
<error/tibble_error_incompatible_size>
Tibble columns must have compatible sizes.
Size 10: Requested with `uvw` argument.
Size 3: Column `c`.
Size 4: Columns `a` and `b`.
* Size 10: Requested with `uvw` argument.
* Size 3: Column `c`.
* Size 4: Columns `a` and `b`.
i Only values of size one are recycled.
Code
error_incompatible_size(10, letters[1:3], c(2, 2, 3),
"Requested with `xyz` argument")
Output
<error/tibble_error_incompatible_size>
Tibble columns must have compatible sizes.
Size 10: Requested with `xyz` argument.
Size 2: Columns `a` and `b`.
Size 3: Column `c`.
* Size 10: Requested with `xyz` argument.
* Size 2: Columns `a` and `b`.
* Size 3: Column `c`.
i Only values of size one are recycled.
Code
error_incompatible_size(NULL, letters[1:3], c(2, 2, 3),
"Requested with `xyz` argument")
Output
<error/tibble_error_incompatible_size>
Tibble columns must have compatible sizes.
Size 2: Columns `a` and `b`.
Size 3: Column `c`.
* Size 2: Columns `a` and `b`.
* Size 3: Column `c`.
i Only values of size one are recycled.
Code
error_incompatible_size(10, 1:3, c(4, 4, 3), "Requested with `uvw` argument")
Output
<error/tibble_error_incompatible_size>
Tibble columns must have compatible sizes.
Size 10: Requested with `uvw` argument.
Size 3: Column at position 3.
Size 4: Columns at positions 1 and 2.
* Size 10: Requested with `uvw` argument.
* Size 3: Column at position 3.
* Size 4: Columns at positions 1 and 2.
i Only values of size one are recycled.
Code
error_incompatible_size(10, 1:3, c(2, 2, 3), "Requested with `xyz` argument")
Output
<error/tibble_error_incompatible_size>
Tibble columns must have compatible sizes.
Size 10: Requested with `xyz` argument.
Size 2: Columns at positions 1 and 2.
Size 3: Column at position 3.
* Size 10: Requested with `xyz` argument.
* Size 2: Columns at positions 1 and 2.
* Size 3: Column at position 3.
i Only values of size one are recycled.
Code
error_incompatible_size(NULL, 1:3, c(2, 2, 3), "Requested with `xyz` argument")
Output
<error/tibble_error_incompatible_size>
Tibble columns must have compatible sizes.
Size 2: Columns at positions 1 and 2.
Size 3: Column at position 3.
* Size 2: Columns at positions 1 and 2.
* Size 3: Column at position 3.
i Only values of size one are recycled.
Code
# # tribble
Expand All @@ -388,8 +388,8 @@
Output
<error/tibble_error_tribble_non_rectangular>
Data must be rectangular.
Found 5 columns.
Found 17 cells.
* Found 5 columns.
* Found 17 cells.
i 17 is not an integer multiple of 5.
Code
error_frame_matrix_list(2:4)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/tibble.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
tibble(a = 1, b = 2:3, c = 4:6, d = 7:10)
Error <tibble_error_incompatible_size>
Tibble columns must have compatible sizes.
Size 2: Existing data.
Size 3: Column `c`.
* Size 2: Existing data.
* Size 3: Column `c`.
i Only values of size one are recycled.

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/tribble.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
tribble(~a, ~b, 1)
Error <tibble_error_tribble_non_rectangular>
Data must be rectangular.
Found 2 columns.
Found 1 cells.
* Found 2 columns.
* Found 1 cells.
i 1 is not an integer multiple of 2.
Code
tribble(a ~ b, 1)
Expand Down
73 changes: 67 additions & 6 deletions tests/testthat/_snaps/utils-msg-format.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,79 @@
# output test

Code
# # Problems
writeLines(problems("header", c("item 1", "item 2")))
Output
header.
x item 1.
x item 2.
Code
writeLines(problems("header", LETTERS))
Output
header.
x A.
x B.
x C.
x D.
x E.
x ... and 21 more problems.
Code
writeLines(problems("header", 1:6))
Output
header.
x 1.
x 2.
x 3.
x 4.
x 5.
x ... and 1 more problem.
Code
# # Bullets
bullets("header", c("item 1", "item 2"))
writeLines(bullets("header", c("item 1", "item 2")))
Output
[1] "header.\nx item 1.\nx item 2."
header.
* item 1.
* item 2.
Code
bullets("header", LETTERS)
writeLines(bullets("header", LETTERS))
Output
[1] "header.\nx A.\nx B.\nx C.\nx D.\nx E.\nx F.\nx G.\nx H.\nx I.\nx J.\nx K.\nx L.\nx M.\nx N.\nx O.\nx P.\nx Q.\nx R.\nx S.\nx T.\nx U.\nx V.\nx W.\nx X.\nx Y.\nx Z."
header.
* A.
* B.
* C.
* D.
* E.
* F.
* G.
* H.
* I.
* J.
* K.
* L.
* M.
* N.
* O.
* P.
* Q.
* R.
* S.
* T.
* U.
* V.
* W.
* X.
* Y.
* Z.
Code
bullets("header", 1:6)
writeLines(bullets("header", 1:6))
Output
[1] "header.\n* 1.\n* 2.\n* 3.\n* 4.\n* 5.\n* 6."
header.
* 1.
* 2.
* 3.
* 4.
* 5.
* 6.
Code
# # Commas
commas("1")
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-msg.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ test_that("aborting with class", {
})

test_that("output test", {
skip_if_not_installed("rlang", "0.4.11.9001")
expect_snapshot({
"# add"
error_add_rows_to_grouped_df()
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ test_that("is_tibble", {
})

test_that("output test", {
skip_if_not_installed("rlang", "0.4.11.9001")
expect_snapshot_with_error({
tibble(a = 1, a = 1)
tibble(a = new_environment())
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-tribble.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ test_that("tribble and frame_matrix cannot have named arguments", {
})

test_that("output test", {
skip_if_not_installed("rlang", "0.4.11.9001")
expect_snapshot_with_error({
tribble(1)
tribble(~a, ~b, 1)
Expand Down
11 changes: 8 additions & 3 deletions tests/testthat/test-utils-msg-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ test_that("pluralise_msg works correctly", {

test_that("output test", {
expect_snapshot({
"# Problems"
writeLines(problems("header", c("item 1", "item 2")))
writeLines(problems("header", LETTERS))
writeLines(problems("header", 1:6))

"# Bullets"
bullets("header", c("item 1", "item 2"))
bullets("header", LETTERS)
bullets("header", 1:6)
writeLines(bullets("header", c("item 1", "item 2")))
writeLines(bullets("header", LETTERS))
writeLines(bullets("header", 1:6))

"# Commas"
commas("1")
Expand Down

0 comments on commit 5322173

Please sign in to comment.