Sha256: a42049b5e742c577aa0f087544ba2cecf624aabf27b93e4aee90ca5b62237054

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module RSpec::RubyContentMatchers
  class HaveSubclass < RSpec::RubyContentMatcher
    attr_reader :klass, :superclass, :type

    def initialize(klass, superclass, type=nil)
      @klass      = klass.to_s.camelize
      @superclass = superclass.to_s.camelize
      @type       = type.to_s.camelize if type      
    end

    def matches?(content) 
      super
      match_res = (content =~ /class\s+#{klass}#{type}\s+<\s+#{superclass}(.*)#{end_expr}/m)
      if block_given? && $1
        ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
        yield ruby_content 
      else
        match_res
      end
    end          

    def failure_message  
      super
      "Expected there to be the subclass #{klass} of #{superclass}"
    end 

    def negative_failure_message
      super
      "Did no expected there to be the subclass #{klass} of #{superclass}"
    end   

    protected

    def end_expr
      "(end$|# #{klass})"
    end
    
  end

  def have_subclass(klass, superclass, type=nil)
    HaveSubclass.new(klass, superclass, type)
  end    
  alias_method :be_subclass, :have_subclass
  
  def have_observer_class(klass)
    HaveSubclass.new(klass, 'ActiveRecord::Observer', :observer)
  end     
  alias_method :be_observer, :have_observer_class
  alias_method :be_observer_class, :have_observer_class

  
  def have_migration(klass)
    HaveSubclass.new(klass, 'ActiveRecord::Migration')
  end      
  alias_method :be_migration, :have_migration  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
generator-spec-0.4.5 lib/generator_spec/matchers/content/have_subclass.rb
generator-spec-0.4.4 lib/generator_spec/matchers/content/have_subclass.rb