equality-expectations: Expectation: is the object equal to a value?

Description Usage Arguments See Also Examples

Description

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
expect_equal(object, expected, ..., info = NULL, label = NULL,
  expected.label = NULL)

expect_equivalent(object, expected, ..., info = NULL, label = NULL,
  expected.label = NULL)

expect_identical(object, expected, info = NULL, label = NULL,
  expected.label = NULL, ...)

expect_reference(object, expected, info = NULL, label = NULL,
  expected.label = NULL)

Arguments

object, expected

Computation and value to compare it to.

Both arguments supports limited unquoting to make it easier to generate readable failures within a function or for loop. See quasi_label for more details.

...

For expect_equal() and expect_equivalent(), passed on compare(), for expect_identical() passed on to identical(). Used to control the details of the comparison.

info

Extra information to be included in the message. This argument is soft-deprecated and should not be used in new code. Instead see alternatives in quasi_label.

label, expected.label

Used to customise failure messages. For expert use only.

See Also

expect_setequal() to test for set equality.

Other expectations: comparison-expectations, expect_length, expect_match, expect_named, expect_null, inheritance-expectations, logical-expectations, output-expectations

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
a <- 10
expect_equal(a, 10)

# Use expect_equal() when testing for numeric equality
sqrt(2) ^ 2 - 1
expect_equal(sqrt(2) ^ 2, 2)
# Neither of these forms take floating point representation errors into
# account
## Not run: 
expect_true(sqrt(2) ^ 2 == 2)
expect_identical(sqrt(2) ^ 2, 2)

## End(Not run)

# You can pass on additional arguments to all.equal:
## Not run: 
# Test the ABSOLUTE difference is within .002
expect_equal(10.01, 10, tolerance = .002, scale = 1)

## End(Not run)

# Test the RELATIVE difference is within .002
x <- 10
expect_equal(10.01, expected = x, tolerance = 0.002, scale = x)

# expect_equivalent ignores attributes
a <- b <- 1:3
names(b) <- letters[1:3]
expect_equivalent(a, b)

r-lib/testthat documentation built on May 1, 2019, 8:14 a.m.