Skip to content

Commit 2654599

Browse files
authored
More test fixes for when translations are present (#7478)
* Switch more tests to test(output=...) capture.output() tests are fine in many places, but these ones tested for messages that have since been translated and must be skipped in foreign more. * Use notOutput= instead of capture.output() tests Where capture.output() was previously used to test the absence of messages, use notOutput= instead so that the test will be skipped. While the test succeeded anyway due to the captured output containing neither the original untranslated message (being matched) nor its translation (that could be printed if a regression happened), skipping the test is more fair. * test.data.table(): count skipped message tests When working in foreign mode, count the occurrences of tests for message content (which are essentially skipped) and report the number if it's non-zero. * test 1590.14: don't assume 1252 if not UTF-8 * Elaborate on what 1252 is * Better test for not printing NULL * Restore test 1775.1 for print() options Instead, test substitute(x) for %iscall% "print" to decide whether to print the x before testing the output.
1 parent d55a69a commit 2654599

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

R/test.data.table.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ test.data.table = function(script="tests.Rraw", verbose=FALSE, pkg=".", silent=F
130130
}
131131
assign("foreign", foreign, envir=env)
132132
assign("nfail", 0L, envir=env)
133+
assign("nskip", 0L, envir=env)
133134
assign("ntest", 0L, envir=env)
134135
assign("prevtest", -1L, envir=env)
135136
assign("whichfail", NULL, envir=env)
@@ -282,6 +283,7 @@ test.data.table = function(script="tests.Rraw", verbose=FALSE, pkg=".", silent=F
282283

283284
nfail = env$nfail
284285
ntest = env$ntest
286+
nskip = env$nskip
285287
if (nfail > 0L) {
286288
# nocov start
287289
stopf(
@@ -331,6 +333,7 @@ test.data.table = function(script="tests.Rraw", verbose=FALSE, pkg=".", silent=F
331333
get("mtext")(lastRSS, side=4L, at=lastRSS, las=1L, font=2L)
332334
}
333335

336+
if (foreign && nskip > 0L) catf("Skipped %d tests for translated messages. ", nskip) # nocov
334337
catf("All %d tests (last %.8g) in %s completed ok in %s\n", ntest, env$prevtest, names(fn), timetaken(env$started.at))
335338
ans = nfail==0L
336339
attr(ans, "timings") = timings # as attr to not upset callers who expect a TRUE/FALSE result
@@ -409,6 +412,7 @@ test = function(num,x,y=TRUE,error=NULL,warning=NULL,message=NULL,output=NULL,no
409412
memtest.id = get("memtest.id", parent.frame())
410413
filename = get("filename", parent.frame())
411414
foreign = get("foreign", parent.frame())
415+
nskip = get("nskip", parent.frame())
412416
showProgress = get("showProgress", parent.frame())
413417
time = nTest = RSS = NULL # to avoid 'no visible binding' note
414418
# TODO(R>=4.0.2): Use add=TRUE up-front in on.exit() once non-positional arguments are supported.
@@ -451,6 +455,11 @@ test = function(num,x,y=TRUE,error=NULL,warning=NULL,message=NULL,output=NULL,no
451455
length(grep(x, y, fixed=TRUE)) || # try treating x as literal first; useful for most messages containing ()[]+ characters
452456
length(tryCatch(grep(x, y, ignore.case=ignore.case), error=function(e)NULL)) # otherwise try x as regexp
453457
}
458+
if (foreign && .test.data.table && (
459+
length(error) || length(warning) || length(message) || length(output) ||
460+
length(notOutput) || length(ignore.warning)
461+
))
462+
assign("nskip", nskip+1L, parent.frame(), inherits=TRUE) # nocov
454463

455464
xsub = substitute(x)
456465
ysub = substitute(y)
@@ -478,7 +487,11 @@ test = function(num,x,y=TRUE,error=NULL,warning=NULL,message=NULL,output=NULL,no
478487
# save the overhead of capture.output() since there are a lot of tests, often called in loops
479488
# Thanks to tryCatch2 by Jan here : https://github.com/jangorecki/logR/blob/master/R/logR.R#L21
480489
} else {
481-
out = capture.output(print(x <- suppressMessages(withCallingHandlers(tryCatch(x, error=eHandler), warning=wHandler, message=mHandler))))
490+
out = if (xsub %iscall% "print") {
491+
capture.output(x <- suppressMessages(withCallingHandlers(tryCatch(x, error=eHandler), warning=wHandler, message=mHandler)))
492+
} else {
493+
capture.output(print(x <- suppressMessages(withCallingHandlers(tryCatch(x, error=eHandler), warning=wHandler, message=mHandler))))
494+
}
482495
}
483496
if (!is.null(options)) {
484497
# some of the options passed to test() may break internal data.table use below (e.g. invalid datatable.alloccol), so undo them ASAP

inst/tests/tests.Rraw

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,17 +2031,19 @@ options(datatable.optimize = 2L)
20312031
test(658.1, DT[ , mean(x), by=grp1, verbose=TRUE], output="GForce optimized j to.*gmean")
20322032
test(658.2, DT[ , list(mean(x)), by=grp1, verbose=TRUE], output="GForce optimized j to.*gmean")
20332033
test(658.3, DT[ , list(mean(x), mean(y)), by=grp1, verbose=TRUE], output="GForce optimized j to.*gmean")
2034-
tt = capture.output(DT[,list(mean(x),mean(y)),by=list(grp1,grp2),verbose=TRUE])
2035-
test(659, !length(grep("Wrote less rows", tt))) # first group is one row with this seed. Ensure we treat this as aggregate case rather than allocate too many rows.
2034+
# first group is one row with this seed. Ensure we treat this as aggregate case rather than allocate too many rows.
2035+
test(659, DT[,list(mean(x),mean(y)),by=list(grp1,grp2),verbose=TRUE], notOutput = "Wrote less rows")
20362036

20372037
# Test .N for logical i subset
20382038
DT = data.table(a=1:10, b=rnorm(10))
20392039
test(660, DT[a==8L, .N], 1L)
20402040

20412041
# Test that growing is sensible in worst case
20422042
DT = data.table(a=rep(1:10,1:10),b=rnorm(55))
2043-
tt = capture.output(DT[,sum(b)*b,by=a,verbose=TRUE])
2044-
test(661, length(grep("growing from",tt))<3) # was 6 when we simply grew enough for latest result
2043+
test(661, DT[,sum(b)*b,by=a,verbose=TRUE],
2044+
notOutput = paste(rep("growing from", 3), collapse = ".*"))
2045+
# Need to test that "growing from" is printed less than three times.
2046+
# The number was previously 6 when we simply grew enough for latest result
20452047

20462048
# Test that adding a new logical column is supported, #2094
20472049
DT=data.table(a=1:3)
@@ -8286,8 +8288,8 @@ test(1579.03, dt[, lapply(.SD, median), keyby=x],
82868288
dt[, lapply(.SD, function(x) median(as.numeric(x))), keyby=x])
82878289
test(1579.04, dt[, lapply(.SD, median, na.rm=TRUE), keyby=x],
82888290
dt[, lapply(.SD, function(x) median(as.numeric(x), na.rm=TRUE)), keyby=x])
8289-
ans = capture.output(dt[, lapply(.SD, median), by=x, verbose=TRUE])
8290-
test(1579.05, any(grepl("GForce optimized", ans)), TRUE)
8291+
test(1579.05, dt[, lapply(.SD, median), by=x, verbose=TRUE],
8292+
output = "GForce optimized")
82918293

82928294
# testing gforce::ghead and gforce::gtail
82938295
# head(.SD, 1) and tail(.SD, 1) optimisation
@@ -8539,8 +8541,8 @@ if (x1==x2) {
85398541
# NB: x1==x2 is a condition in base R, independent of data.table
85408542
test(1590.12, forderv( c(x2,x1,x1,x2)), integer())
85418543
# don't test base ... test(1590.13, base::order(c(x2,x1,x1,x2)), 1:4)
8542-
} else {
8543-
# Windows-1252, #2856
8544+
} else if (identical(l10n_info()$codepage, 1252L)) {
8545+
# US English ANSI codepage on Windows is CP1252, #2856
85448546
test(1590.14, forderv( c(x2,x1,x1,x2)), INT(1,4,2,3))
85458547
# don't test base ... test(1590.15, base::order(c(x2,x1,x1,x2)), INT(1,4,2,3))
85468548
}
@@ -9280,11 +9282,9 @@ test(1633.4, dt[, .SD, by=1:nrow(dt)], data.table(nrow=1:nrow(dt), dt)) # make s
92809282

92819283
# reuse secondary indices
92829284
dt = data.table(x=sample(3, 10, TRUE), y=1:10)
9283-
v1 = capture.output(ans1 <- dt[.(3:2), on="x", verbose=TRUE])
9285+
test(1634.1, dt[.(3:2), on="x", verbose=TRUE], output = "ad hoc")
92849286
setindex(dt, x)
9285-
v2 = capture.output(ans2 <- dt[.(3:2), on="x", verbose=TRUE])
9286-
test(1634.1, any(grepl("ad hoc", v1)), TRUE)
9287-
test(1634.2, any(grepl("existing index", v2)), TRUE)
9287+
test(1634.2, dt[.(3:2), on="x", verbose=TRUE], output = "existing index")
92889288

92899289
# fread's fill argument detects separator better in complex cases as well, #1573
92909290
# if pasted to the console, these tests won't work. But do work when sourced as these are tabs not spaces in text
@@ -10248,33 +10248,33 @@ options("datatable.use.index"=TRUE, "datatable.auto.index"=TRUE)
1024810248
test(1666.01, d[k==1L, verbose=TRUE], d[3L], output="Creating new index 'k'")
1024910249
d = data.table(k=3:1)
1025010250
options("datatable.use.index"=TRUE, "datatable.auto.index"=FALSE)
10251-
test(1666.02, grep("Creating new index", capture.output(d[k==1L, verbose=TRUE])), integer(0)) # do not create index
10251+
test(1666.02, d[k==1L, verbose=TRUE], notOutput="Creating new index") # do not create index
1025210252
d = data.table(k=3:1)
1025310253
options("datatable.use.index"=FALSE, "datatable.auto.index"=FALSE)
10254-
test(1666.03, grep("Creating new index", capture.output(d[k==1L, verbose=TRUE])), integer(0))
10254+
test(1666.03, d[k==1L, verbose=TRUE], notOutput="Creating new index")
1025510255
d = data.table(k=3:1)
1025610256
options("datatable.use.index"=FALSE, "datatable.auto.index"=TRUE)
10257-
test(1666.04, grep("Creating new index", capture.output(d[k==1L, verbose=TRUE])), integer(0))
10257+
test(1666.04, d[k==1L, verbose=TRUE], notOutput="Creating new index")
1025810258
d = data.table(k=3:1) # subset - index
1025910259
setindex(d, k)
1026010260
options("datatable.use.index"=TRUE, "datatable.auto.index"=TRUE)
1026110261
test(1666.05, d[k==1L, verbose=TRUE], d[3L], output="Optimized subsetting with index 'k'")
1026210262
options("datatable.use.index"=TRUE, "datatable.auto.index"=FALSE)
1026310263
test(1666.06, d[k==1L, verbose=TRUE], d[3L], output="Optimized subsetting with index 'k'")
1026410264
options("datatable.use.index"=FALSE, "datatable.auto.index"=FALSE)
10265-
test(1666.07, grep("Using existing index", capture.output(d[k==1L, verbose=TRUE])), integer(0)) # not using existing index
10265+
test(1666.07, d[k==1L, verbose=TRUE], notOutput="Using existing index") # not using existing index
1026610266
options("datatable.use.index"=FALSE, "datatable.auto.index"=TRUE)
10267-
test(1666.08, grep("Using existing index", capture.output(d[k==1L, verbose=TRUE])), integer(0))
10267+
test(1666.08, d[k==1L, verbose=TRUE], notOutput="Using existing index")
1026810268
d1 = data.table(k=3:1) # join - no index
1026910269
d2 = data.table(k=2:4)
1027010270
options("datatable.use.index"=TRUE, "datatable.auto.index"=TRUE)
1027110271
test(1666.09, d1[d2, on="k", verbose=TRUE], d1[d2, on="k"], output="ad hoc")
1027210272
options("datatable.use.index"=TRUE, "datatable.auto.index"=FALSE)
1027310273
test(1666.10, d1[d2, on="k", verbose=TRUE], d1[d2, on="k"], output="ad hoc")
1027410274
options("datatable.use.index"=FALSE, "datatable.auto.index"=FALSE)
10275-
test(1666.11, grep("Looking for existing (secondary) index", capture.output(d1[d2, on="k", verbose=TRUE])), integer(0)) # not looking for index
10275+
test(1666.11, d1[d2, on="k", verbose=TRUE], notOutput="Looking for existing (secondary) index") # not looking for index
1027610276
options("datatable.use.index"=FALSE, "datatable.auto.index"=TRUE)
10277-
test(1666.12, grep("Looking for existing (secondary) index", capture.output(d1[d2, on="k", verbose=TRUE])), integer(0))
10277+
test(1666.12, d1[d2, on="k", verbose=TRUE], notOutput="Looking for existing (secondary) index")
1027810278
d1 = data.table(k=3:1,v1=10:12) # join - index
1027910279
d2 = data.table(k=2:4,v2=20:22)
1028010280
setindex(d1, k)
@@ -11653,8 +11653,7 @@ test(1765.4, {warning("foobar1"); warning("foobar2"); warning("FOO"); 4L}, 4L, i
1165311653
test(1765.5, {warning("foobar1"); warning("foobar2"); warning("FOO"); 4L}, 4L, ignore.warning="2", warning=c("foobar1","FOO"))
1165411654

1165511655
# print(null.data.table()) should not output NULL as well, #1852
11656-
# use capture.output() in this case rather than output= to ensure NULL is not output
11657-
test(1766, capture.output(print(data.table(NULL))), "Null data.table (0 rows and 0 cols)")
11656+
test(1766, data.table(NULL), notOutput = "NULL$")
1165811657

1165911658
# Bug on subset of 1-row data.table when expr returns a named logical vector #2152
1166011659
options(datatable.auto.index=FALSE)
@@ -11888,8 +11887,8 @@ test(1774.17, as.data.table(x, na.rm='a'), error="'na.rm' must be scalar")
1188811887

1188911888
# verify print.keys works
1189011889
DT1 <- data.table(a = 1:3, key = "a")
11891-
test(1775.1, capture.output(print(DT1, print.keys = TRUE)),
11892-
c("Key: <a>", " a", "1: 1", "2: 2", "3: 3"))
11890+
test(1775.1, print(DT1, print.keys = TRUE),
11891+
output = c("Key: <a>", " a", "1: 1", "2: 2", "3: 3"))
1189311892
DT2 <- data.table(a = 1:3, b = 4:6)
1189411893
setindexv(DT2, c("b","a"))
1189511894
test(1775.2, print(DT2, print.keys = TRUE),
@@ -13675,7 +13674,7 @@ test(1962.034, setkeyv(DT, c('a', '')),
1367513674
setkey(DT, a)
1367613675
test(1962.035, {setkeyv(DT, character(0L)); key(DT)}, NULL,
1367713676
warning = 'cols is a character vector of zero length')
13678-
test(1962.036, any(grepl('already ordered', capture.output(setkey(DT, a, verbose = TRUE)))))
13677+
test(1962.036, setkey(DT, a, verbose = TRUE), output = 'already ordered')
1367913678
setnames(DT, '.xi')
1368013679
setkey(DT, NULL)
1368113680
test(1962.037, setkey(DT, .xi),
@@ -16321,7 +16320,7 @@ test(2094.02, X$TAG, rep(names(x), each = 2))
1632116320

1632216321
# use arbitrary column without message when fun.aggregate=length, #2980
1632316322
DT = data.table(a=c(3L, 3L, 2L, 9L, 5L, 10L, 3L, 2L, 9L, 8L), b=rep(1:5, 2))
16324-
test(2095, any(grepl('override', capture.output(dcast(DT, a~b, fun.aggregate=length)), fixed=TRUE)), FALSE)
16323+
test(2095, dcast(DT, a~b, fun.aggregate=length), notOutput='override')
1632516324

1632616325
# gmean intermediate can overflow integers without warning, #986
1632716326
test(2096, data.table(a=c(1L,1L), v=c(2e9L, 2e9L))[, mean(v), a], data.table(a=1L, V1=2e9))
@@ -16705,10 +16704,10 @@ test(2125.05, print(DT, trunc.cols=TRUE, class=TRUE, row.names=FALSE),
1670516704
"1 variable not shown: \\[d <char>\\]"))
1670616705
test(2125.06, print(DT, trunc.cols=TRUE, col.names="none"),
1670716706
output=c("^ 1: 0 bbbbbbbbbbbbb ccccccccccccc", ".*",
16708-
"1 variable not shown: \\[d\\]", ""))
16707+
"1 variable not shown: \\[d\\]$"))
1670916708
test(2125.07, print(DT, trunc.cols=TRUE, class=TRUE, col.names="none"),
1671016709
output=c("^ 1: 0 bbbbbbbbbbbbb", ".*",
16711-
"2 variables not shown: \\[c, d\\]", ""),
16710+
"2 variables not shown: \\[c, d\\]$"),
1671216711
warning = "Column classes will be suppressed when col.names is 'none'")
1671316712
options("width" = 20)
1671416713
DT = data.table(a = vector("integer", 2),
@@ -21491,11 +21490,11 @@ test(2328.2, droplevels(DT), data.table(f=factor(), i=integer(), f2=factor()))
2149121490

2149221491
#6882 print() output with col.names="none"
2149321492
dt = data.table(short = 1:3, verylongcolumnname = 4:6)
21494-
test(2329.1, print(dt, col.names = "none"), output = "1: 1 4\n2: 2 5\n3: 3 6\n")
21493+
test(2329.1, print(dt, col.names = "none"), output = "1: 1 4\n2: 2 5\n3: 3 6$")
2149521494
dt = data.table(x = 123456, y = "wide_string")
21496-
test(2329.2, print(dt, col.names = "none"), output = "1: 123456 wide_string\n")
21495+
test(2329.2, print(dt, col.names = "none"), output = "1: 123456 wide_string$")
2149721496
dt = data.table(a = NA_integer_, b = NaN)
21498-
test(2329.3, print(dt, col.names = "none"), output = "1: NA NaN\n")
21497+
test(2329.3, print(dt, col.names = "none"), output = "1: NA NaN$")
2149921498

2150021499
# Row name extraction from multiple vectors, #7136
2150121500
x <- 1:3

0 commit comments

Comments
 (0)