beCloseTo method

NumericAssertions beCloseTo(
  1. num nearbyValue, {
  2. required num delta,
})

Asserts an numeric value is close to another value within a specified value.

delta The maximum amount of which the two values may differ.

Implementation

NumericAssertions beCloseTo(num nearbyValue, {required num delta}) {
  var diff = nearbyValue - subject!;
  if (diff < 0) diff = -diff;
  final isClose = diff <= delta;

  if (isReversed) {
    Execute.assertion.forCondition(isClose).failWith(
          '$subjectLabel\n    $subject\nshould not be close to\n    $nearbyValue\nwith tolerance $delta\n    but does.',
        );
  } else {
    Execute.assertion.forCondition(!isClose).failWith(
          '$subjectLabel\n    $subject\nshould be close to\n    $nearbyValue\nwith tolerance $delta\n    but does not.',
        );
  }

  return NumericAssertions(subject);
}