rspec-enriched_json 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02a818ba249d1c7880a696e4488a45d827261458a569624263c3983d86eb4f7d
4
- data.tar.gz: bca3f6099bc1779a07bafd486a4da1734d102fa4d1a3ea97ac18dfe2e059d2ed
3
+ metadata.gz: d601f3b9829a2dd3bc4af4a42e65b01819ec6152156552f25b9e188ad47d828d
4
+ data.tar.gz: 1a08c66c4308c8f7e33ab3769c7bc1bc0601ace0588c242690d7edb2d02175d0
5
5
  SHA512:
6
- metadata.gz: e5b929b27773dbe3423097855d06ce700d377f5fad568218d22a329fbae70714ddf38cb15300e458d3e55e61350b062f0bb32c165017205e334a674020f1ceba
7
- data.tar.gz: afa583debdd99a3e93ce77f5764a6b57d22dfb32752565fb9c6f1ebc92b41b9f6801ac191ae7846b685f0c7ac52d8994530d4471ad34008c13b9e6279570da5d
6
+ metadata.gz: 86d612e624b12e6da7e6a32d877479a548ee301d79e5c231a6741843c83f07f635c11415e9945d84c68b076dda67b96712a5786a7d3cd4967923d4526be2f6d8
7
+ data.tar.gz: 5478f0f570d46b58139d82f16f1e305290370ca41ee99045ff382dfa4e77644afab8dd6fd408e98c495d2c23f14fc7c227120e87d1ab08da533dcb77df4e225f
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "json"
4
4
  require "rspec/expectations"
5
+ require "rspec/support/differ"
5
6
 
6
7
  module RSpec
7
8
  module EnrichedJson
@@ -28,12 +29,16 @@ module RSpec
28
29
  return "[Max depth exceeded]" if depth > MAX_SERIALIZATION_DEPTH
29
30
 
30
31
  case value
31
- when nil, Numeric, TrueClass, FalseClass
32
+ when Numeric, TrueClass, FalseClass
32
33
  value
33
34
  when String
34
- truncate_string(value)
35
+ unescape_string_double_quotes(
36
+ truncate_string(value)
37
+ )
35
38
  when Symbol
36
- value.to_s
39
+ serialize_object(value)
40
+ when nil
41
+ serialize_object(value)
37
42
  when Array
38
43
  return "[Large array: #{value.size} items]" if value.size > MAX_ARRAY_SIZE
39
44
  value.map { |v| serialize_value(v, depth + 1) }
@@ -78,6 +83,22 @@ module RSpec
78
83
  "#{str[0...MAX_STRING_LENGTH]}... (truncated)"
79
84
  end
80
85
 
86
+ def unescape_string_double_quotes(str)
87
+ if str.start_with?('"') && str.end_with?('"')
88
+ begin
89
+ # Only undump if it's a valid dumped string
90
+ # Check if the string is properly escaped by attempting undump
91
+ str.undump
92
+ rescue RuntimeError => e
93
+ # If undump fails, just return the original string
94
+ # This handles cases where the string has quotes but isn't a valid dump format
95
+ str
96
+ end
97
+ else
98
+ str
99
+ end
100
+ end
101
+
81
102
  def safe_inspect(obj)
82
103
  truncate_string(obj.inspect)
83
104
  rescue => e
@@ -109,11 +130,17 @@ module RSpec
109
130
  details = {
110
131
  expected: Serializer.serialize_value(expected_raw),
111
132
  actual: Serializer.serialize_value(actual_raw),
112
- original_message: original_message, # Only populated when custom message overrides it
133
+ original_message: original_message, # Only populated when custom message overrides it
113
134
  matcher_name: matcher.class.name,
114
135
  diffable: values_diffable?(expected_raw, actual_raw, matcher)
115
136
  }
116
137
 
138
+ # Generate diff if values are diffable
139
+ if details[:diffable] && expected_raw && actual_raw
140
+ diff = generate_diff(actual_raw, expected_raw)
141
+ details[:diff] = diff unless diff.nil? || diff.strip.empty?
142
+ end
143
+
117
144
  # Capture matcher-specific instance variables
118
145
  matcher_data = extract_matcher_specific_data(matcher)
119
146
  details.merge!(matcher_data) unless matcher_data.empty?
@@ -226,6 +253,20 @@ module RSpec
226
253
  # If any error occurs during checking, assume not diffable
227
254
  false
228
255
  end
256
+
257
+ def generate_diff(actual, expected)
258
+ # Use RSpec's own differ for consistency
259
+ differ = RSpec::Support::Differ.new(
260
+ object_preparer: lambda { |obj|
261
+ RSpec::Matchers::Composable.surface_descriptions_in(obj)
262
+ },
263
+ color: false # Always disable color for JSON output
264
+ )
265
+ differ.diff(actual, expected)
266
+ rescue
267
+ # If diff generation fails, return nil rather than crashing
268
+ nil
269
+ end
229
270
  end
230
271
  end
231
272
  end
@@ -28,6 +28,13 @@ module RSpec
28
28
  if e.is_a?(RSpec::EnrichedJson::EnrichedExpectationNotMetError) && e.details
29
29
  hash[:details] = safe_structured_data(e.details)
30
30
  end
31
+
32
+ if hash.key?(:details) && hash[:details].key?(:expected) && hash[:details].key?(:actual)
33
+ exception_message = hash[:exception][:message]
34
+ if exception_message.include?("\nDiff:")
35
+ hash[:exception][:message] = exception_message.sub(/Diff:.*/m, "").strip
36
+ end
37
+ end
31
38
  end
32
39
  end
33
40
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module EnrichedJson
5
- VERSION = "0.6.0"
5
+ VERSION = "0.6.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-enriched_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raghu Betina
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  - !ruby/object:Gem::Version
121
121
  version: '0'
122
122
  requirements: []
123
- rubygems_version: 3.6.9
123
+ rubygems_version: 3.6.7
124
124
  specification_version: 4
125
125
  summary: Enriches RSpec JSON output with structured failure data
126
126
  test_files: []