require 'spec_helper'

describe AmountHelper do

  describe "#amount" do

    context "with maximum_precision" do

      context "set to 0" do

        it "should return the number with no places after the comma" do
          helper.amount(12345.6789, :maximum_precision => 0).should == "12.345"
        end

      end

      context "set to 2" do

        it "should return the number with no places after the comma" do
          helper.amount(12345.6789, :maximum_precision => 2).should == "12.345,67"
        end

      end

    end

    context "without maximum precision" do

      it "should return the number with full precision" do
        helper.amount(12345.6789).should == "12.345,6789"
      end

    end

    context "with a minimum precision" do

      it "should fill up missing places after the comma with 0's" do
        helper.amount(12345, :minimum_precision => 2).should == "12.345,00"
      end

    end

    context "delimiters for digit triplets" do

      it 'should add a delimiter every three digits' do
        helper.amount(1234567).should == '1.234.567'
      end

      it 'should not add delimiters to fractional amounts' do
        helper.amount(1.234567).should == '1,234567'
      end

      it "should not add delimiters to amounts less than 4 digits in length" do
        helper.amount(123).should == "123"
      end

      it 'should not add delimiters to negative amounts less than 4 digits in length' do
        helper.amount(-123).should == "-123"
      end

      it 'allows custom delimiters' do
        helper.amount(1234567, delimiter: '_').should == '1_234_567'
      end

      it 'it uses no delimiter when receiving a nil delimiter' do
        helper.amount(1234567, delimiter: nil).should == '1234567'
      end

      it 'it uses no delimiter when receiving an empty-string delimiter' do
        helper.amount(1234567, delimiter: '').should == '1234567'
      end

    end

    context 'with rounding' do
      it 'should return the integer value rounded down' do
        helper.amount(1234567.123456, :round => true).should == '1.234.567'
      end

      it 'should return the integer value rounded up' do
        helper.amount(1234567.54321, :round => true).should == '1.234.568'
      end
    end

  end

end

