Class: MatchJson::Matchers::IncludeJson

Inherits:
Object
  • Object
show all
Defined in:
lib/match_json/matchers/include_json.rb

Constant Summary collapse

PATTERNS =
{
  'date_time_iso8601' => /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
  'date' => /^\d{4}-\d{2}-\d{2}/,
  'uuid' => /\h{32}/,
  'email' => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,6}\z/i,
  'string' => /\A.+\z/i
}

Instance Method Summary collapse

Constructor Details

#initialize(expected_json) ⇒ IncludeJson

Returns a new instance of IncludeJson.



13
14
15
# File 'lib/match_json/matchers/include_json.rb', line 13

def initialize(expected_json)
  @expected_json = JSON.parse(expected_json.gsub(/(?<!")(\{\w+\})(?!")/, '"\1:non-string"'))
end

Instance Method Details

#failure_messageObject



23
24
25
# File 'lib/match_json/matchers/include_json.rb', line 23

def failure_message
  @failure_message
end

#failure_message_when_negatedObject



27
28
29
# File 'lib/match_json/matchers/include_json.rb', line 27

def failure_message_when_negated
  "does not support negation"
end

#matches?(actual_json) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/match_json/matchers/include_json.rb', line 17

def matches?(actual_json)
  @actual_json = actual_json.respond_to?(:body) ? JSON.parse(actual_json.body) : JSON.parse(actual_json)

  catch(:match) { json_included?(@actual_json, @expected_json) }
end