Skip to content

Commit

Permalink
#106 Trying out SO solution
Browse files Browse the repository at this point in the history
  • Loading branch information
bms63 committed Sep 29, 2022
1 parent e3ad1e3 commit 5faf839
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -36,6 +36,7 @@ export(desc)
export(dquote)
export(enumerate)
export(expect_dfs_equal)
export(expect_equal_tbl)
export(extract_vars)
export(filter_if)
export(get_constant_vars)
Expand Down
38 changes: 38 additions & 0 deletions R/expect_dfs_equal.R
Expand Up @@ -26,3 +26,41 @@ expect_dfs_equal <- function(base, compare, keys, ...) {
invisible()
}
}

#' Expectation: Are Table Lists are Equal?
#'
#' Uses [diffdf::diffdf()] to compares 2 datasets for any differences
#'
#' @param base Input dataset
#' @param compare Comparison dataset

#' @return
#' An error if `base` and `compare` do not match or `NULL` invisibly if they do
#'
#' @author Ben Straub
#' @keywords test_helper
#' @family test_helper
#'
#' @export
expect_equal_tbl <- function(object, expected, ..., info = NULL) {
act <- testthat::quasi_label(rlang::enquo(object), arg = "object")
exp <- testthat::quasi_label(rlang::enquo(expected), arg = "expected")

# all.equal.list is slightly problematic: it returns TRUE for match, and
# returns a character vector when differences are observed. We extract
# both a match-indicator and a failure message

diffs <- all.equal.list(object, expected, ...)
has_diff <- if (is.logical(diffs)) diffs else FALSE
diff_msg <- paste(diffs, collapse = "\n")

testthat::expect(
has_diff,
failure_message = sprintf(
"%s not equal to %s.\n%s", act$lab, exp$lab, diff_msg
),
info = info
)

invisible(act$val)
}
4 changes: 4 additions & 0 deletions man/expect_dfs_equal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/expect_equal_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-quo.R
Expand Up @@ -57,15 +57,15 @@ test_that("replace_symbol_in_quo Test 5: symbol in expression is replaced", {
# add_suffix_to_vars ----
## Test 6: with single variable ----
test_that("add_suffix_to_vars Test 6: with single variable", {
expect_equal(
expect_equal_tbl(
expected = vars(ADT, desc(AVAL.join), AVALC),
object = add_suffix_to_vars(vars(ADT, desc(AVAL), AVALC), vars = vars(AVAL), suffix = ".join")
)
})

## Test 7: with more than one variable ----
test_that("add_suffix_to_vars Test 7: with more than one variable", {
expect_equal(
expect_equal_tbl(
expected = vars(ADT, desc(AVAL.join), AVALC.join),
object = add_suffix_to_vars(
vars(ADT, desc(AVAL), AVALC),
Expand Down

0 comments on commit 5faf839

Please sign in to comment.