Class: Pact::Matchers::ListDiffFormatter
- Inherits:
-
Object
- Object
- Pact::Matchers::ListDiffFormatter
- Defined in:
- lib/pact/matchers/list_diff_formatter.rb
Instance Attribute Summary collapse
-
#diff ⇒ Object
readonly
Returns the value of attribute diff.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #diff_descriptions(obj, path = [], descriptions = []) ⇒ Object
- #handle_array(array, path, descriptions) ⇒ Object
- #handle_difference(difference, path, descriptions) ⇒ Object
- #handle_hash(hash, path, descriptions) ⇒ Object
- #handle_index_not_found(difference, path, descriptions) ⇒ Object
- #handle_key_not_found(difference, path, descriptions) ⇒ Object
- #handle_mismatched_regexp(difference, path, descriptions) ⇒ Object
- #handle_mismatched_type(difference, path, descriptions) ⇒ Object
- #handle_mismatched_value(difference, path, descriptions) ⇒ Object
- #handle_unexpected_index(difference, path, descriptions) ⇒ Object
- #handle_unexpected_key(difference, path, descriptions) ⇒ Object
-
#initialize(diff, options = {}) ⇒ ListDiffFormatter
constructor
A new instance of ListDiffFormatter.
- #path_to_s(path) ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(diff, options = {}) ⇒ ListDiffFormatter
Returns a new instance of ListDiffFormatter.
7 8 9 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 7 def initialize diff, = {} @diff = diff end |
Instance Attribute Details
#diff ⇒ Object (readonly)
Returns the value of attribute diff.
5 6 7 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 5 def diff @diff end |
Class Method Details
.call(diff, options = {}) ⇒ Object
11 12 13 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 11 def self.call diff, = {} new(diff, ).call end |
Instance Method Details
#call ⇒ Object
19 20 21 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 19 def call to_s end |
#diff_descriptions(obj, path = [], descriptions = []) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 27 def diff_descriptions obj, path = [], descriptions = [] case obj when Hash then handle_hash obj, path, descriptions when Array then handle_array obj, path, descriptions when Difference then handle_difference obj, path, descriptions when TypeDifference then handle_mismatched_type obj, path, descriptions when RegexpDifference then handle_mismatched_regexp obj, path, descriptions when NoDiffAtIndex then nil else raise "Invalid diff, expected Hash, Array, NoDiffAtIndex or Difference, found #{obj.class}" end descriptions end |
#handle_array(array, path, descriptions) ⇒ Object
47 48 49 50 51 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 47 def handle_array array, path, descriptions array.each_with_index do | obj, index | diff_descriptions obj, path + [index], descriptions end end |
#handle_difference(difference, path, descriptions) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 53 def handle_difference difference, path, descriptions case difference.expected when Pact::UnexpectedKey then handle_unexpected_key(difference, path, descriptions) when Pact::UnexpectedIndex then handle_unexpected_index(difference, path, descriptions) else case difference.actual when Pact::KeyNotFound then handle_key_not_found(difference, path, descriptions) when Pact::IndexNotFound then handle_index_not_found(difference, path, descriptions) else handle_mismatched_value(difference, path, descriptions) end end end |
#handle_hash(hash, path, descriptions) ⇒ Object
41 42 43 44 45 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 41 def handle_hash hash, path, descriptions hash.each_pair do | key, value | diff_descriptions value, path + [key.inspect], descriptions end end |
#handle_index_not_found(difference, path, descriptions) ⇒ Object
83 84 85 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 83 def handle_index_not_found difference, path, descriptions descriptions << "\tAt:\n\t\t#{path_to_s(path)}\n\tMissing index with value:\n\t\t#{difference.expected.ai}" end |
#handle_key_not_found(difference, path, descriptions) ⇒ Object
87 88 89 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 87 def handle_key_not_found difference, path, descriptions descriptions << "\tAt:\n\t\t#{path_to_s(path)}\n\tMissing key with value:\n\t\t#{difference.expected.ai}" end |
#handle_mismatched_regexp(difference, path, descriptions) ⇒ Object
75 76 77 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 75 def handle_mismatched_regexp difference, path, descriptions descriptions << "\tAt:\n\t\t#{path_to_s(path)}\n\tExpected to match:\n\t\t#{difference.expected.inspect}\n\tActual:\n\t\t#{difference.actual.ai}" end |
#handle_mismatched_type(difference, path, descriptions) ⇒ Object
79 80 81 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 79 def handle_mismatched_type difference, path, descriptions descriptions << "\tAt:\n\t\t#{path_to_s(path)}\n\tExpected type:\n\t\t#{difference.expected}\n\tActual type:\n\t\t#{difference.actual}" end |
#handle_mismatched_value(difference, path, descriptions) ⇒ Object
71 72 73 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 71 def handle_mismatched_value difference, path, descriptions descriptions << "\tAt:\n\t\t#{path_to_s(path)}\n\tExpected:\n\t\t#{difference.expected.ai}\n\tActual:\n\t\t#{difference.actual.ai}" end |
#handle_unexpected_index(difference, path, descriptions) ⇒ Object
67 68 69 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 67 def handle_unexpected_index difference, path, descriptions descriptions << "\tAt:\n\t\t#{path_to_s(path)}\n\tArray contained unexpected item:\n\t\t#{difference.actual.ai}" end |
#handle_unexpected_key(difference, path, descriptions) ⇒ Object
91 92 93 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 91 def handle_unexpected_key difference, path, descriptions descriptions << "\tAt:\n\t\t#{path_to_s(path)}\n\tHash contained unexpected key with value:\n\t\t#{difference.actual.ai}" end |
#path_to_s(path) ⇒ Object
95 96 97 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 95 def path_to_s path "[" + path.join("][") + "]" end |
#to_hash ⇒ Object
15 16 17 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 15 def to_hash diff end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/pact/matchers/list_diff_formatter.rb', line 23 def to_s diff_descriptions(diff).join("\n") end |