Sha256: 1cbb67429d3e75ffd50fee12e0e6353a530fd7ab4869b1049e95c3e9ae33c742

Contents?: true

Size: 1.22 KB

Versions: 10

Compression:

Stored size: 1.22 KB

Contents

require 'spec_helper'

describe Coercer::String, '.to_float' do
  subject { described_class.new.to_float(string) }

  {
    '1'       => 1.0,
    '+1'      => 1.0,
    '-1'      => -1.0,
    '1.0'     => 1.0,
    '1.0e+1'  => 10.0,
    '1.0e-1'  => 0.1,
    '1.0E+1'  => 10.0,
    '1.0E-1'  => 0.1,
    '+1.0'    => 1.0,
    '+1.0e+1' => 10.0,
    '+1.0e-1' => 0.1,
    '+1.0E+1' => 10.0,
    '+1.0E-1' => 0.1,
    '-1.0'    => -1.0,
    '-1.0e+1' => -10.0,
    '-1.0e-1' => -0.1,
    '-1.0E+1' => -10.0,
    '-1.0E-1' => -0.1,
    '.1'      => 0.1,
    '.1e+1'   => 1.0,
    '.1e-1'   => 0.01,
    '.1E+1'   => 1.0,
    '.1E-1'   => 0.01,
    '1e1'     => 10.0,
    '1E+1'    => 10.0,
    '+1e-1'   => 0.1,
    '-1E1'    => -10.0,
    '-1e-1'   => -0.1,
  }.each do |value, expected|
    context "with #{value.inspect}" do
      let(:string) { value }

      it { should be_instance_of(Float) }

      it { should eql(expected) }
    end
  end

  context 'with an invalid float string' do
    let(:string) { 'non-float' }

    specify { expect { subject }.to raise_error(UnsupportedCoercion) }
  end

  context 'string starts with e' do
    let(:string) { 'e1' }

    specify { expect { subject }.to raise_error(UnsupportedCoercion) }
  end
end

Version data entries

10 entries across 8 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/spec/unit/coercible/coercer/string/to_float_spec.rb
coercible-1.0.0 spec/unit/coercible/coercer/string/to_float_spec.rb