approximately 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "shoulda", ">= 0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.8.3"
7
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Julik
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ = approximately
2
+
3
+ Facilitates comparisons of Floats, primarily in mocking. Some mocking libraries will not allow you to put
4
+ delta assertions within the expectations of your method calls. For example:
5
+
6
+ some_animated_point = mock()
7
+ mock.should_receive(:move_to).with(0.01456, -234.786)
8
+
9
+ Then you feed this mock from the outside, using some iterative algorithm with arbitrary precision, and your
10
+ assertion fails.
11
+
12
+ Expectation not matched: called "move_to" with (0.01455999, -234.78576)
13
+
14
+ This comes because what you should have done is comparing within a delta/epsilon value instead of comparing verbatim.
15
+
16
+ And this is what most mocking libraries will do.
17
+
18
+ Some mocking libraries allow you to stuff an "assert_in_delta" call into the block of the expectation setup, but not
19
+ all of them - and the expectation block might not be called at all if the passed arguments do not match. So what
20
+ we can do instead is create a special object that will be equal to another Float object when they both are in delta
21
+ of one another. So instead you would do this (in your unit/spec framework du jour)
22
+
23
+ include Approximately
24
+
25
+ some_animated_point = mock()
26
+ mock.should_receive(:move_to).with(approx(0.01456, 0.001), approx(-234.786, 0.001))
27
+
28
+ These objects will compare properly with your reference inputs.
29
+
30
+ == Contributing to approximately
31
+
32
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
33
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
34
+ * Fork the project.
35
+ * Start a feature/bugfix branch.
36
+ * Commit and push until you are happy with your contribution.
37
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
38
+ * 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.
39
+
40
+ == Copyright
41
+
42
+ Copyright (c) 2012 Julik. See LICENSE.txt for
43
+ further details.
44
+
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+ require File.dirname(__FILE__) + '/lib/approximately'
14
+
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.version = Approximately::VERSION
19
+ gem.name = "approximately"
20
+ gem.homepage = "http://github.com/julik/approximately"
21
+ gem.license = "MIT"
22
+ gem.summary = %Q{Facilitates float comparisons in mocks}
23
+ gem.email = "me@julik.nl"
24
+ gem.authors = ["Julik"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ task :default => :test
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "approximately"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Julik"]
12
+ s.date = "2012-05-07"
13
+ s.email = "me@julik.nl"
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ "Gemfile",
21
+ "LICENSE.txt",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "approximately.gemspec",
25
+ "lib/approximately.rb",
26
+ "test/helper.rb",
27
+ "test/test_approximately.rb"
28
+ ]
29
+ s.homepage = "http://github.com/julik/approximately"
30
+ s.licenses = ["MIT"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = "1.8.15"
33
+ s.summary = "Facilitates float comparisons in mocks"
34
+
35
+ if s.respond_to? :specification_version then
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
40
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
41
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
42
+ else
43
+ s.add_dependency(%q<shoulda>, [">= 0"])
44
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
46
+ end
47
+ else
48
+ s.add_dependency(%q<shoulda>, [">= 0"])
49
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
51
+ end
52
+ end
53
+
@@ -0,0 +1,32 @@
1
+ module Approximately
2
+ VERSION = "1.0.0"
3
+ DEFAULT_DELTA = 0.01
4
+
5
+ # This object can be used for float comparisons. When
6
+ # compared to another Float (or an object that responds to to_f)
7
+ # it will be equal to that object if the float value of the ApproximateFloat
8
+ # and the value being compared to are within a passed delta
9
+ class DeltaFloat
10
+ include Comparable
11
+ attr_accessor :float, :delta
12
+ alias_method :to_f, :float
13
+
14
+ def initialize(float_value, delta)
15
+ @float, @delta = float_value.to_f, delta
16
+ end
17
+
18
+ def to_f
19
+ @float
20
+ end
21
+
22
+ def <=>(another)
23
+ d = (@float - another.to_f).abs
24
+ return 0 if d < delta
25
+ float <=> another.to_f
26
+ end
27
+ end
28
+
29
+ def approx(float, delta = DEFAULT_DELTA)
30
+ DeltaFloat.new(float, delta)
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'approximately'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class TestApproximately < Test::Unit::TestCase
4
+ include Approximately
5
+
6
+ should "return an approximate object" do
7
+ obj = approx(10)
8
+ assert_kind_of Approximately::DeltaFloat, obj
9
+ assert_kind_of Float, obj.float, "Should have stored a float in the attribute"
10
+ assert_equal obj.float, obj.to_f
11
+ assert_in_delta 10.0, obj.float, 0.01
12
+ end
13
+
14
+ should "compare to another float within delta" do
15
+ d = 0.1
16
+ assert_equal approx(1, d), approx(1, d), "Should be equal within delta"
17
+ assert_equal approx(1, d), approx(0.95, d), "Should be equal within delta"
18
+ assert_not_equal approx(1, d), approx(1.11, d), "Should not be equal outside delta"
19
+ end
20
+
21
+ should "create the object with default delta" do
22
+ a = approx(0.013)
23
+ assert_equal a.delta, Approximately::DEFAULT_DELTA
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: approximately
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Julik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: &9144310 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *9144310
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &9143030 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *9143030
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &9141960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.8.3
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *9141960
47
+ description:
48
+ email: me@julik.nl
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - LICENSE.txt
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - Gemfile
57
+ - LICENSE.txt
58
+ - README.rdoc
59
+ - Rakefile
60
+ - approximately.gemspec
61
+ - lib/approximately.rb
62
+ - test/helper.rb
63
+ - test/test_approximately.rb
64
+ homepage: http://github.com/julik/approximately
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: -499016081
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.15
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Facilitates float comparisons in mocks
92
+ test_files: []