Last active
February 21, 2020 18:53
-
-
Save sharlagelfand/d76bb5f436c161803a2d0d83c0501919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(testthat) | |
library(dplyr, warn.conflicts = FALSE) | |
# expect_equal() returns silently if they're the same | |
expect_equal(iris, iris) | |
# but with an error if they're not | |
expect_equal(iris, mtcars) | |
#> Error: `iris` not equal to `mtcars`. | |
#> Names: 5 string mismatches | |
#> Attributes: < Component "row.names": Modes: numeric, character > | |
#> Attributes: < Component "row.names": target is numeric, current is character > | |
#> Length mismatch: comparison on first 5 components | |
#> Component 1: Numeric: lengths (150, 32) differ | |
#> Component 2: Numeric: lengths (150, 32) differ | |
#> Component 3: Numeric: lengths (150, 32) differ | |
#> Component 4: Numeric: lengths (150, 32) differ | |
#> Component 5: 'current' is not a factor | |
# Test will fail because iris != mtcars and expect_equal() will return an *error* saying so | |
test_that("iris is iris and mtcars isn't iris, using expect_equal", { | |
expect_equal(iris, iris) | |
expect_equal(iris, mtcars) | |
}) | |
#> Error: Test failed: 'iris is iris and mtcars isn't iris, using expect_equal' | |
#> * `iris` not equal to `mtcars`. | |
#> Names: 5 string mismatches | |
#> Attributes: < Component "row.names": Modes: numeric, character > | |
#> Attributes: < Component "row.names": target is numeric, current is character > | |
#> Length mismatch: comparison on first 5 components | |
#> Component 1: Numeric: lengths (150, 32) differ | |
#> Component 2: Numeric: lengths (150, 32) differ | |
#> Component 3: Numeric: lengths (150, 32) differ | |
#> Component 4: Numeric: lengths (150, 32) differ | |
#> Component 5: 'current' is not a factor | |
# all_equal() returns TRUE if they're the same | |
all_equal(iris, iris) | |
#> [1] TRUE | |
# and just a description of what's not the same if they're not | |
all_equal(iris, mtcars) | |
#> [1] "Cols in y but not x: `carb`, `am`, `vs`, `qsec`, `hp`, `drat`, `gear`, `disp`, `cyl`, `wt`, `mpg`. " | |
#> [2] "Cols in x but not y: `Species`, `Petal.Width`, `Petal.Length`, `Sepal.Width`, `Sepal.Length`. " | |
# test passes even though iris != mtcars because all_equal() only returns a strings saying they're not equal | |
test_that("iris is iris and mtcars isn't iris, using all_equal", { | |
all_equal(iris, iris) | |
all_equal(iris, mtcars) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment