Sha256: 632f769a28f9e4c0908d4c8c386ff7d48cfb8071c03e135ec793c16ddb7bdcfe

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

RSpec.describe Necromancer::NumericConverters::StringToIntegerConverter, '.call' do

  subject(:converter) { described_class.new(:string, :integer) }

  {
    '1'       => 1,
    '+1'      => 1,
    '-1'      => -1,
    '1e+1'    => 1,
    '+1e-1'   => 1,
    '-1e1'    => -1,
    '-1e-1'   => -1,
    '1.0'     => 1,
    '1.0e+1'  => 1,
    '1.0e-1'  => 1,
    '-1.0e+1' => -1,
    '-1.0e-1' => -1,
     '.1'     => 0,
    '.1e+1'   => 0,
    '.1e-1'   => 0,
     '-.1e+1' => 0,
    '-.1e-1'  => 0
  }.each do |actual, expected|
    it "converts '#{actual}' to float value" do
      expect(converter.call(actual)).to eql(expected)
    end
  end

  it "raises error for empty string in strict mode" do
    expect {
      converter.call('', strict: true)
    }.to raise_error(Necromancer::ConversionTypeError)
  end

  it "converts empty string to 0 in non-strict mode" do
    expect(converter.call('', strict: false)).to eq(0)
  end

  it "raises error for float in strict mode" do
    expect {
      converter.call('1.2', strict: true)
    }.to raise_error(Necromancer::ConversionTypeError)
  end

  it "converts float to integer in non-strict mode" do
    expect(converter.call(1.2)).to eq(1)
  end

  it "converts mixed string to integer in non-strict mode" do
    expect(converter.call('1abc')).to eq(1)
  end

  it "raises error for mixed string in strict mode" do
    expect {
      converter.call('1abc', strict: true)
    }.to raise_error(Necromancer::ConversionTypeError)
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
pokedex-terminal-0.2.8 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/converters/numeric/string_to_integer_spec.rb
pokedex-terminal-0.2.7 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/converters/numeric/string_to_integer_spec.rb
pokedex-terminal-0.2.6 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/converters/numeric/string_to_integer_spec.rb
pokedex-terminal-0.2.5 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/converters/numeric/string_to_integer_spec.rb
pokedex-terminal-0.2.4 vendor/bundle/ruby/2.7.0/gems/necromancer-0.5.1/spec/unit/converters/numeric/string_to_integer_spec.rb
necromancer-0.5.1 spec/unit/converters/numeric/string_to_integer_spec.rb
necromancer-0.5.0 spec/unit/converters/numeric/string_to_integer_spec.rb