Sha256: 3e4b85cd87c872f889928d90f1d364ca05d12fbe781ac86e0534a74617ce145d

Contents?: true

Size: 1.38 KB

Versions: 20

Compression:

Stored size: 1.38 KB

Contents

module BeModelWithValuesMatcher
 
  class BeModelWithValues
    def initialize(expected_hash, compare_as_strings=false)
      @compare_as_strings = compare_as_strings
      @expected_hash = expected_hash
    end

    def matches?(model)
      @model_hash = model.fill_hash(@expected_hash.keys)
      @diff_keys = @expected_hash.keys - @model_hash.keys
      @diff_act_values = {}
      @diff_exp_values = {}
      return false unless @diff_keys.empty?
      @model_hash.each do |k,v|
        model_value = @compare_as_strings ? @model_hash[k].to_s : @model_hash[k]
        expected_value = @compare_as_strings ? @expected_hash[k].to_s : @expected_hash[k]
        unless model_value == expected_value
          @diff_act_values[k] = v
          @diff_exp_values[k] = @expected_hash[k]
        end
      end
      return @diff_act_values.empty?
    end

    def failure_message
      str = "expected:\n#{@expected_hash.inspect}\n but got:\n#{@model_hash.inspect}"
      str += "\n\nmissing keys: #{@diff_keys.inspect}" unless @diff_keys.empty?
      unless @diff_act_values.empty?
        str += "\n\nunequal values: \nexpected: #{@diff_exp_values.inspect} \n but got:\n#{@diff_act_values.inspect}"
      end
      str
    end
  end

  def be_model_with_values(expected)
    BeModelWithValues.new(expected)
  end

  def be_model_with_values_as_strings(expected)
    BeModelWithValues.new(expected,true)
  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
fossil-0.3.11 spec/be_model_with_values_matcher.rb
fossil-0.3.10 spec/be_model_with_values_matcher.rb
fossil-0.3.9 spec/be_model_with_values_matcher.rb
fossil-0.3.8 spec/be_model_with_values_matcher.rb
fossil-0.3.7 spec/be_model_with_values_matcher.rb
fossil-0.3.6 spec/be_model_with_values_matcher.rb
fossil-0.3.5 spec/be_model_with_values_matcher.rb
fossil-0.3.4 spec/be_model_with_values_matcher.rb
fossil-0.3.3 spec/be_model_with_values_matcher.rb
fossil-0.3.2 spec/be_model_with_values_matcher.rb
fossil-0.3.1 spec/be_model_with_values_matcher.rb
fossil-0.3.0 spec/be_model_with_values_matcher.rb
fossil-0.2.9 spec/be_model_with_values_matcher.rb
fossil-0.2.8 spec/be_model_with_values_matcher.rb
fossil-0.2.7 spec/be_model_with_values_matcher.rb
fossil-0.2.6 spec/be_model_with_values_matcher.rb
fossil-0.2.5 spec/be_model_with_values_matcher.rb
fossil-0.2.4 spec/be_model_with_values_matcher.rb
fossil-0.2.3 spec/be_model_with_values_matcher.rb
fossil-0.2.2 spec/be_model_with_values_matcher.rb