super_diff 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +117 -89
  3. data/lib/super_diff.rb +33 -47
  4. data/lib/super_diff/active_record.rb +41 -0
  5. data/lib/super_diff/active_record/diff_formatters.rb +10 -0
  6. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +23 -0
  7. data/lib/super_diff/active_record/differs.rb +10 -0
  8. data/lib/super_diff/active_record/differs/active_record_relation.rb +30 -0
  9. data/lib/super_diff/active_record/object_inspection.rb +14 -0
  10. data/lib/super_diff/active_record/object_inspection/inspectors.rb +16 -0
  11. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb +38 -0
  12. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb +18 -0
  13. data/lib/super_diff/active_record/object_inspection/map_extension.rb +18 -0
  14. data/lib/super_diff/active_record/operation_sequences.rb +10 -0
  15. data/lib/super_diff/active_record/operation_sequences/active_record_relation.rb +16 -0
  16. data/lib/super_diff/active_record/operational_sequencers.rb +14 -0
  17. data/lib/super_diff/active_record/operational_sequencers/active_record_model.rb +19 -0
  18. data/lib/super_diff/active_record/operational_sequencers/active_record_relation.rb +24 -0
  19. data/lib/super_diff/active_support.rb +33 -0
  20. data/lib/super_diff/active_support/diff_formatters.rb +10 -0
  21. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +36 -0
  22. data/lib/super_diff/active_support/differs.rb +10 -0
  23. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +36 -0
  24. data/lib/super_diff/active_support/object_inspection.rb +14 -0
  25. data/lib/super_diff/active_support/object_inspection/inspectors.rb +12 -0
  26. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +18 -0
  27. data/lib/super_diff/active_support/object_inspection/map_extension.rb +15 -0
  28. data/lib/super_diff/active_support/operation_sequences.rb +10 -0
  29. data/lib/super_diff/active_support/operation_sequences/hash_with_indifferent_access.rb +16 -0
  30. data/lib/super_diff/active_support/operational_sequencers.rb +10 -0
  31. data/lib/super_diff/active_support/operational_sequencers/hash_with_indifferent_access.rb +21 -0
  32. data/lib/super_diff/colorized_document_extensions.rb +17 -0
  33. data/lib/super_diff/csi.rb +45 -15
  34. data/lib/super_diff/csi/bold_sequence.rb +9 -0
  35. data/lib/super_diff/csi/color.rb +62 -0
  36. data/lib/super_diff/csi/color_sequence_block.rb +28 -0
  37. data/lib/super_diff/csi/colorized_document.rb +72 -0
  38. data/lib/super_diff/csi/document.rb +183 -0
  39. data/lib/super_diff/csi/eight_bit_color.rb +72 -26
  40. data/lib/super_diff/csi/four_bit_color.rb +63 -29
  41. data/lib/super_diff/csi/twenty_four_bit_color.rb +79 -18
  42. data/lib/super_diff/csi/uncolorized_document.rb +29 -0
  43. data/lib/super_diff/diff_formatter.rb +10 -15
  44. data/lib/super_diff/diff_formatters.rb +10 -1
  45. data/lib/super_diff/diff_formatters/base.rb +12 -17
  46. data/lib/super_diff/diff_formatters/collection.rb +81 -50
  47. data/lib/super_diff/diff_formatters/{object.rb → custom_object.rb} +12 -9
  48. data/lib/super_diff/diff_formatters/default_object.rb +48 -0
  49. data/lib/super_diff/diff_formatters/multiline_string.rb +31 -0
  50. data/lib/super_diff/differ.rb +35 -32
  51. data/lib/super_diff/differs.rb +16 -1
  52. data/lib/super_diff/differs/array.rb +2 -2
  53. data/lib/super_diff/differs/base.rb +11 -21
  54. data/lib/super_diff/differs/custom_object.rb +26 -0
  55. data/lib/super_diff/differs/default_object.rb +25 -0
  56. data/lib/super_diff/differs/empty.rb +1 -1
  57. data/lib/super_diff/differs/hash.rb +2 -2
  58. data/lib/super_diff/differs/{multi_line_string.rb → multiline_string.rb} +6 -5
  59. data/lib/super_diff/equality_matcher.rb +9 -22
  60. data/lib/super_diff/equality_matchers.rb +19 -1
  61. data/lib/super_diff/equality_matchers/array.rb +6 -4
  62. data/lib/super_diff/equality_matchers/base.rb +8 -16
  63. data/lib/super_diff/equality_matchers/default.rb +60 -0
  64. data/lib/super_diff/equality_matchers/hash.rb +6 -4
  65. data/lib/super_diff/equality_matchers/{multi_line_string.rb → multiline_string.rb} +9 -6
  66. data/lib/super_diff/equality_matchers/primitive.rb +34 -0
  67. data/lib/super_diff/equality_matchers/{single_line_string.rb → singleline_string.rb} +7 -5
  68. data/lib/super_diff/helpers.rb +17 -81
  69. data/lib/super_diff/no_differ_available_error.rb +22 -0
  70. data/lib/super_diff/{errors.rb → no_operational_sequencer_available_error.rb} +0 -0
  71. data/lib/super_diff/object_inspection.rb +24 -0
  72. data/lib/super_diff/object_inspection/inspection_tree.rb +144 -0
  73. data/lib/super_diff/object_inspection/inspector.rb +27 -0
  74. data/lib/super_diff/object_inspection/inspectors.rb +18 -0
  75. data/lib/super_diff/object_inspection/inspectors/array.rb +22 -0
  76. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +27 -0
  77. data/lib/super_diff/object_inspection/inspectors/default_object.rb +47 -0
  78. data/lib/super_diff/object_inspection/inspectors/hash.rb +22 -0
  79. data/lib/super_diff/object_inspection/inspectors/primitive.rb +13 -0
  80. data/lib/super_diff/object_inspection/inspectors/string.rb +13 -0
  81. data/lib/super_diff/object_inspection/map.rb +28 -0
  82. data/lib/super_diff/object_inspection/nodes.rb +49 -0
  83. data/lib/super_diff/object_inspection/nodes/base.rb +86 -0
  84. data/lib/super_diff/object_inspection/nodes/break.rb +15 -0
  85. data/lib/super_diff/object_inspection/nodes/inspection.rb +15 -0
  86. data/lib/super_diff/object_inspection/nodes/nesting.rb +16 -0
  87. data/lib/super_diff/object_inspection/nodes/text.rb +15 -0
  88. data/lib/super_diff/object_inspection/nodes/when_empty.rb +30 -0
  89. data/lib/super_diff/object_inspection/nodes/when_multiline.rb +22 -0
  90. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +30 -0
  91. data/lib/super_diff/object_inspection/nodes/when_singleline.rb +24 -0
  92. data/lib/super_diff/operation_sequences.rb +9 -0
  93. data/lib/super_diff/operation_sequences/base.rb +1 -1
  94. data/lib/super_diff/operation_sequences/{object.rb → custom_object.rb} +4 -3
  95. data/lib/super_diff/operation_sequences/default_object.rb +25 -0
  96. data/lib/super_diff/operational_sequencer.rb +23 -18
  97. data/lib/super_diff/operational_sequencers.rb +12 -1
  98. data/lib/super_diff/operational_sequencers/array.rb +65 -62
  99. data/lib/super_diff/operational_sequencers/base.rb +18 -26
  100. data/lib/super_diff/operational_sequencers/custom_object.rb +35 -0
  101. data/lib/super_diff/operational_sequencers/{object.rb → default_object.rb} +21 -11
  102. data/lib/super_diff/operational_sequencers/hash.rb +8 -5
  103. data/lib/super_diff/operational_sequencers/{multi_line_string.rb → multiline_string.rb} +11 -6
  104. data/lib/super_diff/operations.rb +6 -0
  105. data/lib/super_diff/operations/binary_operation.rb +14 -34
  106. data/lib/super_diff/operations/unary_operation.rb +11 -2
  107. data/lib/super_diff/rails.rb +1 -0
  108. data/lib/super_diff/recursion_guard.rb +47 -0
  109. data/lib/super_diff/rspec-rails.rb +2 -0
  110. data/lib/super_diff/rspec.rb +52 -8
  111. data/lib/super_diff/rspec/augmented_matcher.rb +98 -0
  112. data/lib/super_diff/rspec/configuration.rb +31 -0
  113. data/lib/super_diff/rspec/differ.rb +60 -16
  114. data/lib/super_diff/rspec/differs.rb +13 -0
  115. data/lib/super_diff/rspec/differs/collection_containing_exactly.rb +23 -0
  116. data/lib/super_diff/rspec/differs/partial_array.rb +22 -0
  117. data/lib/super_diff/rspec/differs/partial_hash.rb +22 -0
  118. data/lib/super_diff/rspec/differs/partial_object.rb +22 -0
  119. data/lib/super_diff/rspec/matcher_text_builders.rb +24 -0
  120. data/lib/super_diff/rspec/matcher_text_builders/base.rb +155 -0
  121. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +78 -0
  122. data/lib/super_diff/rspec/matcher_text_builders/contain_exactly.rb +14 -0
  123. data/lib/super_diff/rspec/matcher_text_builders/match.rb +23 -0
  124. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +13 -0
  125. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +99 -0
  126. data/lib/super_diff/rspec/matcher_text_template.rb +240 -0
  127. data/lib/super_diff/rspec/monkey_patches.rb +601 -98
  128. data/lib/super_diff/rspec/object_inspection.rb +8 -0
  129. data/lib/super_diff/rspec/object_inspection/inspectors.rb +24 -0
  130. data/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb +19 -0
  131. data/lib/super_diff/rspec/object_inspection/inspectors/partial_array.rb +22 -0
  132. data/lib/super_diff/rspec/object_inspection/inspectors/partial_hash.rb +21 -0
  133. data/lib/super_diff/rspec/object_inspection/inspectors/partial_object.rb +21 -0
  134. data/lib/super_diff/rspec/object_inspection/map_extension.rb +23 -0
  135. data/lib/super_diff/rspec/operational_sequencers.rb +22 -0
  136. data/lib/super_diff/rspec/operational_sequencers/collection_containing_exactly.rb +97 -0
  137. data/lib/super_diff/rspec/operational_sequencers/partial_array.rb +23 -0
  138. data/lib/super_diff/rspec/operational_sequencers/partial_hash.rb +32 -0
  139. data/lib/super_diff/rspec/operational_sequencers/partial_object.rb +64 -0
  140. data/lib/super_diff/version.rb +1 -1
  141. data/spec/examples.txt +328 -46
  142. data/spec/integration/rails/active_record_spec.rb +19 -0
  143. data/spec/integration/rails/hash_with_indifferent_access_spec.rb +19 -0
  144. data/spec/integration/rspec/be_falsey_matcher_spec.rb +53 -0
  145. data/spec/integration/rspec/be_matcher_spec.rb +565 -0
  146. data/spec/integration/rspec/be_nil_matcher_spec.rb +53 -0
  147. data/spec/integration/rspec/be_predicate_matcher_spec.rb +546 -0
  148. data/spec/integration/rspec/be_truthy_matcher_spec.rb +57 -0
  149. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +368 -0
  150. data/spec/integration/rspec/eq_matcher_spec.rb +874 -0
  151. data/spec/integration/rspec/have_attributes_matcher_spec.rb +299 -0
  152. data/spec/integration/rspec/include_matcher_spec.rb +350 -0
  153. data/spec/integration/rspec/match_matcher_spec.rb +1258 -0
  154. data/spec/integration/rspec/raise_error_matcher_spec.rb +350 -0
  155. data/spec/integration/rspec/respond_to_matcher_spec.rb +994 -0
  156. data/spec/integration/rspec/unhandled_errors_spec.rb +94 -0
  157. data/spec/spec_helper.rb +19 -4
  158. data/spec/support/colorizer.rb +9 -0
  159. data/spec/support/command_runner.rb +4 -0
  160. data/spec/support/integration/helpers.rb +179 -0
  161. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +79 -41
  162. data/spec/support/models/a.rb +11 -0
  163. data/spec/support/models/active_record/person.rb +26 -0
  164. data/spec/support/models/active_record/shipping_address.rb +29 -0
  165. data/spec/support/models/customer.rb +24 -0
  166. data/spec/support/models/empty_class.rb +6 -0
  167. data/spec/support/models/item.rb +10 -0
  168. data/spec/support/models/order.rb +9 -0
  169. data/spec/support/models/person.rb +20 -0
  170. data/spec/support/models/player.rb +33 -0
  171. data/spec/support/models/shipping_address.rb +34 -0
  172. data/spec/support/ruby_versions.rb +7 -0
  173. data/spec/support/shared_examples/active_record.rb +338 -0
  174. data/spec/support/shared_examples/hash_with_indifferent_access.rb +233 -0
  175. data/spec/unit/equality_matcher_spec.rb +579 -171
  176. data/spec/unit/object_inspection_spec.rb +1092 -0
  177. data/spec/unit/rspec/matchers/be_compared_to_spec.rb +23 -0
  178. data/spec/unit/rspec/matchers/be_falsey_spec.rb +9 -0
  179. data/spec/unit/rspec/matchers/be_nil_spec.rb +9 -0
  180. data/spec/unit/rspec/matchers/be_predicate_spec.rb +31 -0
  181. data/spec/unit/rspec/matchers/be_spec.rb +17 -0
  182. data/spec/unit/rspec/matchers/be_truthy_spec.rb +9 -0
  183. data/spec/unit/rspec/matchers/contain_exactly_spec.rb +11 -0
  184. data/spec/unit/rspec/matchers/eq_spec.rb +9 -0
  185. data/spec/unit/rspec/matchers/have_attributes_spec.rb +11 -0
  186. data/spec/unit/rspec/matchers/include_spec.rb +21 -0
  187. data/spec/unit/rspec/matchers/match_spec.rb +9 -0
  188. data/spec/unit/rspec/matchers/raise_error_spec.rb +29 -0
  189. data/spec/unit/rspec/matchers/respond_to_spec.rb +78 -0
  190. data/super_diff.gemspec +4 -2
  191. metadata +231 -34
  192. data/lib/super_diff/csi/color_helper.rb +0 -52
  193. data/lib/super_diff/csi/eight_bit_sequence.rb +0 -27
  194. data/lib/super_diff/csi/four_bit_sequence.rb +0 -24
  195. data/lib/super_diff/csi/sequence.rb +0 -22
  196. data/lib/super_diff/csi/twenty_four_bit_sequence.rb +0 -27
  197. data/lib/super_diff/diff_formatters/multi_line_string.rb +0 -31
  198. data/lib/super_diff/differs/object.rb +0 -68
  199. data/lib/super_diff/equality_matchers/object.rb +0 -18
  200. data/lib/super_diff/value_inspection.rb +0 -11
  201. data/spec/integration/rspec_spec.rb +0 -261
  202. data/spec/support/color_helper.rb +0 -49
  203. data/spec/support/person.rb +0 -23
  204. data/spec/support/person_diff_formatter.rb +0 -15
  205. data/spec/support/person_operation_sequence.rb +0 -14
  206. data/spec/support/person_operational_sequencer.rb +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a67c51aa528c0539aca579800b31aafc363fb85f532ba9a07723e073a2efb050
4
- data.tar.gz: 593449f1083f6a121b51bc74b30c78682f48b9f55e28335613b0babdbde0d4b0
3
+ metadata.gz: a490a3241bf9e0d7c6086d13d1b60b02054839edbcf3bd1516db488cafab1449
4
+ data.tar.gz: da592d721f010fefa021348aefa832e05c07cecc4fdae7588c6bcb4696e45263
5
5
  SHA512:
6
- metadata.gz: 867861728306a60b4ce417ac81836b933f6e5a6f36c27aa8dbe1735c3fd269a240c3901c2fa8cbec4158a65196053e0243da82d2c83567c8c5db12d6cb1b8627
7
- data.tar.gz: 9148ff70d4edc39116fcd00100e5ab7cd3579c13f17eb6920ab6ba047df1e0dc02389b4600bfadd6ea7f73f686dc4881ff05d994c9574f4a97d04c13556eef09
6
+ metadata.gz: 0df2d46d7863e1cae93aa6f1760b4de7b45d34a333570a82ef39278bfd7b49ea8d2dd4d21e3d19dfdd427ffd42486a32c96400cf3755c196382358ed638d06ec
7
+ data.tar.gz: d86b0ac244443fa75f3ab51c6377818dc942bc9fe2b44b14be78a9595da9267e94b20cd0571cde296ef140ad4fd81f915656330e45c3429c256c13e6298ab932
data/README.md CHANGED
@@ -3,158 +3,161 @@
3
3
  [version-badge]: http://img.shields.io/gem/v/super_diff.svg
4
4
  [rubygems]: http://rubygems.org/gems/super_diff
5
5
  [travis-badge]: http://img.shields.io/travis/mcmire/super_diff/master.svg
6
- [travis]: http://travis-ci.org/mcmire/super_diff
7
6
  [downloads-badge]: http://img.shields.io/gem/dtv/super_diff.svg
8
7
  [hound-badge]: https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg
9
8
  [hound]: https://houndci.com
10
9
 
11
- ## Concept
10
+ SuperDiff is a Ruby gem that intelligently displays the differences between two
11
+ data structures of any type.
12
+
13
+ 📢 **[See what's changed in the latest version (0.2.0)][changelog].**
14
+
15
+ [changelog]: CHANGELOG.md
12
16
 
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.
17
+ ## Introduction
16
18
 
17
- Let's say you have two hashes and you want to compare them. Perhaps your first
18
- hash looks like this:
19
+ The primary motivation behind this gem is to replace RSpec's built-in diffing
20
+ capabilities. Sometimes, whenever you use a matcher such as `eq`, `match`,
21
+ `include`, or `have_attributes`, you will get a diff of the two data structures
22
+ you are trying to match against. This is really helpful for strings, but not so
23
+ helpful for other, more "real world" kinds of values, such as arrays, hashes,
24
+ and full-scale objects. The reason this doesn't work is because [all RSpec does
25
+ is run your `expected` and `actual` values through Ruby's PrettyPrinter
26
+ library][rspec-differ-fail] and then perform a diff of these strings.
27
+
28
+ For instance, let's say you wanted to compare these two hashes:
19
29
 
20
30
  ``` ruby
21
- expected = {
31
+ actual = {
22
32
  customer: {
23
- name: "Marty McFly",
33
+ person: SuperDiff::Test::Person.new(name: "Marty McFly, Jr.", age: 17),
24
34
  shipping_address: {
25
- line_1: "123 Main St.",
35
+ line_1: "456 Ponderosa Ct.",
26
36
  city: "Hill Valley",
27
37
  state: "CA",
28
- zip: "90382",
29
- },
38
+ zip: "90382"
39
+ }
30
40
  },
31
41
  items: [
32
42
  {
33
43
  name: "Fender Stratocaster",
34
44
  cost: 100_000,
35
- options: ["red", "blue", "green"],
45
+ options: ["red", "blue", "green"]
36
46
  },
37
- { name: "Chevy 4x4" },
38
- ],
47
+ { name: "Mattel Hoverboard" }
48
+ ]
39
49
  }
40
- ```
41
-
42
- and your second hash looks like this:
43
50
 
44
- ``` ruby
45
- actual = {
51
+ expected = {
46
52
  customer: {
47
- name: "Marty McFly, Jr.",
53
+ person: SuperDiff::Test::Person.new(name: "Marty McFly", age: 17),
48
54
  shipping_address: {
49
- line_1: "456 Ponderosa Ct.",
55
+ line_1: "123 Main St.",
50
56
  city: "Hill Valley",
51
57
  state: "CA",
52
- zip: "90382",
53
- },
58
+ zip: "90382"
59
+ }
54
60
  },
55
61
  items: [
56
62
  {
57
63
  name: "Fender Stratocaster",
58
64
  cost: 100_000,
59
- options: ["red", "blue", "green"],
65
+ options: ["red", "blue", "green"]
60
66
  },
61
- { name: "Mattel Hoverboard" },
62
- ],
67
+ { name: "Chevy 4x4" }
68
+ ]
63
69
  }
64
70
  ```
65
71
 
66
- If you want to know what the difference between them is, you could say:
72
+ If, somewhere in a test, you were to say:
67
73
 
68
74
  ``` ruby
69
- SuperDiff::EqualityMatcher.call(expected, actual)
75
+ expect(actual).to eq(expected)
70
76
  ```
71
77
 
72
- This will give you the following string:
78
+ You would get output that looks like:
73
79
 
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
- ```
80
+ ![Before super_diff](doc/before_super_diff.png)
81
+
82
+ Not great.
107
83
 
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.
84
+ This library provides a diff engine that knows how to figure out the differences
85
+ between any two data structures and display them in a sensible way. Using the
86
+ example above, you'd get this instead:
112
87
 
113
- By the way, SuperDiff doesn't just work with hashes, but arrays as well as other
114
- objects, too!
88
+ ![After super_diff](doc/after_super_diff.png)
115
89
 
116
- ## Usage
90
+ [rspec-differ-fail]: https://github.com/rspec/rspec-support/blob/c69a231d7369dd165ad7ce4742e1a2e21e3462b5/lib/rspec/support/differ.rb#L178
117
91
 
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.
92
+ ## Installation
120
93
 
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.
94
+ Want to try out this gem for yourself? As with most development-related gems,
95
+ there are a couple ways depending on your type of project:
129
96
 
130
- To get started, add the gem to your Gemfile under the `test` group:
97
+ ### Rails apps
98
+
99
+ If you're developing a Rails app, add the following to your Gemfile:
131
100
 
132
101
  ``` ruby
133
102
  gem "super_diff"
134
103
  ```
135
104
 
136
- Then, open up `spec_helper` and add this line somewhere:
105
+ After running `bundle install`, add the following to your `rails_helper`:
106
+
107
+ ``` ruby
108
+ require "super_diff/rspec-rails"
109
+ ```
110
+
111
+ You're done!
112
+
113
+ ### Libraries
114
+
115
+ If you're developing a library, add the following to your gemspec:
116
+
117
+ ``` ruby
118
+ spec.add_development_dependency "super_diff"
119
+ ```
120
+
121
+ Now add the following to your `spec_helper`:
137
122
 
138
123
  ``` ruby
139
124
  require "super_diff/rspec"
140
125
  ```
141
126
 
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.
127
+ You're done!
128
+
129
+ ## Configuration
130
+
131
+ As capable as this library is, it doesn't know how to deal with every kind of
132
+ object out there. You might find it necessary to instruct the gem on how to diff
133
+ your object. To do this, you can use a configuration block. Simply add this to
134
+ your test helper file (either `rails_helper` or `spec_helper`):
135
+
136
+ ``` ruby
137
+ SuperDiff::RSpec.configure do |config|
138
+ config.add_extra_differ_class(YourDiffer)
139
+ config.add_extra_operational_sequencer_class(YourOperationalSequencer)
140
+ config.add_extra_diff_formatter_class(YourDiffFormatter)
141
+ end
142
+ ```
143
+
144
+ *(More info here in the future on adding a custom differ, operational sequencer,
145
+ and diff formatter. Also explanations on what these are.)*
144
146
 
145
147
  ## Contributing
146
148
 
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.
149
+ If you encounter a bug or have an idea for how this could be better, feel free
150
+ to [create an issue](https://github.com/mcmire/super_diff/issues).
149
151
 
150
152
  If you'd like to submit a PR instead, here's how to get started. First, fork
151
- this repo and then run:
153
+ this repo. Then, when you've cloned your fork, run:
152
154
 
153
155
  ```
154
156
  bundle install
155
157
  ```
156
158
 
157
- This will install dependencies. From here you can run all of the tests:
159
+ This will install various dependencies. After this, you can run all of the
160
+ tests:
158
161
 
159
162
  ```
160
163
  bundle exec rake
@@ -169,6 +172,31 @@ bundle exec rspec spec/unit/...
169
172
 
170
173
  Finally, submit your PR and I'll take a look at it when I get a chance.
171
174
 
175
+ ## Compatibility
176
+
177
+ `super_diff` is [tested][travis] to work with Ruby >= 2.4.x, RSpec 3.x, and
178
+ Rails >= 5.x.
179
+
180
+ [travis]: http://travis-ci.org/mcmire/super_diff
181
+
182
+ ## Inspiration/Thanks
183
+
184
+ In developing this gem I made use of or was heavily inspired by these libraries:
185
+
186
+ * [Diff::LCS][diff-lcs], the library I started with in the [original version of
187
+ this gem][original-version] (made in 2011!)
188
+ * The pretty-printing algorithms and API within [PrettyPrinter][pretty-printer]
189
+ and [AwesomePrint][awesome-print], from which I borrowed ideas to develop
190
+ the [inspectors][inspection-tree].
191
+
192
+ Thank you so much!
193
+
194
+ [original-version]: https://github.com/mcmire/super_diff/tree/old-master
195
+ [diff-lcs]: https://github.com/halostatue/diff-lcs
196
+ [pretty-printer]: https://github.com/ruby/ruby/tree/master/lib/prettyprint.rb
197
+ [awesome-print]: https://github.com/awesome-print/awesome_print
198
+ [inspection-tree]: https://github.com/mcmire/super_diff/blob/master/lib/super_diff/object_inspection/inspection_tree.rb
199
+
172
200
  ## Copyright/License
173
201
 
174
- © 2018 Elliot Winkler, released under the [MIT license](LICENSE).
202
+ © 2018-2019 Elliot Winkler, released under the [MIT license](LICENSE).
data/lib/super_diff.rb CHANGED
@@ -1,50 +1,36 @@
1
- require "patience_diff"
1
+ require "attr_extras/explicit"
2
2
  require "diff-lcs"
3
+ require "patience_diff"
3
4
 
4
- require_relative "super_diff/csi"
5
- require_relative "super_diff/errors"
6
- require_relative "super_diff/helpers"
7
- require_relative "super_diff/value_inspection"
8
-
9
- require_relative "super_diff/equality_matchers/base"
10
- require_relative "super_diff/equality_matchers/array"
11
- require_relative "super_diff/equality_matchers/hash"
12
- require_relative "super_diff/equality_matchers/multi_line_string"
13
- require_relative "super_diff/equality_matchers/single_line_string"
14
- require_relative "super_diff/equality_matchers/object"
15
- require_relative "super_diff/equality_matchers"
16
- require_relative "super_diff/equality_matcher"
17
-
18
- require_relative "super_diff/operations/unary_operation"
19
- require_relative "super_diff/operations/binary_operation"
20
-
21
- require_relative "super_diff/operation_sequences/base"
22
- require_relative "super_diff/operation_sequences/array"
23
- require_relative "super_diff/operation_sequences/hash"
24
- require_relative "super_diff/operation_sequences/object"
25
-
26
- require_relative "super_diff/operational_sequencers/base"
27
- require_relative "super_diff/operational_sequencers/array"
28
- require_relative "super_diff/operational_sequencers/hash"
29
- require_relative "super_diff/operational_sequencers/multi_line_string"
30
- require_relative "super_diff/operational_sequencers/object"
31
- require_relative "super_diff/operational_sequencers"
32
- require_relative "super_diff/operational_sequencer"
33
-
34
- require_relative "super_diff/diff_formatters/collection"
35
- require_relative "super_diff/diff_formatters/base"
36
- require_relative "super_diff/diff_formatters/array"
37
- require_relative "super_diff/diff_formatters/hash"
38
- require_relative "super_diff/diff_formatters/multi_line_string"
39
- require_relative "super_diff/diff_formatters/object"
40
- require_relative "super_diff/diff_formatters"
41
- require_relative "super_diff/diff_formatter"
5
+ module SuperDiff
6
+ autoload(
7
+ :ColorizedDocumentExtensions,
8
+ "super_diff/colorized_document_extensions",
9
+ )
10
+ autoload :Csi, "super_diff/csi"
11
+ autoload :DiffFormatter, "super_diff/diff_formatter"
12
+ autoload :DiffFormatters, "super_diff/diff_formatters"
13
+ autoload :Differ, "super_diff/differ"
14
+ autoload :Differs, "super_diff/differs"
15
+ autoload :EqualityMatcher, "super_diff/equality_matcher"
16
+ autoload :EqualityMatchers, "super_diff/equality_matchers"
17
+ autoload :Helpers, "super_diff/helpers"
18
+ autoload :NoDifferAvailableError, "super_diff/no_differ_available_error"
19
+ autoload(
20
+ :NoOperationalSequencerAvailableError,
21
+ "super_diff/no_operational_sequencer_available_error",
22
+ )
23
+ autoload :ObjectInspection, "super_diff/object_inspection"
24
+ autoload :OperationalSequencer, "super_diff/operational_sequencer"
25
+ autoload :OperationalSequencers, "super_diff/operational_sequencers"
26
+ autoload :OperationSequences, "super_diff/operation_sequences"
27
+ autoload :Operations, "super_diff/operations"
28
+ autoload :RecursionGuard, "super_diff/recursion_guard"
42
29
 
43
- require_relative "super_diff/differs/base"
44
- require_relative "super_diff/differs/array"
45
- require_relative "super_diff/differs/empty"
46
- require_relative "super_diff/differs/hash"
47
- require_relative "super_diff/differs/multi_line_string"
48
- require_relative "super_diff/differs/object"
49
- require_relative "super_diff/differs"
50
- require_relative "super_diff/differ"
30
+ COLORS = {
31
+ alpha: :magenta,
32
+ beta: :yellow,
33
+ border: :blue,
34
+ header: :white,
35
+ }.freeze
36
+ end
@@ -0,0 +1,41 @@
1
+ require "super_diff/active_support"
2
+
3
+ module SuperDiff
4
+ module ActiveRecord
5
+ autoload :DiffFormatters, "super_diff/active_record/diff_formatters"
6
+ autoload :Differs, "super_diff/active_record/differs"
7
+ autoload(
8
+ :ObjectInspection,
9
+ "super_diff/active_record/object_inspection",
10
+ )
11
+ autoload(
12
+ :OperationSequences,
13
+ "super_diff/active_record/operation_sequences",
14
+ )
15
+ autoload(
16
+ :OperationalSequencers,
17
+ "super_diff/active_record/operational_sequencers",
18
+ )
19
+ end
20
+ end
21
+
22
+ if defined?(SuperDiff::RSpec)
23
+ SuperDiff::RSpec.configure do |config|
24
+ config.add_extra_differ_class(
25
+ SuperDiff::ActiveRecord::Differs::ActiveRecordRelation,
26
+ )
27
+ config.add_extra_operational_sequencer_class(
28
+ SuperDiff::ActiveRecord::OperationalSequencers::ActiveRecordModel,
29
+ )
30
+ config.add_extra_operational_sequencer_class(
31
+ SuperDiff::ActiveRecord::OperationalSequencers::ActiveRecordRelation,
32
+ )
33
+ config.add_extra_diff_formatter_class(
34
+ SuperDiff::ActiveRecord::DiffFormatters::ActiveRecordRelation,
35
+ )
36
+ end
37
+ end
38
+
39
+ SuperDiff::ObjectInspection.map.prepend(
40
+ SuperDiff::ActiveRecord::ObjectInspection::MapExtension,
41
+ )
@@ -0,0 +1,10 @@
1
+ module SuperDiff
2
+ module ActiveRecord
3
+ module DiffFormatters
4
+ autoload(
5
+ :ActiveRecordRelation,
6
+ "super_diff/active_record/diff_formatters/active_record_relation",
7
+ )
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ module SuperDiff
2
+ module ActiveRecord
3
+ module DiffFormatters
4
+ class ActiveRecordRelation < SuperDiff::DiffFormatters::Base
5
+ def self.applies_to?(operations)
6
+ operations.is_a?(OperationSequences::ActiveRecordRelation)
7
+ end
8
+
9
+ def call
10
+ SuperDiff::DiffFormatters::Collection.call(
11
+ open_token: "#<ActiveRecord::Relation [",
12
+ close_token: "]>",
13
+ collection_prefix: collection_prefix,
14
+ build_item_prefix: proc { "" },
15
+ operations: operations,
16
+ indent_level: indent_level,
17
+ add_comma: add_comma?,
18
+ )
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end