Giter Club home page Giter Club logo

testidentical's Introduction

testidentical

library(testidentical)

These are equal

# Same env, different files, simple function (no if {} branch)
identical(test1_simple, test2_simple)
#> [1] TRUE

No way to make these equal

# Same env, different files, more complex function
identical(test1_if, test2_if)
#> [1] FALSE

identical(test1_if, test2_if, ignore.environment = TRUE)
#> [1] FALSE

^ This eventually calls R_body_no_src() which doesn’t recursively remove srcref/srcfile/wholeSrcref attributes. https://github.com/wch/r-source/blob/9bb47ca929c41a133786fa8fff7c70162bb75e50/src/main/util.c#L631

This is a problem because the if {} closures of test1_if() and tests_if() contain srcref info

if_body1 <- body(test1_if)[[2]]
if_body2 <- body(test2_if)[[2]]

# they look the same
if_body1
#> if (TRUE) {
#>     x
#> } else {
#>     NULL
#> }
if_body2
#> if (TRUE) {
#>     x
#> } else {
#>     NULL
#> }

# but this closure had srcref info that wasn't removed!
attributes(if_body1[[3]])
#> $srcref
#> $srcref[[1]]
#> {
#> 
#> $srcref[[2]]
#> x
#> 
#> 
#> $srcfile
#> /Users/davis/Desktop/r/playground/packages/testidentical/R/test1.R 
#> 
#> $wholeSrcref
#> .packageName <- "testidentical"
#> #line 1 "/Users/davis/Desktop/r/playground/packages/testidentical/R/test1.R"
#> #' @export
#> test1_simple <- function(x) {
#>   x
#> }
#> 
#> #' @export
#> test1_if <- function(x) {
#>   if (TRUE) {
#>     x
#>   }

attributes(if_body2[[3]])
#> $srcref
#> $srcref[[1]]
#> {
#> 
#> $srcref[[2]]
#> x
#> 
#> 
#> $srcfile
#> /Users/davis/Desktop/r/playground/packages/testidentical/R/test2.R 
#> 
#> $wholeSrcref
#> .packageName <- "testidentical"
#> #line 1 "/Users/davis/Desktop/r/playground/packages/testidentical/R/test1.R"
#> #' @export
#> test1_simple <- function(x) {
#>   x
#> }
#> 
#> #' @export
#> test1_if <- function(x) {
#>   if (TRUE) {
#>     x
#>   } else {
#>     NULL
#>   }
#> }
#> #line 1 "/Users/davis/Desktop/r/playground/packages/testidentical/R/test2.R"
#> #' @export
#> test2_simple <- function(x) {
#>   x
#> }
#> 
#> #' @export
#> test2_if <- function(x) {
#>   if (TRUE) {
#>     x
#>   }

So those differing attributes caused the functions to not look identical.

# removeSource() will recursively remove srcref/srcfile/wholeSrcref attributes
identical(
  removeSource(test1_if), 
  removeSource(test2_if)
)
#> [1] TRUE

# See, it was removed before comparison
attributes(body(removeSource(test1_if))[[2]][[3]])
#> NULL

testidentical's People

Contributors

davisvaughan avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.