super_diff 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8efa30260e2d32c10ea6571d37a393756b216c1ba2c991d28a4124a0822c7e6f
4
- data.tar.gz: a98a146c39eda8aeb3735869ffadacb279c26b76f2bf3f4b2ebc212fd6e8dbe1
3
+ metadata.gz: 7325f6b92497f0f81a67431c8e6581fc165b3f3dff4f01b2d18bcfabaab648ec
4
+ data.tar.gz: c358e94ecaab9d20b22cb7f3d60315057b6cdf1e415a902d1ae402529344beb4
5
5
  SHA512:
6
- metadata.gz: a0fdf3017f5a38865bb8cfbb15dfacf3a070f42c23883a6821b39a4b2bb00da3c9beb1e65c417b5094693ab14de18edf9934f23c7f242849cc2dfb950ee60649
7
- data.tar.gz: 6484896862126281d2f051d7ef17b760426083bfba3f9ba7f93936f331dca3cee187cbd8e75e46e6a9f86cbcd17d38e47fd9c2096a7ffdb6dc4290bed763c92f
6
+ metadata.gz: f32b832ab5647a3f6a73319e4a95e5b0ee3e346046c0b1ac4046169a439d9474d6f1ac37c791a9851f66070f82941d1a3520fdf477a57c828bf4945edab1994b
7
+ data.tar.gz: 042406c46094b4c0d9b5b1c88870e899672851b9a03f31cfec702d2119e5a73db6f23209e206a0b9b450336d288c2c62c1dfe62cb58f1701ad24498dd7540098
data/README.md CHANGED
@@ -7,32 +7,33 @@
7
7
  [hound-badge]: https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg
8
8
  [hound]: https://houndci.com
9
9
 
10
- SuperDiff is a gem that hooks into RSpec to intelligently display the
11
- differences between two data structures of any type.
10
+ SuperDiff is a gem that hooks into RSpec
11
+ to intelligently display the differences between two data structures of any type.
12
12
 
13
- 📢 **[See what's changed in the latest version (0.3.0)][changelog].**
13
+ 📢 **[See what's changed in the latest version (0.4.1)][changelog].**
14
14
 
15
15
  [changelog]: CHANGELOG.md
16
16
 
17
17
  ## Introduction
18
18
 
19
- The primary motivation behind this gem is to vastly improve upon RSpec's
20
- built-in diffing capabilities.
19
+ The primary motivation behind this gem
20
+ is to vastly improve upon RSpec's built-in diffing capabilities.
21
21
 
22
- Sometimes, whenever you use a matcher such as `eq`, `match`, `include`, or
23
- `have_attributes`, you will get a diff of the two data structures you are trying
24
- to match against. This is great if all you want to do is compare multi-line
25
- strings. But if you want to compare other, more "real world" kinds of values
26
- nested data structures (arrays, hashes, and full-scale objects), such as what
27
- you might work with when developing API endpoints or testing methods that make
28
- database calls and return a set of model objects — then you are out of luck.
29
- Since [RSpec merely runs your `expected` and `actual` values through Ruby's
30
- PrettyPrinter library][rspec-differ-fail] and then performs a diff of these
31
- strings, the output it produces leaves much to be desired.
22
+ Sometimes, whenever you use a matcher such as `eq`, `match`, `include`, or `have_attributes`,
23
+ you will get a diff of the two data structures you are trying to match against.
24
+ This is great if all you want to do is compare multi-line strings.
25
+ But if you want to compare other, more "real world" kinds of values,
26
+ such as what you might work with when developing API endpoints
27
+ or testing methods that make database calls and return a set of model objects,
28
+ then you are out of luck.
29
+ Since [RSpec merely runs your `expected` and `actual` values through Ruby's PrettyPrinter library][rspec-differ-fail]
30
+ and then performs a diff of these strings,
31
+ the output it produces leaves much to be desired.
32
32
 
33
33
  [rspec-differ-fail]: https://github.com/rspec/rspec-support/blob/c69a231d7369dd165ad7ce4742e1a2e21e3462b5/lib/rspec/support/differ.rb#L178
34
34
 
35
- For instance, let's say you wanted to compare these two hashes:
35
+ For instance,
36
+ let's say you wanted to compare these two hashes:
36
37
 
37
38
  ``` ruby
38
39
  actual = {
@@ -86,36 +87,60 @@ You would get output that looks like this:
86
87
 
87
88
  ![Before super_diff](doc/before.png)
88
89
 
89
- What this library does is to provide a diff engine that knows how to figure out
90
- the differences between any two data structures and display them in a sensible
91
- way. So, using the example above, you'd get this instead:
90
+ What this library does
91
+ is to provide a diff engine
92
+ that knows how to figure out the differences between any two data structures
93
+ and display them in a sensible way.
94
+ So, using the example above,
95
+ you'd get this instead:
92
96
 
93
97
  ![After super_diff](doc/after.png)
94
98
 
95
99
  ## Installation
96
100
 
97
- Want to try out this gem for yourself? As with most development-related gems,
98
- there are a couple ways depending on your type of project:
101
+ There are a few different ways to install `super_diff`
102
+ depending on your type of project.
99
103
 
100
104
  ### Rails apps
101
105
 
102
- If you're developing a Rails app, add the following to your Gemfile:
106
+ If you're developing a Rails app,
107
+ add the following to your Gemfile:
103
108
 
104
109
  ``` ruby
105
- gem "super_diff"
110
+ group :test do
111
+ gem "super_diff"
112
+ end
106
113
  ```
107
114
 
108
- After running `bundle install`, add the following to your `rails_helper`:
115
+ After running `bundle install`,
116
+ add the following to your `rails_helper`:
109
117
 
110
118
  ``` ruby
111
119
  require "super_diff/rspec-rails"
112
120
  ```
113
121
 
114
- You're done!
122
+ ### Projects using some part of Rails (e.g. ActiveModel)
115
123
 
116
- ### Libraries
124
+ If you're developing an app using Hanami or Sinatra,
125
+ or merely using a part of Rails such as ActiveModel,
126
+ add the following to your Gemfile where appropriate:
117
127
 
118
- If you're developing a library, add the following to your gemspec:
128
+ ``` ruby
129
+ gem "super_diff"
130
+ ```
131
+
132
+ After running `bundle install`,
133
+ add the following to your `spec_helper`:
134
+
135
+ ``` ruby
136
+ require "super_diff/rspec"
137
+ require "super_diff/active_support"
138
+ ```
139
+
140
+ ### Gems
141
+
142
+ If you're developing a gem,
143
+ add the following to your gemspec:
119
144
 
120
145
  ``` ruby
121
146
  spec.add_development_dependency "super_diff"
@@ -127,14 +152,16 @@ Now add the following to your `spec_helper`:
127
152
  require "super_diff/rspec"
128
153
  ```
129
154
 
130
- You're done!
131
-
132
155
  ## Configuration
133
156
 
134
- As capable as this library is, it doesn't know how to deal with every kind of
135
- object out there. You might find it necessary to instruct the gem on how to diff
136
- your object. To do this, you can use a configuration block. Simply add this to
137
- your test helper file (`rails_helper` or `spec_helper`):
157
+ As capable as this library is,
158
+ it doesn't know how to deal with every kind of object out there.
159
+ If you have a custom class,
160
+ and instances of your class aren't appearing in diffs like you like,
161
+ you might find it necessary to instruct the gem on how to handle them.
162
+ In that case
163
+ you would add something like this to your test helper file
164
+ (`rails_helper` or `spec_helper`):
138
165
 
139
166
  ``` ruby
140
167
  SuperDiff::RSpec.configure do |config|
@@ -144,55 +171,45 @@ SuperDiff::RSpec.configure do |config|
144
171
  end
145
172
  ```
146
173
 
147
- *(More info here in the future on adding a custom differ, operational sequencer,
148
- and diff formatter. Also explanations on what these are.)*
174
+ *(More info here in the future on adding a custom differ, operational sequencer, and diff formatter.
175
+ Also explanations on what these are.)*
149
176
 
150
- ## Contributing
151
-
152
- If you encounter a bug or have an idea for how this could be better, feel free
153
- to [create an issue](https://github.com/mcmire/super_diff/issues).
154
-
155
- If you'd like to submit a PR instead, here's how to get started. First, fork
156
- this repo. Then, when you've cloned your fork, run:
157
-
158
- ```
159
- bin/setup
160
- ```
177
+ ## Support
161
178
 
162
- This will install various dependencies. After this, you can run all of the
163
- tests:
179
+ My goal for this library is to improve your development experience.
180
+ If this is not the case,
181
+ and you encounter a bug or have a suggestion,
182
+ feel free to [create an issue][issues-list].
183
+ I'll try to respond to it as soon as I can!
164
184
 
165
- ```
166
- bundle exec rake
167
- ```
168
-
169
- If you update one of the tests, you can run it like so:
185
+ [issues-list]: https://github.com/mcmire/super_diff/issues
170
186
 
171
- ```
172
- bin/rspec spec/integration/...
173
- bin/rspec spec/unit/...
174
- ```
187
+ ## Contributing
175
188
 
176
- Finally, submit your PR and I'll take a look at it when I get a chance.
189
+ Any contributions to improve this library are welcome!
190
+ Please see the [contributing](./CONTRIBUTING.md) document for more on how to do that.
177
191
 
178
192
  ## Compatibility
179
193
 
180
- `super_diff` is [tested][travis] to work with Ruby >= 2.4.x, RSpec 3.x, and
181
- Rails >= 5.x.
194
+ `super_diff` is [tested][travis] to work with
195
+ Ruby >= 2.4.x,
196
+ RSpec 3.x,
197
+ and Rails >= 5.x.
182
198
 
183
199
  [travis]: http://travis-ci.org/mcmire/super_diff
184
200
 
185
201
  ## Inspiration/Thanks
186
202
 
187
- In developing this gem I made use of or was heavily inspired by these libraries:
203
+ In developing this gem
204
+ I made use of or was heavily inspired by these libraries:
188
205
 
189
- * [Diff::LCS][diff-lcs], the library I started with in the [original version of
190
- this gem][original-version] (made in 2011!)
191
- * The pretty-printing algorithms and API within [PrettyPrinter][pretty-printer]
192
- and [AwesomePrint][awesome-print], from which I borrowed ideas to develop
193
- the [inspectors][inspection-tree].
206
+ * [Diff::LCS][diff-lcs],
207
+ the library I started with in the [original version of this gem][original-version]
208
+ (made in 2011!)
209
+ * The pretty-printing algorithms and API within [PrettyPrinter][pretty-printer] and [AwesomePrint][awesome-print],
210
+ from which I borrowed ideas to develop the [inspectors][inspection-tree].
194
211
 
195
- Thank you so much!
212
+ Thank you to the authors of these libraries!
196
213
 
197
214
  [original-version]: https://github.com/mcmire/super_diff/tree/old-master
198
215
  [diff-lcs]: https://github.com/halostatue/diff-lcs
@@ -200,6 +217,7 @@ Thank you so much!
200
217
  [awesome-print]: https://github.com/awesome-print/awesome_print
201
218
  [inspection-tree]: https://github.com/mcmire/super_diff/blob/master/lib/super_diff/object_inspection/inspection_tree.rb
202
219
 
203
- ## Copyright/License
220
+ ## Author/License
204
221
 
205
- © 2018-2019 Elliot Winkler, released under the [MIT license](LICENSE).
222
+ `super_diff` was created and is maintained by Elliot Winkler.
223
+ It is released under the [MIT license](LICENSE).
@@ -91,10 +91,12 @@ module RSpec
91
91
  @skip_shared_group_trace = options.fetch(:skip_shared_group_trace, false)
92
92
  # Patch to convert options[:failure_lines] to groups
93
93
  if options.include?(:failure_lines)
94
- @failure_line_groups = {
95
- lines: options[:failure_lines],
96
- already_colorized: false
97
- }
94
+ @failure_line_groups = [
95
+ {
96
+ lines: options[:failure_lines],
97
+ already_colorized: false
98
+ }
99
+ ]
98
100
  end
99
101
  end
100
102
 
@@ -1,3 +1,3 @@
1
1
  module SuperDiff
2
- VERSION = "0.4.0".freeze
2
+ VERSION = "0.4.1".freeze
3
3
  end
data/spec/examples.txt ADDED
@@ -0,0 +1,5 @@
1
+ example_id | status | run_time |
2
+ ---------------------------------------------------------- | ------ | -------------- |
3
+ ./spec/integration/rspec/unhandled_errors_spec.rb[1:1:1:1] | passed | 1.28 seconds |
4
+ ./spec/integration/rspec/unhandled_errors_spec.rb[1:1:2:1] | passed | 1.31 seconds |
5
+ ./spec/integration/rspec/unhandled_errors_spec.rb[1:2:1] | failed | 0.7241 seconds |
@@ -66,4 +66,25 @@ RSpec.describe "Integration with RSpec and unhandled errors", type: :integration
66
66
  end
67
67
  end
68
68
  end
69
+
70
+ context "when multiple exception occur" do
71
+ it "highlights the first line in red, and then leaves the rest of the message alone" do
72
+ as_both_colored_and_uncolored do |color_enabled|
73
+ program = <<~PROGRAM.strip
74
+ #{set_up_with("super_diff/rspec", color_enabled: color_enabled)}
75
+ RSpec.describe "test" do
76
+ after(:each) do
77
+ raise "Some kind of after error or whatever\\n\\nThis is another line"
78
+ end
79
+ it "passes" do
80
+ raise "Some kind of error or whatever\\n\\nThis is another line"
81
+ end
82
+ end
83
+ PROGRAM
84
+
85
+ expect(program).
86
+ to produce_output_when_run('Some kind of after error or whatever')
87
+ end
88
+ end
89
+ end
69
90
  end
@@ -1,115 +1,373 @@
1
1
  shared_examples_for "integration with HashWithIndifferentAccess" do
2
2
  describe "and RSpec's #eq matcher" do
3
3
  context "when the actual value is a HashWithIndifferentAccess" do
4
- it "produces the correct output" do
5
- as_both_colored_and_uncolored do |color_enabled|
6
- snippet = <<~TEST.strip
7
- expected = {
8
- line_1: "123 Main St.",
9
- city: "Hill Valley",
10
- state: "CA",
11
- zip: "90382",
12
- }
13
- actual = HashWithIndifferentAccess.new({
14
- line_1: "456 Ponderosa Ct.",
15
- city: "Oakland",
16
- state: "CA",
17
- zip: "91234",
18
- })
19
- expect(actual).to eq(expected)
20
- TEST
21
- program = make_rspec_rails_test_program(
22
- snippet,
23
- color_enabled: color_enabled,
24
- )
25
-
26
- expected_output = build_expected_output(
27
- color_enabled: color_enabled,
28
- snippet: "expect(actual).to eq(expected)",
29
- expectation: proc {
30
- line do
31
- plain "Expected "
32
- beta %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
33
- end
34
-
35
- line do
36
- plain " to eq "
37
- alpha %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
38
- end
39
- },
40
- diff: proc {
41
- plain_line %| #<HashWithIndifferentAccess {|
42
- alpha_line %|- "line_1" => "123 Main St.",|
43
- beta_line %|+ "line_1" => "456 Ponderosa Ct.",|
44
- alpha_line %|- "city" => "Hill Valley",|
45
- beta_line %|+ "city" => "Oakland",|
46
- plain_line %| "state" => "CA",|
47
- alpha_line %|- "zip" => "90382"|
48
- beta_line %|+ "zip" => "91234"|
49
- plain_line %| }>|
50
- },
51
- )
52
-
53
- expect(program).
54
- to produce_output_when_run(expected_output).
55
- in_color(color_enabled)
4
+ context "and both hashes are one-dimensional" do
5
+ context "and the expected hash contains symbol keys" do
6
+ it "produces the correct output" do
7
+ as_both_colored_and_uncolored do |color_enabled|
8
+ snippet = <<~TEST.strip
9
+ expected = {
10
+ line_1: "123 Main St.",
11
+ city: "Hill Valley",
12
+ state: "CA",
13
+ zip: "90382",
14
+ }
15
+ actual = HashWithIndifferentAccess.new({
16
+ line_1: "456 Ponderosa Ct.",
17
+ city: "Oakland",
18
+ state: "CA",
19
+ zip: "91234",
20
+ })
21
+ expect(actual).to eq(expected)
22
+ TEST
23
+ program = make_rspec_rails_test_program(
24
+ snippet,
25
+ color_enabled: color_enabled,
26
+ )
27
+
28
+ expected_output = build_expected_output(
29
+ color_enabled: color_enabled,
30
+ snippet: "expect(actual).to eq(expected)",
31
+ expectation: proc {
32
+ line do
33
+ plain "Expected "
34
+ beta %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
35
+ end
36
+
37
+ line do
38
+ plain " to eq "
39
+ alpha %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
40
+ end
41
+ },
42
+ diff: proc {
43
+ plain_line %| #<HashWithIndifferentAccess {|
44
+ alpha_line %|- "line_1" => "123 Main St.",|
45
+ beta_line %|+ "line_1" => "456 Ponderosa Ct.",|
46
+ alpha_line %|- "city" => "Hill Valley",|
47
+ beta_line %|+ "city" => "Oakland",|
48
+ plain_line %| "state" => "CA",|
49
+ alpha_line %|- "zip" => "90382"|
50
+ beta_line %|+ "zip" => "91234"|
51
+ plain_line %| }>|
52
+ },
53
+ )
54
+
55
+ expect(program).
56
+ to produce_output_when_run(expected_output).
57
+ in_color(color_enabled)
58
+ end
59
+ end
60
+ end
61
+
62
+ context "and the expected hash contains string keys" do
63
+ it "produces the correct output" do
64
+ as_both_colored_and_uncolored do |color_enabled|
65
+ snippet = <<~TEST.strip
66
+ expected = {
67
+ "line_1" => "123 Main St.",
68
+ "city" => "Hill Valley",
69
+ "state" => "CA",
70
+ "zip" => "90382",
71
+ }
72
+ actual = HashWithIndifferentAccess.new({
73
+ line_1: "456 Ponderosa Ct.",
74
+ city: "Oakland",
75
+ state: "CA",
76
+ zip: "91234",
77
+ })
78
+ expect(actual).to eq(expected)
79
+ TEST
80
+ program = make_rspec_rails_test_program(
81
+ snippet,
82
+ color_enabled: color_enabled,
83
+ )
84
+
85
+ expected_output = build_expected_output(
86
+ color_enabled: color_enabled,
87
+ snippet: "expect(actual).to eq(expected)",
88
+ expectation: proc {
89
+ line do
90
+ plain "Expected "
91
+ beta %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
92
+ end
93
+
94
+ line do
95
+ plain " to eq "
96
+ alpha %|{ "line_1" => "123 Main St.", "city" => "Hill Valley", "state" => "CA", "zip" => "90382" }|
97
+ end
98
+ },
99
+ diff: proc {
100
+ plain_line %| #<HashWithIndifferentAccess {|
101
+ alpha_line %|- "line_1" => "123 Main St.",|
102
+ beta_line %|+ "line_1" => "456 Ponderosa Ct.",|
103
+ alpha_line %|- "city" => "Hill Valley",|
104
+ beta_line %|+ "city" => "Oakland",|
105
+ plain_line %| "state" => "CA",|
106
+ alpha_line %|- "zip" => "90382"|
107
+ beta_line %|+ "zip" => "91234"|
108
+ plain_line %| }>|
109
+ },
110
+ )
111
+
112
+ expect(program).
113
+ to produce_output_when_run(expected_output).
114
+ in_color(color_enabled)
115
+ end
116
+ end
56
117
  end
57
118
  end
58
119
  end
59
120
 
60
121
  context "when the expected value is a HashWithIndifferentAccess" do
61
- it "produces the correct output" do
62
- as_both_colored_and_uncolored do |color_enabled|
63
- snippet = <<~TEST.strip
64
- expected = HashWithIndifferentAccess.new({
65
- line_1: "456 Ponderosa Ct.",
66
- city: "Oakland",
67
- state: "CA",
68
- zip: "91234",
69
- })
70
- actual = {
71
- line_1: "123 Main St.",
72
- city: "Hill Valley",
73
- state: "CA",
74
- zip: "90382",
75
- }
76
- expect(actual).to eq(expected)
77
- TEST
78
- program = make_rspec_rails_test_program(
79
- snippet,
80
- color_enabled: color_enabled,
81
- )
82
-
83
- expected_output = build_expected_output(
84
- color_enabled: color_enabled,
85
- snippet: "expect(actual).to eq(expected)",
86
- expectation: proc {
87
- line do
88
- plain "Expected "
89
- beta %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
90
- end
91
-
92
- line do
93
- plain " to eq "
94
- alpha %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
95
- end
96
- },
97
- diff: proc {
98
- plain_line %| #<HashWithIndifferentAccess {|
99
- alpha_line %|- "line_1" => "456 Ponderosa Ct.",|
100
- beta_line %|+ "line_1" => "123 Main St.",|
101
- alpha_line %|- "city" => "Oakland",|
102
- beta_line %|+ "city" => "Hill Valley",|
103
- plain_line %| "state" => "CA",|
104
- alpha_line %|- "zip" => "91234"|
105
- beta_line %|+ "zip" => "90382"|
106
- plain_line %| }>|
107
- },
108
- )
109
-
110
- expect(program).
111
- to produce_output_when_run(expected_output).
112
- in_color(color_enabled)
122
+ context "and both hashes are one-dimensional" do
123
+ context "and the actual hash contains symbol keys" do
124
+ it "produces the correct output" do
125
+ as_both_colored_and_uncolored do |color_enabled|
126
+ snippet = <<~TEST.strip
127
+ expected = HashWithIndifferentAccess.new({
128
+ line_1: "456 Ponderosa Ct.",
129
+ city: "Oakland",
130
+ state: "CA",
131
+ zip: "91234",
132
+ })
133
+ actual = {
134
+ line_1: "123 Main St.",
135
+ city: "Hill Valley",
136
+ state: "CA",
137
+ zip: "90382",
138
+ }
139
+ expect(actual).to eq(expected)
140
+ TEST
141
+ program = make_rspec_rails_test_program(
142
+ snippet,
143
+ color_enabled: color_enabled,
144
+ )
145
+
146
+ expected_output = build_expected_output(
147
+ color_enabled: color_enabled,
148
+ snippet: "expect(actual).to eq(expected)",
149
+ expectation: proc {
150
+ line do
151
+ plain "Expected "
152
+ beta %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
153
+ end
154
+
155
+ line do
156
+ plain " to eq "
157
+ alpha %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
158
+ end
159
+ },
160
+ diff: proc {
161
+ plain_line %| #<HashWithIndifferentAccess {|
162
+ alpha_line %|- "line_1" => "456 Ponderosa Ct.",|
163
+ beta_line %|+ "line_1" => "123 Main St.",|
164
+ alpha_line %|- "city" => "Oakland",|
165
+ beta_line %|+ "city" => "Hill Valley",|
166
+ plain_line %| "state" => "CA",|
167
+ alpha_line %|- "zip" => "91234"|
168
+ beta_line %|+ "zip" => "90382"|
169
+ plain_line %| }>|
170
+ },
171
+ )
172
+
173
+ expect(program).
174
+ to produce_output_when_run(expected_output).
175
+ in_color(color_enabled)
176
+ end
177
+ end
178
+ end
179
+
180
+ context "and the actual hash contains string keys" do
181
+ it "produces the correct output" do
182
+ as_both_colored_and_uncolored do |color_enabled|
183
+ snippet = <<~TEST.strip
184
+ expected = HashWithIndifferentAccess.new({
185
+ line_1: "456 Ponderosa Ct.",
186
+ city: "Oakland",
187
+ state: "CA",
188
+ zip: "91234",
189
+ })
190
+ actual = {
191
+ "line_1" => "123 Main St.",
192
+ "city" => "Hill Valley",
193
+ "state" => "CA",
194
+ "zip" => "90382",
195
+ }
196
+ expect(actual).to eq(expected)
197
+ TEST
198
+ program = make_rspec_rails_test_program(
199
+ snippet,
200
+ color_enabled: color_enabled,
201
+ )
202
+
203
+ expected_output = build_expected_output(
204
+ color_enabled: color_enabled,
205
+ snippet: "expect(actual).to eq(expected)",
206
+ expectation: proc {
207
+ line do
208
+ plain "Expected "
209
+ beta %|{ "line_1" => "123 Main St.", "city" => "Hill Valley", "state" => "CA", "zip" => "90382" }|
210
+ end
211
+
212
+ line do
213
+ plain " to eq "
214
+ alpha %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
215
+ end
216
+ },
217
+ diff: proc {
218
+ plain_line %| #<HashWithIndifferentAccess {|
219
+ alpha_line %|- "line_1" => "456 Ponderosa Ct.",|
220
+ beta_line %|+ "line_1" => "123 Main St.",|
221
+ alpha_line %|- "city" => "Oakland",|
222
+ beta_line %|+ "city" => "Hill Valley",|
223
+ plain_line %| "state" => "CA",|
224
+ alpha_line %|- "zip" => "91234"|
225
+ beta_line %|+ "zip" => "90382"|
226
+ plain_line %| }>|
227
+ },
228
+ )
229
+
230
+ expect(program).
231
+ to produce_output_when_run(expected_output).
232
+ in_color(color_enabled)
233
+ end
234
+ end
235
+ end
236
+ end
237
+
238
+ context "and both hashes are multi-dimensional" do
239
+ context "and the actual hash contains symbol keys" do
240
+ it "produces the correct output" do
241
+ as_both_colored_and_uncolored do |color_enabled|
242
+ snippet = <<~TEST.strip
243
+ expected = HashWithIndifferentAccess.new({
244
+ shipments: [
245
+ HashWithIndifferentAccess.new({
246
+ estimated_delivery: HashWithIndifferentAccess.new({
247
+ from: '2019-05-06',
248
+ to: '2019-05-06'
249
+ })
250
+ })
251
+ ]
252
+ })
253
+ actual = {
254
+ shipments: [
255
+ {
256
+ estimated_delivery: {
257
+ from: '2019-05-06',
258
+ to: '2019-05-09'
259
+ }
260
+ }
261
+ ]
262
+ }
263
+ expect(actual).to eq(expected)
264
+ TEST
265
+ program = make_rspec_rails_test_program(
266
+ snippet,
267
+ color_enabled: color_enabled,
268
+ )
269
+
270
+ expected_output = build_expected_output(
271
+ color_enabled: color_enabled,
272
+ snippet: "expect(actual).to eq(expected)",
273
+ expectation: proc {
274
+ line do
275
+ plain "Expected "
276
+ beta %|{ shipments: [{ estimated_delivery: { from: "2019-05-06", to: "2019-05-09" } }] }|
277
+ end
278
+
279
+ line do
280
+ plain " to eq "
281
+ alpha %|#<HashWithIndifferentAccess { "shipments" => [#<HashWithIndifferentAccess { "estimated_delivery" => #<HashWithIndifferentAccess { "from" => "2019-05-06", "to" => "2019-05-06" }> }>] }>|
282
+ end
283
+ },
284
+ diff: proc {
285
+ plain_line %| #<HashWithIndifferentAccess {|
286
+ plain_line %| "shipments" => [|
287
+ plain_line %| {|
288
+ plain_line %| "estimated_delivery" => {|
289
+ plain_line %| "from" => "2019-05-06",|
290
+ alpha_line %|- "to" => "2019-05-06"|
291
+ beta_line %|+ "to" => "2019-05-09"|
292
+ plain_line %| }|
293
+ plain_line %| }|
294
+ plain_line %| ]|
295
+ plain_line %| }>|
296
+ },
297
+ )
298
+
299
+ expect(program).
300
+ to produce_output_when_run(expected_output).
301
+ in_color(color_enabled)
302
+ end
303
+ end
304
+ end
305
+
306
+ context "and the actual hash contains string keys" do
307
+ it "produces the correct output" do
308
+ as_both_colored_and_uncolored do |color_enabled|
309
+ snippet = <<~TEST.strip
310
+ expected = HashWithIndifferentAccess.new({
311
+ shipments: [
312
+ HashWithIndifferentAccess.new({
313
+ estimated_delivery: HashWithIndifferentAccess.new({
314
+ from: '2019-05-06',
315
+ to: '2019-05-06'
316
+ })
317
+ })
318
+ ]
319
+ })
320
+ actual = {
321
+ 'shipments' => [
322
+ {
323
+ 'estimated_delivery' => {
324
+ 'from' => '2019-05-06',
325
+ 'to' => '2019-05-09'
326
+ }
327
+ }
328
+ ]
329
+ }
330
+ expect(actual).to eq(expected)
331
+ TEST
332
+ program = make_rspec_rails_test_program(
333
+ snippet,
334
+ color_enabled: color_enabled,
335
+ )
336
+
337
+ expected_output = build_expected_output(
338
+ color_enabled: color_enabled,
339
+ snippet: "expect(actual).to eq(expected)",
340
+ expectation: proc {
341
+ line do
342
+ plain "Expected "
343
+ beta %|{ "shipments" => [{ "estimated_delivery" => { "from" => "2019-05-06", "to" => "2019-05-09" } }] }|
344
+ end
345
+
346
+ line do
347
+ plain " to eq "
348
+ alpha %|#<HashWithIndifferentAccess { "shipments" => [#<HashWithIndifferentAccess { "estimated_delivery" => #<HashWithIndifferentAccess { "from" => "2019-05-06", "to" => "2019-05-06" }> }>] }>|
349
+ end
350
+ },
351
+ diff: proc {
352
+ plain_line %| #<HashWithIndifferentAccess {|
353
+ plain_line %| "shipments" => [|
354
+ plain_line %| {|
355
+ plain_line %| "estimated_delivery" => {|
356
+ plain_line %| "from" => "2019-05-06",|
357
+ alpha_line %|- "to" => "2019-05-06"|
358
+ beta_line %|+ "to" => "2019-05-09"|
359
+ plain_line %| }|
360
+ plain_line %| }|
361
+ plain_line %| ]|
362
+ plain_line %| }>|
363
+ },
364
+ )
365
+
366
+ expect(program).
367
+ to produce_output_when_run(expected_output).
368
+ in_color(color_enabled)
369
+ end
370
+ end
113
371
  end
114
372
  end
115
373
  end
@@ -117,115 +375,373 @@ shared_examples_for "integration with HashWithIndifferentAccess" do
117
375
 
118
376
  describe "and RSpec's #match matcher" do
119
377
  context "when the actual value is a HashWithIndifferentAccess" do
120
- it "produces the correct output" do
121
- as_both_colored_and_uncolored do |color_enabled|
122
- snippet = <<~TEST.strip
123
- expected = {
124
- line_1: "123 Main St.",
125
- city: "Hill Valley",
126
- state: "CA",
127
- zip: "90382",
128
- }
129
- actual = HashWithIndifferentAccess.new({
130
- line_1: "456 Ponderosa Ct.",
131
- city: "Oakland",
132
- state: "CA",
133
- zip: "91234",
134
- })
135
- expect(actual).to match(expected)
136
- TEST
137
- program = make_rspec_rails_test_program(
138
- snippet,
139
- color_enabled: color_enabled,
140
- )
141
-
142
- expected_output = build_expected_output(
143
- color_enabled: color_enabled,
144
- snippet: "expect(actual).to match(expected)",
145
- expectation: proc {
146
- line do
147
- plain "Expected "
148
- beta %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
149
- end
150
-
151
- line do
152
- plain "to match "
153
- alpha %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
154
- end
155
- },
156
- diff: proc {
157
- plain_line %| #<HashWithIndifferentAccess {|
158
- alpha_line %|- "line_1" => "123 Main St.",|
159
- beta_line %|+ "line_1" => "456 Ponderosa Ct.",|
160
- alpha_line %|- "city" => "Hill Valley",|
161
- beta_line %|+ "city" => "Oakland",|
162
- plain_line %| "state" => "CA",|
163
- alpha_line %|- "zip" => "90382"|
164
- beta_line %|+ "zip" => "91234"|
165
- plain_line %| }>|
166
- },
167
- )
168
-
169
- expect(program).
170
- to produce_output_when_run(expected_output).
171
- in_color(color_enabled)
378
+ context "and both hashes are one-dimensional" do
379
+ context "and the expected hash contains symbol keys" do
380
+ it "produces the correct output" do
381
+ as_both_colored_and_uncolored do |color_enabled|
382
+ snippet = <<~TEST.strip
383
+ expected = {
384
+ line_1: "123 Main St.",
385
+ city: "Hill Valley",
386
+ state: "CA",
387
+ zip: "90382",
388
+ }
389
+ actual = HashWithIndifferentAccess.new({
390
+ line_1: "456 Ponderosa Ct.",
391
+ city: "Oakland",
392
+ state: "CA",
393
+ zip: "91234",
394
+ })
395
+ expect(actual).to match(expected)
396
+ TEST
397
+ program = make_rspec_rails_test_program(
398
+ snippet,
399
+ color_enabled: color_enabled,
400
+ )
401
+
402
+ expected_output = build_expected_output(
403
+ color_enabled: color_enabled,
404
+ snippet: "expect(actual).to match(expected)",
405
+ expectation: proc {
406
+ line do
407
+ plain "Expected "
408
+ beta %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
409
+ end
410
+
411
+ line do
412
+ plain "to match "
413
+ alpha %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
414
+ end
415
+ },
416
+ diff: proc {
417
+ plain_line %| #<HashWithIndifferentAccess {|
418
+ alpha_line %|- "line_1" => "123 Main St.",|
419
+ beta_line %|+ "line_1" => "456 Ponderosa Ct.",|
420
+ alpha_line %|- "city" => "Hill Valley",|
421
+ beta_line %|+ "city" => "Oakland",|
422
+ plain_line %| "state" => "CA",|
423
+ alpha_line %|- "zip" => "90382"|
424
+ beta_line %|+ "zip" => "91234"|
425
+ plain_line %| }>|
426
+ },
427
+ )
428
+
429
+ expect(program).
430
+ to produce_output_when_run(expected_output).
431
+ in_color(color_enabled)
432
+ end
433
+ end
434
+ end
435
+
436
+ context "and the expected hash contains string keys" do
437
+ it "produces the correct output" do
438
+ as_both_colored_and_uncolored do |color_enabled|
439
+ snippet = <<~TEST.strip
440
+ expected = {
441
+ "line_1" => "123 Main St.",
442
+ "city" => "Hill Valley",
443
+ "state" => "CA",
444
+ "zip" => "90382",
445
+ }
446
+ actual = HashWithIndifferentAccess.new({
447
+ line_1: "456 Ponderosa Ct.",
448
+ city: "Oakland",
449
+ state: "CA",
450
+ zip: "91234",
451
+ })
452
+ expect(actual).to match(expected)
453
+ TEST
454
+ program = make_rspec_rails_test_program(
455
+ snippet,
456
+ color_enabled: color_enabled,
457
+ )
458
+
459
+ expected_output = build_expected_output(
460
+ color_enabled: color_enabled,
461
+ snippet: "expect(actual).to match(expected)",
462
+ expectation: proc {
463
+ line do
464
+ plain "Expected "
465
+ beta %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
466
+ end
467
+
468
+ line do
469
+ plain "to match "
470
+ alpha %|{ "line_1" => "123 Main St.", "city" => "Hill Valley", "state" => "CA", "zip" => "90382" }|
471
+ end
472
+ },
473
+ diff: proc {
474
+ plain_line %| #<HashWithIndifferentAccess {|
475
+ alpha_line %|- "line_1" => "123 Main St.",|
476
+ beta_line %|+ "line_1" => "456 Ponderosa Ct.",|
477
+ alpha_line %|- "city" => "Hill Valley",|
478
+ beta_line %|+ "city" => "Oakland",|
479
+ plain_line %| "state" => "CA",|
480
+ alpha_line %|- "zip" => "90382"|
481
+ beta_line %|+ "zip" => "91234"|
482
+ plain_line %| }>|
483
+ },
484
+ )
485
+
486
+ expect(program).
487
+ to produce_output_when_run(expected_output).
488
+ in_color(color_enabled)
489
+ end
490
+ end
172
491
  end
173
492
  end
174
493
  end
175
494
 
176
495
  context "when the expected value is a HashWithIndifferentAccess" do
177
- it "produces the correct output" do
178
- as_both_colored_and_uncolored do |color_enabled|
179
- snippet = <<~TEST.strip
180
- expected = HashWithIndifferentAccess.new({
181
- line_1: "456 Ponderosa Ct.",
182
- city: "Oakland",
183
- state: "CA",
184
- zip: "91234",
185
- })
186
- actual = {
187
- line_1: "123 Main St.",
188
- city: "Hill Valley",
189
- state: "CA",
190
- zip: "90382",
191
- }
192
- expect(actual).to match(expected)
193
- TEST
194
- program = make_rspec_rails_test_program(
195
- snippet,
196
- color_enabled: color_enabled,
197
- )
198
-
199
- expected_output = build_expected_output(
200
- color_enabled: color_enabled,
201
- snippet: "expect(actual).to match(expected)",
202
- expectation: proc {
203
- line do
204
- plain "Expected "
205
- beta %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
206
- end
207
-
208
- line do
209
- plain "to match "
210
- alpha %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
211
- end
212
- },
213
- diff: proc {
214
- plain_line %| #<HashWithIndifferentAccess {|
215
- alpha_line %|- "line_1" => "456 Ponderosa Ct.",|
216
- beta_line %|+ "line_1" => "123 Main St.",|
217
- alpha_line %|- "city" => "Oakland",|
218
- beta_line %|+ "city" => "Hill Valley",|
219
- plain_line %| "state" => "CA",|
220
- alpha_line %|- "zip" => "91234"|
221
- beta_line %|+ "zip" => "90382"|
222
- plain_line %| }>|
223
- },
224
- )
225
-
226
- expect(program).
227
- to produce_output_when_run(expected_output).
228
- in_color(color_enabled)
496
+ context "and both hashes are one-dimensional" do
497
+ context "and the actual hash contains symbol keys" do
498
+ it "produces the correct output" do
499
+ as_both_colored_and_uncolored do |color_enabled|
500
+ snippet = <<~TEST.strip
501
+ expected = HashWithIndifferentAccess.new({
502
+ line_1: "456 Ponderosa Ct.",
503
+ city: "Oakland",
504
+ state: "CA",
505
+ zip: "91234",
506
+ })
507
+ actual = {
508
+ line_1: "123 Main St.",
509
+ city: "Hill Valley",
510
+ state: "CA",
511
+ zip: "90382",
512
+ }
513
+ expect(actual).to match(expected)
514
+ TEST
515
+ program = make_rspec_rails_test_program(
516
+ snippet,
517
+ color_enabled: color_enabled,
518
+ )
519
+
520
+ expected_output = build_expected_output(
521
+ color_enabled: color_enabled,
522
+ snippet: "expect(actual).to match(expected)",
523
+ expectation: proc {
524
+ line do
525
+ plain "Expected "
526
+ beta %|{ line_1: "123 Main St.", city: "Hill Valley", state: "CA", zip: "90382" }|
527
+ end
528
+
529
+ line do
530
+ plain "to match "
531
+ alpha %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
532
+ end
533
+ },
534
+ diff: proc {
535
+ plain_line %| #<HashWithIndifferentAccess {|
536
+ alpha_line %|- "line_1" => "456 Ponderosa Ct.",|
537
+ beta_line %|+ "line_1" => "123 Main St.",|
538
+ alpha_line %|- "city" => "Oakland",|
539
+ beta_line %|+ "city" => "Hill Valley",|
540
+ plain_line %| "state" => "CA",|
541
+ alpha_line %|- "zip" => "91234"|
542
+ beta_line %|+ "zip" => "90382"|
543
+ plain_line %| }>|
544
+ },
545
+ )
546
+
547
+ expect(program).
548
+ to produce_output_when_run(expected_output).
549
+ in_color(color_enabled)
550
+ end
551
+ end
552
+ end
553
+
554
+ context "and the actual hash contains string keys" do
555
+ it "produces the correct output" do
556
+ as_both_colored_and_uncolored do |color_enabled|
557
+ snippet = <<~TEST.strip
558
+ expected = HashWithIndifferentAccess.new({
559
+ line_1: "456 Ponderosa Ct.",
560
+ city: "Oakland",
561
+ state: "CA",
562
+ zip: "91234",
563
+ })
564
+ actual = {
565
+ "line_1" => "123 Main St.",
566
+ "city" => "Hill Valley",
567
+ "state" => "CA",
568
+ "zip" => "90382",
569
+ }
570
+ expect(actual).to match(expected)
571
+ TEST
572
+ program = make_rspec_rails_test_program(
573
+ snippet,
574
+ color_enabled: color_enabled,
575
+ )
576
+
577
+ expected_output = build_expected_output(
578
+ color_enabled: color_enabled,
579
+ snippet: "expect(actual).to match(expected)",
580
+ expectation: proc {
581
+ line do
582
+ plain "Expected "
583
+ beta %|{ "line_1" => "123 Main St.", "city" => "Hill Valley", "state" => "CA", "zip" => "90382" }|
584
+ end
585
+
586
+ line do
587
+ plain "to match "
588
+ alpha %|#<HashWithIndifferentAccess { "line_1" => "456 Ponderosa Ct.", "city" => "Oakland", "state" => "CA", "zip" => "91234" }>|
589
+ end
590
+ },
591
+ diff: proc {
592
+ plain_line %| #<HashWithIndifferentAccess {|
593
+ alpha_line %|- "line_1" => "456 Ponderosa Ct.",|
594
+ beta_line %|+ "line_1" => "123 Main St.",|
595
+ alpha_line %|- "city" => "Oakland",|
596
+ beta_line %|+ "city" => "Hill Valley",|
597
+ plain_line %| "state" => "CA",|
598
+ alpha_line %|- "zip" => "91234"|
599
+ beta_line %|+ "zip" => "90382"|
600
+ plain_line %| }>|
601
+ },
602
+ )
603
+
604
+ expect(program).
605
+ to produce_output_when_run(expected_output).
606
+ in_color(color_enabled)
607
+ end
608
+ end
609
+ end
610
+ end
611
+
612
+ context "and both hashes are multi-dimensional" do
613
+ context "and the actual hash contains symbol keys" do
614
+ it "produces the correct output" do
615
+ as_both_colored_and_uncolored do |color_enabled|
616
+ snippet = <<~TEST.strip
617
+ expected = HashWithIndifferentAccess.new({
618
+ shipments: [
619
+ HashWithIndifferentAccess.new({
620
+ estimated_delivery: HashWithIndifferentAccess.new({
621
+ from: '2019-05-06',
622
+ to: '2019-05-06'
623
+ })
624
+ })
625
+ ]
626
+ })
627
+ actual = {
628
+ shipments: [
629
+ {
630
+ estimated_delivery: {
631
+ from: '2019-05-06',
632
+ to: '2019-05-09'
633
+ }
634
+ }
635
+ ]
636
+ }
637
+ expect(actual).to match(expected)
638
+ TEST
639
+ program = make_rspec_rails_test_program(
640
+ snippet,
641
+ color_enabled: color_enabled,
642
+ )
643
+
644
+ expected_output = build_expected_output(
645
+ color_enabled: color_enabled,
646
+ snippet: "expect(actual).to match(expected)",
647
+ expectation: proc {
648
+ line do
649
+ plain "Expected "
650
+ beta %|{ shipments: [{ estimated_delivery: { from: "2019-05-06", to: "2019-05-09" } }] }|
651
+ end
652
+
653
+ line do
654
+ plain "to match "
655
+ alpha %|#<HashWithIndifferentAccess { "shipments" => [#<HashWithIndifferentAccess { "estimated_delivery" => #<HashWithIndifferentAccess { "from" => "2019-05-06", "to" => "2019-05-06" }> }>] }>|
656
+ end
657
+ },
658
+ diff: proc {
659
+ plain_line %| #<HashWithIndifferentAccess {|
660
+ plain_line %| "shipments" => [|
661
+ plain_line %| {|
662
+ plain_line %| "estimated_delivery" => {|
663
+ plain_line %| "from" => "2019-05-06",|
664
+ alpha_line %|- "to" => "2019-05-06"|
665
+ beta_line %|+ "to" => "2019-05-09"|
666
+ plain_line %| }|
667
+ plain_line %| }|
668
+ plain_line %| ]|
669
+ plain_line %| }>|
670
+ },
671
+ )
672
+
673
+ expect(program).
674
+ to produce_output_when_run(expected_output).
675
+ in_color(color_enabled)
676
+ end
677
+ end
678
+ end
679
+
680
+ context "and the actual hash contains string keys" do
681
+ it "produces the correct output" do
682
+ as_both_colored_and_uncolored do |color_enabled|
683
+ snippet = <<~TEST.strip
684
+ expected = HashWithIndifferentAccess.new({
685
+ shipments: [
686
+ HashWithIndifferentAccess.new({
687
+ estimated_delivery: HashWithIndifferentAccess.new({
688
+ from: '2019-05-06',
689
+ to: '2019-05-06'
690
+ })
691
+ })
692
+ ]
693
+ })
694
+ actual = {
695
+ 'shipments' => [
696
+ {
697
+ 'estimated_delivery' => {
698
+ 'from' => '2019-05-06',
699
+ 'to' => '2019-05-09'
700
+ }
701
+ }
702
+ ]
703
+ }
704
+ expect(actual).to match(expected)
705
+ TEST
706
+ program = make_rspec_rails_test_program(
707
+ snippet,
708
+ color_enabled: color_enabled,
709
+ )
710
+
711
+ expected_output = build_expected_output(
712
+ color_enabled: color_enabled,
713
+ snippet: "expect(actual).to match(expected)",
714
+ expectation: proc {
715
+ line do
716
+ plain "Expected "
717
+ beta %|{ "shipments" => [{ "estimated_delivery" => { "from" => "2019-05-06", "to" => "2019-05-09" } }] }|
718
+ end
719
+
720
+ line do
721
+ plain "to match "
722
+ alpha %|#<HashWithIndifferentAccess { "shipments" => [#<HashWithIndifferentAccess { "estimated_delivery" => #<HashWithIndifferentAccess { "from" => "2019-05-06", "to" => "2019-05-06" }> }>] }>|
723
+ end
724
+ },
725
+ diff: proc {
726
+ plain_line %| #<HashWithIndifferentAccess {|
727
+ plain_line %| "shipments" => [|
728
+ plain_line %| {|
729
+ plain_line %| "estimated_delivery" => {|
730
+ plain_line %| "from" => "2019-05-06",|
731
+ alpha_line %|- "to" => "2019-05-06"|
732
+ beta_line %|+ "to" => "2019-05-09"|
733
+ plain_line %| }|
734
+ plain_line %| }|
735
+ plain_line %| ]|
736
+ plain_line %| }>|
737
+ },
738
+ )
739
+
740
+ expect(program).
741
+ to produce_output_when_run(expected_output).
742
+ in_color(color_enabled)
743
+ end
744
+ end
229
745
  end
230
746
  end
231
747
  end