Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

approximately

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

approximately

  • 1.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

= approximately

Facilitates comparisons of Floats, primarily in mocking. Some mocking libraries will not allow you to put delta assertions within the expectations of your method calls. For example:

some_animated_point = mock() some_animated_point.should_receive(:move_to).with(0.01456, -234.786)

Then you feed this mock from the outside, using some iterative algorithm with arbitrary precision, and your assertion fails.

Expectation not matched: called "move_to" with (0.01455999, -234.78576)

This comes because what you should have done is comparing within a delta/epsilon value instead of comparing verbatim.

However, once the expected argument has been swallowed by the mocking library you don't really have any control on what is going to happen to it, and in most mocking libraries you cannot add detailed assertions on the expectation arguments.

So what we can do instead is create a special object that will be equal to another Float object when they both are in delta of one another. For example:

Approximately.approx(23.4, 0.1) == 23.44 # true

In your unit testing/spec framework du jour your test would look like this:

include Approximately

some_animated_point = mock() some_animated_point.should_receive(:move_to).with(approx(0.01456, 0.001), approx(-234.786, 0.001))

These objects will compare properly with your reference inputs.

You can also get this magic float object without including Approximately by calling

Approximately.approx(23.4, 0.1)

This module is completely uninvasive and contains no hacks for minitest, RSpec or other testing/mocking frameworks.

== Contributing to approximately

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

== Copyright

Copyright (c) 2012 Julik. See LICENSE.txt for further details.

FAQs

Package last updated on 07 Sep 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc