Sha256: 8a16017cdbaea86f5db230feb6e371744a096cfaecad7d4fb31aa6bd5b71064b

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

require 'spec_helper'

module XML
  module MappingExtensions

    class MimeTypeSpecElem
      include XML::Mapping
      mime_type_node :mime_type, '@mime_type', default_value: nil

      def self.from_str(mt_string)
        xml_string = mt_string ? "<elem mime_type='#{mt_string}'/>" : '<elem/>'
        doc = REXML::Document.new(xml_string)
        load_from_xml(doc.root)
      end
    end

    describe MimeTypeNode do
      def to_mime_type(str)
        MimeTypeSpecElem.from_str(str).mime_type
      end

      def to_text(mime_type)
        elem = MimeTypeSpecElem.new
        elem.mime_type = mime_type
        xml = elem.save_to_xml
        xml.attributes['mime_type']
      end

      it 'accepts a standard MIME type' do
        mt_str = 'text/plain'
        mt = MIME::Types[mt_str].first
        expect(to_mime_type(mt_str)).to eq(mt)
      end

      it 'accepts a non-standard MIME type' do
        mt_str = 'elvis/presley'
        mt = MIME::Type.new(mt_str)
        expect(to_mime_type(mt_str)).to eq(mt)
      end

      it 'doesn\'t set an attribute for a nil value' do
        expect(to_text(nil)).to be_nil
      end

      it 'fails if mime_type isn\'t a MIME type' do
        mt_str = 'I am not a mime type'
        expect { to_mime_type(mt_str) }.to raise_error(MIME::Type::InvalidContentType)
      end

      it 'parses a nil mime-type as nil' do
        elem = MimeTypeSpecElem.parse_xml('<elem/>')
        expect(elem.mime_type).to be_nil
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
xml-mapping_extensions-0.4.9 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
xml-mapping_extensions-0.4.8 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
xml-mapping_extensions-0.4.7 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
xml-mapping_extensions-0.4.6 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
xml-mapping_extensions-0.4.5 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
xml-mapping_extensions-0.4.4 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
xml-mapping_extensions-0.4.3 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
xml-mapping_extensions-0.4.2 spec/unit/xml/mapping_extensions/mime_type_node_spec.rb