class MatchJson::Matchers::IncludeJson
Constants
- PATTERNS
Public Class Methods
new(expected_json)
click to toggle source
# 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
Public Instance Methods
failure_message()
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 23 def failure_message @failure_message end
failure_message_when_negated()
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 27 def failure_message_when_negated "does not support negation" end
matches?(actual_json)
click to toggle source
# 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
Private Instance Methods
array_included?(actual, expected, nested, raise_error)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 56 def array_included?(actual, expected, nested, raise_error) expected.each do |value| if actual.nil? || (!actual.any? { |actual_value| equal_value?(actual_value, value, nested, false) }) @failure_message = %Q("#{value.to_json}" was not found in\n ) @failure_message << %Q("#{nested}":) if !nested.empty? @failure_message << "#{actual.to_json}" if raise_error throw(:match, false) else return false end end end end
compare_with_pattern(value, actual)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 82 def compare_with_pattern(value, actual) case when regexp?(value) reg_exp = value.match(/{re:(.*)}/)[1] actual =~ Regexp.new(reg_exp) when defined_regexp?(value) defined_regexps = PATTERNS.keys.find_all { |pattern| pattern.is_a? Regexp } matches = value.match(/{(\w+):(\w+)}/) reg_exp = defined_regexps.find {|re| re.match("#{matches[1]}:#{matches[2]}") } PATTERNS[reg_exp].call(actual, matches[2]) when pattern?(value) actual =~ PATTERNS["#{value.tr('{}', '')}"] when non_string_pattern?(value) && !actual.is_a?(String) actual.to_s =~ PATTERNS["#{value.tr('{}', '').sub(':non-string', '')}"] else value == actual end end
compared_different_types?(actual, expected)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 117 def compared_different_types?(actual, expected) actual.class != expected.class end
defined_regexp?(str)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 113 def defined_regexp?(str) !!(str =~ /\A\{\w+:\w+\}\z/) end
equal_value?(actual, expected, nested = '', raise_error = true)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 72 def equal_value?(actual, expected, nested = '', raise_error = true) case expected when Array then array_included?(actual, expected, nested, raise_error) when Hash then hash_included?(actual, expected, nested, raise_error) when String then compare_with_pattern(expected, actual) else actual == expected end end
hash_included?(actual, expected, nested, raise_error)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 37 def hash_included?(actual, expected, nested, raise_error) if compared_different_types?(actual, expected) @failure_message = %Q(Different types of compared elements:\n #{actual.class} for #{actual.to_json}\nand #{expected.class} for #{expected.to_json}) throw(:match, false) end expected.each do |key, value| if (!equal_value?(actual[key], value, "#{nested} > #{key}", raise_error)) @failure_message = %Q("#{key}":#{value.to_json} was not found in\n #{actual.to_json}) if raise_error throw(:match, false) else return false end end end end
json_included?(actual, expected)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 33 def json_included?(actual, expected) equal_value?(actual, expected) end
non_string_pattern?(str)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 105 def non_string_pattern?(str) !!(str =~ /\A\{\w+\}:non-string\z/) end
pattern?(str)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 101 def pattern?(str) !!(str =~ /\A\{\w+\}\z/) end
regexp?(str)
click to toggle source
# File lib/match_json/matchers/include_json.rb, line 109 def regexp?(str) !!(str =~ /\A\{re:.+\}\z/) end