rspec-enriched_json 0.3.0 → 0.5.0
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 +4 -4
- data/README.md +8 -10
- data/lib/rspec/enriched_json/enriched_expectation_not_met_error.rb +3 -3
- data/lib/rspec/enriched_json/expectation_helper_wrapper.rb +3 -5
- data/lib/rspec/enriched_json/formatters/enriched_json_formatter.rb +8 -8
- data/lib/rspec/enriched_json/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 897a7b35c81a2a9b629d4af420b37e5f404e360ad92d6bfb06a983c580687f2c
|
4
|
+
data.tar.gz: 3907177ad79acea11dff76511d2fbb6d5577a6e234147a6a66b47519fdd3e527
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5573a16911801b63c1210357122e97436e1cb5a5ad2d5e3addc089b8afd60bc3a1bb995c34a7a252f4ed926123e011f7c331c8ec681ad2907f6d079d946c4e95
|
7
|
+
data.tar.gz: 6114b639b4b64796f06629cbf4ac5b323d62647175a10738af938236e1c261883097dc1d914fb179b5b2431d05063f5b2a28a6c0a72473b3546bed43c5c7255f
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ To see the difference between RSpec's built-in JSON formatter and this enriched
|
|
10
10
|
ruby demo.rb
|
11
11
|
```
|
12
12
|
|
13
|
-
This interactive demo script runs the same failing tests with both formatters and shows you the difference side-by-side.
|
13
|
+
This interactive demo script runs the same failing tests with both formatters and shows you the difference side-by-side.
|
14
14
|
|
15
15
|
**What you'll see:**
|
16
16
|
- **Built-in formatter**: Failure information embedded in string messages
|
@@ -83,14 +83,12 @@ With this gem, you get structured data alongside the original message:
|
|
83
83
|
"message": "\nexpected: \"Hello, Ruby!\"\n got: \"Hello, World!\"\n\n(compared using ==)\n",
|
84
84
|
"backtrace": ["./spec/example_spec.rb:5:in `block (2 levels) in <top (required)>'"]
|
85
85
|
},
|
86
|
-
"
|
86
|
+
"details": {
|
87
87
|
"expected": "Hello, Ruby!",
|
88
88
|
"actual": "Hello, World!",
|
89
89
|
"matcher_name": "RSpec::Matchers::BuiltIn::Eq",
|
90
90
|
"original_message": null,
|
91
|
-
"
|
92
|
-
"diffable": true
|
93
|
-
}
|
91
|
+
"diffable": true
|
94
92
|
}
|
95
93
|
}
|
96
94
|
```
|
@@ -104,27 +102,27 @@ With this gem, you get structured data alongside the original message:
|
|
104
102
|
- **Graceful degradation**: Regular exceptions (non-expectation failures) work normally
|
105
103
|
- **Enhanced metadata capture**: Test location, tags, hierarchy, and custom metadata
|
106
104
|
- **Robust error recovery**: Handles objects that fail to serialize without crashing
|
107
|
-
- **Diff information**: Includes `
|
105
|
+
- **Diff information**: Includes `diffable` to help tools determine if values can be meaningfully diffed
|
108
106
|
|
109
107
|
## Examples
|
110
108
|
|
111
109
|
### Simple Values
|
112
110
|
```ruby
|
113
111
|
expect(1 + 1).to eq(3)
|
114
|
-
#
|
112
|
+
# details: { "expected": 3, "actual": 2 }
|
115
113
|
```
|
116
114
|
|
117
115
|
### Collections
|
118
116
|
```ruby
|
119
117
|
expect([1, 2, 3]).to eq([1, 2, 4])
|
120
|
-
#
|
118
|
+
# details: { "expected": [1, 2, 4], "actual": [1, 2, 3] }
|
121
119
|
```
|
122
120
|
|
123
121
|
### Complex Objects
|
124
122
|
```ruby
|
125
123
|
Product = Struct.new(:name, :price)
|
126
124
|
expect(Product.new("Laptop", 999)).to eq(Product.new("Laptop", 899))
|
127
|
-
#
|
125
|
+
# details includes class info and struct values
|
128
126
|
```
|
129
127
|
|
130
128
|
### Custom Messages
|
@@ -132,7 +130,7 @@ expect(Product.new("Laptop", 999)).to eq(Product.new("Laptop", 899))
|
|
132
130
|
expect(balance).to be >= required,
|
133
131
|
"Insufficient funds: $#{balance} available, $#{required} required"
|
134
132
|
# exception.message: "Insufficient funds: $50 available, $100 required"
|
135
|
-
#
|
133
|
+
# details: { "original_message": "expected: >= 100\n got: 50" }
|
136
134
|
```
|
137
135
|
|
138
136
|
### Metadata Capture
|
@@ -6,11 +6,11 @@ module RSpec
|
|
6
6
|
module EnrichedJson
|
7
7
|
# Custom exception that carries structured data alongside the message
|
8
8
|
class EnrichedExpectationNotMetError < RSpec::Expectations::ExpectationNotMetError
|
9
|
-
attr_reader :
|
9
|
+
attr_reader :details
|
10
10
|
|
11
|
-
def initialize(message,
|
11
|
+
def initialize(message, details = {})
|
12
12
|
super(message)
|
13
|
-
@
|
13
|
+
@details = details
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -106,18 +106,16 @@ module RSpec
|
|
106
106
|
actual_raw = extract_value(matcher, :actual)
|
107
107
|
|
108
108
|
# Collect structured data
|
109
|
-
|
109
|
+
details = {
|
110
110
|
expected: Serializer.serialize_value(expected_raw),
|
111
111
|
actual: Serializer.serialize_value(actual_raw),
|
112
112
|
original_message: original_message, # Only populated when custom message overrides it
|
113
113
|
matcher_name: matcher.class.name,
|
114
|
-
|
115
|
-
diffable: values_diffable?(expected_raw, actual_raw, matcher)
|
116
|
-
}
|
114
|
+
diffable: values_diffable?(expected_raw, actual_raw, matcher)
|
117
115
|
}
|
118
116
|
|
119
117
|
# Raise new exception with data attached
|
120
|
-
raise EnrichedExpectationNotMetError.new(e.message,
|
118
|
+
raise EnrichedExpectationNotMetError.new(e.message, details)
|
121
119
|
end
|
122
120
|
|
123
121
|
private
|
@@ -25,8 +25,8 @@ module RSpec
|
|
25
25
|
}
|
26
26
|
|
27
27
|
# Add structured data if available
|
28
|
-
if e.is_a?(RSpec::EnrichedJson::EnrichedExpectationNotMetError) && e.
|
29
|
-
hash[:
|
28
|
+
if e.is_a?(RSpec::EnrichedJson::EnrichedExpectationNotMetError) && e.details
|
29
|
+
hash[:details] = safe_structured_data(e.details)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -84,13 +84,13 @@ module RSpec
|
|
84
84
|
hierarchy
|
85
85
|
end
|
86
86
|
|
87
|
-
def safe_structured_data(
|
87
|
+
def safe_structured_data(details)
|
88
88
|
{
|
89
|
-
expected: safe_serialize(
|
90
|
-
actual: safe_serialize(
|
91
|
-
matcher_name:
|
92
|
-
original_message:
|
93
|
-
|
89
|
+
expected: safe_serialize(details[:expected]),
|
90
|
+
actual: safe_serialize(details[:actual]),
|
91
|
+
matcher_name: details[:matcher_name],
|
92
|
+
original_message: details[:original_message],
|
93
|
+
diffable: details[:diffable]
|
94
94
|
}.compact
|
95
95
|
end
|
96
96
|
|