Sha256: 2bd1c48339a1ecd32dadf4af0515e2b4f978139647bfc13f1bed390c74563997

Contents?: true

Size: 1.88 KB

Versions: 22

Compression:

Stored size: 1.88 KB

Contents

begin
  require 'spec'
  require 'appengine-sdk'
rescue LoadError
  require 'rubygems'
  require 'spec'
  require 'appengine-sdk'
end

$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'appengine-apis/testing'
AppEngine::Testing.setup

class ProtoMatcher
  def compare(hash, proto, prefix='')
    hash.each do |key, value|
      name = "#{prefix}#{key}"
      case value
      when Array
        if value[0].kind_of? Hash
          count = proto.send("#{key}_size")
          compare_value("#{name}.size", value.size, count)
          value.each_with_index do |item, index|
            break if index == count
            compare(item, proto.send(key, index), "#{name}[#{index}].")
          end
        else
          actual = proto.send("#{key}s").to_a
          compare_value(name, value, actual)
        end
      when Hash
        compare(value, proto.send(key), "#{name}.")
      else
        compare_value(name, value, proto.send(key))
      end
    end
  end

  def compare_value(label, expected, actual)
    if expected != actual
      @failures << "%s differs. expected: %s actual: %s" %
          [label, expected.inspect, actual.inspect]
    end
  end

  def initialize(klass, expected)
    @klass = klass
    @expected = expected
  end

  def matches(bytes)
    @failures = []
    @proto = @klass.new
    @proto.parse_from(bytes)
    compare(@expected, @proto)
    @failures.empty?
  end

  def ==(bytes)
    Spec::Expectations.fail_with(failure_message) unless matches(bytes)
    true
  end

  def failure_message
    @failures.join("\n")
  end
end

module ProtoMethods
  def proto(klass, hash)
    ProtoMatcher.new(klass, hash)
  end
  alias be_proto proto

  def mock_delegate
    delegate = mock("apiproxy")
    delegate.instance_eval do
      class << self
        include AppEngine::ApiProxy::Delegate
      end
    end
  end
end

Spec::Runner.configure do |config|
  config.include(ProtoMethods)
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
appengine-apis-0.0.35 spec/spec_helper.rb
appengine-apis-0.0.34 spec/spec_helper.rb
appengine-apis-0.0.33 spec/spec_helper.rb
appengine-apis-0.0.32 spec/spec_helper.rb
appengine-apis-0.0.31 spec/spec_helper.rb
appengine-apis-0.0.30 spec/spec_helper.rb
appengine-apis-0.0.29 spec/spec_helper.rb
appengine-apis-0.0.28 spec/spec_helper.rb
appengine-apis-0.0.27 spec/spec_helper.rb
appengine-apis-0.0.25 spec/spec_helper.rb
appengine-apis-0.0.24 spec/spec_helper.rb
appengine-apis-0.0.23 spec/spec_helper.rb
appengine-apis-0.0.22 spec/spec_helper.rb
appengine-apis-0.0.21 spec/spec_helper.rb
appengine-apis-0.0.20 spec/spec_helper.rb
appengine-apis-0.0.19 spec/spec_helper.rb
appengine-apis-0.0.18 spec/spec_helper.rb
appengine-apis-0.0.17 spec/spec_helper.rb
appengine-apis-0.0.16 spec/spec_helper.rb
appengine-apis-0.0.15 spec/spec_helper.rb