super_diff 0.5.3 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +46 -19
  3. data/lib/super_diff.rb +1 -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 +1 -1
  8. data/lib/super_diff/diff_formatters/multiline_string.rb +4 -4
  9. data/lib/super_diff/equality_matchers/array.rb +2 -2
  10. data/lib/super_diff/equality_matchers/default.rb +2 -2
  11. data/lib/super_diff/equality_matchers/hash.rb +2 -2
  12. data/lib/super_diff/equality_matchers/multiline_string.rb +2 -2
  13. data/lib/super_diff/equality_matchers/primitive.rb +2 -2
  14. data/lib/super_diff/equality_matchers/singleline_string.rb +2 -2
  15. data/lib/super_diff/gem_version.rb +45 -0
  16. data/lib/super_diff/rspec.rb +7 -0
  17. data/lib/super_diff/rspec/matcher_text_builders/base.rb +7 -7
  18. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +6 -6
  19. data/lib/super_diff/rspec/matcher_text_builders/contain_exactly.rb +1 -1
  20. data/lib/super_diff/rspec/matcher_text_builders/have_predicate.rb +4 -4
  21. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +1 -1
  22. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +5 -5
  23. data/lib/super_diff/rspec/monkey_patches.rb +21 -13
  24. data/lib/super_diff/rspec/operation_tree_builders/collection_containing_exactly.rb +12 -1
  25. data/lib/super_diff/version.rb +1 -1
  26. data/spec/examples.txt +390 -5
  27. data/spec/integration/rspec/be_falsey_matcher_spec.rb +10 -10
  28. data/spec/integration/rspec/be_matcher_spec.rb +100 -100
  29. data/spec/integration/rspec/be_nil_matcher_spec.rb +10 -10
  30. data/spec/integration/rspec/be_predicate_matcher_spec.rb +103 -103
  31. data/spec/integration/rspec/be_truthy_matcher_spec.rb +10 -10
  32. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +107 -107
  33. data/spec/integration/rspec/eq_matcher_spec.rb +230 -230
  34. data/spec/integration/rspec/have_attributes_matcher_spec.rb +129 -129
  35. data/spec/integration/rspec/have_predicate_matcher_spec.rb +65 -65
  36. data/spec/integration/rspec/include_matcher_spec.rb +73 -73
  37. data/spec/integration/rspec/match_array_matcher_spec.rb +119 -119
  38. data/spec/integration/rspec/match_matcher_spec.rb +274 -274
  39. data/spec/integration/rspec/raise_error_matcher_spec.rb +86 -86
  40. data/spec/integration/rspec/respond_to_matcher_spec.rb +240 -240
  41. data/spec/integration/rspec/third_party_matcher_spec.rb +8 -8
  42. data/spec/integration/rspec/unhandled_errors_spec.rb +5 -5
  43. data/spec/spec_helper.rb +22 -13
  44. data/spec/support/integration/helpers.rb +6 -2
  45. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +1 -1
  46. data/spec/support/integration/test_programs/base.rb +2 -0
  47. data/spec/support/integration/test_programs/rspec_active_record.rb +1 -1
  48. data/spec/support/integration/test_programs/rspec_active_support.rb +17 -0
  49. data/spec/support/integration/test_programs/rspec_rails.rb +1 -1
  50. data/spec/support/shared_examples/active_record.rb +108 -108
  51. data/spec/support/shared_examples/hash_with_indifferent_access.rb +196 -232
  52. data/spec/tmp/integration_spec.rb +15 -0
  53. data/spec/unit/equality_matchers/main_spec.rb +403 -403
  54. data/super_diff.gemspec +1 -1
  55. metadata +11 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5427cb2217992c9dffd57826837caaa750ddcd422625498c3bca9cf40ae8e32
4
- data.tar.gz: 540a3e3ea47bc98c4abf45ff6ff216563f70f1492bb2c06a54525b6414b306ae
3
+ metadata.gz: 0fc8e5b83b3b02559e96bbd06f8703b688d56b32588639f7d49a4774d6869433
4
+ data.tar.gz: 5f0c9d472aa1170df1a52cc41eab384c4526342cf336e380cdbd01f9e95a3b61
5
5
  SHA512:
6
- metadata.gz: 1bf06c03ce556532384242e0193628bae55134e3edf65003e1d0b7a1015e25af6f9ce68bd23a7ca2ebadddb0093103a643f9bb742116d2456f20146bb948f380
7
- data.tar.gz: c32489d6915ac9f7fc3cf1c0427683977b3a569a99e36a7d8e47f1209e74efcf3f86b3986b1e40ed51d2321d89f1e8530bdda79352c1d53fd94798370f2261de
6
+ metadata.gz: 24306d1fba59520092a8a446e0a9219a3e9e89813fd3818f2769f737084cc8f6565b12497c86bacb4b9a8f9c5a4b13bafe0479e330cc8169a6c574e11cc74a56
7
+ data.tar.gz: 514af50e6272ae3095b752d778b7f3bc776923bccb87a2ab8a50c2f8019c9aba991e129d2dad88248c621594ab5972029a798ada0e64eb8f8a27acc0e7cde496
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
@@ -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.
@@ -204,12 +231,12 @@ I'll get notified right away!
204
231
 
205
232
  ## Compatibility
206
233
 
207
- `super_diff` is [tested][travis] to work with
234
+ `super_diff` is [tested][gh-actions] to work with
208
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
@@ -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
  [
@@ -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,14 +11,14 @@ module SuperDiff
11
11
 
12
12
  #{
13
13
  Helpers.style(
14
- :alpha,
14
+ :expected,
15
15
  "Expected: " +
16
16
  ObjectInspection.inspect(expected, as_single_line: true),
17
17
  )
18
18
  }
19
19
  #{
20
20
  Helpers.style(
21
- :beta,
21
+ :actual,
22
22
  " Actual: " +
23
23
  ObjectInspection.inspect(actual, as_single_line: true),
24
24
  )
@@ -19,7 +19,7 @@ module SuperDiff
19
19
 
20
20
  def expected_line
21
21
  Helpers.style(
22
- :alpha,
22
+ :expected,
23
23
  "Expected: " +
24
24
  ObjectInspection.inspect(expected, as_single_line: true),
25
25
  )
@@ -27,7 +27,7 @@ module SuperDiff
27
27
 
28
28
  def actual_line
29
29
  Helpers.style(
30
- :beta,
30
+ :actual,
31
31
  " Actual: " +
32
32
  ObjectInspection.inspect(actual, as_single_line: true),
33
33
  )
@@ -11,14 +11,14 @@ module SuperDiff
11
11
 
12
12
  #{
13
13
  Helpers.style(
14
- :alpha,
14
+ :expected,
15
15
  "Expected: " +
16
16
  ObjectInspection.inspect(expected, as_single_line: true),
17
17
  )
18
18
  }
19
19
  #{
20
20
  Helpers.style(
21
- :beta,
21
+ :actual,
22
22
  " Actual: " +
23
23
  ObjectInspection.inspect(actual, as_single_line: true),
24
24
  )
@@ -12,14 +12,14 @@ 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
17
  ObjectInspection.inspect(expected, as_single_line: true),
18
18
  )
19
19
  }
20
20
  #{
21
21
  Helpers.style(
22
- :beta,
22
+ :actual,
23
23
  " Actual: " +
24
24
  ObjectInspection.inspect(actual, as_single_line: true),
25
25
  )
@@ -15,14 +15,14 @@ module SuperDiff
15
15
 
16
16
  #{
17
17
  Helpers.style(
18
- :alpha,
18
+ :expected,
19
19
  "Expected: " +
20
20
  ObjectInspection.inspect(expected, as_single_line: true),
21
21
  )
22
22
  }
23
23
  #{
24
24
  Helpers.style(
25
- :beta,
25
+ :actual,
26
26
  " Actual: " +
27
27
  ObjectInspection.inspect(actual, as_single_line: true),
28
28
  )
@@ -11,14 +11,14 @@ module SuperDiff
11
11
 
12
12
  #{
13
13
  Helpers.style(
14
- :alpha,
14
+ :expected,
15
15
  "Expected: " +
16
16
  ObjectInspection.inspect(expected, as_single_line: true),
17
17
  )
18
18
  }
19
19
  #{
20
20
  Helpers.style(
21
- :beta,
21
+ :actual,
22
22
  " Actual: " +
23
23
  ObjectInspection.inspect(actual, as_single_line: true),
24
24
  )
@@ -0,0 +1,45 @@
1
+ module SuperDiff
2
+ class GemVersion
3
+ def initialize(version)
4
+ @version = Gem::Version.new(version.to_s)
5
+ end
6
+
7
+ def <(other)
8
+ compare?(:<, other)
9
+ end
10
+
11
+ def <=(other)
12
+ compare?(:<=, other)
13
+ end
14
+
15
+ def ==(other)
16
+ compare?(:==, other)
17
+ end
18
+
19
+ def >=(other)
20
+ compare?(:>=, other)
21
+ end
22
+
23
+ def >(other)
24
+ compare?(:>, other)
25
+ end
26
+
27
+ def =~(other)
28
+ Gem::Requirement.new(other).satisfied_by?(version)
29
+ end
30
+
31
+ def to_s
32
+ version.to_s
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :version
38
+
39
+ def compare?(operator, other_version)
40
+ Gem::Requirement.
41
+ new("#{operator} #{other_version}").
42
+ satisfied_by?(version)
43
+ end
44
+ end
45
+ end
@@ -61,6 +61,13 @@ module SuperDiff
61
61
  value.is_a?(::RSpec::Matchers::AliasedMatcher)
62
62
  end
63
63
 
64
+ def self.rspec_version
65
+ @_rspec_version ||= begin
66
+ require "rspec/core/version"
67
+ GemVersion.new(::RSpec::Core::Version::STRING)
68
+ end
69
+ end
70
+
64
71
  SuperDiff.configuration.tap do |config|
65
72
  config.add_extra_differ_classes(
66
73
  Differs::CollectionContainingExactly,
@@ -45,12 +45,12 @@ module SuperDiff
45
45
  def add_extra_after_error
46
46
  end
47
47
 
48
- def beta_color
49
- SuperDiff::COLORS.fetch(:beta)
48
+ def actual_color
49
+ SuperDiff.configuration.actual_color
50
50
  end
51
51
 
52
- def alpha_color
53
- SuperDiff::COLORS.fetch(:alpha)
52
+ def expected_color
53
+ SuperDiff.configuration.expected_color
54
54
  end
55
55
 
56
56
  private
@@ -91,7 +91,7 @@ module SuperDiff
91
91
  end
92
92
 
93
93
  def add_actual_value
94
- template.add_text_in_color(beta_color) { actual }
94
+ template.add_text_in_color(actual_color) { actual }
95
95
  end
96
96
 
97
97
  def expected_section
@@ -110,7 +110,7 @@ module SuperDiff
110
110
  else
111
111
  template.add_text " "
112
112
  template.add_text_in_color(
113
- alpha_color,
113
+ expected_color,
114
114
  expected_for_failure_message,
115
115
  )
116
116
  end
@@ -121,7 +121,7 @@ module SuperDiff
121
121
  add_expected_value_to(template, expected_for_description)
122
122
  else
123
123
  template.add_text " "
124
- template.add_text_in_color(alpha_color, expected_for_description)
124
+ template.add_text_in_color(expected_color, expected_for_description)
125
125
  end
126
126
  end
127
127