Skip to content

Issues with inheritance #92

@dhersz

Description

@dhersz

Hi. I noted some issues with inheritance in the functions expect_inherits() and expect_error().

The problem is that when checking for inheritance from multiple classes the functions accept objects that inherit from just one of the classes, and not all of them. I hope this reprex below makes it clearer:

 library(tinytest)
 
 my_error <- errorCondition("my_error", class = c("a", "b"))
 
 expect_error(stop(my_error), class = c("a", "c"))
#> ----- PASSED      : <-->
#>  call| expect_error(stop(my_error), class = c("a", "c"))
 expect_error(stop(my_error), class = c("d", "c"))
#> Warning in if (is.na(x)) "" else gsub("\\n *", paste0("\n", with),
#> paste0(with, : the condition has length > 1 and only the first element will be
#> used
#> ----- FAILED[xcpt]: <-->
#>  call| expect_error(stop(my_error), class = c("d", "c"))
#>  diff| Error of class 'a, b, error, condition', does not inherit from 'd' ----- FAILED[xcpt]: <-->
#>  call| expect_error(stop(my_error), class = c("d", "c"))
#>  diff| Error of class 'a, b, error, condition', does not inherit from 'c'
 
 e <- tryCatch(stop(my_error), error = function(cnd) cnd)
 expect_inherits(e, c("a", "c"))
#> ----- PASSED      : <-->
#>  call| expect_inherits(e, c("a", "c"))
 expect_inherits(e, c("d", "c"))
#> ----- FAILED[attr]: <-->
#>  call| expect_inherits(e, c("d", "c"))
#>  diff| Expected object of class <d, c>, got <a, b, error, condition>

(please note the strange warning in the second expect_error() call as well)

I'm not actually sure if this is intended behaviour or not, but I'd expect the tests to fail in all 4 cases, not only two of them. A quick "fix" to this problem would be adjusting this line:

res <- inherits(current, class)

To read something like (and then adjust the test message):

res  <- all(inherits(current, class, which = TRUE))

A similar change would have to occur in expect_error().

I have tested this with tinytest's dev version:

 sessionInfo()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
#> 
#> locale:
#>  [1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=pt_BR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] tinytest_1.3.1.3
#> 
#> loaded via a namespace (and not attached):
#>  [1] knitr_1.33        magrittr_2.0.1    rlang_0.4.11      fansi_0.5.0      
#>  [5] stringr_1.4.0     styler_1.4.1      highr_0.9         tools_4.1.1      
#>  [9] parallel_4.1.1    xfun_0.23         utf8_1.2.1        withr_2.4.2      
#> [13] htmltools_0.5.1.1 ellipsis_0.3.2    yaml_2.2.1        digest_0.6.27    
#> [17] tibble_3.1.2      lifecycle_1.0.0   crayon_1.4.1      purrr_0.3.4      
#> [21] vctrs_0.3.8       fs_1.5.0          glue_1.4.2        evaluate_0.14    
#> [25] rmarkdown_2.8     reprex_2.0.0      stringi_1.6.2     compiler_4.1.1   
#> [29] pillar_1.6.1      backports_1.2.1   pkgconfig_2.0.3

Created on 2021-10-26 by the reprex package (v2.0.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions