Sha256: cbd76fa1217ee6990a6e7d4989a92421ff0a541ea5fd898a9bb2e61609b852da

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

require 'spec_helper'

describe OptParseValidator::OptIntegerRange do
  subject(:opt) { described_class.new(['--range RANGE'], attrs) }
  let(:attrs)   { {} }

  describe '#validate' do
    context 'when incorrect number of ranges given' do
      it 'raises an error' do
        expect { opt.validate('1-2-3') }.to raise_error('Incorrect number of ranges found: 3, should be 2')
      end
    end

    context 'when not an integer range' do
      it 'raises an error' do
        expect { opt.validate('a-e') }.to raise_error('Argument is not a valid integer range')
      end
    end

    context 'when a valid range' do
      it 'returns the range' do
        expect(opt.validate('1-5')).to eql((1..5))
      end

      context 'when another separator' do
        let(:attrs) { super().merge(separator: ':') }

        it 'returns the range' do
          expect(opt.validate('0:10')).to eql((0..10))
        end
      end
    end

    context 'when nil or "" supplied' do
      context 'when no value_if_empty attribute' do
        it 'raises an error' do
          [nil, ''].each do |value|
            expect { opt.validate(value) }.to raise_error 'Empty option value supplied'
          end
        end
      end

      context 'when value_if_empty attribute' do
        let(:attrs) { super().merge(value_if_empty: '0-2') }

        it 'returns the value_if_empty value' do
          [nil, ''].each do |value|
            expect(opt.validate(value)).to eql((0..2))
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opt_parse_validator-0.0.9 spec/lib/opt_parse_validator/opts/integer_range_spec.rb
opt_parse_validator-0.0.8 spec/lib/opt_parse_validator/opts/integer_range_spec.rb
opt_parse_validator-0.0.7 spec/lib/opt_parse_validator/opts/integer_range_spec.rb