Sha256: 14591ff8b7ff15e7cc38b40eb4be4985a1a0d0524bcecc14492bc3acbf31f860

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

require 'spec_helper'

describe Virtus::Coercion::String, '.to_integer' do
  subject { described_class.to_integer(string) }

  min_float = Float::MIN
  max_float = (Float::MAX / 10).to_s.to_f  # largest float that can be parsed

  {
    '1'            => 1,
    '+1'           => 1,
    '-1'           => -1,
    '1.0'          => 1,
    '1.0e+1'       => 10,
    '1.0e-1'       => 0,
    '1.0E+1'       => 10,
    '1.0E-1'       => 0,
    '+1.0'         => 1,
    '+1.0e+1'      => 10,
    '+1.0e-1'      => 0,
    '+1.0E+1'      => 10,
    '+1.0E-1'      => 0,
    '-1.0'         => -1,
    '-1.0e+1'      => -10,
    '-1.0e-1'      => 0,
    '-1.0E+1'      => -10,
    '-1.0E-1'      => 0,
    '.1'           => 0,
    '.1e+1'        => 1,
    '.1e-1'        => 0,
    '.1E+1'        => 1,
    '.1E-1'        => 0,
    min_float.to_s => min_float.to_i,
    max_float.to_s => max_float.to_i,
  }.each do |value, expected|
    context "with #{value.inspect}" do
      let(:string) { value }

      it { should be_kind_of(Integer) }

      it { should eql(expected) }
    end
  end

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

    it { should equal(string) }
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
virtus-0.5.1 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
virtus-0.5.0 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
virtus-0.4.2 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
virtus-0.4.1 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
virtus-0.4.0 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
virtus-0.3.0 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
virtus-0.2.0 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb
virtus-0.1.0 spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb