Sha256: c6be45316a5a9ba09ca06d3203252492e31549043acb330b65b205aa357d1df3

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

require 'spec_helper'
require 'parameters/types/hash'
require 'parameters/types/symbol'
require 'parameters/types/integer'

describe Parameters::Types::Hash do
  let(:array) { [:a, 1, :b, 2]     }
  let(:hash)  { {:a => 1, :b => 2} }

  describe "coerce" do
    subject { described_class }

    it "should accept Arrays" do
      subject.coerce(array).should == hash
    end

    it "should call #to_hash" do
      subject.coerce(hash).should == hash
    end
  end

  context "instance" do
    context "with key-type" do
      let(:string_keys) { {'a' => 1, 'b' => 2} }

      subject { described_class.new(Parameters::Types::Symbol,Parameters::Types::Object) }

      it "should have a Ruby type" do
        subject.to_ruby.should == Hash[Symbol => Object]
      end

      describe "#===" do
        it "should check the type of each key" do
          subject.should_not === string_keys

          subject.should === hash
        end
      end

      describe "#coerce" do
        it "should coerce each key" do
          subject.coerce(string_keys).should == hash
        end
      end
    end

    context "with value-type" do
      let(:string_values) { {:a => '1', :b => '2'} }

      subject { described_class.new(Parameters::Types::Object,Parameters::Types::Integer) }

      it "should have a Ruby type" do
        subject.to_ruby.should == Hash[Object => Integer]
      end

      describe "#===" do
        it "should check the type of each value" do
          subject.should_not === string_values

          subject.should === hash
        end
      end

      describe "#coerce" do
        it "should coerce each value" do
          subject.coerce(string_values).should == hash
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
parameters-0.3.1 spec/types/hash_spec.rb
parameters-0.3.0 spec/types/hash_spec.rb