Sha256: 5538cba201f73604dffc10e9f1f31ac36ac61df8d8f17723114581d73c3a56f2

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module FixtureBackground
  class Background
    attr_reader :background_block
    
    def initialize(full_class_name, test_unit_class, parent, blk)
      @test_unit_class = test_unit_class
      @full_class_name = full_class_name
      @parent = parent
      @background_block = blk
      
      Generator.new(@full_class_name, background_signature, fixture_path, ancestors_and_own_background_blocks) unless background_valid?
    end
    
    def ancestors_and_own_background_blocks
      (@parent ? @parent.ancestors_and_own_background_blocks : []) << @background_block
    end

    def background_valid?
      (IO.read("#{fixture_path}/.version") rescue nil) == background_signature
      false
    end

    def background_signature
      stack = caller.reject { |line| line =~ Regexp.new(__FILE__) }
      test_file_path = File.expand_path(stack.first.match(/^(.+\.rb):/)[1])
      block_syntax = ''
      IO.read(test_file_path).scan(/(?:\A|\n)([ \t]*)background\s(?:do|{)(.*?)\n\1end/m) do |match|
        block_syntax << match[1].gsub(/\s+/, '')
      end
      Digest::MD5.hexdigest(block_syntax)
    end

    def fixture_path 
      Rails.root.to_s + "/test/backgrounds/#{@full_class_name.underscore}/"
    end
    
    def class_for_test
      klass = Kernel.const_set(@full_class_name, Class.new(@test_unit_class))
      klass.fixture_path = fixture_path
      klass.fixtures :all
      klass 
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fixture_background-0.9.0.1 lib/fixture_background/background.rb
fixture_background-0.9 lib/fixture_background/background.rb