Sha256: e71adf05d4e64522cfafef2210ad2edf44e8ee151a210e6141d044d1fad4e82b

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require 'spec_helper'

class Tester
  using EnsureIt if ENSURE_IT_REFINED

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

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

describe EnsureIt do
  shared_examples 'symbolizer' do
    it 'and returns self for symbol' do
      expect(call_for(:test)).to eq :test
    end

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

    it 'and returns symbol if it in values' do
      expect(call_for(:test, values: %i(test me))).to eq :test
      expect(call_for('me', values: %i(test me))).to eq :me
    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_symbol' do
    it_behaves_like 'symbolizer'
    it_behaves_like 'niller for unmet objects', except: [String, Symbol]
    it_behaves_like 'values checker', :one, :test, values: %i(one two)
    it_behaves_like 'values checker', 'one', 'test', values: %i(one two)

    it 'returns nil if value not in values option' do
      expect(call_for(:val, values: %i(test me))).to be_nil
    end
  end

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

    it 'raises error if value not in values option' do
      expect {
        call_for(:val, values: %i(test me))
      }.to raise_error EnsureIt::Error
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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