Permalink
Browse files

print 0 if 0

  • Loading branch information...
1 parent 3496857 commit 8c1fc7615776ab87e2b7e29ec2f4b92ef9d166c9 @dchelimsky dchelimsky committed May 20, 2010
Showing with 22 additions and 14 deletions.
  1. +4 −3 lib/rspec/core/formatters/helpers.rb
  2. +2 −2 rspec-core.gemspec
  3. +16 −9 spec/rspec/core/formatters/helpers_spec.rb
@@ -8,14 +8,15 @@ module Helpers
SUB_SECOND_PRECISION = 5
DEFAULT_PRECISION = 2
- def format_seconds(float, precision = nil)
+ def format_seconds(float)
precision ||= (float < 1) ? SUB_SECOND_PRECISION : DEFAULT_PRECISION
formatted = sprintf("%.#{precision}f", float)
strip_trailing_zeroes(formatted)
end
def strip_trailing_zeroes(string)
- string.sub(/[^1-9]+$/, '')
+ stripped = string.sub(/[^1-9]+$/, '')
+ stripped.empty? ? "0" : stripped
end
end
@@ -24,4 +25,4 @@ def strip_trailing_zeroes(string)
end
-end
+end
View
@@ -184,7 +184,7 @@ Gem::Specification.new do |s|
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{rspec}
- s.rubygems_version = %q{1.3.6.1}
+ s.rubygems_version = %q{1.3.6}
s.summary = %q{rspec-core-2.0.0.beta.8}
s.test_files = [
"spec/autotest/failed_results_re_spec.rb",
@@ -223,7 +223,7 @@ Gem::Specification.new do |s|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec-expectations>, [">= 2.0.0.beta.8"])
s.add_development_dependency(%q<rspec-mocks>, [">= 2.0.0.beta.8"])
s.add_development_dependency(%q<cucumber>, [">= 0.5.3"])
@@ -5,19 +5,25 @@
let(:helper) { helper = Object.new.extend(RSpec::Core::Formatters::Helpers) }
describe "format seconds" do
- it "uses passed in precision if specified" do
- pending("get regex right to handle this case where we don't want it to consume all zeroes") do
- helper.format_seconds(0.00005, 2).should == "0.00"
- end
- end
-
context "sub second times" do
it "returns 5 digits of precision" do
- helper.format_seconds(0.000005).should == "0.00001"
+ helper.format_seconds(0.000006).should == "0.00001"
+ end
+
+ it "strips off trailing zeroes beyond sub-second precision" do
+ helper.format_seconds(0.020000).should == "0.02"
end
- it "strips off trailing zeroes" do
- helper.format_seconds(0.02000).should == "0.02"
+ context "0" do
+ it "strips off trailing zeroes" do
+ helper.format_seconds(0.00000000001).should == "0"
+ end
+ end
+
+ context "> 1" do
+ it "strips off trailing zeroes" do
+ helper.format_seconds(1.00000000001).should == "1"
+ end
end
end
@@ -32,6 +38,7 @@
helper.format_seconds(5).should == "5"
helper.format_seconds(5.0).should == "5"
end
+
end
end

0 comments on commit 8c1fc76

Please sign in to comment.