Class: RSpec::JsonMatchers::Matchers::BeJsonWithContentMatcher Abstract Private

Inherits:
BeJsonMatcher
  • Object
show all
Defined in:
lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is abstract.

Parent of matcher classes that requires #at_path & #with_exact_keys This is not merged with BeJsonMatcher since it should be able to be used alone

Instance Method Summary collapse

Methods inherited from BeJsonMatcher

#description, #with_content

Constructor Details

#initialize(expected) ⇒ BeJsonWithContentMatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BeJsonWithContentMatcher.



21
22
23
24
# File 'lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb', line 21

def initialize(expected)
  @expected     = expected
  @path         = JsonMatchers::Utils::KeyPath::Path.new("")
end

Instance Method Details

#actualObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override RSpec::JsonMatchers::Matchers::BeJsonMatcher#actual It return actual object extracted by #path And also detect & set state for path error (either it’s invalid or fails to extract)

Returns:

  • (Object)

    extracted object but could be object in the middle when extraction failed



42
43
44
45
46
# File 'lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb', line 42

def actual
  result = path.extract(super)
  has_path_error! if result.failed?
  result.object
end

#at_path(path) ⇒ BeJsonWithSomethingMatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the path to be used for object, to avoid passing a deep nested Hash or Array as expectation Defaults to “” (if this is not called)

The path uses period (“.”) as separator for parts Also period cannot be used as path name as a side-effect

This does NOT raise error if the path is invalid (like having 2 periods, 1 period at the start/end of string) But it will fail the example with both ‘should` & `should_not`

Parameters:

  • path (String)

    the “path” to be used

Returns:

  • (BeJsonWithSomethingMatcher)

    the match itself



65
66
67
68
# File 'lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb', line 65

def at_path(path)
  @path = JsonMatchers::Utils::KeyPath::Path.new(path)
  self
end

#does_not_match?(*args) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


30
31
32
# File 'lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb', line 30

def does_not_match?(*args)
  !matches?(*args) && has_valid_path?
end

#failure_messageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Overrides RSpec::JsonMatchers::Matchers::BeJsonMatcher#failure_message



71
72
73
74
# File 'lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb', line 71

def failure_message
  return super if has_parser_error?
  failure_message_for(true)
end

#failure_message_when_negatedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Overrides RSpec::JsonMatchers::Matchers::BeJsonMatcher#failure_message_when_negated



77
78
79
80
# File 'lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb', line 77

def failure_message_when_negated
  return super if has_parser_error?
  failure_message_for(false)
end

#matches?(*_args) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


26
27
28
# File 'lib/rspec/json_matchers/matchers/be_json_with_content_matcher.rb', line 26

def matches?(*_args)
  super && has_valid_path? && expected_and_actual_matched?
end