<commit_before>require 'spec_helper'
require 'crichton/representor/serializer'

module Crichton
  module Representor
    describe Serializer do
      def clear_serializers
        Serializer.instance_variable_set('@registered_serializers', {})
      end
      
      def clear_media_type_serializer(serializer = nil)
        Crichton::Representor.send(:remove_const, serializer || :MediaTypeSerializer)
        clear_serializers
      end
      
      def create_media_type_serializer(serializer = nil)
        eval("class #{serializer || 'MediaTypeSerializer'} < Crichton::Representor::Serializer; end")
      end
      
      def wrap(example)
        create_media_type_serializer
        example.run
        clear_media_type_serializer
      end
      
      let(:object) do
        Class.new { include Representor }.new
      end
      
      context 'when subclassed' do
        context 'with serializer subclasses with well-formed names' do
          around do |example|
            wrap(example)
          end

          it 'auto registers sublclassed serializers' do
            Serializer.registered_serializers[:media_type].should == MediaTypeSerializer
          end
        end
        
        context 'with alternate media types defined for the serializer' do
          it 'auto registers alternate media types' do
            eval("class MediaTypeSerializer < Crichton::Representor::Serializer; alternate_media_types " <<
              ":alt_media_type, 'other_alt_media_type'; end")
            Serializer.registered_serializers[:alt_media_type].should == MediaTypeSerializer
            Serializer.registered_serializers[:other_alt_media_type].should == MediaTypeSerializer
          end
        end
  
        context 'with serializer subclasses with mal-formed names' do
          after do
            clear_media_type_serializer(@serializer)
          end
          
          it 'raises an error when the name does not end in Serializer' do
            @serializer = :MediaTypeSerializers
            expect { create_media_type_serializer(@serializer) }.to raise_error(Crichton::Representor::Error,
              /Subclasses .* must follow the naming convention OptionalModule::MediaTypeSerializer.*/)
          end
        end
      end

      describe '.build' do
        context 'with existing subclasses' do
          around do |example|
            wrap(example)
          end
          
          it 'builds serializer instances associated with a media type' do
            Serializer.build(:media_type, object).should be_instance_of(MediaTypeSerializer)
          end

          it 'raises an error if object is not a Crichton::Representor' do
            expect { Serializer.build(:media_type, mock('object')) }.to raise_error(ArgumentError,
              /^The object .* is not a Crichton::Representor.$/)
          end
        end
        
        it 'raises an error if the type does not have a registered serializer' do
          expect { Serializer.build(:some_media_type, object) }.to raise_error(Crichton::Representor::Error,
            /^No representor serializer is registered that corresponds to the type 'some_media_type'.$/)
        end
      end
      
      describe '.registered_serializers' do
        around do |example|
          clear_serializers
          example.run
          clear_serializers
        end
        
        it 'returns an empty hash if no serializers are registered' do
          Serializer.registered_serializers.should == {}
        end
        
        context 'with existing subclasses with well-formed names' do
          around do |example|
            wrap(example)
          end
          
          it 'returns a hash of registered serializer classes' do
            Serializer.registered_serializers[:media_type].should == MediaTypeSerializer
          end
        end
      end
    end
  end
end<commit_msg>Fix order dependency in spec.<commit_after>require 'spec_helper'
require 'crichton/representor/serializer'

module Crichton
  module Representor
    describe Serializer do
      def create_media_type_serializer(serializer = nil)
        serializer ||= :MediaTypeSerializer
        Crichton::Representor.send(:remove_const, serializer) if Representor.const_defined?(serializer)
        clear_serializers
        
        eval("class #{serializer} < Crichton::Representor::Serializer; end")
      end

      def clear_serializers
        Serializer.instance_variable_set('@registered_serializers', {})
      end

      let(:object) do
        Class.new { include Representor }.new
      end

      context 'when subclassed' do
        context 'with serializer subclasses with well-formed names' do
          it 'auto registers sublclassed serializers' do
            create_media_type_serializer
            Serializer.registered_serializers[:media_type].should == MediaTypeSerializer
          end
        end

        context 'with alternate media types defined for the serializer' do
          before do
            eval("class MediaTypeSerializer < Crichton::Representor::Serializer; alternate_media_types " <<
              ":alt_media_type, 'other_alt_media_type'; end")
          end
          
          it 'auto registers alternate media types as symbols' do
            Serializer.registered_serializers[:alt_media_type].should == MediaTypeSerializer
          end

          it 'auto registers alternate media types as strings' do
            Serializer.registered_serializers[:other_alt_media_type].should == MediaTypeSerializer
          end
        end

        context 'with serializer subclasses with mal-formed names' do
          it 'raises an error when the name does not end in Serializer' do
            @serializer = :MediaTypeSerializers
            expect { create_media_type_serializer(@serializer) }.to raise_error(Crichton::Representor::Error,
              /Subclasses .* must follow the naming convention OptionalModule::MediaTypeSerializer.*/)
          end
        end
      end

      describe '.build' do
        context 'with existing subclasses' do
          before do
            create_media_type_serializer
          end

          it 'builds serializer instances associated with a media type' do
            Serializer.build(:media_type, object).should be_instance_of(MediaTypeSerializer)
          end

          it 'raises an error if object is not a Crichton::Representor' do
            expect { Serializer.build(:media_type, mock('object')) }.to raise_error(ArgumentError,
              /^The object .* is not a Crichton::Representor.$/)
          end
        end

        it 'raises an error if the type does not have a registered serializer' do
          expect { Serializer.build(:some_media_type, object) }.to raise_error(Crichton::Representor::Error,
            /^No representor serializer is registered that corresponds to the type 'some_media_type'.$/)
        end
      end

      describe '.registered_serializers' do
        context 'without any registered serializers' do
          it 'returns an empty hash if no serializers are registered' do
            clear_serializers
            Serializer.registered_serializers.should == {}
          end
        end

        context 'with existing subclasses with well-formed names' do
          it 'returns a hash of registered serializer classes' do
            create_media_type_serializer
            Serializer.registered_serializers[:media_type].should == MediaTypeSerializer
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0638382763665381, 'fraction_numerical': 0.0, 'mean_word_length': 2.3171592412880457, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3514048', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1837, 'n_words': 556}