Skip to content

Commit

Permalink
fix: added tests for assert_set_equal
Browse files Browse the repository at this point in the history
Completes issue #110
  • Loading branch information
selkamand committed Nov 12, 2024
1 parent 11dc89f commit 73aec2a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions R/set_operations.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ sets_are_equivalent <- function(x, y){


if(failure_mode == "both"){
return(paste0("{arg_name} is missing ",missing_plural_the,"required value",missing_plural,": {setopts_exlusive_to_first(y, x)}, and contains ",extra_plural_the, "unexpected value", extra_plural,": {setopts_exlusive_to_first(x, y)}."))
return(paste0("'{arg_name}' is missing ",missing_plural_the,"required value",missing_plural,": {setopts_exlusive_to_first(y, x)}, and contains ",extra_plural_the, "unexpected value", extra_plural,": {setopts_exlusive_to_first(x, y)}."))
}
else if(failure_mode == "extra"){
return(paste0("{arg_name} contains ", extra_plural_the, "unexpected value",extra_plural,": {setopts_exlusive_to_first(x, y)}."))
return(paste0("'{arg_name}' contains ", extra_plural_the, "unexpected value",extra_plural,": {setopts_exlusive_to_first(x, y)}."))
}
else if(failure_mode == "missing"){
return(paste0("{arg_name} is missing" ,missing_plural_the, " required value",missing_plural," {setopts_exlusive_to_first(y, x)}."))
return(paste0("'{arg_name}' is missing" ,missing_plural_the, " required value",missing_plural,": {setopts_exlusive_to_first(y, x)}."))
}
}
32 changes: 32 additions & 0 deletions tests/testthat/_snaps/assert_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,35 @@
Error:
! 'list(1, 2)' must be a scalar, not a list

# assert_set_equal() works [plain]

Code
assert_set_equal(c(1, 2, 3), c(1, 2))
Condition
Error:
! 'c(1, 2, 3)' contains an unexpected value: 3.

---

Code
assert_set_equal(c("A", "B"), c("A", "B", "C"))
Condition
Error:
! 'c("A", "B")' is missing a required value: C.

---

Code
assert_set_equal(c(1, 3, 4), c(1, 2, 3))
Condition
Error:
! 'c(1, 3, 4)' is missing a required value: 2, and contains an unexpected value: 4.

---

Code
assert_set_equal(c("A", "B", "C"), c(1, 2, 3))
Condition
Error:
! 'c("A", "B", "C")' is missing required values: 1, 2, and 3, and contains unexpected values: A, B, and C.

24 changes: 24 additions & 0 deletions tests/testthat/test-assert_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,27 @@ cli::test_that_cli("assert_one_of() works", config = "plain", {
# Custom error messages work
expect_error(assert_one_of(2, c(1, 3), msg = "Custom error message"), "Custom error message")
})


cli::test_that_cli("assert_set_equal() works", config = "plain", {
# Works for sets that are equivalent (same elements, ignoring order and duplicates)
expect_true(assert_set_equal(c(1, 2, 3), c(3, 2, 1))) # Same elements, different order
expect_true(assert_set_equal(c("A", "B", "C"), c("C", "A", "B"))) # Same elements, different order
expect_true(assert_set_equal(c(1, 2, 2, 3), c(3, 2, 1))) # Duplicate in x, equivalent to y

# Aborts for sets that differ in elements
expect_snapshot(assert_set_equal(c(1, 2, 3), c(1, 2)), error = TRUE) # Missing 3 in y
expect_snapshot(assert_set_equal(c("A", "B"), c("A", "B", "C")), error = TRUE) # Missing "C" in x
expect_snapshot(assert_set_equal(c(1, 3, 4), c(1, 2, 3)), error = TRUE) # Extra 4 in x, missing 2

# Aborts for sets with different types
expect_snapshot(assert_set_equal(c("A", "B", "C"), c(1, 2, 3)), error = TRUE) # Different types

# Error messages use variable name of passed arguments
x <- c(1, 2)
y <- c(1, 2, 3)
expect_error(assert_set_equal(x, y), "'x' is missing a required value: 3", fixed = TRUE)

# Custom error messages work
expect_error(assert_set_equal(c(1, 2, 3), c(1, 2), msg = "Custom error message"), "Custom error message")
})

0 comments on commit 73aec2a

Please sign in to comment.