R: Expectation: is the object equal to a value?

equality-expectations {testthat} | R Documentation

## Expectation: is the object equal to a value?

### Description

- `expect_identical` tests with `identical()`

- `expect_equal` tests with `all.equal()`

- `expect_setequal` ignores order and duplicates

- `expect_equivalent` tests with `all.equal()` and `check.attributes = FALSE`

- `expect_reference` tests if two symbols point to the same underlying object in memory (requires rlang 1.2.9000 or greater)

### Usage

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

    expect_setequal(object, expected)

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

    expect_identical(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` | object to test
`expected` | Expected value
`...` | other values passed to `all.equal()`
`info` | extra information to be included in the message (useful when writing tests in loops).
`label` | object label. When `NULL`, computed from deparsed object.
`expected.label` | Equivalent of `label` for shortcut form.

### See Also

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

### Examples

    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)

[Package testthat version 2.0.1 Index]