Sha256: 7c72e6b9cc49c8fc6752d8fff21995a30ec6c5b07430a66df4eabb7332a95521

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

require 'spec_helper'

class Tester
  using EnsureIt if ENSURE_IT_REFINED

  def ensure_string(*args)
    obj.ensure_string(*args)
  end

  def ensure_string!(*args)
    obj.ensure_string!(*args)
  end
end

describe EnsureIt do
  shared_examples 'stringifier' do
    it 'and returns self for string' do
      obj = 'test'
      expect(call_for(obj)).to eq obj
    end

    it 'and converts symbols to string' do
      expect(call_for(:test)).to eq 'test'
    end

    it 'and converts numbers to string with :numbers option' do
      expect(call_for(100, numbers: true)).to eq '100'
      expect(call_for(0.5, numbers: true)).to eq '0.5'
      expect(call_for(Rational(2, 3), numbers: true)).to eq '2/3'
    end

    it 'and downcases value with downcase option' do
      expect(call_for(:teST, downcase: true)).to eq 'test'
      expect(call_for('teST', downcase: true)).to eq 'test'
    end

    it 'and calls ensure_name with name_of option' do
      expect(EnsureIt::StringUtils).to receive(:ensure_name).with(
        'test', name_of: :setter, downcase: nil
      ).and_call_original
      expect(call_for('test', name_of: :setter)).to eq 'test='
    end
  end

  describe '#ensure_string' do
    it_behaves_like 'stringifier'
    it_behaves_like 'niller for unmet objects', except: [String, Symbol]
    it_behaves_like 'values checker', :one, :test, values: %w(one two)
    it_behaves_like 'values checker', 'one', 'test', values: %w(one two)
  end

  describe '#ensure_string!' do
    it_behaves_like 'stringifier'
    it_behaves_like(
      'banger for unmet objects',
      except: [String, Symbol],
      message: /should be a String or a Symbol/
    )

    it 'raises correct error message with :numbers option' do
      expect { call_for(nil, numbers: true) }.to raise_error(
        EnsureIt::Error,
        /should be a String or a Symbol or a Numeric/
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ensure_it-1.0.0 spec/lib/ensure_string_spec.rb
ensure_it-0.1.5 spec/lib/ensure_string_spec.rb
ensure_it-0.1.4 spec/lib/ensure_string_spec.rb