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
@@ -9,29 +9,14 @@ module SuperDiff
9
9
  extend AttrExtras.mixin
10
10
  include ImplementationChecks
11
11
 
12
- method_object(
13
- :expected,
14
- :actual,
15
- [
16
- :indent_level!,
17
- index_in_collection: nil,
18
- ],
19
- )
12
+ method_object :expected, :actual, [:indent_level!]
20
13
 
21
14
  def call
22
- operation_tree.to_diff(
23
- indent_level: indent_level,
24
- collection_prefix: nil,
25
- add_comma: false,
26
- )
15
+ operation_tree.to_diff(indentation_level: indent_level)
27
16
  end
28
17
 
29
18
  protected
30
19
 
31
- def indentation
32
- " " * (indent_level * 2)
33
- end
34
-
35
20
  def operation_tree_builder_class
36
21
  unimplemented_instance_method!
37
22
  end
@@ -39,10 +24,7 @@ module SuperDiff
39
24
  private
40
25
 
41
26
  def operation_tree
42
- operation_tree_builder_class.call(
43
- expected: expected,
44
- actual: actual,
45
- )
27
+ operation_tree_builder_class.call(expected: expected, actual: actual)
46
28
  end
47
29
  end
48
30
  end
@@ -7,7 +7,7 @@ module SuperDiff
7
7
  actual.respond_to?(:attributes_for_super_diff)
8
8
  end
9
9
 
10
- private
10
+ protected
11
11
 
12
12
  def operation_tree_builder_class
13
13
  OperationTreeBuilders::CustomObject
@@ -5,7 +5,7 @@ module SuperDiff
5
5
  expected.class == actual.class
6
6
  end
7
7
 
8
- private
8
+ protected
9
9
 
10
10
  def operation_tree
11
11
  OperationTreeBuilders::Main.call(
@@ -5,7 +5,7 @@ module SuperDiff
5
5
  expected.is_a?(::Hash) && actual.is_a?(::Hash)
6
6
  end
7
7
 
8
- private
8
+ protected
9
9
 
10
10
  def operation_tree_builder_class
11
11
  OperationTreeBuilders::Hash
@@ -8,19 +8,13 @@ module SuperDiff
8
8
  :actual,
9
9
  [
10
10
  indent_level: 0,
11
- index_in_collection: nil,
12
11
  omit_empty: false,
13
12
  ],
14
13
  )
15
14
 
16
15
  def call
17
16
  if resolved_class
18
- resolved_class.call(
19
- expected,
20
- actual,
21
- indent_level: indent_level,
22
- index_in_collection: index_in_collection,
23
- )
17
+ resolved_class.call(expected, actual, indent_level: indent_level)
24
18
  else
25
19
  raise Errors::NoDifferAvailableError.create(expected, actual)
26
20
  end
@@ -6,7 +6,7 @@ module SuperDiff
6
6
  (expected.include?("\n") || actual.include?("\n"))
7
7
  end
8
8
 
9
- private
9
+ protected
10
10
 
11
11
  def operation_tree_builder_class
12
12
  OperationTreeBuilders::MultilineString
@@ -5,7 +5,7 @@ module SuperDiff
5
5
  SuperDiff.time_like?(expected) && SuperDiff.time_like?(actual)
6
6
  end
7
7
 
8
- private
8
+ protected
9
9
 
10
10
  def operation_tree_builder_class
11
11
  OperationTreeBuilders::TimeLike
@@ -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_lines: false),
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_lines: false),
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_lines: false),
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_lines: false),
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_lines: false),
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_lines: false),
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_lines: false),
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_lines: false),
25
25
  )
26
26
  }
27
27
 
@@ -2,11 +2,8 @@ module SuperDiff
2
2
  module EqualityMatchers
3
3
  class Primitive < Base
4
4
  def self.applies_to?(value)
5
- value.is_a?(Symbol) ||
6
- value.is_a?(Numeric) ||
7
- # TODO: Test this
8
- value == true ||
9
- value == false
5
+ # TODO: Test all of these options
6
+ SuperDiff.primitive?(value)
10
7
  end
11
8
 
12
9
  def fail
@@ -15,16 +12,16 @@ module SuperDiff
15
12
 
16
13
  #{
17
14
  Helpers.style(
18
- :alpha,
15
+ :expected,
19
16
  "Expected: " +
20
- ObjectInspection.inspect(expected, as_single_line: true),
17
+ SuperDiff.inspect_object(expected, as_lines: false),
21
18
  )
22
19
  }
23
20
  #{
24
21
  Helpers.style(
25
- :beta,
22
+ :actual,
26
23
  " Actual: " +
27
- ObjectInspection.inspect(actual, as_single_line: true),
24
+ SuperDiff.inspect_object(actual, as_lines: false),
28
25
  )
29
26
  }
30
27
  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_lines: false),
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_lines: false),
24
24
  )
25
25
  }
26
26
  OUTPUT
@@ -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
@@ -1,7 +1,9 @@
1
1
  module SuperDiff
2
2
  module Helpers
3
+ extend self
4
+
3
5
  # TODO: Simplify this
4
- def self.style(*args, color_enabled: true, **opts, &block)
6
+ def style(*args, color_enabled: true, **opts, &block)
5
7
  klass =
6
8
  if color_enabled && Csi.color_enabled?
7
9
  Csi::ColorizedDocument
@@ -20,7 +22,7 @@ module SuperDiff
20
22
  document
21
23
  end
22
24
 
23
- def self.plural_type_for(value)
25
+ def plural_type_for(value)
24
26
  case value
25
27
  when Numeric then "numbers"
26
28
  when String then "strings"
@@ -28,5 +30,53 @@ module SuperDiff
28
30
  else "objects"
29
31
  end
30
32
  end
33
+
34
+ def jruby?
35
+ defined?(JRUBY_VERSION)
36
+ end
37
+
38
+ def ruby_version_matches?(version_string)
39
+ Gem::Requirement.new(version_string).satisfied_by?(
40
+ Gem::Version.new(RUBY_VERSION),
41
+ )
42
+ end
43
+
44
+ if jruby?
45
+ def object_address_for(object)
46
+ # Source: <https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/RubyBasicObject.java>
47
+ "0x%x" % object.hash
48
+ end
49
+ elsif ruby_version_matches?(">= 2.7.0")
50
+ require "json"
51
+ require "objspace"
52
+
53
+ def object_address_for(object)
54
+ # Sources: <https://bugs.ruby-lang.org/issues/15408> and <https://bugs.ruby-lang.org/issues/15626#Object-ID>
55
+ address = JSON.parse(ObjectSpace.dump(object))["address"]
56
+ "0x%016x" % Integer(address, 16)
57
+ end
58
+ else
59
+ def object_address_for(object)
60
+ "0x%016x" % (object.object_id * 2)
61
+ end
62
+ end
63
+
64
+ def with_slice_of_array_replaced(array, range, replacement)
65
+ beginning =
66
+ if range.begin > 0
67
+ array[Range.new(0, range.begin - 1)]
68
+ else
69
+ []
70
+ end
71
+
72
+ ending =
73
+ if range.end <= array.length - 1
74
+ array[Range.new(range.end + 1, array.length - 1)]
75
+ else
76
+ []
77
+ end
78
+
79
+ beginning + [replacement] + ending
80
+ end
31
81
  end
32
82
  end
@@ -0,0 +1,83 @@
1
+ module SuperDiff
2
+ class Line
3
+ extend AttrExtras.mixin
4
+
5
+ ICONS = { delete: "-", insert: "+", noop: " " }.freeze
6
+ COLORS = { insert: :actual, delete: :expected, noop: :plain }.freeze
7
+
8
+ rattr_initialize(
9
+ [
10
+ :type!,
11
+ :indentation_level!,
12
+ :value!,
13
+ prefix: "",
14
+ add_comma: false,
15
+ children: [],
16
+ elided: false,
17
+ collection_bookend: nil,
18
+ complete_bookend: nil,
19
+ ],
20
+ )
21
+ attr_query :add_comma?
22
+ attr_query :elided?
23
+
24
+ def clone_with(overrides = {})
25
+ self.class.new(
26
+ type: type,
27
+ indentation_level: indentation_level,
28
+ prefix: prefix,
29
+ value: value,
30
+ add_comma: add_comma?,
31
+ children: children,
32
+ elided: elided?,
33
+ collection_bookend: collection_bookend,
34
+ complete_bookend: complete_bookend,
35
+ **overrides,
36
+ )
37
+ end
38
+
39
+ def icon
40
+ ICONS.fetch(type)
41
+ end
42
+
43
+ def color
44
+ COLORS.fetch(type)
45
+ end
46
+
47
+ def with_comma
48
+ clone_with(add_comma: true)
49
+ end
50
+
51
+ def as_elided
52
+ clone_with(elided: true)
53
+ end
54
+
55
+ def with_value_prepended(prelude)
56
+ clone_with(value: prelude + value)
57
+ end
58
+
59
+ def with_value_appended(suffix)
60
+ clone_with(value: value + suffix)
61
+ end
62
+
63
+ def prefixed_with(prefix)
64
+ clone_with(prefix: prefix + self.prefix)
65
+ end
66
+
67
+ def with_complete_bookend(complete_bookend)
68
+ clone_with(complete_bookend: complete_bookend)
69
+ end
70
+
71
+ def opens_collection?
72
+ collection_bookend == :open
73
+ end
74
+
75
+ def closes_collection?
76
+ collection_bookend == :close
77
+ end
78
+
79
+ def complete_bookend?
80
+ complete_bookend != nil
81
+ end
82
+ end
83
+ end