super_diff 0.5.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +46 -20
  3. data/lib/super_diff.rb +46 -7
  4. data/lib/super_diff/active_record.rb +7 -7
  5. data/lib/super_diff/active_record/differs/active_record_relation.rb +3 -13
  6. data/lib/super_diff/active_record/object_inspection.rb +2 -6
  7. data/lib/super_diff/active_record/object_inspection/inspection_tree_builders.rb +16 -0
  8. data/lib/super_diff/active_record/object_inspection/{inspectors → inspection_tree_builders}/active_record_model.rb +19 -20
  9. data/lib/super_diff/active_record/object_inspection/{inspectors → inspection_tree_builders}/active_record_relation.rb +9 -8
  10. data/lib/super_diff/active_record/operation_tree_flatteners.rb +10 -0
  11. data/lib/super_diff/active_record/operation_tree_flatteners/active_record_relation.rb +17 -0
  12. data/lib/super_diff/active_record/operation_trees/active_record_relation.rb +8 -7
  13. data/lib/super_diff/active_support.rb +8 -8
  14. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +3 -13
  15. data/lib/super_diff/active_support/object_inspection.rb +2 -6
  16. data/lib/super_diff/active_support/object_inspection/{inspectors.rb → inspection_tree_builders.rb} +2 -2
  17. data/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb +37 -0
  18. data/lib/super_diff/active_support/operation_tree_builders/hash_with_indifferent_access.rb +17 -0
  19. data/lib/super_diff/active_support/operation_tree_flatteners.rb +10 -0
  20. data/lib/super_diff/active_support/operation_tree_flatteners/hash_with_indifferent_access.rb +17 -0
  21. data/lib/super_diff/active_support/operation_trees/hash_with_indifferent_access.rb +4 -7
  22. data/lib/super_diff/colorized_document_extensions.rb +9 -6
  23. data/lib/super_diff/configuration.rb +102 -19
  24. data/lib/super_diff/csi.rb +1 -1
  25. data/lib/super_diff/csi/four_bit_color.rb +0 -2
  26. data/lib/super_diff/diff_formatters/collection.rb +2 -2
  27. data/lib/super_diff/diff_formatters/multiline_string.rb +4 -4
  28. data/lib/super_diff/differs/array.rb +1 -1
  29. data/lib/super_diff/differs/base.rb +3 -21
  30. data/lib/super_diff/differs/custom_object.rb +1 -1
  31. data/lib/super_diff/differs/default_object.rb +1 -1
  32. data/lib/super_diff/differs/hash.rb +1 -1
  33. data/lib/super_diff/differs/main.rb +1 -7
  34. data/lib/super_diff/differs/multiline_string.rb +1 -1
  35. data/lib/super_diff/differs/time_like.rb +1 -1
  36. data/lib/super_diff/equality_matchers/array.rb +4 -4
  37. data/lib/super_diff/equality_matchers/default.rb +4 -4
  38. data/lib/super_diff/equality_matchers/hash.rb +4 -4
  39. data/lib/super_diff/equality_matchers/multiline_string.rb +4 -4
  40. data/lib/super_diff/equality_matchers/primitive.rb +6 -9
  41. data/lib/super_diff/equality_matchers/singleline_string.rb +4 -4
  42. data/lib/super_diff/gem_version.rb +45 -0
  43. data/lib/super_diff/helpers.rb +52 -2
  44. data/lib/super_diff/line.rb +83 -0
  45. data/lib/super_diff/object_inspection.rb +12 -9
  46. data/lib/super_diff/object_inspection/inspection_tree.rb +183 -81
  47. data/lib/super_diff/object_inspection/inspection_tree_builders.rb +44 -0
  48. data/lib/super_diff/object_inspection/inspection_tree_builders/array.rb +38 -0
  49. data/lib/super_diff/object_inspection/inspection_tree_builders/base.rb +27 -0
  50. data/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb +37 -0
  51. data/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb +63 -0
  52. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/defaults.rb +1 -2
  53. data/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb +46 -0
  54. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/main.rb +5 -10
  55. data/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb +21 -0
  56. data/lib/super_diff/object_inspection/inspection_tree_builders/time_like.rb +64 -0
  57. data/lib/super_diff/object_inspection/nodes.rb +33 -32
  58. data/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb +97 -0
  59. data/lib/super_diff/object_inspection/nodes/as_prefix_when_rendering_to_lines.rb +27 -0
  60. data/lib/super_diff/object_inspection/nodes/as_prelude_when_rendering_to_lines.rb +27 -0
  61. data/lib/super_diff/object_inspection/nodes/as_single_line.rb +33 -0
  62. data/lib/super_diff/object_inspection/nodes/base.rb +55 -20
  63. data/lib/super_diff/object_inspection/nodes/inspection.rb +47 -7
  64. data/lib/super_diff/object_inspection/nodes/nesting.rb +16 -5
  65. data/lib/super_diff/object_inspection/nodes/only_when.rb +54 -0
  66. data/lib/super_diff/object_inspection/nodes/text.rb +16 -2
  67. data/lib/super_diff/object_inspection/nodes/when_empty.rb +21 -6
  68. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +20 -5
  69. data/lib/super_diff/object_inspection/nodes/when_rendering_to_lines.rb +27 -0
  70. data/lib/super_diff/object_inspection/nodes/when_rendering_to_string.rb +27 -0
  71. data/lib/super_diff/object_inspection/prefix_for_next_node.rb +6 -0
  72. data/lib/super_diff/object_inspection/prelude_for_next_node.rb +6 -0
  73. data/lib/super_diff/operation_tree_builders/array.rb +7 -10
  74. data/lib/super_diff/operation_tree_builders/base.rb +6 -6
  75. data/lib/super_diff/operation_tree_builders/custom_object.rb +5 -2
  76. data/lib/super_diff/operation_tree_builders/default_object.rb +1 -1
  77. data/lib/super_diff/operation_tree_builders/defaults.rb +1 -1
  78. data/lib/super_diff/operation_tree_builders/hash.rb +1 -8
  79. data/lib/super_diff/operation_tree_builders/multiline_string.rb +2 -6
  80. data/lib/super_diff/operation_tree_builders/time_like.rb +2 -2
  81. data/lib/super_diff/operation_tree_flatteners.rb +20 -0
  82. data/lib/super_diff/operation_tree_flatteners/array.rb +15 -0
  83. data/lib/super_diff/operation_tree_flatteners/base.rb +54 -0
  84. data/lib/super_diff/operation_tree_flatteners/collection.rb +139 -0
  85. data/lib/super_diff/operation_tree_flatteners/custom_object.rb +28 -0
  86. data/lib/super_diff/operation_tree_flatteners/default_object.rb +32 -0
  87. data/lib/super_diff/operation_tree_flatteners/hash.rb +41 -0
  88. data/lib/super_diff/operation_tree_flatteners/multiline_string.rb +17 -0
  89. data/lib/super_diff/operation_trees/array.rb +4 -7
  90. data/lib/super_diff/operation_trees/base.rb +39 -16
  91. data/lib/super_diff/operation_trees/custom_object.rb +4 -8
  92. data/lib/super_diff/operation_trees/default_object.rb +28 -13
  93. data/lib/super_diff/operation_trees/hash.rb +4 -7
  94. data/lib/super_diff/operation_trees/main.rb +1 -1
  95. data/lib/super_diff/operation_trees/multiline_string.rb +4 -7
  96. data/lib/super_diff/operations/binary_operation.rb +1 -6
  97. data/lib/super_diff/operations/unary_operation.rb +2 -30
  98. data/lib/super_diff/recursion_guard.rb +3 -3
  99. data/lib/super_diff/rspec.rb +41 -13
  100. data/lib/super_diff/rspec/differs/collection_including.rb +4 -2
  101. data/lib/super_diff/rspec/differs/hash_including.rb +4 -2
  102. data/lib/super_diff/rspec/matcher_text_builders/base.rb +7 -7
  103. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +6 -6
  104. data/lib/super_diff/rspec/matcher_text_builders/contain_exactly.rb +1 -1
  105. data/lib/super_diff/rspec/matcher_text_builders/have_predicate.rb +4 -4
  106. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +1 -1
  107. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +5 -5
  108. data/lib/super_diff/rspec/monkey_patches.rb +353 -306
  109. data/lib/super_diff/rspec/object_inspection.rb +4 -1
  110. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders.rb +44 -0
  111. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/collection_containing_exactly.rb +9 -8
  112. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_including.rb +34 -0
  113. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb +103 -0
  114. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb +36 -0
  115. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/instance_of.rb +26 -0
  116. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/kind_of.rb +26 -0
  117. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/object_having_attributes.rb +10 -12
  118. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/primitive.rb +10 -0
  119. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/value_within.rb +33 -0
  120. data/lib/super_diff/rspec/operation_tree_builders/collection_containing_exactly.rb +12 -4
  121. data/lib/super_diff/rspec/operation_tree_builders/collection_including.rb +10 -3
  122. data/lib/super_diff/rspec/operation_tree_builders/hash_including.rb +10 -3
  123. data/lib/super_diff/tiered_lines.rb +4 -0
  124. data/lib/super_diff/tiered_lines_elider.rb +490 -0
  125. data/lib/super_diff/tiered_lines_formatter.rb +79 -0
  126. data/lib/super_diff/version.rb +1 -1
  127. data/spec/examples.txt +407 -5
  128. data/spec/integration/rails/active_support_spec.rb +19 -0
  129. data/spec/integration/rspec/be_falsey_matcher_spec.rb +10 -10
  130. data/spec/integration/rspec/be_matcher_spec.rb +100 -100
  131. data/spec/integration/rspec/be_nil_matcher_spec.rb +10 -10
  132. data/spec/integration/rspec/be_predicate_matcher_spec.rb +103 -103
  133. data/spec/integration/rspec/be_truthy_matcher_spec.rb +10 -10
  134. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +113 -107
  135. data/spec/integration/rspec/eq_matcher_spec.rb +223 -277
  136. data/spec/integration/rspec/have_attributes_matcher_spec.rb +138 -129
  137. data/spec/integration/rspec/have_predicate_matcher_spec.rb +65 -65
  138. data/spec/integration/rspec/include_matcher_spec.rb +73 -73
  139. data/spec/integration/rspec/match_array_matcher_spec.rb +126 -119
  140. data/spec/integration/rspec/match_matcher_spec.rb +362 -274
  141. data/spec/integration/rspec/raise_error_matcher_spec.rb +86 -86
  142. data/spec/integration/rspec/respond_to_matcher_spec.rb +240 -240
  143. data/spec/integration/rspec/third_party_matcher_spec.rb +8 -8
  144. data/spec/integration/rspec/unhandled_errors_spec.rb +5 -5
  145. data/spec/spec_helper.rb +33 -13
  146. data/spec/support/command_runner.rb +15 -25
  147. data/spec/support/helpers.rb +21 -0
  148. data/spec/support/integration/helpers.rb +8 -2
  149. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +4 -4
  150. data/spec/support/integration/test_programs/base.rb +38 -10
  151. data/spec/support/integration/test_programs/rspec_active_record.rb +1 -1
  152. data/spec/support/integration/test_programs/rspec_active_support.rb +17 -0
  153. data/spec/support/integration/test_programs/rspec_rails.rb +1 -1
  154. data/spec/support/shared_examples/active_record.rb +109 -108
  155. data/spec/support/shared_examples/active_support.rb +65 -0
  156. data/spec/support/shared_examples/elided_diffs.rb +914 -0
  157. data/spec/support/shared_examples/hash_with_indifferent_access.rb +196 -232
  158. data/spec/support/unit/helpers.rb +15 -0
  159. data/spec/support/unit/matchers/match_output.rb +41 -0
  160. data/spec/unit/active_record/object_inspection_spec.rb +273 -0
  161. data/spec/unit/equality_matchers/main_spec.rb +445 -465
  162. data/spec/unit/helpers_spec.rb +61 -0
  163. data/spec/unit/operation_tree_flatteners/array_spec.rb +604 -0
  164. data/spec/unit/operation_tree_flatteners/custom_object_spec.rb +667 -0
  165. data/spec/unit/operation_tree_flatteners/default_object_spec.rb +687 -0
  166. data/spec/unit/operation_tree_flatteners/hash_spec.rb +632 -0
  167. data/spec/unit/operation_tree_flatteners/multiline_string_spec.rb +121 -0
  168. data/spec/unit/rspec/object_inspection_spec.rb +446 -0
  169. data/spec/unit/super_diff_spec.rb +1958 -0
  170. data/spec/unit/tiered_lines_elider_spec.rb +6356 -0
  171. data/spec/unit/tiered_lines_formatter_spec.rb +193 -0
  172. data/super_diff.gemspec +1 -1
  173. metadata +95 -50
  174. data/lib/super_diff/active_record/diff_formatters.rb +0 -10
  175. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +0 -23
  176. data/lib/super_diff/active_record/object_inspection/inspectors.rb +0 -16
  177. data/lib/super_diff/active_support/diff_formatters.rb +0 -10
  178. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +0 -36
  179. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +0 -28
  180. data/lib/super_diff/diff_formatters.rb +0 -14
  181. data/lib/super_diff/diff_formatters/array.rb +0 -21
  182. data/lib/super_diff/diff_formatters/base.rb +0 -33
  183. data/lib/super_diff/diff_formatters/custom_object.rb +0 -30
  184. data/lib/super_diff/diff_formatters/default_object.rb +0 -46
  185. data/lib/super_diff/diff_formatters/defaults.rb +0 -10
  186. data/lib/super_diff/diff_formatters/hash.rb +0 -34
  187. data/lib/super_diff/diff_formatters/main.rb +0 -41
  188. data/lib/super_diff/object_inspection/inspectors.rb +0 -23
  189. data/lib/super_diff/object_inspection/inspectors/array.rb +0 -32
  190. data/lib/super_diff/object_inspection/inspectors/base.rb +0 -36
  191. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +0 -37
  192. data/lib/super_diff/object_inspection/inspectors/default_object.rb +0 -61
  193. data/lib/super_diff/object_inspection/inspectors/hash.rb +0 -32
  194. data/lib/super_diff/object_inspection/inspectors/primitive.rb +0 -28
  195. data/lib/super_diff/object_inspection/inspectors/string.rb +0 -23
  196. data/lib/super_diff/object_inspection/inspectors/time_like.rb +0 -23
  197. data/lib/super_diff/object_inspection/nodes/break.rb +0 -15
  198. data/lib/super_diff/object_inspection/nodes/when_multiline.rb +0 -22
  199. data/lib/super_diff/object_inspection/nodes/when_singleline.rb +0 -24
  200. data/lib/super_diff/rspec/object_inspection/inspectors.rb +0 -40
  201. data/lib/super_diff/rspec/object_inspection/inspectors/collection_including.rb +0 -28
  202. data/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb +0 -31
  203. data/lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb +0 -23
  204. data/lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb +0 -23
  205. data/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb +0 -13
  206. data/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb +0 -29
  207. data/spec/support/object_id.rb +0 -27
  208. data/spec/support/ruby_versions.rb +0 -11
  209. data/spec/unit/object_inspection_spec.rb +0 -1168
@@ -1,15 +1,18 @@
1
1
  module SuperDiff
2
2
  module ActiveSupport
3
- autoload :DiffFormatters, "super_diff/active_support/diff_formatters"
4
3
  autoload :Differs, "super_diff/active_support/differs"
5
4
  autoload :ObjectInspection, "super_diff/active_support/object_inspection"
5
+ autoload(
6
+ :OperationTrees,
7
+ "super_diff/active_support/operation_trees",
8
+ )
6
9
  autoload(
7
10
  :OperationTreeBuilders,
8
11
  "super_diff/active_support/operation_tree_builders",
9
12
  )
10
13
  autoload(
11
- :OperationalSequences,
12
- "super_diff/active_support/operational_sequences",
14
+ :OperationTreeFlatteners,
15
+ "super_diff/active_support/operation_tree_flatteners",
13
16
  )
14
17
 
15
18
  SuperDiff.configure do |config|
@@ -19,11 +22,8 @@ module SuperDiff
19
22
  config.add_extra_operation_tree_builder_classes(
20
23
  OperationTreeBuilders::HashWithIndifferentAccess,
21
24
  )
22
- config.add_extra_diff_formatter_classes(
23
- DiffFormatters::HashWithIndifferentAccess,
24
- )
25
- config.add_extra_inspector_classes(
26
- ObjectInspection::Inspectors::HashWithIndifferentAccess,
25
+ config.add_extra_inspection_tree_builder_classes(
26
+ ObjectInspection::InspectionTreeBuilders::HashWithIndifferentAccess,
27
27
  )
28
28
  end
29
29
  end
@@ -13,20 +13,10 @@ module SuperDiff
13
13
  )
14
14
  end
15
15
 
16
- def call
17
- DiffFormatters::HashWithIndifferentAccess.call(
18
- operation_tree,
19
- indent_level: indent_level,
20
- )
21
- end
16
+ protected
22
17
 
23
- private
24
-
25
- def operation_tree
26
- OperationTreeBuilders::HashWithIndifferentAccess.call(
27
- expected: expected,
28
- actual: actual,
29
- )
18
+ def operation_tree_builder_class
19
+ OperationTreeBuilders::HashWithIndifferentAccess
30
20
  end
31
21
  end
32
22
  end
@@ -2,12 +2,8 @@ module SuperDiff
2
2
  module ActiveSupport
3
3
  module ObjectInspection
4
4
  autoload(
5
- :Inspectors,
6
- "super_diff/active_support/object_inspection/inspectors",
7
- )
8
- autoload(
9
- :MapExtension,
10
- "super_diff/active_support/object_inspection/map_extension",
5
+ :InspectionTreeBuilders,
6
+ "super_diff/active_support/object_inspection/inspection_tree_builders",
11
7
  )
12
8
  end
13
9
  end
@@ -1,10 +1,10 @@
1
1
  module SuperDiff
2
2
  module ActiveSupport
3
3
  module ObjectInspection
4
- module Inspectors
4
+ module InspectionTreeBuilders
5
5
  autoload(
6
6
  :HashWithIndifferentAccess,
7
- "super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access",
7
+ "super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access",
8
8
  )
9
9
  end
10
10
  end
@@ -0,0 +1,37 @@
1
+ module SuperDiff
2
+ module ActiveSupport
3
+ module ObjectInspection
4
+ module InspectionTreeBuilders
5
+ class HashWithIndifferentAccess < SuperDiff::ObjectInspection::InspectionTreeBuilders::Base
6
+ def self.applies_to?(value)
7
+ value.is_a?(::HashWithIndifferentAccess)
8
+ end
9
+
10
+ def call
11
+ SuperDiff::ObjectInspection::InspectionTree.new do
12
+ as_lines_when_rendering_to_lines(collection_bookend: :open) do
13
+ add_text "#<HashWithIndifferentAccess {"
14
+ end
15
+
16
+ when_rendering_to_string do
17
+ add_text " "
18
+ end
19
+
20
+ nested do |hash|
21
+ insert_hash_inspection_of(hash)
22
+ end
23
+
24
+ when_rendering_to_string do
25
+ add_text " "
26
+ end
27
+
28
+ as_lines_when_rendering_to_lines(collection_bookend: :close) do
29
+ add_text "}>"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -2,6 +2,17 @@ module SuperDiff
2
2
  module ActiveSupport
3
3
  module OperationTreeBuilders
4
4
  class HashWithIndifferentAccess < SuperDiff::OperationTreeBuilders::Hash
5
+ def self.applies_to?(expected, actual)
6
+ (
7
+ expected.is_a?(::HashWithIndifferentAccess) &&
8
+ actual.is_a?(::Hash)
9
+ ) ||
10
+ (
11
+ expected.is_a?(::Hash) &&
12
+ actual.is_a?(::HashWithIndifferentAccess)
13
+ )
14
+ end
15
+
5
16
  def initialize(expected:, actual:, **rest)
6
17
  super
7
18
 
@@ -15,6 +26,12 @@ module SuperDiff
15
26
  @actual = actual.to_h
16
27
  end
17
28
  end
29
+
30
+ protected
31
+
32
+ def build_operation_tree
33
+ OperationTrees::HashWithIndifferentAccess.new([])
34
+ end
18
35
  end
19
36
  end
20
37
  end
@@ -0,0 +1,10 @@
1
+ module SuperDiff
2
+ module ActiveSupport
3
+ module OperationTreeFlatteners
4
+ autoload(
5
+ :HashWithIndifferentAccess,
6
+ "super_diff/active_support/operation_tree_flatteners/hash_with_indifferent_access",
7
+ )
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module SuperDiff
2
+ module ActiveSupport
3
+ module OperationTreeFlatteners
4
+ class HashWithIndifferentAccess < SuperDiff::OperationTreeFlatteners::Hash
5
+ protected
6
+
7
+ def open_token
8
+ "#<HashWithIndifferentAccess {"
9
+ end
10
+
11
+ def close_token
12
+ "}>"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -2,13 +2,10 @@ module SuperDiff
2
2
  module ActiveSupport
3
3
  module OperationTrees
4
4
  class HashWithIndifferentAccess < SuperDiff::OperationTrees::Base
5
- def to_diff(indent_level:, collection_prefix:, add_comma:)
6
- DiffFormatters::HashWithIndifferentAccess.call(
7
- self,
8
- indent_level: indent_level,
9
- collection_prefix: collection_prefix,
10
- add_comma: add_comma,
11
- )
5
+ protected
6
+
7
+ def operation_tree_flattener_class
8
+ OperationTreeFlatteners::HashWithIndifferentAccess
12
9
  end
13
10
  end
14
11
  end
@@ -6,12 +6,15 @@ 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)
11
- end
12
-
13
- def beta(*args, **opts, &block)
14
- colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:beta), &block)
9
+ [:actual, :border, :elision_marker, :expected, :header].each do |method_name|
10
+ define_method(method_name) do |*args, **opts, &block|
11
+ colorize(
12
+ *args,
13
+ **opts,
14
+ fg: SuperDiff.configuration.public_send("#{method_name}_color"),
15
+ &block
16
+ )
17
+ end
15
18
  end
16
19
  end
17
20
  end
@@ -1,26 +1,101 @@
1
1
  module SuperDiff
2
2
  class Configuration
3
3
  attr_reader(
4
+ :extra_diff_formatter_classes,
4
5
  :extra_differ_classes,
6
+ :extra_inspection_tree_builder_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
+ :diff_elision_enabled,
14
+ :diff_elision_maximum,
15
+ :elision_marker_color,
16
+ :expected_color,
17
+ :header_color,
9
18
  )
10
19
 
11
- def initialize
20
+ def initialize(options = {})
21
+ @actual_color = :yellow
22
+ @border_color = :blue
23
+ @color_enabled = color_enabled_by_default?
24
+ @diff_elision_enabled = false
25
+ @diff_elision_maximum = 0
26
+ @elision_marker_color = :cyan
27
+ @expected_color = :magenta
28
+ @extra_diff_formatter_classes = [].freeze
12
29
  @extra_differ_classes = [].freeze
30
+ @extra_inspection_tree_builder_classes = [].freeze
13
31
  @extra_operation_tree_builder_classes = [].freeze
14
32
  @extra_operation_tree_classes = [].freeze
15
- @extra_diff_formatter_classes = [].freeze
16
- @extra_inspector_classes = [].freeze
33
+ @header_color = :white
34
+
35
+ merge!(options)
36
+ end
37
+
38
+ def initialize_dup(original)
39
+ super
40
+ @extra_differ_classes = original.extra_differ_classes.dup.freeze
41
+ @extra_operation_tree_builder_classes =
42
+ original.extra_operation_tree_builder_classes.dup.freeze
43
+ @extra_operation_tree_classes =
44
+ original.extra_operation_tree_classes.dup.freeze
45
+ @extra_inspection_tree_builder_classes =
46
+ original.extra_inspection_tree_builder_classes.dup.freeze
47
+ end
48
+
49
+ def color_enabled?
50
+ @color_enabled
51
+ end
52
+
53
+ def diff_elision_enabled?
54
+ @diff_elision_enabled
17
55
  end
18
56
 
57
+ def merge!(configuration_or_options)
58
+ options =
59
+ if configuration_or_options.is_a?(self.class)
60
+ configuration_or_options.to_h
61
+ else
62
+ configuration_or_options
63
+ end
64
+
65
+ options.each do |key, value|
66
+ instance_variable_set("@#{key}", value)
67
+ end
68
+
69
+ updated
70
+ end
71
+
72
+ def updated
73
+ SuperDiff::Csi.color_enabled = color_enabled?
74
+ end
75
+
76
+ def add_extra_diff_formatter_classes(*classes)
77
+ @extra_diff_formatter_classes =
78
+ (@extra_diff_formatter_classes + classes).freeze
79
+ end
80
+ alias_method(
81
+ :add_extra_diff_formatter_class,
82
+ :add_extra_diff_formatter_classes,
83
+ )
84
+
19
85
  def add_extra_differ_classes(*classes)
20
86
  @extra_differ_classes = (@extra_differ_classes + classes).freeze
21
87
  end
22
88
  alias_method :add_extra_differ_class, :add_extra_differ_classes
23
89
 
90
+ def add_extra_inspection_tree_builder_classes(*classes)
91
+ @extra_inspection_tree_builder_classes =
92
+ (@extra_inspection_tree_builder_classes + classes).freeze
93
+ end
94
+ alias_method(
95
+ :add_extra_inspection_tree_builder_class,
96
+ :add_extra_inspection_tree_builder_classes,
97
+ )
98
+
24
99
  def add_extra_operation_tree_builder_classes(*classes)
25
100
  @extra_operation_tree_builder_classes =
26
101
  (@extra_operation_tree_builder_classes + classes).freeze
@@ -39,22 +114,30 @@ module SuperDiff
39
114
  :add_extra_operation_tree_classes,
40
115
  )
41
116
 
42
- def add_extra_diff_formatter_classes(*classes)
43
- @extra_diff_formatter_classes =
44
- (@extra_diff_formatter_classes + classes).freeze
117
+ def to_h
118
+ {
119
+ actual_color: actual_color,
120
+ border_color: border_color,
121
+ color_enabled: color_enabled?,
122
+ diff_elision_enabled: diff_elision_enabled?,
123
+ diff_elision_maximum: diff_elision_maximum,
124
+ elision_marker_color: elision_marker_color,
125
+ expected_color: expected_color,
126
+ extra_diff_formatter_classes: extra_diff_formatter_classes.dup,
127
+ extra_differ_classes: extra_differ_classes.dup,
128
+ extra_inspection_tree_builder_classes:
129
+ extra_inspection_tree_builder_classes.dup,
130
+ extra_operation_tree_builder_classes:
131
+ extra_operation_tree_builder_classes.dup,
132
+ extra_operation_tree_classes: extra_operation_tree_classes.dup,
133
+ header_color: header_color,
134
+ }
45
135
  end
46
- alias_method(
47
- :add_extra_diff_formatter_class,
48
- :add_extra_diff_formatter_classes,
49
- )
50
136
 
51
- def add_extra_inspector_classes(*classes)
52
- @extra_inspector_classes =
53
- (@extra_inspector_classes + classes).freeze
137
+ private
138
+
139
+ def color_enabled_by_default?
140
+ ENV["CI"] == "true" || $stdout.respond_to?(:tty?) && $stdout.tty?
54
141
  end
55
- alias_method(
56
- :add_extra_inspector_class,
57
- :add_extra_inspector_classes,
58
- )
59
142
  end
60
143
  end
@@ -58,6 +58,6 @@ module SuperDiff
58
58
  end
59
59
  end
60
60
 
61
- self.color_enabled = STDOUT.tty?
61
+ self.color_enabled = false
62
62
  end
63
63
  end
@@ -87,8 +87,6 @@ module SuperDiff
87
87
  def interpret_sequence!(sequence)
88
88
  match = sequence.match(OPENING_REGEX)
89
89
 
90
- # binding.pry
91
-
92
90
  if match
93
91
  interpret_code!(match[1].to_i)
94
92
  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
@@ -5,7 +5,7 @@ module SuperDiff
5
5
  expected.is_a?(::Array) && actual.is_a?(::Array)
6
6
  end
7
7
 
8
- private
8
+ protected
9
9
 
10
10
  def operation_tree_builder_class
11
11
  OperationTreeBuilders::Array