Learn R Programming

testthat (version 0.9.1)

equals: Expectation: is the object equal (with numerical tolerance) to a value?

Description

Comparison performed using all.equal.

Usage

equals(expected, label = NULL, ...)

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

Arguments

expected
Expected value
label
For full form, label of expected object used in error messages. Useful to override default (deparsed expected expression) when doing tests in a loop. For short cut form, object label. When NULL, computed from deparsed object.
...
other values passed to all.equal
object
object to test
info
extra information to be included in the message (useful when writing tests in loops).
expected.label
Equivalent of label for shortcut form.

See Also

Other expectations: equals_reference, expect_equal_to_reference; expect-compare, expect_less_than, expect_more_than, is_less_than, is_more_than; expect_equivalent, is_equivalent_to; expect_error, throws_error; expect_false, expect_true, is_false, is_true; expect_identical, is_identical_to; expect_is, is_a; expect_match, matches; expect_message, shows_message; expect_named, has_names; expect_null, is_null; expect_output, prints_text; expect_warning, gives_warning; takes_less_than

Examples

Run this code
a <- 10
expect_equal(a, 10)

# Use equals() 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
expect_true(sqrt(2) ^ 2 == 2)
expect_identical(sqrt(2) ^ 2, 2)

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

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

Run the code above in your browser using DataLab