Skip to content

Commit eb9eca6

Browse files
committed
replace expect_not_in() and expect_not_contains() by expect_disjoint()
1 parent 86ef654 commit eb9eca6

File tree

6 files changed

+24
-97
lines changed

6 files changed

+24
-97
lines changed

NAMESPACE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export(expect)
8282
export(expect_condition)
8383
export(expect_contains)
8484
export(expect_cpp_tests_pass)
85+
export(expect_disjoint)
8586
export(expect_equal)
8687
export(expect_equal_to_reference)
8788
export(expect_equivalent)
@@ -113,8 +114,6 @@ export(expect_no_match)
113114
export(expect_no_message)
114115
export(expect_no_success)
115116
export(expect_no_warning)
116-
export(expect_not_contains)
117-
export(expect_not_in)
118117
export(expect_null)
119118
export(expect_output)
120119
export(expect_output_file)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# testthat (development version)
22

3-
* New `expect_not_contains()` and `expect_not_in()` to check for the absence of values (#1851).
3+
* New `expect_disjoint()` to check for the absence of values (#1851).
44
* New `vignette("mocking")` explains mocking in detail (#1265).
55
* New `vignette("challenging-functions")` provides an index to other documentation organised by testing challenges (#1265).
66
* When running a test interactively, testthat now reports the number of succeses. The results should also be more useful if you are using nested tests.

R/expect-setequal.R

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
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`).
97
#' * `expect_in(x, y)` tests that every element of `x` is in `y`
108
#' (i.e. `x` is a subset of `y`).
11-
#' * `expect_not_in(x, y)` tests that no element of `x` is in `y`
9+
#' * `expect_disjoint(x, y)` tests that no element of `x` is in `y`
1210
#' (i.e. `x` is disjoint from `y`).
1311
#' * `expect_mapequal(x, y)` treats lists as if they are mappings between names
1412
#' and values. Concretely, this drops `NULL`s in both objects and sorts
@@ -121,32 +119,6 @@ expect_contains <- function(object, expected) {
121119
pass(act$val)
122120
}
123121

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

151123
#' @export
152124
#' @rdname expect_setequal
@@ -177,24 +149,24 @@ expect_in <- function(object, expected) {
177149

178150
#' @export
179151
#' @rdname expect_setequal
180-
expect_not_in <- function(object, expected) {
152+
expect_disjoint <- function(object, expected) {
181153
act <- quasi_label(enquo(object))
182154
exp <- quasi_label(enquo(expected))
183155

184156
check_vector(object)
185157
check_vector(expected)
186158

187-
act_found <- act$val %in% exp$val
188-
if (any(act_found)) {
159+
act_common <- act$val %in% exp$val
160+
if (any(act_common)) {
189161
msg_exp <- sprintf(
190-
"Expected %s to contain no values from %s.",
162+
"Expected %s to be disjoint from %s.",
191163
act$lab,
192164
exp$lab
193165
)
194166
msg_act <- c(
195167
sprintf("Actual: %s", values(act$val)),
196168
sprintf("Expected: none of %s", values(exp$val)),
197-
sprintf("Invalid: %s", values(act$val[act_found]))
169+
sprintf("Invalid: %s", values(act$val[act_common]))
198170
)
199171
fail(c(msg_exp, msg_act))
200172
}

man/expect_setequal.Rd

Lines changed: 3 additions & 8 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: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,6 @@
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-
146124
# expect_in() gives useful message on failure
147125

148126
Code
@@ -165,24 +143,24 @@
165143
Expected: "d", "e"
166144
Invalid: "a", "b"
167145

168-
# expect_not_in() gives useful message on failure
146+
# expect_disjoint() gives useful message on failure
169147

170148
Code
171-
expect_not_in(x1, x2)
149+
expect_disjoint(x1, x2)
172150
Condition
173151
Error:
174-
! Expected `x1` to contain no values from `x2`.
152+
! Expected `x1` to be disjoint from `x2`.
175153
Actual: "a", "b", "c"
176154
Expected: none of "c", "d"
177155
Invalid: "c"
178156

179157
---
180158

181159
Code
182-
expect_not_in(x1, x3)
160+
expect_disjoint(x1, x3)
183161
Condition
184162
Error:
185-
! Expected `x1` to contain no values from `x3`.
163+
! Expected `x1` to be disjoint from `x3`.
186164
Actual: "a", "b", "c"
187165
Expected: none of "b", "c", "d"
188166
Invalid: "b", "c"

tests/testthat/test-expect-setequal.R

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,6 @@ 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-
151134

152135
# in ----------------------------------------------------------------
153136

@@ -166,19 +149,19 @@ test_that("expect_in() gives useful message on failure", {
166149
expect_snapshot_failure(expect_in(x1, x3))
167150
})
168151

169-
# not_in ----------------------------------------------------------------
152+
# disjoint ----------------------------------------------------------------
170153

171-
test_that("expect_not_in() succeeds when appropriate", {
172-
expect_success(expect_not_in(1, letters))
173-
expect_success(expect_not_in(LETTERS, letters))
174-
expect_success(expect_not_in(character(), letters))
154+
test_that("expect_disjoint() succeeds when appropriate", {
155+
expect_success(expect_disjoint(1, letters))
156+
expect_success(expect_disjoint(LETTERS, letters))
157+
expect_success(expect_disjoint(character(), letters))
175158
})
176159

177-
test_that("expect_not_in() gives useful message on failure", {
160+
test_that("expect_disjoint() gives useful message on failure", {
178161
x1 <- c("a", "b", "c")
179162
x2 <- c("c", "d")
180163
x3 <- c("b", "c", "d")
181164

182-
expect_snapshot_failure(expect_not_in(x1, x2))
183-
expect_snapshot_failure(expect_not_in(x1, x3))
165+
expect_snapshot_failure(expect_disjoint(x1, x2))
166+
expect_snapshot_failure(expect_disjoint(x1, x3))
184167
})

0 commit comments

Comments
 (0)