Class: EqJsonMessageGenerator
- Inherits:
-
Object
- Object
- EqJsonMessageGenerator
- Defined in:
- lib/message_generator.rb
Instance Method Summary collapse
- #generateDifferentKeyMessage ⇒ Object
- #generateDifferentSizeArrayMessage ⇒ Object
- #generateDifferentValueMessage ⇒ Object
- #generateExpectedItemNotFoundInArray(expected_item, expected_count, actual_count) ⇒ Object
- #generateTypeMissMatchFailureMessage ⇒ Object
- #getExpectedActualJson ⇒ Object
- #getJsonType(rubyJsonObject) ⇒ Object
- #getObjectsNotIn(hash1, hash2) ⇒ Object
-
#initialize(matcher) ⇒ EqJsonMessageGenerator
constructor
A new instance of EqJsonMessageGenerator.
Constructor Details
#initialize(matcher) ⇒ EqJsonMessageGenerator
Returns a new instance of EqJsonMessageGenerator.
5 6 7 8 |
# File 'lib/message_generator.rb', line 5 def initialize(matcher) @matcher = matcher @colorizer = EqJsonColorizer.new end |
Instance Method Details
#generateDifferentKeyMessage ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/message_generator.rb', line 54 def generateDifferentKeyMessage() if @matcher.currentActualObj.nil? objectsNotInExpected = getObjectsNotIn(@matcher.actual, @matcher.expected); objectsNotInActual = getObjectsNotIn(@matcher.expected, @matcher.actual); else objectsNotInExpected = getObjectsNotIn(@matcher.currentActualObj, @matcher.currentExpectedObj); objectsNotInActual = getObjectsNotIn(@matcher.currentExpectedObj, @matcher.currentActualObj); end jsonErrorInfo = "JSON path #{@matcher.jsonPath}\n" unless objectsNotInExpected.empty? jsonErrorInfo << "expected does not contain #{objectsNotInExpected.to_json}\n" end unless objectsNotInActual.empty? jsonErrorInfo << @colorizer.green("actual does not contain #{objectsNotInActual.to_json}\n") end differ = RSpec::Support::Differ.new differ = RSpec::Support::Differ.new( :object_preparer => lambda {|expected| RSpec::Matchers::Composable.surface_descriptions_in(expected)}, :color => RSpec::Matchers.configuration.color? ) if @matcher.currentActualObj.nil? @difference = differ.diff(@matcher.expected, @matcher.actual) else @difference = differ.diff(@matcher.currentExpectedObj, @matcher.currentActualObj) end return getExpectedActualJson() + "\n" + "\nDiff:\n" + jsonErrorInfo + @difference end |
#generateDifferentSizeArrayMessage ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/message_generator.rb', line 121 def generateDifferentSizeArrayMessage() if @matcher.currentActualObj.nil? expectedLength = @matcher.expected.length actualLength = @matcher.actual.length else expectedLength = @matcher.currentExpectedObj.length actualLength = @matcher.currentActualObj.length end jsonErrorInfo = "JSON path #{@matcher.jsonPath}[] expected length #{expectedLength} " + "actual length #{actualLength}\n" return getExpectedActualJson() + "\n" + "\nDiff:\n" + jsonErrorInfo end |
#generateDifferentValueMessage ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/message_generator.rb', line 32 def generateDifferentValueMessage() # TODO have item in todo list to use a diff if the value is a String # with mutiple lines so leaving this diff code even though it is not # being used differ = RSpec::Support::Differ.new differ = RSpec::Support::Differ.new( :object_preparer => lambda {|expected| RSpec::Matchers::Composable.surface_descriptions_in(expected)}, :color => RSpec::Matchers.configuration.color? ) @difference = differ.diff_as_object(@matcher.currentExpectedObj, @matcher.currentActualObj) # End unused code return getExpectedActualJson() + "\n" + "Diff:\n" + "JSON path #{@matcher.jsonPath}\n" + "\texpected: \"#{@matcher.currentExpectedObj}\"\n" + @colorizer.green("\t got: \"#{@matcher.currentActualObj}\"") end |
#generateExpectedItemNotFoundInArray(expected_item, expected_count, actual_count) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/message_generator.rb', line 138 def generateExpectedItemNotFoundInArray(expected_item, expected_count, actual_count) # if @matcher.currentActualObj.nil? # objectsNotInExpected = getObjectsNotInArray(@matcher.actual, @matcher.expected); # objectsNotInActual = getObjectsNotInArray(@matcher.expected, @matcher.actual); # else # objectsNotInExpected = getObjectsNotIn(@matcher.currentActualObj, @matcher.currentExpectedObj); # objectsNotInActual = getObjectsNotIn(@matcher.currentExpectedObj, @matcher.currentActualObj); # end if actual_count == 0 jsonErrorInfo = "JSON path #{@matcher.jsonPath}[] could not find:\n" + "#{expected_item.to_json}\n" + "in actual\n" else jsonErrorInfo = "JSON path #{@matcher.jsonPath}[] wrong number of:\n" + "#{expected_item.to_json}\n" + "in actual\n" + "expected: #{expected_count}\n" + @colorizer.green(" got: #{actual_count}") + "\n" end # unless objectsNotInExpected.empty? # jsonErrorInfo << "expected does not contain #{objectsNotInExpected.to_json}\n" # end # # unless objectsNotInActual.empty? # jsonErrorInfo << @colorizer.green("actual does not contain #{objectsNotInActual.to_json}\n") # end return getExpectedActualJson() + "\n" + "\nDiff:\n" + jsonErrorInfo end |
#generateTypeMissMatchFailureMessage ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/message_generator.rb', line 10 def generateTypeMissMatchFailureMessage() if @matcher.currentJsonKey.nil? actualType = getJsonType(@matcher.actual) expectedType = getJsonType(@matcher.expected) else actualType = getJsonType(@matcher.currentActualObj) expectedType = getJsonType(@matcher.currentExpectedObj) currentJsonDiff = "\tExpected: #{@matcher.currentExpectedObj.to_json}\n" + @colorizer.green("\t Actual: #{@matcher.currentActualObj.to_json}") + "\n" end jsonErrorInfo = "JSON path #{@matcher.jsonPath} expected #{expectedType} type but actual is #{actualType}\n" unless currentJsonDiff.nil? jsonErrorInfo << currentJsonDiff end return getExpectedActualJson() +"\n" + "Diff:\n" + "#{jsonErrorInfo}" end |
#getExpectedActualJson ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/message_generator.rb', line 92 def getExpectedActualJson expectedJson=@matcher.expected.to_json; actualJson=@matcher.actual.to_json; return "Expected: #{expectedJson}\n" + @colorizer.green(" Actual: #{actualJson}") end |
#getJsonType(rubyJsonObject) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/message_generator.rb', line 110 def getJsonType(rubyJsonObject) case rubyJsonObject when Array return "array" when Hash return "object" else return "not json" end end |
#getObjectsNotIn(hash1, hash2) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/message_generator.rb', line 100 def getObjectsNotIn(hash1, hash2) missing = {} hash1.each do |hash1_key, hash1_value| unless hash2.has_key?(hash1_key) missing[hash1_key] = hash1_value end end return missing end |