Skip to content

Commit bcf716e

Browse files
lucaslarson25aitap
authored andcommitted
Improved verbosity of data.table:::test() function for mismatched expected warnings (#7706)
* Improved verbosity of data.table:::test() function for mismatched expected warnings * Apply suggestion from @aitap Fixed NEWS.md punctuation and spacing. --------- Co-authored-by: aitap <krylov.r00t@gmail.com>
1 parent d8701f7 commit bcf716e

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
7. `fread()` would not give a warning when every second line of input was empty, [#3339](https://github.com/Rdatatable/data.table/issues/3339). Now, a warning message 'The rows in this file appear to be separated by blank lines.' is given and suggests to set `blank.lines.skip` to `TRUE`. Thanks to @Henrik-P for the report and @Asa-Henry for the fix.
5050

51+
8. `test()` now reports multiple expected warnings more clearly when `warning=` has length greater than 1L, instead of printing a collapsed or repeated mismatch summary after messages like `Test 1 produced 1 warnings but expected 2`, [#7092](https://github.com/Rdatatable/data.table/issues/7092). Expected and observed warnings are now printed on separate aligned lines, making small differences easier to spot. Thanks @MichaelChirico for the report, @ben-schwen for assistance, and @lucaslarson25, @tjdavis51, @D3VTHSTVR, and @car723 for the fix.
52+
5153
### Notes
5254

5355
1. {data.table} now depends on R 3.5.0 (2018).

R/test.data.table.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,16 @@ test = function(num, x, y=TRUE,
576576
}
577577
if (length(expected) != length(observed) && (!foreign || is.null(ignore.warning))) {
578578
# nocov start
579-
catf("Test %s produced %d %ss but expected %d\n%s\n%s\n", numStr, length(observed), type, length(expected), paste("Expected:", expected), paste("Observed:", observed, collapse = "\n"))
579+
align_messages = function(label, x) paste(
580+
c(
581+
paste0(label, x[1L]),
582+
if (length(x) > 1L) paste0(strrep(" ", nchar(label)), x[-1L])
583+
),
584+
collapse = "\n"
585+
)
586+
expected_text = align_messages("Expected: ", expected)
587+
observed_text = align_messages("Observed: ", observed)
588+
catf("Test %s produced %d %ss but expected %d\n%s\n%s\n", numStr, length(observed), type, length(expected), expected_text, observed_text)
580589
fail = TRUE
581590
# nocov end
582591
} else if (!foreign) {

inst/tests/tests.Rraw

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21607,3 +21607,9 @@ test(2370.2, yearmon(x, format="numeric"), yearmon(x)) # numeric is the default
2160721607
test(2370.3, yearmon(x, format="character"), c("1111M11", "2019M01", "2019M02", "2019M03", "2019M12", "2020M02", "2020M03", "2020M12", "2040M01", "2040M12", "2100M03", NA_character_))
2160821608
test(2370.4, yearmon("2016-08-03 01:02:03.45", format="character"), "2016M08")
2160921609
test(2370.5, yearmon(NA, format="character"), NA_character_)
21610+
21611+
# multiple expected/observed warnings in test() are printed on aligned lines, #7092
21612+
test(2371.1, test(0, {warning("a"); 2L}, 2L, warning=c("a", "b")), FALSE,
21613+
output="Test 0 produced 1 warnings but expected 2\nExpected: a\n b\nObserved: a")
21614+
test(2372.2, test(0, {warning("a"); warning("b"); 2L}, 2L, warning="a"), FALSE,
21615+
output="Test 0 produced 2 warnings but expected 1\nExpected: a\nObserved: a\n b")

0 commit comments

Comments
 (0)