super_diff 0.5.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +49 -22
  3. data/lib/super_diff.rb +25 -7
  4. data/lib/super_diff/colorized_document_extensions.rb +4 -4
  5. data/lib/super_diff/configuration.rb +32 -22
  6. data/lib/super_diff/csi.rb +2 -1
  7. data/lib/super_diff/diff_formatters/collection.rb +2 -2
  8. data/lib/super_diff/diff_formatters/multiline_string.rb +4 -4
  9. data/lib/super_diff/equality_matchers/array.rb +4 -4
  10. data/lib/super_diff/equality_matchers/default.rb +4 -4
  11. data/lib/super_diff/equality_matchers/hash.rb +4 -4
  12. data/lib/super_diff/equality_matchers/multiline_string.rb +4 -4
  13. data/lib/super_diff/equality_matchers/primitive.rb +4 -4
  14. data/lib/super_diff/equality_matchers/singleline_string.rb +4 -4
  15. data/lib/super_diff/gem_version.rb +45 -0
  16. data/lib/super_diff/object_inspection.rb +0 -8
  17. data/lib/super_diff/object_inspection/nodes/inspection.rb +1 -1
  18. data/lib/super_diff/operation_trees/base.rb +2 -0
  19. data/lib/super_diff/recursion_guard.rb +2 -0
  20. data/lib/super_diff/rspec.rb +7 -0
  21. data/lib/super_diff/rspec/matcher_text_builders/base.rb +7 -7
  22. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +6 -6
  23. data/lib/super_diff/rspec/matcher_text_builders/contain_exactly.rb +1 -1
  24. data/lib/super_diff/rspec/matcher_text_builders/have_predicate.rb +4 -4
  25. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +1 -1
  26. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +5 -5
  27. data/lib/super_diff/rspec/monkey_patches.rb +353 -306
  28. data/lib/super_diff/rspec/operation_tree_builders/collection_containing_exactly.rb +12 -1
  29. data/lib/super_diff/version.rb +1 -1
  30. data/spec/combustion/Gemfile.lock +173 -0
  31. data/spec/examples.txt +406 -5
  32. data/spec/integration/rspec/be_falsey_matcher_spec.rb +10 -10
  33. data/spec/integration/rspec/be_matcher_spec.rb +100 -100
  34. data/spec/integration/rspec/be_nil_matcher_spec.rb +10 -10
  35. data/spec/integration/rspec/be_predicate_matcher_spec.rb +103 -103
  36. data/spec/integration/rspec/be_truthy_matcher_spec.rb +10 -10
  37. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +107 -107
  38. data/spec/integration/rspec/eq_matcher_spec.rb +230 -230
  39. data/spec/integration/rspec/have_attributes_matcher_spec.rb +129 -129
  40. data/spec/integration/rspec/have_predicate_matcher_spec.rb +65 -65
  41. data/spec/integration/rspec/include_matcher_spec.rb +73 -73
  42. data/spec/integration/rspec/match_array_matcher_spec.rb +149 -107
  43. data/spec/integration/rspec/match_matcher_spec.rb +274 -274
  44. data/spec/integration/rspec/raise_error_matcher_spec.rb +86 -86
  45. data/spec/integration/rspec/respond_to_matcher_spec.rb +240 -240
  46. data/spec/integration/rspec/third_party_matcher_spec.rb +8 -8
  47. data/spec/integration/rspec/unhandled_errors_spec.rb +5 -5
  48. data/spec/spec_helper.rb +30 -13
  49. data/spec/support/integration/helpers.rb +6 -2
  50. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +1 -1
  51. data/spec/support/integration/test_programs/base.rb +2 -0
  52. data/spec/support/integration/test_programs/rspec_active_record.rb +1 -1
  53. data/spec/support/integration/test_programs/rspec_active_support.rb +17 -0
  54. data/spec/support/integration/test_programs/rspec_rails.rb +1 -1
  55. data/spec/support/shared_examples/active_record.rb +108 -108
  56. data/spec/support/shared_examples/hash_with_indifferent_access.rb +196 -232
  57. data/spec/unit/equality_matchers/main_spec.rb +403 -403
  58. data/spec/unit/{object_inspection_spec.rb → super_diff_spec.rb} +136 -76
  59. data/super_diff.gemspec +2 -1
  60. metadata +15 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7bdda657427ecef6cf2c8d65b9485b73da6c1ab64dbc0f608ce185028ca8bd6a
4
- data.tar.gz: 7476633c6bf06aee6087fc9156743ac56d83507380f44b2236e6c32710088d9f
3
+ metadata.gz: be7fd08d833b19c6a99401a1c7b80c448903c315d198b09e55baf70ee663dcfc
4
+ data.tar.gz: ea79d7bb6469e711058b71716c9ce1a23d6353381a8ae876c6bb180a57e78e2a
5
5
  SHA512:
6
- metadata.gz: 45059e4f3d8932f71d6a7f76afe1694e20687a453a9bacb82b1bc0e7c4ed4ec3fc5db20639a9d799b718761a1f053787ed83a867e9adba1d661cc0c03e26b06d
7
- data.tar.gz: 7efd5ddfd5e753e569ba126eb214657250234c4e3968a92cd6e87ca01907dcb6feb71aaa5491b7938f38e8a7a634355ece2533fb7a30252e4ad93613f5e0eb2e
6
+ metadata.gz: 75a1258f1d15d4a830a89ad898cefed4d6379d012fd7aa50d61f82672f10d0acacbf32e88177970fe46afa977dbce42c30212194867eefa1a72eb874417a1156
7
+ data.tar.gz: eb40c7881a21263fc89c6c2cfd1e54c5277449f98a32fb266bc720c43177772480d9834b61c4614102c236942d46ffcea82bc4e5090ad5f70dfc6d15f1d9e184
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # SuperDiff [![Gem Version][version-badge]][rubygems] [![Build Status][travis-badge]][travis] ![Downloads][downloads-badge] [![IssueHunt][issuehunt-badge]][issuehunt]
1
+ # SuperDiff [![Gem Version][version-badge]][rubygems] [![Build Status][gh-actions-badge]][gh-actions] ![Downloads][downloads-badge] [![IssueHunt][issuehunt-badge]][issuehunt]
2
2
 
3
3
  [version-badge]: http://img.shields.io/gem/v/super_diff.svg
4
4
  [rubygems]: http://rubygems.org/gems/super_diff
5
- [travis-badge]: http://img.shields.io/travis/mcmire/super_diff/master.svg
5
+ [gh-actions-badge]: http://img.shields.io/github/workflow/status/mcmire/super_diff/SuperDiff/master
6
6
  [downloads-badge]: http://img.shields.io/gem/dtv/super_diff.svg
7
7
  [hound]: https://houndci.com
8
8
  [issuehunt-badge]: https://img.shields.io/badge/sponsored_through-IssueHunt-2EC28C
@@ -11,7 +11,7 @@
11
11
  SuperDiff is a gem that hooks into RSpec
12
12
  to intelligently display the differences between two data structures of any type.
13
13
 
14
- 📢 **[See what's changed in recent versions.][changelog].**
14
+ 📢 **[See what's changed in recent versions.][changelog]**
15
15
 
16
16
  [changelog]: CHANGELOG.md
17
17
 
@@ -155,14 +155,47 @@ require "super_diff/rspec"
155
155
 
156
156
  ## Configuration
157
157
 
158
- As capable as this library is,
159
- it doesn't know how to deal with every kind of object out there.
160
- If you have a custom class,
161
- and instances of your class aren't appearing in diffs like you like,
162
- you might find it necessary to instruct the gem on how to handle them.
163
- In that case
164
- you would add something like this to your test helper file
165
- (`rails_helper` or `spec_helper`):
158
+ You can customize the behavior of the gem
159
+ by adding a configuration block
160
+ to your test helper file
161
+ (`rails_helper` or `spec_helper`)
162
+ which looks something like this:
163
+
164
+ ``` ruby
165
+ SuperDiff.configure do |config|
166
+ # ...
167
+ end
168
+ ```
169
+
170
+ ### Customizing colors
171
+
172
+ If you don't like the colors that SuperDiff uses,
173
+ you can change them like this:
174
+
175
+ ``` ruby
176
+ SuperDiff.configure do |config|
177
+ config.actual_color = :green
178
+ config.expected_color = :red
179
+ config.border_color = :yellow
180
+ config.header_color = :yellow
181
+ end
182
+ ```
183
+
184
+ See [eight_bit_color.rb](lib/super_diff/csi/eight_bit_color.rb)
185
+ for the list of available colors.
186
+
187
+ ### Diffing custom objects
188
+
189
+ If you are comparing two data structures
190
+ that involve a class that is specific to your project,
191
+ the resulting diff may not look as good as diffs involving native or primitive objects.
192
+ This happens because if SuperDiff doesn't recognize a class,
193
+ it will fall back to a generic representation when diffing instances of that class.
194
+ Fortunately, the gem has a pluggable interface
195
+ that allows you to insert your own implementations
196
+ of key pieces involved in the diffing process.
197
+ I'll have more about how that works soon,
198
+ but here is what such a configuration would look like:
166
199
 
167
200
  ``` ruby
168
201
  SuperDiff.configure do |config|
@@ -173,12 +206,6 @@ SuperDiff.configure do |config|
173
206
  end
174
207
  ```
175
208
 
176
- *(More info here in the future on adding a custom differ,
177
- operation tree builder,
178
- operation tree,
179
- and diff formatter.
180
- Also explanations on what these are.)*
181
-
182
209
  ## Support
183
210
 
184
211
  My goal for this library is to improve your development experience.
@@ -199,17 +226,17 @@ Please see the [contributing](./CONTRIBUTING.md) document for more on how to do
199
226
  If there's a change you want implemented, you can choose to sponsor that change!
200
227
  `super_diff` is set up on IssueHunt,
201
228
  so feel free to search for an existing issue (or make your own)
202
- and [add a bounty](https://issuehunt.io/r/mcmire/super_diff/issues).
229
+ and [add a bounty](https://issuehunt.io/r/mcmire/super_diff).
203
230
  I'll get notified right away!
204
231
 
205
232
  ## Compatibility
206
233
 
207
- `super_diff` is [tested][travis] to work with
208
- Ruby >= 2.4.x,
234
+ `super_diff` is [tested][gh-actions] to work with
235
+ Ruby >= 2.5.x,
209
236
  RSpec 3.x,
210
237
  and Rails >= 5.x.
211
238
 
212
- [travis]: http://travis-ci.org/mcmire/super_diff
239
+ [gh-actions]: https://github.com/mcmire/super_diff/actions?query=workflow%3ASuperDiff
213
240
 
214
241
  ## Inspiration/Thanks
215
242
 
@@ -232,5 +259,5 @@ Thank you to the authors of these libraries!
232
259
 
233
260
  ## Author/License
234
261
 
235
- `super_diff` was created and is maintained by Elliot Winkler.
262
+ SuperDiff was created and is maintained by Elliot Winkler.
236
263
  It is released under the [MIT license](LICENSE).
data/lib/super_diff.rb CHANGED
@@ -13,6 +13,7 @@ module SuperDiff
13
13
  autoload :Differs, "super_diff/differs"
14
14
  autoload :EqualityMatchers, "super_diff/equality_matchers"
15
15
  autoload :Errors, "super_diff/errors"
16
+ autoload :GemVersion, "super_diff/gem_version"
16
17
  autoload :Helpers, "super_diff/helpers"
17
18
  autoload :ImplementationChecks, "super_diff/implementation_checks"
18
19
  autoload :ObjectInspection, "super_diff/object_inspection"
@@ -21,13 +22,6 @@ module SuperDiff
21
22
  autoload :Operations, "super_diff/operations"
22
23
  autoload :RecursionGuard, "super_diff/recursion_guard"
23
24
 
24
- COLORS = {
25
- alpha: :magenta,
26
- beta: :yellow,
27
- border: :blue,
28
- header: :white,
29
- }.freeze
30
-
31
25
  def self.configure
32
26
  yield configuration
33
27
  end
@@ -36,10 +30,34 @@ module SuperDiff
36
30
  @_configuration ||= Configuration.new
37
31
  end
38
32
 
33
+ def self.inspect_object(object, as_single_line:, indent_level: 0)
34
+ ObjectInspection::Inspectors::Main.call(
35
+ object,
36
+ as_single_line: as_single_line,
37
+ indent_level: indent_level,
38
+ )
39
+ end
40
+
39
41
  def self.time_like?(value)
40
42
  # Check for ActiveSupport's #acts_like_time? for their time-like objects
41
43
  # (like ActiveSupport::TimeWithZone).
42
44
  (value.respond_to?(:acts_like_time?) && value.acts_like_time?) ||
43
45
  value.is_a?(Time)
44
46
  end
47
+
48
+ def self.insert_overrides(target_module, mod = nil, &block)
49
+ if mod
50
+ target_module.prepend(mod)
51
+ else
52
+ target_module.prepend(Module.new(&block))
53
+ end
54
+ end
55
+
56
+ def self.insert_singleton_overrides(target_module, mod = nil, &block)
57
+ if mod
58
+ target_module.singleton_class.prepend(mod)
59
+ else
60
+ target_module.singleton_class.prepend(Module.new(&block))
61
+ end
62
+ end
45
63
  end
@@ -6,12 +6,12 @@ module SuperDiff
6
6
  end
7
7
  end
8
8
 
9
- def alpha(*args, **opts, &block)
10
- colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:alpha), &block)
9
+ def expected(*args, **opts, &block)
10
+ colorize(*args, **opts, fg: SuperDiff.configuration.expected_color, &block)
11
11
  end
12
12
 
13
- def beta(*args, **opts, &block)
14
- colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:beta), &block)
13
+ def actual(*args, **opts, &block)
14
+ colorize(*args, **opts, fg: SuperDiff.configuration.actual_color, &block)
15
15
  end
16
16
  end
17
17
  end
@@ -1,26 +1,54 @@
1
1
  module SuperDiff
2
2
  class Configuration
3
3
  attr_reader(
4
+ :extra_diff_formatter_classes,
4
5
  :extra_differ_classes,
6
+ :extra_inspector_classes,
5
7
  :extra_operation_tree_builder_classes,
6
8
  :extra_operation_tree_classes,
7
- :extra_diff_formatter_classes,
8
- :extra_inspector_classes,
9
+ )
10
+ attr_accessor(
11
+ :actual_color,
12
+ :border_color,
13
+ :expected_color,
14
+ :header_color,
9
15
  )
10
16
 
11
17
  def initialize
18
+ @actual_color = :yellow
19
+ @border_color = :blue
20
+ @expected_color = :magenta
21
+ @extra_diff_formatter_classes = [].freeze
12
22
  @extra_differ_classes = [].freeze
23
+ @extra_inspector_classes = [].freeze
13
24
  @extra_operation_tree_builder_classes = [].freeze
14
25
  @extra_operation_tree_classes = [].freeze
15
- @extra_diff_formatter_classes = [].freeze
16
- @extra_inspector_classes = [].freeze
26
+ @header_color = :white
17
27
  end
18
28
 
29
+ def add_extra_diff_formatter_classes(*classes)
30
+ @extra_diff_formatter_classes =
31
+ (@extra_diff_formatter_classes + classes).freeze
32
+ end
33
+ alias_method(
34
+ :add_extra_diff_formatter_class,
35
+ :add_extra_diff_formatter_classes,
36
+ )
37
+
19
38
  def add_extra_differ_classes(*classes)
20
39
  @extra_differ_classes = (@extra_differ_classes + classes).freeze
21
40
  end
22
41
  alias_method :add_extra_differ_class, :add_extra_differ_classes
23
42
 
43
+ def add_extra_inspector_classes(*classes)
44
+ @extra_inspector_classes =
45
+ (@extra_inspector_classes + classes).freeze
46
+ end
47
+ alias_method(
48
+ :add_extra_inspector_class,
49
+ :add_extra_inspector_classes,
50
+ )
51
+
24
52
  def add_extra_operation_tree_builder_classes(*classes)
25
53
  @extra_operation_tree_builder_classes =
26
54
  (@extra_operation_tree_builder_classes + classes).freeze
@@ -38,23 +66,5 @@ module SuperDiff
38
66
  :add_extra_operation_tree_class,
39
67
  :add_extra_operation_tree_classes,
40
68
  )
41
-
42
- def add_extra_diff_formatter_classes(*classes)
43
- @extra_diff_formatter_classes =
44
- (@extra_diff_formatter_classes + classes).freeze
45
- end
46
- alias_method(
47
- :add_extra_diff_formatter_class,
48
- :add_extra_diff_formatter_classes,
49
- )
50
-
51
- def add_extra_inspector_classes(*classes)
52
- @extra_inspector_classes =
53
- (@extra_inspector_classes + classes).freeze
54
- end
55
- alias_method(
56
- :add_extra_inspector_class,
57
- :add_extra_inspector_classes,
58
- )
59
69
  end
60
70
  end
@@ -58,6 +58,7 @@ module SuperDiff
58
58
  end
59
59
  end
60
60
 
61
- self.color_enabled = STDOUT.tty?
61
+ self.color_enabled = ENV["CI"] == "true" || STDOUT.tty?
62
+ # puts "(SuperDiff::Csi) Super::Csi.color_enabled: #{color_enabled?}"
62
63
  end
63
64
  end
@@ -4,7 +4,7 @@ module SuperDiff
4
4
  extend AttrExtras.mixin
5
5
 
6
6
  ICONS = { delete: "-", insert: "+" }.freeze
7
- STYLES = { insert: :beta, delete: :alpha, noop: :plain }.freeze
7
+ STYLES = { insert: :actual, delete: :expected, noop: :plain }.freeze
8
8
 
9
9
  method_object(
10
10
  [
@@ -94,7 +94,7 @@ module SuperDiff
94
94
  end
95
95
 
96
96
  def build_chunk_by_inspecting(value, prefix:, icon:)
97
- inspection = ObjectInspection.inspect(
97
+ inspection = SuperDiff.inspect_object(
98
98
  value,
99
99
  as_single_line: false,
100
100
  )
@@ -15,12 +15,12 @@ module SuperDiff
15
15
  operation_tree.reduce([]) do |array, operation|
16
16
  case operation.name
17
17
  when :change
18
- array << Helpers.style(:alpha, "- #{operation.left_value}")
19
- array << Helpers.style(:beta, "+ #{operation.right_value}")
18
+ array << Helpers.style(:expected, "- #{operation.left_value}")
19
+ array << Helpers.style(:actual, "+ #{operation.right_value}")
20
20
  when :delete
21
- array << Helpers.style(:alpha, "- #{operation.value}")
21
+ array << Helpers.style(:expected, "- #{operation.value}")
22
22
  when :insert
23
- array << Helpers.style(:beta, "+ #{operation.value}")
23
+ array << Helpers.style(:actual, "+ #{operation.value}")
24
24
  else
25
25
  array << Helpers.style(:plain, " #{operation.value}")
26
26
  end
@@ -11,16 +11,16 @@ module SuperDiff
11
11
 
12
12
  #{
13
13
  Helpers.style(
14
- :alpha,
14
+ :expected,
15
15
  "Expected: " +
16
- ObjectInspection.inspect(expected, as_single_line: true),
16
+ SuperDiff.inspect_object(expected, as_single_line: true),
17
17
  )
18
18
  }
19
19
  #{
20
20
  Helpers.style(
21
- :beta,
21
+ :actual,
22
22
  " Actual: " +
23
- ObjectInspection.inspect(actual, as_single_line: true),
23
+ SuperDiff.inspect_object(actual, as_single_line: true),
24
24
  )
25
25
  }
26
26
 
@@ -19,17 +19,17 @@ module SuperDiff
19
19
 
20
20
  def expected_line
21
21
  Helpers.style(
22
- :alpha,
22
+ :expected,
23
23
  "Expected: " +
24
- ObjectInspection.inspect(expected, as_single_line: true),
24
+ SuperDiff.inspect_object(expected, as_single_line: true),
25
25
  )
26
26
  end
27
27
 
28
28
  def actual_line
29
29
  Helpers.style(
30
- :beta,
30
+ :actual,
31
31
  " Actual: " +
32
- ObjectInspection.inspect(actual, as_single_line: true),
32
+ SuperDiff.inspect_object(actual, as_single_line: true),
33
33
  )
34
34
  end
35
35
 
@@ -11,16 +11,16 @@ module SuperDiff
11
11
 
12
12
  #{
13
13
  Helpers.style(
14
- :alpha,
14
+ :expected,
15
15
  "Expected: " +
16
- ObjectInspection.inspect(expected, as_single_line: true),
16
+ SuperDiff.inspect_object(expected, as_single_line: true),
17
17
  )
18
18
  }
19
19
  #{
20
20
  Helpers.style(
21
- :beta,
21
+ :actual,
22
22
  " Actual: " +
23
- ObjectInspection.inspect(actual, as_single_line: true),
23
+ SuperDiff.inspect_object(actual, as_single_line: true),
24
24
  )
25
25
  }
26
26
 
@@ -12,16 +12,16 @@ module SuperDiff
12
12
  #{
13
13
  # TODO: This whole thing should not be red or green, just the values
14
14
  Helpers.style(
15
- :alpha,
15
+ :expected,
16
16
  "Expected: " +
17
- ObjectInspection.inspect(expected, as_single_line: true),
17
+ SuperDiff.inspect_object(expected, as_single_line: true),
18
18
  )
19
19
  }
20
20
  #{
21
21
  Helpers.style(
22
- :beta,
22
+ :actual,
23
23
  " Actual: " +
24
- ObjectInspection.inspect(actual, as_single_line: true),
24
+ SuperDiff.inspect_object(actual, as_single_line: true),
25
25
  )
26
26
  }
27
27
 
@@ -15,16 +15,16 @@ module SuperDiff
15
15
 
16
16
  #{
17
17
  Helpers.style(
18
- :alpha,
18
+ :expected,
19
19
  "Expected: " +
20
- ObjectInspection.inspect(expected, as_single_line: true),
20
+ SuperDiff.inspect_object(expected, as_single_line: true),
21
21
  )
22
22
  }
23
23
  #{
24
24
  Helpers.style(
25
- :beta,
25
+ :actual,
26
26
  " Actual: " +
27
- ObjectInspection.inspect(actual, as_single_line: true),
27
+ SuperDiff.inspect_object(actual, as_single_line: true),
28
28
  )
29
29
  }
30
30
  OUTPUT
@@ -11,16 +11,16 @@ module SuperDiff
11
11
 
12
12
  #{
13
13
  Helpers.style(
14
- :alpha,
14
+ :expected,
15
15
  "Expected: " +
16
- ObjectInspection.inspect(expected, as_single_line: true),
16
+ SuperDiff.inspect_object(expected, as_single_line: true),
17
17
  )
18
18
  }
19
19
  #{
20
20
  Helpers.style(
21
- :beta,
21
+ :actual,
22
22
  " Actual: " +
23
- ObjectInspection.inspect(actual, as_single_line: true),
23
+ SuperDiff.inspect_object(actual, as_single_line: true),
24
24
  )
25
25
  }
26
26
  OUTPUT