Sha256: cd9821c79934e2775473b34c5f57512cfc56198c28232b30a79d3295fd4df31b

Contents?: true

Size: 1.72 KB

Versions: 15

Compression:

Stored size: 1.72 KB

Contents

require 'spec_helper'

include Restspec::Schema::Types

describe ArrayType do
  let(:type) { ArrayType.new }
  let(:attribute) { double }

  describe '#example_for' do
    it 'returns an empty array' do
      expect(type.example_for(attribute)).to eq([])
    end

    context 'with the example option: length' do
      let(:type) { ArrayType.new(example_options: { length: 3 }) }

      it 'raises an error asking for a parameterized type' do
        expect { type.example_for(attribute) }.to raise_error
      end

      context 'with a parameterized type' do
        let(:type) { ArrayType.new(example_options: { length: 3 }).of(StringType.new) }
        let(:parameterized_type) { StringType.new }

        it 'returns an array of that length with elements of that type' do
          array = type.example_for(attribute)
          expect(array.size).to eq(3)

          array.each do |item|
            expect(parameterized_type.valid?(attribute, item)).to eq(true)
          end
        end
      end
    end
  end

  describe '#valid?' do
    it 'validates that the value is an array' do
      expect(type.valid?(attribute, [1, 2, 3])).to eq(true)
      expect(type.valid?(attribute, {})).to eq(false)
      expect(type.valid?(attribute, nil)).to eq(false)
      expect(type.valid?(attribute, "string")).to eq(false)
    end

    context 'with a parameterized type' do
      let(:type) { ArrayType.new.of(StringType.new) }

      it 'validates arrays of the parameterized type' do
        expect(type.valid?(attribute, [1, 2, 3])).to eq(false)
        expect(type.valid?(attribute, [])).to eq(true)
        expect(type.valid?(attribute, ['hola', 'mundo'])).to eq(true)
        expect(type.valid?(attribute, ['hola', 1])).to eq(false)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
restspec-0.3.2 spec/restspec/schema/types/array_type_spec.rb
restspec-0.3.1 spec/restspec/schema/types/array_type_spec.rb
restspec-0.3.0 spec/restspec/schema/types/array_type_spec.rb
restspec-0.2.6 spec/restspec/schema/types/array_type_spec.rb
restspec-0.2.5 spec/restspec/schema/types/array_type_spec.rb
restspec-0.2.4 spec/restspec/schema/types/array_type_spec.rb
restspec-0.2.3 spec/restspec/schema/types/array_type_spec.rb
restspec-0.2.2 spec/restspec/schema/types/array_type_spec.rb
restspec-0.2.1 spec/restspec/schema/types/array_type_spec.rb
restspec-0.2 spec/restspec/schema/types/array_type_spec.rb
restspec-0.1 spec/restspec/schema/types/array_type_spec.rb
restspec-0.0.4 spec/restspec/schema/types/array_type_spec.rb
restspec-0.0.3 spec/restspec/schema/types/array_type_spec.rb
restspec-0.0.2 spec/restspec/schema/types/array_type_spec.rb
restspec-0.0.1 spec/restspec/schema/types/array_type_spec.rb