super_diff 0.6.2 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -1
  3. data/lib/super_diff/active_record/differs/active_record_relation.rb +3 -13
  4. data/lib/super_diff/active_record/object_inspection/{inspectors → inspection_tree_builders}/active_record_model.rb +19 -20
  5. data/lib/super_diff/active_record/object_inspection/{inspectors → inspection_tree_builders}/active_record_relation.rb +9 -8
  6. data/lib/super_diff/active_record/object_inspection/inspection_tree_builders.rb +16 -0
  7. data/lib/super_diff/active_record/object_inspection.rb +2 -6
  8. data/lib/super_diff/active_record/operation_tree_flatteners/active_record_relation.rb +17 -0
  9. data/lib/super_diff/active_record/operation_tree_flatteners.rb +10 -0
  10. data/lib/super_diff/active_record/operation_trees/active_record_relation.rb +8 -7
  11. data/lib/super_diff/active_record.rb +7 -7
  12. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +3 -13
  13. data/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb +37 -0
  14. data/lib/super_diff/active_support/object_inspection/{inspectors.rb → inspection_tree_builders.rb} +2 -2
  15. data/lib/super_diff/active_support/object_inspection.rb +2 -6
  16. data/lib/super_diff/active_support/operation_tree_builders/hash_with_indifferent_access.rb +17 -0
  17. data/lib/super_diff/active_support/operation_tree_flatteners/hash_with_indifferent_access.rb +17 -0
  18. data/lib/super_diff/active_support/operation_tree_flatteners.rb +10 -0
  19. data/lib/super_diff/active_support/operation_trees/hash_with_indifferent_access.rb +4 -7
  20. data/lib/super_diff/active_support.rb +8 -8
  21. data/lib/super_diff/colorized_document_extensions.rb +9 -6
  22. data/lib/super_diff/configuration.rb +81 -8
  23. data/lib/super_diff/csi/four_bit_color.rb +0 -2
  24. data/lib/super_diff/csi.rb +1 -2
  25. data/lib/super_diff/differs/array.rb +1 -1
  26. data/lib/super_diff/differs/base.rb +3 -21
  27. data/lib/super_diff/differs/custom_object.rb +1 -1
  28. data/lib/super_diff/differs/default_object.rb +1 -1
  29. data/lib/super_diff/differs/hash.rb +1 -1
  30. data/lib/super_diff/differs/main.rb +1 -7
  31. data/lib/super_diff/differs/multiline_string.rb +1 -1
  32. data/lib/super_diff/differs/time_like.rb +1 -1
  33. data/lib/super_diff/equality_matchers/array.rb +2 -2
  34. data/lib/super_diff/equality_matchers/default.rb +2 -2
  35. data/lib/super_diff/equality_matchers/hash.rb +2 -2
  36. data/lib/super_diff/equality_matchers/multiline_string.rb +2 -2
  37. data/lib/super_diff/equality_matchers/primitive.rb +4 -7
  38. data/lib/super_diff/equality_matchers/singleline_string.rb +2 -2
  39. data/lib/super_diff/helpers.rb +56 -2
  40. data/lib/super_diff/line.rb +83 -0
  41. data/lib/super_diff/object_inspection/inspection_tree.rb +183 -81
  42. data/lib/super_diff/object_inspection/inspection_tree_builders/array.rb +38 -0
  43. data/lib/super_diff/object_inspection/inspection_tree_builders/base.rb +27 -0
  44. data/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb +37 -0
  45. data/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb +63 -0
  46. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/defaults.rb +1 -2
  47. data/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb +46 -0
  48. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/main.rb +5 -10
  49. data/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb +21 -0
  50. data/lib/super_diff/object_inspection/inspection_tree_builders/time_like.rb +64 -0
  51. data/lib/super_diff/object_inspection/inspection_tree_builders.rb +44 -0
  52. data/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb +97 -0
  53. data/lib/super_diff/object_inspection/nodes/as_prefix_when_rendering_to_lines.rb +27 -0
  54. data/lib/super_diff/object_inspection/nodes/as_prelude_when_rendering_to_lines.rb +27 -0
  55. data/lib/super_diff/object_inspection/nodes/as_single_line.rb +33 -0
  56. data/lib/super_diff/object_inspection/nodes/base.rb +55 -20
  57. data/lib/super_diff/object_inspection/nodes/inspection.rb +47 -7
  58. data/lib/super_diff/object_inspection/nodes/nesting.rb +16 -5
  59. data/lib/super_diff/object_inspection/nodes/only_when.rb +54 -0
  60. data/lib/super_diff/object_inspection/nodes/text.rb +16 -2
  61. data/lib/super_diff/object_inspection/nodes/when_empty.rb +21 -6
  62. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +20 -5
  63. data/lib/super_diff/object_inspection/nodes/when_rendering_to_lines.rb +27 -0
  64. data/lib/super_diff/object_inspection/nodes/when_rendering_to_string.rb +27 -0
  65. data/lib/super_diff/object_inspection/nodes.rb +33 -32
  66. data/lib/super_diff/object_inspection/prefix_for_next_node.rb +6 -0
  67. data/lib/super_diff/object_inspection/prelude_for_next_node.rb +6 -0
  68. data/lib/super_diff/object_inspection.rb +12 -1
  69. data/lib/super_diff/operation_tree_builders/array.rb +7 -10
  70. data/lib/super_diff/operation_tree_builders/base.rb +6 -6
  71. data/lib/super_diff/operation_tree_builders/custom_object.rb +5 -2
  72. data/lib/super_diff/operation_tree_builders/default_object.rb +1 -1
  73. data/lib/super_diff/operation_tree_builders/defaults.rb +1 -1
  74. data/lib/super_diff/operation_tree_builders/hash.rb +1 -8
  75. data/lib/super_diff/operation_tree_builders/multiline_string.rb +2 -6
  76. data/lib/super_diff/operation_tree_builders/time_like.rb +2 -2
  77. data/lib/super_diff/operation_tree_flatteners/array.rb +15 -0
  78. data/lib/super_diff/operation_tree_flatteners/base.rb +54 -0
  79. data/lib/super_diff/operation_tree_flatteners/collection.rb +139 -0
  80. data/lib/super_diff/operation_tree_flatteners/custom_object.rb +28 -0
  81. data/lib/super_diff/operation_tree_flatteners/default_object.rb +32 -0
  82. data/lib/super_diff/operation_tree_flatteners/hash.rb +41 -0
  83. data/lib/super_diff/operation_tree_flatteners/multiline_string.rb +17 -0
  84. data/lib/super_diff/operation_tree_flatteners.rb +20 -0
  85. data/lib/super_diff/operation_trees/array.rb +4 -7
  86. data/lib/super_diff/operation_trees/base.rb +39 -16
  87. data/lib/super_diff/operation_trees/custom_object.rb +4 -8
  88. data/lib/super_diff/operation_trees/default_object.rb +28 -13
  89. data/lib/super_diff/operation_trees/hash.rb +4 -7
  90. data/lib/super_diff/operation_trees/main.rb +1 -1
  91. data/lib/super_diff/operation_trees/multiline_string.rb +4 -7
  92. data/lib/super_diff/operations/binary_operation.rb +1 -6
  93. data/lib/super_diff/operations/unary_operation.rb +2 -30
  94. data/lib/super_diff/recursion_guard.rb +3 -3
  95. data/lib/super_diff/rspec/differs/collection_including.rb +4 -2
  96. data/lib/super_diff/rspec/differs/hash_including.rb +4 -2
  97. data/lib/super_diff/rspec/monkey_patches.rb +2 -2
  98. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/collection_containing_exactly.rb +9 -8
  99. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_including.rb +34 -0
  100. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb +103 -0
  101. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb +36 -0
  102. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/instance_of.rb +26 -0
  103. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/kind_of.rb +26 -0
  104. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/object_having_attributes.rb +10 -12
  105. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/primitive.rb +10 -0
  106. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/value_within.rb +33 -0
  107. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders.rb +44 -0
  108. data/lib/super_diff/rspec/object_inspection.rb +4 -1
  109. data/lib/super_diff/rspec/operation_tree_builders/collection_containing_exactly.rb +0 -3
  110. data/lib/super_diff/rspec/operation_tree_builders/collection_including.rb +10 -3
  111. data/lib/super_diff/rspec/operation_tree_builders/hash_including.rb +10 -3
  112. data/lib/super_diff/rspec.rb +34 -13
  113. data/lib/super_diff/tiered_lines.rb +4 -0
  114. data/lib/super_diff/tiered_lines_elider.rb +490 -0
  115. data/lib/super_diff/tiered_lines_formatter.rb +79 -0
  116. data/lib/super_diff/version.rb +1 -1
  117. data/lib/super_diff.rb +28 -6
  118. data/spec/examples.txt +482 -403
  119. data/spec/integration/rails/active_support_spec.rb +19 -0
  120. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +12 -6
  121. data/spec/integration/rspec/eq_matcher_spec.rb +33 -87
  122. data/spec/integration/rspec/have_attributes_matcher_spec.rb +17 -8
  123. data/spec/integration/rspec/match_array_matcher_spec.rb +14 -7
  124. data/spec/integration/rspec/match_matcher_spec.rb +93 -5
  125. data/spec/spec_helper.rb +4 -1
  126. data/spec/support/command_runner.rb +15 -25
  127. data/spec/support/helpers.rb +21 -0
  128. data/spec/support/integration/helpers.rb +2 -0
  129. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +3 -3
  130. data/spec/support/integration/test_programs/base.rb +36 -10
  131. data/spec/support/shared_examples/active_record.rb +3 -2
  132. data/spec/support/shared_examples/active_support.rb +65 -0
  133. data/spec/support/shared_examples/elided_diffs.rb +914 -0
  134. data/spec/support/shared_examples/hash_with_indifferent_access.rb +16 -16
  135. data/spec/support/unit/helpers.rb +15 -0
  136. data/spec/support/unit/matchers/match_output.rb +41 -0
  137. data/spec/tmp/warnings_logger/all_warnings.txt +2 -0
  138. data/spec/unit/active_record/object_inspection_spec.rb +273 -0
  139. data/spec/unit/equality_matchers/main_spec.rb +55 -75
  140. data/spec/unit/helpers_spec.rb +75 -0
  141. data/spec/unit/operation_tree_flatteners/array_spec.rb +604 -0
  142. data/spec/unit/operation_tree_flatteners/custom_object_spec.rb +667 -0
  143. data/spec/unit/operation_tree_flatteners/default_object_spec.rb +687 -0
  144. data/spec/unit/operation_tree_flatteners/hash_spec.rb +632 -0
  145. data/spec/unit/operation_tree_flatteners/multiline_string_spec.rb +121 -0
  146. data/spec/unit/rspec/object_inspection_spec.rb +446 -0
  147. data/spec/unit/super_diff_spec.rb +1530 -800
  148. data/spec/unit/tiered_lines_elider_spec.rb +6356 -0
  149. data/spec/unit/tiered_lines_formatter_spec.rb +193 -0
  150. metadata +91 -52
  151. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +0 -23
  152. data/lib/super_diff/active_record/diff_formatters.rb +0 -10
  153. data/lib/super_diff/active_record/object_inspection/inspectors.rb +0 -16
  154. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +0 -36
  155. data/lib/super_diff/active_support/diff_formatters.rb +0 -10
  156. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +0 -28
  157. data/lib/super_diff/diff_formatters/array.rb +0 -21
  158. data/lib/super_diff/diff_formatters/base.rb +0 -33
  159. data/lib/super_diff/diff_formatters/custom_object.rb +0 -30
  160. data/lib/super_diff/diff_formatters/default_object.rb +0 -46
  161. data/lib/super_diff/diff_formatters/defaults.rb +0 -10
  162. data/lib/super_diff/diff_formatters/hash.rb +0 -34
  163. data/lib/super_diff/diff_formatters/main.rb +0 -41
  164. data/lib/super_diff/diff_formatters.rb +0 -14
  165. data/lib/super_diff/object_inspection/inspectors/array.rb +0 -32
  166. data/lib/super_diff/object_inspection/inspectors/base.rb +0 -36
  167. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +0 -37
  168. data/lib/super_diff/object_inspection/inspectors/default_object.rb +0 -61
  169. data/lib/super_diff/object_inspection/inspectors/hash.rb +0 -32
  170. data/lib/super_diff/object_inspection/inspectors/primitive.rb +0 -28
  171. data/lib/super_diff/object_inspection/inspectors/string.rb +0 -23
  172. data/lib/super_diff/object_inspection/inspectors/time_like.rb +0 -23
  173. data/lib/super_diff/object_inspection/inspectors.rb +0 -23
  174. data/lib/super_diff/object_inspection/nodes/break.rb +0 -15
  175. data/lib/super_diff/object_inspection/nodes/when_multiline.rb +0 -22
  176. data/lib/super_diff/object_inspection/nodes/when_singleline.rb +0 -24
  177. data/lib/super_diff/rspec/object_inspection/inspectors/collection_including.rb +0 -28
  178. data/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb +0 -31
  179. data/lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb +0 -23
  180. data/lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb +0 -23
  181. data/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb +0 -13
  182. data/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb +0 -29
  183. data/lib/super_diff/rspec/object_inspection/inspectors.rb +0 -40
  184. data/spec/combustion/Gemfile.lock +0 -173
  185. data/spec/support/object_id.rb +0 -27
  186. data/spec/support/ruby_versions.rb +0 -11
@@ -0,0 +1,44 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module InspectionTreeBuilders
4
+ autoload(
5
+ :Base,
6
+ "super_diff/object_inspection/inspection_tree_builders/base",
7
+ )
8
+ autoload(
9
+ :Array,
10
+ "super_diff/object_inspection/inspection_tree_builders/array",
11
+ )
12
+ autoload(
13
+ :CustomObject,
14
+ "super_diff/object_inspection/inspection_tree_builders/custom_object",
15
+ )
16
+ autoload(
17
+ :DefaultObject,
18
+ "super_diff/object_inspection/inspection_tree_builders/default_object",
19
+ )
20
+ autoload(
21
+ :Hash,
22
+ "super_diff/object_inspection/inspection_tree_builders/hash",
23
+ )
24
+ autoload(
25
+ :Main,
26
+ "super_diff/object_inspection/inspection_tree_builders/main",
27
+ )
28
+ autoload(
29
+ :Primitive,
30
+ "super_diff/object_inspection/inspection_tree_builders/primitive",
31
+ )
32
+ autoload(
33
+ :String,
34
+ "super_diff/object_inspection/inspection_tree_builders/string",
35
+ )
36
+ autoload(
37
+ :TimeLike,
38
+ "super_diff/object_inspection/inspection_tree_builders/time_like",
39
+ )
40
+ end
41
+ end
42
+ end
43
+
44
+ require "super_diff/object_inspection/inspection_tree_builders/defaults"
@@ -0,0 +1,97 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module Nodes
4
+ class AsLinesWhenRenderingToLines < Base
5
+ def self.node_name
6
+ :as_lines_when_rendering_to_lines
7
+ end
8
+
9
+ def self.method_name
10
+ :as_lines_when_rendering_to_lines
11
+ end
12
+
13
+ def initialize(
14
+ tree,
15
+ *args,
16
+ add_comma: false,
17
+ collection_bookend: nil,
18
+ **rest
19
+ )
20
+ super(tree, *args, **rest)
21
+
22
+ @add_comma = add_comma
23
+ @collection_bookend = collection_bookend
24
+ end
25
+
26
+ def render_to_string(object)
27
+ # TODO: This happens a lot, can we simplify this?
28
+ string =
29
+ if block
30
+ render_to_string_in_subtree(object)
31
+ else
32
+ immediate_value.to_s
33
+ end
34
+
35
+ if add_comma?
36
+ string + ","
37
+ else
38
+ string
39
+ end
40
+ end
41
+
42
+ def render_to_lines(object, type:, indentation_level:)
43
+ lines =
44
+ if block
45
+ render_to_lines_in_subtree(
46
+ object,
47
+ type: type,
48
+ indentation_level: indentation_level,
49
+ disallowed_node_names: [
50
+ :line,
51
+ :as_lines_when_rendering_to_lines,
52
+ ],
53
+ )
54
+ else
55
+ [
56
+ SuperDiff::Line.new(
57
+ type: type,
58
+ indentation_level: indentation_level,
59
+ value: immediate_value.to_s,
60
+ ),
61
+ ]
62
+ end
63
+
64
+ with_collection_bookend_added_to_last_line_in(
65
+ with_add_comma_added_to_last_line_in(lines),
66
+ )
67
+ end
68
+
69
+ private
70
+
71
+ attr_reader :collection_bookend
72
+
73
+ def add_comma?
74
+ @add_comma
75
+ end
76
+
77
+ def with_collection_bookend_added_to_last_line_in(lines)
78
+ if collection_bookend
79
+ lines[0..-2] + [
80
+ lines[-1].clone_with(collection_bookend: collection_bookend),
81
+ ]
82
+ else
83
+ lines
84
+ end
85
+ end
86
+
87
+ def with_add_comma_added_to_last_line_in(lines)
88
+ if add_comma?
89
+ lines[0..-2] + [lines[-1].clone_with(add_comma: add_comma?)]
90
+ else
91
+ lines
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,27 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module Nodes
4
+ class AsPrefixWhenRenderingToLines < Base
5
+ def self.name
6
+ :as_prefix_when_rendering_to_lines
7
+ end
8
+
9
+ def self.method_name
10
+ :as_prefix_when_rendering_to_lines
11
+ end
12
+
13
+ def render_to_string(object)
14
+ if block
15
+ render_to_string_in_subtree(object)
16
+ else
17
+ immediate_value.to_s
18
+ end
19
+ end
20
+
21
+ def render_to_lines(object, **)
22
+ ObjectInspection::PrefixForNextNode.new(render_to_string(object))
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module Nodes
4
+ class AsPreludeWhenRenderingToLines < Base
5
+ def self.name
6
+ :as_prelude_when_rendering_to_lines
7
+ end
8
+
9
+ def self.method_name
10
+ :as_prelude_when_rendering_to_lines
11
+ end
12
+
13
+ def render_to_string(object)
14
+ if block
15
+ render_to_string_in_subtree(object)
16
+ else
17
+ immediate_value.to_s
18
+ end
19
+ end
20
+
21
+ def render_to_lines(object, **)
22
+ ObjectInspection::PreludeForNextNode.new(render_to_string(object))
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module Nodes
4
+ class AsSingleLine < Base
5
+ def self.node_name
6
+ :as_single_line
7
+ end
8
+
9
+ def self.method_name
10
+ :as_single_line
11
+ end
12
+
13
+ def render_to_string(object)
14
+ if block
15
+ render_to_string_in_subtree(object)
16
+ else
17
+ immediate_value.to_s
18
+ end
19
+ end
20
+
21
+ def render_to_lines(object, type:, indentation_level:)
22
+ [
23
+ SuperDiff::Line.new(
24
+ type: type,
25
+ indentation_level: indentation_level,
26
+ value: render_to_string(object),
27
+ ),
28
+ ]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -2,7 +2,18 @@ module SuperDiff
2
2
  module ObjectInspection
3
3
  module Nodes
4
4
  class Base
5
- def initialize(tree, *args, &block)
5
+ def self.node_name
6
+ unimplemented_class_method!
7
+ end
8
+
9
+ def self.method_name
10
+ unimplemented_class_method!
11
+ end
12
+
13
+ include ImplementationChecks
14
+ extend ImplementationChecks
15
+
16
+ def initialize(tree, *args, **options, &block)
6
17
  if !args.empty? && block
7
18
  raise ArgumentError.new(
8
19
  "You cannot provide both an immediate value and a lazy value. " +
@@ -13,35 +24,40 @@ module SuperDiff
13
24
  @tree = tree
14
25
  @immediate_value = args.first
15
26
  @block = block
27
+ @options = options
16
28
  end
17
29
 
18
30
  def clone_with(
19
31
  tree: @tree,
20
32
  immediate_value: @immediate_value,
21
- block: @block
33
+ block: @block,
34
+ **rest
22
35
  )
23
36
  if block
24
- self.class.new(tree, &block)
37
+ self.class.new(tree, **options, **rest, &block)
25
38
  else
26
- self.class.new(tree, immediate_value)
39
+ self.class.new(tree, immediate_value, **options, **rest)
27
40
  end
28
41
  end
29
42
 
30
- def type
31
- self.class.name.
32
- sub(/^(.+)::(.+?)$/, '\2').
33
- gsub(/([a-z])([A-Z])/, '\1_\2').
34
- downcase.
35
- to_sym
43
+ def render(object, preferably_as_lines:, **rest)
44
+ if options[:as_lines] || preferably_as_lines
45
+ render_to_lines(object, **rest)
46
+ else
47
+ render_to_string(object)
48
+ end
36
49
  end
37
50
 
38
51
  # rubocop:disable Lint/UnusedMethodArgument
39
- def evaluate(object, indent_level:, as_single_line:)
52
+ def render_to_string(object)
40
53
  # rubocop:enable Lint/UnusedMethodArgument
41
- raise NotImplementedError.new(
42
- "Your node must provide an #evaluate method. " +
43
- "(Keep in mind #evaluate may be called more than once for a node!)",
44
- )
54
+ unimplemented_instance_method!
55
+ end
56
+
57
+ # rubocop:disable Lint/UnusedMethodArgument
58
+ def render_to_lines(object, type:, indentation_level:)
59
+ # rubocop:enable Lint/UnusedMethodArgument
60
+ unimplemented_instance_method!
45
61
  end
46
62
 
47
63
  def pretty_print(pp)
@@ -61,7 +77,7 @@ module SuperDiff
61
77
 
62
78
  private
63
79
 
64
- attr_reader :tree, :immediate_value, :block
80
+ attr_reader :tree, :immediate_value, :block, :options
65
81
 
66
82
  def pretty_print_variables
67
83
  if block
@@ -71,13 +87,32 @@ module SuperDiff
71
87
  end
72
88
  end
73
89
 
74
- def evaluate_in_subtree(object, indent_level:, as_single_line:)
90
+ def evaluate_block(object)
91
+ tree.evaluate_block(object, &block)
92
+ end
93
+
94
+ def render_to_string_in_subtree(object)
75
95
  subtree = InspectionTree.new
76
96
  subtree.evaluate_block(object, &block)
77
- subtree.evaluate(
97
+ subtree.render_to_string(object)
98
+ end
99
+
100
+ def render_to_lines_in_subtree(
101
+ object,
102
+ type:,
103
+ indentation_level:,
104
+ disallowed_node_names: [],
105
+ **rest
106
+ )
107
+ subtree = InspectionTree.new(
108
+ disallowed_node_names: disallowed_node_names,
109
+ )
110
+ subtree.evaluate_block(object, &block)
111
+ subtree.render_to_lines(
78
112
  object,
79
- indent_level: indent_level,
80
- as_single_line: as_single_line,
113
+ type: type,
114
+ indentation_level: indentation_level,
115
+ **rest,
81
116
  )
82
117
  end
83
118
  end
@@ -2,19 +2,59 @@ module SuperDiff
2
2
  module ObjectInspection
3
3
  module Nodes
4
4
  class Inspection < Base
5
- def evaluate(object, indent_level:, as_single_line:)
5
+ def self.node_name
6
+ :inspection
7
+ end
8
+
9
+ def self.method_name
10
+ :add_inspection_of
11
+ end
12
+
13
+ def render_to_string(object)
14
+ value =
15
+ if block
16
+ evaluate_block(object)
17
+ else
18
+ immediate_value
19
+ end
20
+
21
+ SuperDiff::RecursionGuard.
22
+ guarding_recursion_of(value) do |already_seen|
23
+ if already_seen
24
+ SuperDiff::RecursionGuard::PLACEHOLDER
25
+ else
26
+ SuperDiff.inspect_object(value, as_lines: false)
27
+ end
28
+ end
29
+ end
30
+
31
+ def render_to_lines(object, type:, indentation_level:)
6
32
  value =
7
33
  if block
8
- tree.evaluate_block(object, &block)
34
+ evaluate_block(object)
9
35
  else
10
36
  immediate_value
11
37
  end
12
38
 
13
- SuperDiff.inspect_object(
14
- value,
15
- indent_level: indent_level,
16
- as_single_line: as_single_line,
17
- )
39
+ SuperDiff::RecursionGuard.
40
+ guarding_recursion_of(value) do |already_seen|
41
+ if already_seen
42
+ [
43
+ SuperDiff::Line.new(
44
+ type: type,
45
+ indentation_level: indentation_level,
46
+ value: SuperDiff::RecursionGuard::PLACEHOLDER,
47
+ ),
48
+ ]
49
+ else
50
+ SuperDiff.inspect_object(
51
+ value,
52
+ as_lines: true,
53
+ type: type,
54
+ indentation_level: indentation_level,
55
+ )
56
+ end
57
+ end
18
58
  end
19
59
  end
20
60
  end
@@ -2,12 +2,23 @@ module SuperDiff
2
2
  module ObjectInspection
3
3
  module Nodes
4
4
  class Nesting < Base
5
- def evaluate(object, indent_level:, as_single_line:)
6
- evaluate_in_subtree(
5
+ def self.node_name
6
+ :nesting
7
+ end
8
+
9
+ def self.method_name
10
+ :nested
11
+ end
12
+
13
+ def render_to_string(object)
14
+ render_to_string_in_subtree(object)
15
+ end
16
+
17
+ def render_to_lines(object, type:, indentation_level:)
18
+ render_to_lines_in_subtree(
7
19
  object,
8
- indent_level: indent_level + 1,
9
- as_single_line: as_single_line,
10
- &block
20
+ type: type,
21
+ indentation_level: indentation_level + 1,
11
22
  )
12
23
  end
13
24
  end
@@ -0,0 +1,54 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module Nodes
4
+ class OnlyWhen < Base
5
+ def self.node_name
6
+ :only_when
7
+ end
8
+
9
+ def self.method_name
10
+ :only_when
11
+ end
12
+
13
+ def initialize(tree, test, **options, &block)
14
+ @tree = tree
15
+ @test = test
16
+ @block = block
17
+ @options = options
18
+ end
19
+
20
+ def render_to_string(object)
21
+ if test_passes?
22
+ render_to_string_in_subtree(object)
23
+ else
24
+ ""
25
+ end
26
+ end
27
+
28
+ def render_to_lines(object, type:, indentation_level:)
29
+ if test_passes?
30
+ render_to_lines_in_subtree(
31
+ object,
32
+ type: type,
33
+ indentation_level: indentation_level,
34
+ )
35
+ else
36
+ []
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ attr_reader :test
43
+
44
+ def test_passes?
45
+ if test.arity == 1
46
+ test.call(object)
47
+ else
48
+ test.call
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -2,13 +2,27 @@ module SuperDiff
2
2
  module ObjectInspection
3
3
  module Nodes
4
4
  class Text < Base
5
- def evaluate(object, **)
5
+ def self.node_name
6
+ :text
7
+ end
8
+
9
+ def self.method_name
10
+ :add_text
11
+ end
12
+
13
+ def render_to_string(object)
6
14
  if block
7
- tree.evaluate_block(object, &block).to_s
15
+ evaluate_block(object).to_s
8
16
  else
9
17
  immediate_value.to_s
10
18
  end
11
19
  end
20
+
21
+ def render_to_lines(object, **)
22
+ # NOTE: This is a bit of a hack since it returns a string instead of a
23
+ # Line, but it is handled in InspectionTree (see UpdateTieredLines)
24
+ render_to_string(object)
25
+ end
12
26
  end
13
27
  end
14
28
  end
@@ -2,16 +2,31 @@ module SuperDiff
2
2
  module ObjectInspection
3
3
  module Nodes
4
4
  class WhenEmpty < Base
5
- def evaluate(object, indent_level:, as_single_line:)
5
+ def self.node_name
6
+ :when_empty
7
+ end
8
+
9
+ def self.method_name
10
+ :when_empty
11
+ end
12
+
13
+ def render_to_string(object)
6
14
  if empty?(object)
7
- evaluate_in_subtree(
15
+ render_to_string_in_subtree(object)
16
+ else
17
+ ""
18
+ end
19
+ end
20
+
21
+ def render_to_lines(object, type:, indentation_level:)
22
+ if empty?(object)
23
+ render_to_lines_in_subtree(
8
24
  object,
9
- indent_level: indent_level,
10
- as_single_line: as_single_line,
11
- &block
25
+ type: type,
26
+ indentation_level: indentation_level,
12
27
  )
13
28
  else
14
- ""
29
+ []
15
30
  end
16
31
  end
17
32
 
@@ -2,15 +2,30 @@ module SuperDiff
2
2
  module ObjectInspection
3
3
  module Nodes
4
4
  class WhenNonEmpty < Base
5
- def evaluate(object, indent_level:, as_single_line:)
5
+ def self.node_name
6
+ :when_non_empty
7
+ end
8
+
9
+ def self.method_name
10
+ :when_non_empty
11
+ end
12
+
13
+ def render_to_string(object)
6
14
  if empty?(object)
7
15
  ""
8
16
  else
9
- evaluate_in_subtree(
17
+ render_to_string_in_subtree(object)
18
+ end
19
+ end
20
+
21
+ def render_to_lines(object, type:, indentation_level:)
22
+ if empty?(object)
23
+ []
24
+ else
25
+ render_to_lines_in_subtree(
10
26
  object,
11
- indent_level: indent_level,
12
- as_single_line: as_single_line,
13
- &block
27
+ type: type,
28
+ indentation_level: indentation_level,
14
29
  )
15
30
  end
16
31
  end
@@ -0,0 +1,27 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module Nodes
4
+ class WhenRenderingToLines < Base
5
+ def self.node_name
6
+ :when_rendering_to_lines
7
+ end
8
+
9
+ def self.method_name
10
+ :when_rendering_to_lines
11
+ end
12
+
13
+ def render_to_string(*)
14
+ ""
15
+ end
16
+
17
+ def render_to_lines(object, type:, indentation_level:)
18
+ render_to_lines_in_subtree(
19
+ object,
20
+ type: type,
21
+ indentation_level: indentation_level,
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module SuperDiff
2
+ module ObjectInspection
3
+ module Nodes
4
+ class WhenRenderingToString < Base
5
+ def self.node_name
6
+ :when_rendering_to_string
7
+ end
8
+
9
+ def self.method_name
10
+ :when_rendering_to_string
11
+ end
12
+
13
+ def render_to_string(object)
14
+ if block
15
+ render_to_string_in_subtree(object)
16
+ else
17
+ immediate_value.to_s
18
+ end
19
+ end
20
+
21
+ def render_to_lines(*, **)
22
+ []
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end