ALTREP package interaction with testthat

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ALTREP package interaction with testthat

Wang Jiefei
Hi



I found a weird problem in testthat while working with an ALTREP package.
The package name is AltWrapper. My ALTREP serialize function is called even
when I'm trying to serialize a non-ALTREP object.  Here is an example



```

context("altrep")

length_func<-function(x){

    return(length(x))

}

setAltClass(className = "test", classType = "real")

setAltMethod(className = "test", getLength = length_func)

testData = runif(10)

myAltrep = makeAltrep("test", testData)



test_that("Auto serialize", {

    browser()

    A = 10

    A_serialized=serialize(A,NULL)

    B = new.env()

    B_serialized=serialize(B,NULL)

})

```

There is nothing but two variables A and B in the test function. I use
browser() to stop and debug the code. Here is the weird thing, my package
function is called when I’m trying to serialize *A* and *B*:

```

Browse[1]> A = 10

Browse[1]> A_serialized=serialize(A,NULL)

Browse[1]> B = new.env()

Browse[1]> B_serialized=serialize(B,NULL)

serializing data

Auto serializing data

[1] "Internal serialize altWrapper function"

```

The output indicates that my package function is called. The function is
designed for an ALTREP object. Since *B* is not an ALTREP, I don’t
understand why the ALTREP related function has been called. Also, variable
A works find. It seems like R dispatches the serialize function according
to the variable type, but I am not sure if there is any way to see the
dispatching rules. Here are the things I have tried:

1. if I comment out the code that defines an ALTREP in my example, the bug
will disappear, so it might relate to the implementation of ALTREP.

2. If I run the test script in the global environment, R wouldn’t call the
internal function, it only happens when I call `devtools::test()`.


I am stuck here. I do not know if it is a bug of my package or testthat or
the ALTREP implementation of R or possibly the interaction between them. I
will appreciate if anyone can shed light on it. Here is the link to my
package test file:

https://github.com/Jiefei-Wang/AltWrapper/blob/master/tests/testthat/test_test.R




Best,

Jiefei

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: ALTREP package interaction with testthat

Dirk Eddelbuettel

On 12 August 2019 at 12:56, Wang Jiefei wrote:
| I am stuck here. I do not know if it is a bug of my package or testthat or
| the ALTREP implementation of R or possibly the interaction between them.

I don't have any concrete advice for your issue here but when I find myself
in similar debugging situations involving four or more moving parts, I
usually try to remove one or two. Once I am down to 'just base R and my
package' it tends to become much clearer where the issue is.

_Narrator: Base R usually wins._

Dirk

--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel