Skip to content

Commit

Permalink
Add tests, including updating broken test
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelChirico committed Dec 4, 2024
1 parent 50cc877 commit 32e5cf2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
17 changes: 13 additions & 4 deletions tests/autoprint.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DT[FALSE,a:=3L] # no
DT[a==4L,a:=5L] # no
DT[a %in% 4:8, a:=5L] # no
DT # yes
print(DT[2,a:=4L]) # no
print(DT[2,a:=4L]) # yes, as of #6631
print(DT) # yes
if (TRUE) DT[2,a:=5L] # no. used to print before v1.9.5
if (TRUE) if (TRUE) DT[2,a:=6L] # no. used to print before v1.9.5
Expand All @@ -20,7 +20,7 @@ DT # yes. 2nd time needed, or solutions below
(function(){DT[2,a:=5L];NULL})() # print NULL
DT[] # yes. guaranteed print
(function(){DT[2,a:=5L];NULL})() # print NULL
print(DT) # no. only DT[] is guaranteed print from v1.9.6 and R 3.2.0
print(DT) # yes. restored in #6631 behavior that had changed in 1.9.6.
(function(){DT[2,a:=5L][];NULL})() # print NULL
DT # yes. i) function needs to add [] after last one, so that "DT" alone is guaranteed anyway
(function(){DT[2,a:=5L];DT[];NULL})() # print NULL
Expand All @@ -29,9 +29,9 @@ DT2 = data.table(b=3:4) # no
(function(){DT[2,a:=6L];DT2[1,b:=7L];NULL})()
DT # yes. last := was on DT2 not DT
{DT[2,a:=6L];invisible()} # no
print(DT) # no
print(DT) # yes
(function(){print(DT[2,a:=7L]);print(DT);invisible()})() # yes*2
{print(DT[2,a:=8L]);print(DT);invisible()} # yes*1 Not within function so as at prompt
{print(DT[2,a:=8L]);print(DT);invisible()} # yes*2 as at prompt, again as of #6631
DT[1][,a:=9L] # no (was too tricky to detect that DT[1] is a new object). Simple rule is that := always doesn't print
DT[2,a:=10L][1] # yes
DT[1,a:=10L][1,a:=10L] # no
Expand All @@ -43,3 +43,12 @@ DT[1,a:=10L][] # yes. ...[] == oops, forgot print(...)
tryCatch(DT[,foo:=ColumnNameTypo], error=function(e) e$message) # error: not found.
DT # yes
DT # yes

# child class of data.table doesn't induce unintended print, #3029
dt <- data.table(x = 1)
class(dt) <- c("foo", "data.table", "data.frame")
print.foo <- function(x, ...) {
NextMethod("print")
}

dt[, y := 1] # no
41 changes: 33 additions & 8 deletions tests/autoprint.Rout.save
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

R version 4.3.2 (2023-10-31) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R Under development (unstable) (2024-12-01 r87412) -- "Unsuffered Consequences"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Expand Down Expand Up @@ -44,7 +44,11 @@ Index: <a>
<int>
1: 1
2: 3
> print(DT[2,a:=4L]) # no
> print(DT[2,a:=4L]) # yes, as of #6631
a
<int>
1: 1
2: 4
> print(DT) # yes
a
<int>
Expand All @@ -69,7 +73,11 @@ NULL
2: 5
> (function(){DT[2,a:=5L];NULL})() # print NULL
NULL
> print(DT) # no. only DT[] is guaranteed print from v1.9.6 and R 3.2.0
> print(DT) # yes. restored in #6631 behavior that had changed in 1.9.6.
a
<int>
1: 1
2: 5
> (function(){DT[2,a:=5L][];NULL})() # print NULL
NULL
> DT # yes. i) function needs to add [] after last one, so that "DT" alone is guaranteed anyway
Expand All @@ -93,7 +101,11 @@ NULL
1: 1
2: 6
> {DT[2,a:=6L];invisible()} # no
> print(DT) # no
> print(DT) # yes
a
<int>
1: 1
2: 6
> (function(){print(DT[2,a:=7L]);print(DT);invisible()})() # yes*2
a
<int>
Expand All @@ -103,7 +115,11 @@ NULL
<int>
1: 1
2: 7
> {print(DT[2,a:=8L]);print(DT);invisible()} # yes*1 Not within function so as at prompt
> {print(DT[2,a:=8L]);print(DT);invisible()} # yes*2 as at prompt, again as of #6631
a
<int>
1: 1
2: 8
a
<int>
1: 1
Expand Down Expand Up @@ -136,6 +152,15 @@ NULL
1: 10
2: 10
>
> # child class of data.table doesn't induce unintended print, #3029
> dt <- data.table(x = 1)
> class(dt) <- c("foo", "data.table", "data.frame")
> print.foo <- function(x, ...) {
+ NextMethod("print")
+ }
>
> dt[, y := 1] # no
>
> proc.time()
user system elapsed
0.223 0.016 0.231
0.209 0.053 0.288

0 comments on commit 32e5cf2

Please sign in to comment.