Sha256: 6a737adbd44d82355f63722d8ec982c3a4b6fc55766f37af8a665c85a8f0b96c

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

require 'spec_helper'

describe Numeric do
  describe '#percent_of' do
    context 'when number is float' do
      let(:number) { 120.0 }

      it 'converts to percentage of number' do
        expect(number.percent_of(240)).to eq(50)
      end

      it 'converts to percentage of number' do
        expect(number.percent_of(60)).to eq(200)
      end

      it 'do not raise an error when divisor is 0' do
        expect(100.0.percent_of(0)).to eq(Float::INFINITY)
      end

      it 'do not raise an error when divisor is 0.0' do
        expect(100.0.percent_of(0.0)).to eq(Float::INFINITY)
      end
    end

    context 'when number is integer' do
      it 'converts to percentage of number' do
        expect(500.percent_of(50)).to eq(1000)
      end

      it 'converts to percentage of number' do
        expect(0.percent_of(50)).to eq(0)
      end

      it 'converts to percentage of number' do
        expect(10.percent_of(100)).to eq(10)
      end

      it 'do not raise an error when divisor is 0' do
        expect(100.percent_of(0)).to eq(Float::INFINITY)
      end
    end

    context 'when a number is 0' do
      context 'when divident is 0' do
        it { expect(0.percent_of(100)).to eq(0) }
      end

      context 'when divisor is 0' do
        it { expect(100.percent_of(0)).to eq(Float::INFINITY) }
      end

      context 'both are 0' do
        it { expect(0.percent_of(0)).to eq(Float::INFINITY) }
      end
    end

    context 'when divisor is nil' do
      it { expect(100.percent_of(nil)).to eq(Float::INFINITY) }
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
darthjee-core_ext-1.5.6 spec/lib/numeric_spec.rb
darthjee-core_ext-1.5.5 spec/lib/numeric_spec.rb
darthjee-core_ext-1.5.4 spec/lib/numeric_spec.rb
darthjee-core_ext-1.5.3 spec/lib/numeric_spec.rb
darthjee-core_ext-1.5.2 spec/lib/numeric_spec.rb
darthjee-core_ext-1.5.1 spec/lib/numeric_spec.rb
darthjee-core_ext-1.5.0 spec/lib/numeric_spec.rb
darthjee-core_ext-1.4.1 spec/lib/numeric_spec.rb
darthjee-core_ext-1.4.0 spec/lib/numeric_spec.rb
darthjee-core_ext-1.3.1 spec/lib/numeric_spec.rb
darthjee-core_ext-1.3.0 spec/lib/numeric_spec.rb
darthjee-core_ext-1.2.6 spec/lib/numeric_spec.rb
bidu-core_ext-1.2.5 spec/lib/numeric_spec.rb
bidu-core_ext-1.2.4 spec/lib/numeric_spec.rb
bidu-core_ext-1.2.3 spec/lib/numeric_spec.rb
bidu-core_ext-1.2.2 spec/lib/numeric_spec.rb
bidu-core_ext-1.2.1 spec/lib/numeric_spec.rb
bidu-core_ext-1.2.0 spec/lib/numeric_spec.rb
bidu-core_ext-1.1.1 spec/lib/numeric_spec.rb
bidu-core_ext-1.0.0 spec/lib/numeric_spec.rb