Sha256: e533678b4cb25c04cef580f0c42255e660ce5876a70fe1f674d963b8d8f059f2

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

# This is the mechanism for sharing state between Cucumber steps.
# If you're using instance variables, YOU'RE DOING IT WRONG.
module Kookaburra
  class TestData
    def initialize
      @data = Hash.new do |hash, key|
        hash[key] = Hash.new { |hash, key| hash[key] = HashWithIndifferentAccess.new }
      end
    end

    def __collection(collection_key)
      @data[collection_key]
    end
    def __fetch_data(collection_key, value_key)
      __collection(collection_key).fetch(value_key)
    rescue IndexError => e
      raise e.exception("Key #{value_key.inspect} not found in #{collection_key}")
    end
    def __get_data(collection_key, value_key)
      __collection(collection_key)[value_key]
    end
    def __set_data(collection_key, value_key, value_hash = {})
      __collection(collection_key)[value_key] = HashWithIndifferentAccess.new(value_hash)
    end

    def self.provide_collection(name)
      class_eval <<-RUBY
        def #{name}(key = :default)
          __get_data(:#{name}, key)
        end
        def fetch_#{name}(key = :default)
          __fetch_data(:#{name}, key)
        end
        def set_#{name}(key, value_hash = {})
          __set_data(:#{name}, key, value_hash)
        end
      RUBY
    end

    Defaults = HashWithIndifferentAccess.new
    def default(key)
      # NOTE: Marshal seems clunky, but gives us a deep copy.
      # This keeps mutations from being preserved between test runs.
      ( @default ||= Marshal::load(Marshal.dump(Defaults)) )[key]
    end

    def factory
      @factory ||= Kookaburra::TestData::Factory.new(self)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kookaburra-0.0.4 lib/kookaburra/test_data.rb
kookaburra-0.0.3 lib/kookaburra/test_data.rb
kookaburra-0.0.2 lib/kookaburra/test_data.rb