summaryrefslogtreecommitdiffstats
path: root/test/lib/spec/matchers/be_close.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/lib/spec/matchers/be_close.rb')
-rw-r--r--test/lib/spec/matchers/be_close.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/test/lib/spec/matchers/be_close.rb b/test/lib/spec/matchers/be_close.rb
new file mode 100644
index 000000000..b09e3fd2f
--- /dev/null
+++ b/test/lib/spec/matchers/be_close.rb
@@ -0,0 +1,37 @@
+module Spec
+ module Matchers
+
+ class BeClose #:nodoc:
+ def initialize(expected, delta)
+ @expected = expected
+ @delta = delta
+ end
+
+ def matches?(actual)
+ @actual = actual
+ (@actual - @expected).abs < @delta
+ end
+
+ def failure_message
+ "expected #{@expected} +/- (<#{@delta}), got #{@actual}"
+ end
+
+ def description
+ "be close to #{@expected} (+- #{@delta})"
+ end
+ end
+
+ # :call-seq:
+ # should be_close(expected, delta)
+ # should_not be_close(expected, delta)
+ #
+ # Passes if actual == expected +/- delta
+ #
+ # == Example
+ #
+ # result.should be_close(3.0, 0.5)
+ def be_close(expected, delta)
+ Matchers::BeClose.new(expected, delta)
+ end
+ end
+end \ No newline at end of file