Skip to content

Commit d4d8b3b

Browse files
committed
Update README to better reflect primary usage
[ci skip]
1 parent ced8e70 commit d4d8b3b

File tree

3 files changed

+92
-87
lines changed

3 files changed

+92
-87
lines changed

README.md

Lines changed: 92 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -3,149 +3,147 @@
33
[version-badge]: http://img.shields.io/gem/v/super_diff.svg
44
[rubygems]: http://rubygems.org/gems/super_diff
55
[travis-badge]: http://img.shields.io/travis/mcmire/super_diff/master.svg
6-
[travis]: http://travis-ci.org/mcmire/super_diff
76
[downloads-badge]: http://img.shields.io/gem/dtv/super_diff.svg
87
[hound-badge]: https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg
98
[hound]: https://houndci.com
109

11-
## Concept
10+
SuperDiff is a tool that displays the differences between two data structures of
11+
any type in Ruby.
12+
13+
## Introduction
1214

13-
SuperDiff is a utility that helps you diff two complex data structures in Ruby
14-
and gives you helpful output to show you exactly how the two data structures
15-
differ.
15+
The primary motivation behind this gem is to replace RSpec's built-in diffing
16+
capabilities. Sometimes, whenever you use a matcher such as `eq`, `match`,
17+
`include`, or `have_attributes`, you will get a diff of the two data structures
18+
you are trying to match against. This is really helpful for strings, but not so
19+
helpful for other, more "real world" kinds of values, such as arrays, hashes,
20+
and full-scale objects. The reason this doesn't work is because [RSpec will
21+
naively run your `expected` and `actual` values through Ruby's PrettyPrinter
22+
library][rspec-differ-fail] and then perform a diff of these strings.
1623

17-
Let's say you have two hashes and you want to compare them. Perhaps your first
18-
hash looks like this:
24+
For instance, let's say you wanted to compare these two hashes:
1925

2026
``` ruby
21-
expected = {
27+
actual = {
2228
customer: {
23-
name: "Marty McFly",
29+
person: SuperDiff::Test::Person.new(name: "Marty McFly, Jr.", age: 17),
2430
shipping_address: {
25-
line_1: "123 Main St.",
31+
line_1: "456 Ponderosa Ct.",
2632
city: "Hill Valley",
2733
state: "CA",
28-
zip: "90382",
29-
},
34+
zip: "90382"
35+
}
3036
},
3137
items: [
3238
{
3339
name: "Fender Stratocaster",
3440
cost: 100_000,
35-
options: ["red", "blue", "green"],
41+
options: ["red", "blue", "green"]
3642
},
37-
{ name: "Chevy 4x4" },
38-
],
43+
{ name: "Mattel Hoverboard" }
44+
]
3945
}
40-
```
41-
42-
and your second hash looks like this:
4346

44-
``` ruby
45-
actual = {
47+
expected = {
4648
customer: {
47-
name: "Marty McFly, Jr.",
49+
person: SuperDiff::Test::Person.new(name: "Marty McFly", age: 17),
4850
shipping_address: {
49-
line_1: "456 Ponderosa Ct.",
51+
line_1: "123 Main St.",
5052
city: "Hill Valley",
5153
state: "CA",
52-
zip: "90382",
53-
},
54+
zip: "90382"
55+
}
5456
},
5557
items: [
5658
{
5759
name: "Fender Stratocaster",
5860
cost: 100_000,
59-
options: ["red", "blue", "green"],
61+
options: ["red", "blue", "green"]
6062
},
61-
{ name: "Mattel Hoverboard" },
62-
],
63+
{ name: "Chevy 4x4" }
64+
]
6365
}
6466
```
6567

66-
If you want to know what the difference between them is, you could say:
68+
If, somewhere in a test, you were to say:
6769

6870
``` ruby
69-
SuperDiff::EqualityMatcher.call(expected, actual)
71+
expect(actual).to eq(expected)
7072
```
7173

72-
This will give you the following string:
74+
You would get output that looks like:
7375

74-
```
75-
Differing hashes.
76-
77-
Expected: { customer: { name: "Marty McFly", shipping_address: { line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" } }, items: [{ name: "Fender Stratocaster", cost: 100000, options: ["red", "blue", "green"] }, { name: "Chevy 4x4" }] }
78-
Got: { customer: { name: "Marty McFly, Jr.", shipping_address: { line_1: "456 Ponderosa Ct.", city: "Hill Valley", state: "CA", zip: "90382" } }, items: [{ name: "Fender Stratocaster", cost: 100000, options: ["red", "blue", "green"] }, { name: "Mattel Hoverboard" }] }
79-
80-
Diff:
81-
82-
{
83-
customer: {
84-
- name: "Marty McFly",
85-
+ name: "Marty McFly, Jr.",
86-
shipping_address: {
87-
- line_1: "123 Main St.",
88-
+ line_1: "456 Ponderosa Ct.",
89-
city: "Hill Valley",
90-
state: "CA",
91-
zip: "90382"
92-
}
93-
},
94-
items: [
95-
{
96-
name: "Fender Stratocaster",
97-
cost: 100000,
98-
options: ["red", "blue", "green"]
99-
},
100-
{
101-
- name: "Chevy 4x4"
102-
+ name: "Mattel Hoverboard"
103-
}
104-
]
105-
}
106-
```
76+
![Before super_diff](doc/before_super_diff.png)
77+
78+
Not great.
10779

108-
When printed to a terminal, this will display in color, so the "expected" value
109-
in the summary and deleted lines in the diff will appear in red, while the
110-
"actual" value in the summary and inserted lines in the diff will appear in
111-
green.
80+
This library provides a sophisticated set of comparators that know how to
81+
intelligent compute the differences between two data structures and display them
82+
in a way that makes sense. Using the example above, you'd get this instead:
11283

113-
By the way, SuperDiff doesn't just work with hashes, but arrays as well as other
114-
objects, too!
84+
![After super_diff](doc/after_super_diff.png)
11585

116-
## Usage
86+
[rspec-differ-fail]: https://github.com/rspec/rspec-support/blob/c69a231d7369dd165ad7ce4742e1a2e21e3462b5/lib/rspec/support/differ.rb#L178
11787

118-
There are two ways to use this gem. One way is to use the API methods that this
119-
gem provides, such as the method presented above.
88+
## Installation
12089

121-
However, this gem was really designed for use specifically with RSpec. In recent
122-
years, RSpec has added a feature where if you're comparing two objects in a test
123-
and your test fails, you will see a diff between those objects (provided the
124-
objects are large enough). However, this diff is not always the most helpful.
125-
It's very common when writing tests for API endpoints to work with giant JSON
126-
hashes, and RSpec's diffs are not sufficient in highlighting changes between
127-
such structures. Therefore, this gem provides an integration layer where you can
128-
replace RSpec's differ with SuperDiff.
90+
Want to try out this gem for yourself? As with most development-related gems,
91+
there are a couple ways depending on your type of project:
12992

130-
To get started, add the gem to your Gemfile under the `test` group:
93+
### Rails apps
94+
95+
If you're developing a Rails app, add the following to your Gemfile:
13196

13297
``` ruby
13398
gem "super_diff"
13499
```
135100

136-
Then, open up `spec_helper` and add this line somewhere:
101+
After running `bundle install`, add the following to your `rails_helper`:
137102

138103
``` ruby
139104
require "super_diff/rspec"
140105
```
141106

142-
Now try writing a test using `eq` to compare two large data structures, and you
143-
should see a diff similar to the one given above.
107+
You're done!
108+
109+
### Libraries
110+
111+
If you're developing a library, add the following to your gemspec:
112+
113+
``` ruby
114+
spec.add_development_dependency "super_diff"
115+
```
116+
117+
Now add the following to your `spec_helper`:
118+
119+
``` ruby
120+
require "super_diff/rspec"
121+
```
122+
123+
You're done!
124+
125+
## Configuration
126+
127+
As capable as this library is, it doesn't know how to deal with every kind of
128+
object out there. You might find it necessary to instruct the gem on how to diff
129+
your object. To do this, you can use a configuration block. Simply add this to
130+
your test helper file (either `rails_helper` or `spec_helper`):
131+
132+
``` ruby
133+
SuperDiff::RSpec.configure do |config|
134+
config.extra_differ_classes << YourDiffer
135+
config.extra_operational_sequencer_classes << YourOperationalSequencer
136+
config.extra_diff_formatter_classes << YourDiffFormatter
137+
end
138+
```
139+
140+
*(More info here in the future on adding a custom differ, operational sequencer,
141+
and diff formatter. Also explanations on what these are.)*
144142

145143
## Contributing
146144

147-
If you encounter a bug or have an idea for how this could be better, I'm all
148-
ears! Feel free to create an issue.
145+
If you encounter a bug or have an idea for how this could be better, feel free
146+
to create an issue.
149147

150148
If you'd like to submit a PR instead, here's how to get started. First, fork
151149
this repo and then run:
@@ -169,6 +167,13 @@ bundle exec rspec spec/unit/...
169167

170168
Finally, submit your PR and I'll take a look at it when I get a chance.
171169

170+
## Compatibility
171+
172+
`super_diff` is [tested][travis] to work with RSpec 3.x, Ruby >= 2.4.x, and
173+
JRuby >= 9.2.x.
174+
175+
[travis]: http://travis-ci.org/mcmire/super_diff
176+
172177
## Copyright/License
173178

174-
© 2018 Elliot Winkler, released under the [MIT license](LICENSE).
179+
© 2018-2019 Elliot Winkler, released under the [MIT license](LICENSE).

doc/after_super_diff.png

557 KB
Loading

doc/before_super_diff.png

561 KB
Loading

0 commit comments

Comments
 (0)