Skip to content

Commit 25959a4

Browse files
committed
add expect_not_contains() (#1851)
1 parent 387580b commit 25959a4

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export(expect_no_match)
113113
export(expect_no_message)
114114
export(expect_no_success)
115115
export(expect_no_warning)
116+
export(expect_not_contains)
116117
export(expect_null)
117118
export(expect_output)
118119
export(expect_output_file)

R/expect-setequal.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#' and that every element of `y` occurs in `x`.
55
#' * `expect_contains(x, y)` tests that `x` contains every element of `y`
66
#' (i.e. `y` is a subset of `x`).
7+
#' * `expect_not_contains(x, y)` tests that `x` contains none of the elements
8+
#' of `y` (i.e. `y` is disjoint from `x`).
79
#' * `expect_in(x, y)` tests every element of `x` is in `y`
810
#' (i.e. `x` is a subset of `y`).
911
#' * `expect_mapequal(x, y)` treats lists as if they are mappings between names
@@ -117,6 +119,33 @@ expect_contains <- function(object, expected) {
117119
pass(act$val)
118120
}
119121

122+
#' @export
123+
#' @rdname expect_setequal
124+
expect_not_contains <- function(object, expected) {
125+
act <- quasi_label(enquo(object))
126+
exp <- quasi_label(enquo(expected))
127+
128+
check_vector(object)
129+
check_vector(expected)
130+
131+
exp_found <- exp$val %in% act$val
132+
if (any(exp_found)) {
133+
msg_exp <- sprintf(
134+
"Expected %s to contain none of the values in %s.",
135+
act$lab,
136+
exp$lab
137+
)
138+
msg_act <- c(
139+
sprintf("Actual: %s", values(act$val)),
140+
sprintf("Expected: none of %s", values(exp$val)),
141+
sprintf("Found: %s", values(exp$val[exp_found]))
142+
)
143+
fail(c(msg_exp, msg_act))
144+
}
145+
146+
pass(act$val)
147+
}
148+
120149
#' @export
121150
#' @rdname expect_setequal
122151
expect_in <- function(object, expected) {

man/expect_setequal.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/expect-setequal.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@
121121
Expected: "d", "e"
122122
Missing: "d", "e"
123123

124+
# expect_not_contains() gives useful message on failure
125+
126+
Code
127+
expect_not_contains(x1, x2)
128+
Condition
129+
Error:
130+
! Expected `x1` to contain none of the values in `x2`.
131+
Actual: "a", "b", "c"
132+
Expected: none of "c", "d"
133+
Found: "c"
134+
135+
---
136+
137+
Code
138+
expect_not_contains(x1, x3)
139+
Condition
140+
Error:
141+
! Expected `x1` to contain none of the values in `x3`.
142+
Actual: "a", "b", "c"
143+
Expected: none of "b", "c", "d"
144+
Found: "b", "c"
145+
124146
# expect_in() gives useful message on failure
125147

126148
Code

tests/testthat/test-expect-setequal.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ test_that("expect_contains() gives useful message on failure", {
131131
expect_snapshot_failure(expect_contains(x1, x3))
132132
})
133133

134+
# not_contains ----------------------------------------------------------------
135+
136+
test_that("expect_not_contains() succeeds when appropriate", {
137+
expect_success(expect_not_contains(letters, 1))
138+
expect_success(expect_not_contains(letters, LETTERS))
139+
expect_success(expect_not_contains(letters, character()))
140+
})
141+
142+
test_that("expect_not_contains() gives useful message on failure", {
143+
x1 <- c("a", "b", "c")
144+
x2 <- c("c", "d")
145+
x3 <- c("b", "c", "d")
146+
147+
expect_snapshot_failure(expect_not_contains(x1, x2))
148+
expect_snapshot_failure(expect_not_contains(x1, x3))
149+
})
150+
134151

135152
# in ----------------------------------------------------------------
136153

0 commit comments

Comments
 (0)