Skip to content

Instantly share code, notes, and snippets.

@eadz
Created September 2, 2010 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eadz/561637 to your computer and use it in GitHub Desktop.
Save eadz/561637 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Price do
it "formats a price correctly" do
Price.new(1.00).to_s.should == '$1.00'
Price.new(3.4).to_s.should == '$3.40'
Price.new(3000).to_s.should == '$30.00'
Price.new(3001).to_s.should == '$30.01'
Price.new(300000).to_s.should == '$3,000.00'
Price.new(0.1).to_s.should == '$0.10'
Price.new(0.01).to_s.should == '$0.01'
Price.new(0.10).to_s.should == '$0.10'
Price.new(0.99).to_s.should == '$0.99'
Price.new(19.99).to_s.should == '$19.99'
end
it "stores cents accuratly" do
Price.new(1).amount.should == 1
Price.new(100).amount.should == 100
Price.new(10000).amount.should == 10000
Price.new(1.4).amount.should == 140
Price.new('$3').amount.should == 300
Price.new('$3.50').amount.should == 350
Price.new('$19.99').amount.should == 1999
Price.new('$300.50').amount.should == 30050
Price.new('$300.5').amount.should == 30050
Price.new('$300.0049').amount.should == 30000
Price.new('$300.0051').amount.should == 30001
end
it "is sane" do
Price.new('$3.50').to_s.should == '$3.50'
Price.new('$344.50').to_s.should == '$344.50'
Price.new('$19.99').to_s.should == '$19.99'
Price.new('$19.01').to_s.should == '$19.01'
Price.new('$3,444.50').to_s.should == '$3,444.50'
Price.new('$3,444.501').to_s.should == '$3,444.50'
Price.new('$3,444.509').to_s.should == '$3,444.51'
Price.new('$3,444.5').to_s.should == '$3,444.50'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment