Sha256: ff6e0b3fadb5f3179e337422309b928fa9f35239afb0ed338ababca77cacf74d

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module RSpec
  module Siren
    module Matchers
      class HasProperty
        def initialize(expected_name)
          @expected_name = expected_name
        end

        def matches?(target)
          @target = target

          @value = safe_properties[@expected_name]

          property_exists? && (!has_expected_value? || expected_value_matches?)
        end

        def with_value(expected_value)
          @expected_value = expected_value
          self
        end

        def has_expected_value?
          !!@expected_value
        end

        def expected_value_matches?
          @value == @expected_value
        end

        def property_exists?
          safe_properties.has_key?(@expected_name)
        end

        def description
          message = "have"
          message << " property '#{@expected_name}'"
          message << " with value '#{@expected_value}'"
        end

        def failure_message
          message = "expected"
          message << " property '#{@expected_name}'"
          message << " with value '#{@expected_value}'" if @expected_value
          message << ". Found"
          if !property_exists?
            message << " no such property."
          else
            message << " with value '#{@value}'"
          end
          message
        end

        private

        def safe_properties
          @target[:properties] || @target["properties"] || {}
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-siren-1.1.0 lib/rspec/siren/matchers/has_property.rb
rspec-siren-1.0.1 lib/rspec/siren/matchers/has_property.rb
rspec-siren-1.0.0 lib/rspec/siren/matchers/has_property.rb