Skip to content

Commit

Permalink
Simplified fuzzy number implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Jan 14, 2013
1 parent e6c872d commit b33c899
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 77 deletions.
2 changes: 2 additions & 0 deletions examples/failing/natural_failing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
(puts "Ha ha world", ! true)
}

Then { Math.sqrt(10) == about(3.1623).percent(0.0001) }

describe "Error Examples" do
When(:result) { fail "OUCH" }
Then { result == :ok }
Expand Down
51 changes: 11 additions & 40 deletions lib/rspec/given/fuzzy_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ class FuzzyNumber # :nodoc:

DEFAULT_EPSILON = 10 * Float::EPSILON

def initialize(number, options=nil)
def initialize(number)
@number = number
case options
when Numeric
@delta = options
when Hash
@delta = delta_from_options(options, number)
end
@delta ||= (number * DEFAULT_EPSILON)
@delta = number * DEFAULT_EPSILON
end

def ==(other)
Expand All @@ -25,42 +19,19 @@ def to_s
"<Approximately #{@number} +/- #{@delta}>"
end

private

OPTIONS = {
epsilon: ->(neps, number) { number * (neps * Float::EPSILON) },
percent: ->(percent, number) { number * (percent / 100.0) },
delta: ->(delta, number) { delta },
}

def delta_from_options(options, number)
validate_hash_options(options)
key = options.keys.first
OPTIONS[key].(options[key], number)
def delta(delta)
@delta = delta
self
end

def validate_hash_options(options)
validate_only_one_option(options)
validate_known_options(options)
def percent(percentage)
@delta = @number = (percentage / 100.0)
self
end

def validate_only_one_option(options)
if options.size < 1
fail ArgumentError, "No options given"
end
if options.size > 1
fail ArgumentError, "Too many options: '#{options.keys.join(', ')}'"
end
end

VALID_KEYS = OPTIONS.keys

def validate_known_options(options)
options.keys.each do |k|
if ! VALID_KEYS.include?(k)
fail ArgumentError, "Invalid option: '#{k}'"
end
end
def epsilon(neps)
@delta = @number * (neps * Float::EPSILON)
self
end
end

Expand Down
41 changes: 4 additions & 37 deletions spec/lib/rspec/given/fuzzy_number_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,9 @@


describe "fixed deltas" do
context "when created with non-hash delta" do
Given(:delta) { 0.0001 }
Given(:number) { about(10, delta) }

Then { 10 == number }
Then { number == 10 }

Then { (10 + 0.0001) == number }
Then { (10 - 0.0001) == number }

Then { (10 + 0.000100001) != number }
Then { (10 - 0.000100001) != number }
end

context "when created with explicit delta" do
Given(:exact_number) { 10 }
Given(:number) { about(exact_number, delta: 0.001) }
Given(:number) { about(exact_number).delta(0.001) }

Then { exact_number == number }

Expand All @@ -36,7 +22,7 @@

describe "percentage deltas" do
Given(:exact_number) { 1 }
Given(:number) { about(exact_number, percent: 25) }
Given(:number) { about(exact_number).percent(25) }

Then { exact_number == number }

Expand Down Expand Up @@ -82,32 +68,13 @@
context "when created with small epsilon" do
Given(:neps) { 100 }
Given(:exact_number) { 10 }
Given(:number) { about(exact_number, epsilon: neps) }
Given(:number) { about(exact_number).epsilon(neps) }
Then { exact_number == number }
end
end

describe "#to_s" do
Given(:number) { about(10, delta: 0.0001) }
Given(:number) { about(10).delta(0.0001) }
Then { number.to_s == "<Approximately 10 +/- 0.0001>" }
end

describe "invalid options" do
context "with an illegal option" do
When(:result) { about(10, junk: 1) }
Then { result == have_failed(ArgumentError, /invalid.*junk/i) }
end

context "with too many options" do
When(:result) { about(10, epsilon: 1, delta: 10) }
Then { result == have_failed(ArgumentError, /too many/i) }
And { result == have_failed(ArgumentError, /epsilon/i) }
And { result == have_failed(ArgumentError, /delta/i) }
end

context "with no options" do
When(:result) { about(10, {}) }
Then { result == have_failed(ArgumentError, /no options/i) }
end
end
end

0 comments on commit b33c899

Please sign in to comment.