inexact_equality_warning 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae03455fe61747ac2eb8451499e8f16f416e11dd
4
+ data.tar.gz: 63bceb16e59cbd174328bbed7e065f7feff02e01
5
+ SHA512:
6
+ metadata.gz: 4a7921d82180fb2d5487e68ea37233dfa31ca5748b4403e18ae8795dfd0d3d9efdf600ffaa3688c4bf473416c018ff52e800faebf7250584b5863871fe354d24
7
+ data.tar.gz: 48e48d662db050e4b3ae8fef947d1aa4382c3d0a5a8979887d552b67df5cc851808e9e25d2e9a31c1ba38b931043a2ea3c65a50716375b5cc68926ece01e2796
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1
6
+ - 2.2
7
+ - ruby-head
8
+ script:
9
+ - bundle exec rspec
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org/"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ (MIT License)
2
+
3
+ Copyright (c) 2015 Adam Prescott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ ```ruby
2
+ x = 1.0 - 0.9 - 0.1
3
+ x == 0.0
4
+ # WARNING: Testing for equality between inexact floats is ill-advised, when comparing -2.7755575615628914e-17 and 0 (/path/to/file.rb:123)
5
+ # => false
6
+ ```
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "inexact_equality_warning"
3
+ gem.version = "0.0.1"
4
+ gem.authors = ["Adam Prescott"]
5
+ gem.email = ["adam@aprescott.com"]
6
+ gem.description = "Warn when comparing floats for equality."
7
+ gem.summary = "You do not want to compare floating point numbers for equality."
8
+ gem.homepage = "https://github.com/aprescott/ruby-inexact_equality_warning"
9
+
10
+ gem.files = Dir["{lib/**/*,spec/**/*,*.gemspec}"] + %w[.rspec .travis.yml LICENSE Gemfile README.md]
11
+ gem.test_files = Dir["spec/*"]
12
+ gem.require_path = "lib"
13
+ gem.licenses = ["MIT"]
14
+
15
+ gem.required_ruby_version = ">= 2.0.0"
16
+
17
+ gem.add_development_dependency("rspec", ">= 3.0")
18
+ end
@@ -0,0 +1,10 @@
1
+ # it would be good to use #inexact? here -- https://bugs.ruby-lang.org/issues/5321
2
+ module EqualityWarning
3
+ def ==(other)
4
+ warn "WARNING: Testing for equality between inexact floats is ill-advised, when comparing #{self} and #{other} (#{caller.first})" if self.is_a?(Float) || other.is_a?(Float)
5
+ super
6
+ end
7
+ end
8
+
9
+ # classes that are leaf nodes of all subclasses of Numeric
10
+ [Fixnum, Float, Bignum, Rational, Complex].each { |klass| klass.prepend(EqualityWarning) }
@@ -0,0 +1,52 @@
1
+ def capturing_stderr(&block)
2
+ original_stderr = $stderr
3
+ $stderr = output_capturer = StringIO.new
4
+ begin
5
+ yield
6
+ ensure
7
+ $stderr = original_stderr
8
+ end
9
+ output_capturer.string
10
+ end
11
+
12
+ RSpec.describe EqualityWarning do
13
+ def expect_warning(x, y)
14
+ expect(capturing_stderr { x == y }).to include("WARNING: Testing for equality between inexact floats is ill-advised, when comparing #{x} and #{y} (#{__FILE__}:#{__LINE__}:in `block in #{__method__}'")
15
+ end
16
+
17
+ def expect_no_warning(x, y)
18
+ expect(capturing_stderr { x == y }).to_not include("WARNING")
19
+ end
20
+
21
+ [
22
+ 1.0, 1.0,
23
+ 1.0, 1.0,
24
+ 1.0, 1,
25
+ 1, 1.0,
26
+ 1.0, Complex(1, 0),
27
+ 1.0, Complex(0, 2),
28
+ 1.0, 2**1000, # Bignum
29
+ ].each_slice(2) do |x, y|
30
+ specify { expect_warning(x, y) }
31
+ end
32
+
33
+ [
34
+ "anything", 1.0,
35
+ 1, 1,
36
+ Complex(1, 0), Complex(0, 2),
37
+ "anything", "anything",
38
+ 2**1000, 2**1000, # Bignum
39
+ ].each_slice(2) do |x, y|
40
+ specify { expect_no_warning(x, y) }
41
+ end
42
+
43
+ it "is included on all Numeric leaf subclasses" do
44
+ constants = Object.constants.map { |sym| Object.const_get(sym) }
45
+ prepended_classes = constants.select { |klass| klass.is_a?(Class) && klass.ancestors.include?(EqualityWarning) }
46
+ numeric_classes = constants.select { |klass| klass.is_a?(Class) && klass < Numeric }
47
+
48
+ expect(prepended_classes).to eq([Fixnum, Float, Bignum, Rational, Complex])
49
+ # compact all `numeric_classes` by removing any that are subclasses of another class in `numeric_classes`
50
+ expect(numeric_classes.reject { |klass| numeric_classes.any? { |other_class| other_class < klass } }).to eq(prepended_classes)
51
+ end
52
+ end
@@ -0,0 +1,76 @@
1
+ require "bundler/setup"
2
+ require "inexact_equality_warning"
3
+
4
+ RSpec.configure do |config|
5
+ # rspec-expectations config goes here. You can use an alternate
6
+ # assertion/expectation library such as wrong or the stdlib/minitest
7
+ # assertions if you prefer.
8
+ config.expect_with :rspec do |expectations|
9
+ # This option will default to `true` in RSpec 4. It makes the `description`
10
+ # and `failure_message` of custom matchers include text for helper methods
11
+ # defined using `chain`, e.g.:
12
+ # be_bigger_than(2).and_smaller_than(4).description
13
+ # # => "be bigger than 2 and smaller than 4"
14
+ # ...rather than:
15
+ # # => "be bigger than 2"
16
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
17
+
18
+ expectations.syntax = :expect
19
+ end
20
+
21
+ # rspec-mocks config goes here. You can use an alternate test double
22
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
23
+ config.mock_with :rspec do |mocks|
24
+ # Prevents you from mocking or stubbing a method that does not exist on
25
+ # a real object. This is generally recommended, and will default to
26
+ # `true` in RSpec 4.
27
+ mocks.verify_partial_doubles = true
28
+
29
+ mocks.syntax = :expect
30
+ end
31
+
32
+ # These two settings work together to allow you to limit a spec run
33
+ # to individual examples or groups you care about by tagging them with
34
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
35
+ # get run.
36
+ config.filter_run :focus
37
+ config.run_all_when_everything_filtered = true
38
+
39
+ # Limits the available syntax to the non-monkey patched syntax that is
40
+ # recommended. For more details, see:
41
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
42
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
43
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
44
+ config.disable_monkey_patching!
45
+
46
+ # This setting enables warnings. It's recommended, but in some cases may
47
+ # be too noisy due to issues in dependencies.
48
+ config.warnings = true
49
+
50
+ # Many RSpec users commonly either run the entire suite or an individual
51
+ # file, and it's useful to allow more verbose output when running an
52
+ # individual spec file.
53
+ if config.files_to_run.one?
54
+ # Use the documentation formatter for detailed output,
55
+ # unless a formatter has already been configured
56
+ # (e.g. via a command-line flag).
57
+ # config.default_formatter = "doc"
58
+ end
59
+
60
+ # Print the 10 slowest examples and example groups at the
61
+ # end of the spec run, to help surface which specs are running
62
+ # particularly slow.
63
+ # config.profile_examples = 10
64
+
65
+ # Run specs in random order to surface order dependencies. If you find an
66
+ # order dependency and want to debug it, you can fix the order by providing
67
+ # the seed, which is printed after each run.
68
+ # --seed 1234
69
+ config.order = :random
70
+
71
+ # Seed global randomization in this process using the `--seed` CLI option.
72
+ # Setting this allows you to use `--seed` to deterministically reproduce
73
+ # test failures related to randomization by passing the same `--seed` value
74
+ # as the one that triggered the failure.
75
+ Kernel.srand config.seed
76
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inexact_equality_warning
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Prescott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Warn when comparing floats for equality.
28
+ email:
29
+ - adam@aprescott.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".travis.yml"
36
+ - Gemfile
37
+ - LICENSE
38
+ - README.md
39
+ - inexact_equality_warning.gemspec
40
+ - lib/inexact_equality_warning.rb
41
+ - spec/inexact_equality_warning_spec.rb
42
+ - spec/spec_helper.rb
43
+ homepage: https://github.com/aprescott/ruby-inexact_equality_warning
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.0.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.2.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: You do not want to compare floating point numbers for equality.
67
+ test_files:
68
+ - spec/inexact_equality_warning_spec.rb
69
+ - spec/spec_helper.rb