Mockingbird Documentation 0.16.0

Function around(_:​tolerance:​)

public func around<T: FloatingPoint>(_ value: T, tolerance: T) -> T

Matches floating point arguments within some tolerance.

Mathematical operations on floating point numbers can cause loss of precision. Fuzzily match floating point arguments instead of using exact values to increase the robustness of tests.

protocol Bird {
  func canChirp(volume: Double) -> Bool
}

given(bird.canChirp(volume: around(42.0, tolerance: 0.1)))
  .willReturn(true)

print(bird.canChirp(volume: 42.0))     // Prints "true"
print(bird.canChirp(volume: 42.0999))  // Prints "true"
print(bird.canChirp(volume: 42.1))     // Prints "false"

Parameters

value T

The expected value.

tolerance T

Only matches if the absolute difference is strictly less than this value.