Sha256: 7a71d8f69e41aff7208d734c3b8da04cd9548ba10febb8a9d26a951ea095c6e0

Contents?: true

Size: 1.57 KB

Versions: 17

Compression:

Stored size: 1.57 KB

Contents

require File.join(File.dirname(__FILE__), '../spec_helper.rb')


describe Attributor::Container do

  context '.decode_json' do

    let(:mock_type) do
      Class.new do
        include Attributor::Container
        def self.native_type
          ::Hash   
        end
      end
    end       
    context 'for valid JSON strings' do
      it "parses JSON string into the native type" do
        a_hash = {"a" => 1, "b" => 2}
        json_hash =  JSON.dump( a_hash )
        mock_type.decode_json(json_hash).should == a_hash
      end
      it 'complains when trying to decode a non-String value' do
        expect{ 
          mock_type.decode_json(Object.new)
        }.to raise_error(Attributor::DeserializationError, /Error deserializing a Object using JSON/)
      end
      
      it 'complains when the deserialized value is not of the native_type' do
        expect{ 
          mock_type.decode_json("[1,2,3]")
        }.to raise_error(Attributor::CoercionError, /Error coercing from Array/)
      end
      
      it 'complains if there is a error deserializing the string' do
        expect{ 
          mock_type.decode_json("{invalid_json}")
        }.to raise_error(Attributor::DeserializationError, /Error deserializing a String using JSON/)
      end
      
      it 'uses the passed context in the output error' do
        expect{ 
          mock_type.decode_json("{invalid_json}",["my_context","attribute_name"])
        }.to raise_error(Attributor::DeserializationError, /Error deserializing a String using JSON.* while loading my_context.attribute_name/)
      end

    end
  end
  
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
attributor-5.0.2 spec/types/container_spec.rb
attributor-5.0.1 spec/types/container_spec.rb
attributor-5.0 spec/types/container_spec.rb
attributor-4.2.0 spec/types/container_spec.rb
attributor-4.1.0 spec/types/container_spec.rb
attributor-4.0.1 spec/types/container_spec.rb
attributor-4.0.0 spec/types/container_spec.rb
attributor-3.0.1 spec/types/container_spec.rb
attributor-3.0 spec/types/container_spec.rb
attributor-2.6.1 spec/types/container_spec.rb
attributor-2.6.0 spec/types/container_spec.rb
attributor-2.5.0 spec/types/container_spec.rb
attributor-2.4.0 spec/types/container_spec.rb
attributor-2.3.0 spec/types/container_spec.rb
attributor-2.2.1 spec/types/container_spec.rb
attributor-2.2.0 spec/types/container_spec.rb
attributor-2.1.0 spec/types/container_spec.rb