super_diff 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -1
  3. data/lib/super_diff.rb +27 -6
  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 +81 -8
  24. data/lib/super_diff/csi.rb +1 -2
  25. data/lib/super_diff/csi/four_bit_color.rb +0 -2
  26. data/lib/super_diff/differs/array.rb +1 -1
  27. data/lib/super_diff/differs/base.rb +3 -21
  28. data/lib/super_diff/differs/custom_object.rb +1 -1
  29. data/lib/super_diff/differs/default_object.rb +1 -1
  30. data/lib/super_diff/differs/hash.rb +1 -1
  31. data/lib/super_diff/differs/main.rb +1 -7
  32. data/lib/super_diff/differs/multiline_string.rb +1 -1
  33. data/lib/super_diff/differs/time_like.rb +1 -1
  34. data/lib/super_diff/equality_matchers/array.rb +2 -2
  35. data/lib/super_diff/equality_matchers/default.rb +2 -2
  36. data/lib/super_diff/equality_matchers/hash.rb +2 -2
  37. data/lib/super_diff/equality_matchers/multiline_string.rb +2 -2
  38. data/lib/super_diff/equality_matchers/primitive.rb +4 -7
  39. data/lib/super_diff/equality_matchers/singleline_string.rb +2 -2
  40. data/lib/super_diff/helpers.rb +52 -2
  41. data/lib/super_diff/line.rb +83 -0
  42. data/lib/super_diff/object_inspection.rb +12 -1
  43. data/lib/super_diff/object_inspection/inspection_tree.rb +183 -81
  44. data/lib/super_diff/object_inspection/inspection_tree_builders.rb +44 -0
  45. data/lib/super_diff/object_inspection/inspection_tree_builders/array.rb +38 -0
  46. data/lib/super_diff/object_inspection/inspection_tree_builders/base.rb +27 -0
  47. data/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb +37 -0
  48. data/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb +63 -0
  49. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/defaults.rb +1 -2
  50. data/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb +46 -0
  51. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/main.rb +5 -10
  52. data/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb +21 -0
  53. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/time_like.rb +19 -18
  54. data/lib/super_diff/object_inspection/nodes.rb +33 -32
  55. data/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb +97 -0
  56. data/lib/super_diff/object_inspection/nodes/as_prefix_when_rendering_to_lines.rb +27 -0
  57. data/lib/super_diff/object_inspection/nodes/as_prelude_when_rendering_to_lines.rb +27 -0
  58. data/lib/super_diff/object_inspection/nodes/as_single_line.rb +33 -0
  59. data/lib/super_diff/object_inspection/nodes/base.rb +55 -20
  60. data/lib/super_diff/object_inspection/nodes/inspection.rb +47 -7
  61. data/lib/super_diff/object_inspection/nodes/nesting.rb +16 -5
  62. data/lib/super_diff/object_inspection/nodes/only_when.rb +54 -0
  63. data/lib/super_diff/object_inspection/nodes/text.rb +16 -2
  64. data/lib/super_diff/object_inspection/nodes/when_empty.rb +21 -6
  65. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +20 -5
  66. data/lib/super_diff/object_inspection/nodes/when_rendering_to_lines.rb +27 -0
  67. data/lib/super_diff/object_inspection/nodes/when_rendering_to_string.rb +27 -0
  68. data/lib/super_diff/object_inspection/prefix_for_next_node.rb +6 -0
  69. data/lib/super_diff/object_inspection/prelude_for_next_node.rb +6 -0
  70. data/lib/super_diff/operation_tree_builders/array.rb +7 -10
  71. data/lib/super_diff/operation_tree_builders/base.rb +6 -6
  72. data/lib/super_diff/operation_tree_builders/custom_object.rb +5 -2
  73. data/lib/super_diff/operation_tree_builders/default_object.rb +1 -1
  74. data/lib/super_diff/operation_tree_builders/hash.rb +0 -7
  75. data/lib/super_diff/operation_tree_builders/multiline_string.rb +2 -6
  76. data/lib/super_diff/operation_tree_flatteners.rb +20 -0
  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_trees/array.rb +4 -7
  85. data/lib/super_diff/operation_trees/base.rb +39 -16
  86. data/lib/super_diff/operation_trees/custom_object.rb +4 -8
  87. data/lib/super_diff/operation_trees/default_object.rb +28 -13
  88. data/lib/super_diff/operation_trees/hash.rb +4 -7
  89. data/lib/super_diff/operation_trees/main.rb +1 -1
  90. data/lib/super_diff/operation_trees/multiline_string.rb +4 -7
  91. data/lib/super_diff/operations/binary_operation.rb +1 -6
  92. data/lib/super_diff/operations/unary_operation.rb +2 -30
  93. data/lib/super_diff/recursion_guard.rb +3 -3
  94. data/lib/super_diff/rspec.rb +12 -13
  95. data/lib/super_diff/rspec/monkey_patches.rb +2 -2
  96. data/lib/super_diff/rspec/object_inspection.rb +4 -1
  97. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders.rb +44 -0
  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/{inspectors → inspection_tree_builders}/collection_including.rb +9 -8
  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/{inspectors → inspection_tree_builders}/instance_of.rb +3 -5
  103. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/kind_of.rb +3 -5
  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/operation_tree_builders/collection_containing_exactly.rb +0 -3
  108. data/lib/super_diff/tiered_lines.rb +4 -0
  109. data/lib/super_diff/tiered_lines_elider.rb +490 -0
  110. data/lib/super_diff/tiered_lines_formatter.rb +79 -0
  111. data/lib/super_diff/version.rb +1 -1
  112. data/spec/examples.txt +407 -410
  113. data/spec/integration/rails/active_support_spec.rb +19 -0
  114. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +12 -6
  115. data/spec/integration/rspec/eq_matcher_spec.rb +22 -88
  116. data/spec/integration/rspec/have_attributes_matcher_spec.rb +6 -7
  117. data/spec/integration/rspec/match_array_matcher_spec.rb +14 -7
  118. data/spec/integration/rspec/match_matcher_spec.rb +10 -5
  119. data/spec/spec_helper.rb +1 -0
  120. data/spec/support/command_runner.rb +15 -25
  121. data/spec/support/helpers.rb +21 -0
  122. data/spec/support/integration/helpers.rb +2 -0
  123. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +3 -3
  124. data/spec/support/integration/test_programs/base.rb +36 -10
  125. data/spec/support/shared_examples/active_record.rb +3 -2
  126. data/spec/support/shared_examples/active_support.rb +65 -0
  127. data/spec/support/shared_examples/elided_diffs.rb +914 -0
  128. data/spec/support/shared_examples/hash_with_indifferent_access.rb +16 -16
  129. data/spec/support/unit/helpers.rb +15 -0
  130. data/spec/support/unit/matchers/match_output.rb +41 -0
  131. data/spec/unit/active_record/object_inspection_spec.rb +273 -0
  132. data/spec/unit/equality_matchers/main_spec.rb +51 -71
  133. data/spec/unit/helpers_spec.rb +61 -0
  134. data/spec/unit/operation_tree_flatteners/array_spec.rb +604 -0
  135. data/spec/unit/operation_tree_flatteners/custom_object_spec.rb +667 -0
  136. data/spec/unit/operation_tree_flatteners/default_object_spec.rb +687 -0
  137. data/spec/unit/operation_tree_flatteners/hash_spec.rb +632 -0
  138. data/spec/unit/operation_tree_flatteners/multiline_string_spec.rb +121 -0
  139. data/spec/unit/rspec/object_inspection_spec.rb +446 -0
  140. data/spec/unit/super_diff_spec.rb +1488 -846
  141. data/spec/unit/tiered_lines_elider_spec.rb +6356 -0
  142. data/spec/unit/tiered_lines_formatter_spec.rb +193 -0
  143. metadata +88 -50
  144. data/lib/super_diff/active_record/diff_formatters.rb +0 -10
  145. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +0 -23
  146. data/lib/super_diff/active_record/object_inspection/inspectors.rb +0 -16
  147. data/lib/super_diff/active_support/diff_formatters.rb +0 -10
  148. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +0 -36
  149. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +0 -28
  150. data/lib/super_diff/diff_formatters.rb +0 -14
  151. data/lib/super_diff/diff_formatters/array.rb +0 -21
  152. data/lib/super_diff/diff_formatters/base.rb +0 -33
  153. data/lib/super_diff/diff_formatters/custom_object.rb +0 -30
  154. data/lib/super_diff/diff_formatters/default_object.rb +0 -46
  155. data/lib/super_diff/diff_formatters/defaults.rb +0 -10
  156. data/lib/super_diff/diff_formatters/hash.rb +0 -34
  157. data/lib/super_diff/diff_formatters/main.rb +0 -41
  158. data/lib/super_diff/object_inspection/inspectors.rb +0 -23
  159. data/lib/super_diff/object_inspection/inspectors/array.rb +0 -32
  160. data/lib/super_diff/object_inspection/inspectors/base.rb +0 -36
  161. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +0 -37
  162. data/lib/super_diff/object_inspection/inspectors/default_object.rb +0 -61
  163. data/lib/super_diff/object_inspection/inspectors/hash.rb +0 -32
  164. data/lib/super_diff/object_inspection/inspectors/primitive.rb +0 -28
  165. data/lib/super_diff/object_inspection/inspectors/string.rb +0 -23
  166. data/lib/super_diff/object_inspection/nodes/break.rb +0 -15
  167. data/lib/super_diff/object_inspection/nodes/when_multiline.rb +0 -22
  168. data/lib/super_diff/object_inspection/nodes/when_singleline.rb +0 -24
  169. data/lib/super_diff/rspec/object_inspection/inspectors.rb +0 -40
  170. data/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb +0 -36
  171. data/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb +0 -13
  172. data/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb +0 -29
  173. data/spec/support/object_id.rb +0 -27
  174. data/spec/support/ruby_versions.rb +0 -11
@@ -0,0 +1,61 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperDiff::Helpers do
4
+ shared_examples 'helper tests' do
5
+ describe "with_slice_of_array_replaced" do
6
+ context "if the given range covers the whole array" do
7
+ it "returns a copy of the array replaced with the replacement" do
8
+ updated_array = helper.with_slice_of_array_replaced(
9
+ [1, 2, 3],
10
+ 0..2,
11
+ :x,
12
+ )
13
+
14
+ expect(updated_array).to eq([:x])
15
+ end
16
+ end
17
+
18
+ context "if the given range is at the start of the array" do
19
+ it "returns the selected slice replaced with the replacement + the rest" do
20
+ updated_array = helper.with_slice_of_array_replaced(
21
+ [1, 2, 3, 4, 5],
22
+ 0..2,
23
+ :x,
24
+ )
25
+
26
+ expect(updated_array).to eq([:x, 4, 5])
27
+ end
28
+ end
29
+
30
+ context "if the given range is at the end of the array" do
31
+ it "returns the selected slice replaced with the replacement + the rest" do
32
+ updated_array = helper.with_slice_of_array_replaced(
33
+ [1, 2, 3, 4, 5],
34
+ 2..4,
35
+ :x,
36
+ )
37
+
38
+ expect(updated_array).to eq([1, 2, :x])
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ describe 'as class methods' do
45
+ let(:helper) { described_class }
46
+
47
+ include_examples 'helper tests'
48
+ end
49
+
50
+ describe 'as instance methods' do
51
+ let(:helper) { described_class }
52
+ let(:helper) do
53
+ mod = described_class
54
+ Object.new.tap do |helper|
55
+ helper.singleton_class.class_eval { include mod }
56
+ end
57
+ end
58
+
59
+ include_examples 'helper tests'
60
+ end
61
+ end
@@ -0,0 +1,604 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe SuperDiff::OperationTreeFlatteners::Array do
4
+ context "given an empty tree" do
5
+ it "returns a set of lines which are simply the open token and close token" do
6
+ expect(described_class.call([])).to match([
7
+ an_object_having_attributes(
8
+ type: :noop,
9
+ indentation_level: 0,
10
+ prefix: "",
11
+ value: %([),
12
+ collection_bookend: :open,
13
+ complete_bookend: :open,
14
+ ),
15
+ an_object_having_attributes(
16
+ type: :noop,
17
+ indentation_level: 0,
18
+ prefix: "",
19
+ value: %(]),
20
+ collection_bookend: :close,
21
+ complete_bookend: :close,
22
+ ),
23
+ ])
24
+ end
25
+ end
26
+
27
+ context "given a tree of only noops" do
28
+ it "returns a series of lines from inspecting each value, creating multiple lines upon encountering inner data structures" do
29
+ collection = Array.new(3) { :some_value }
30
+ operation_tree = SuperDiff::OperationTrees::Array.new([
31
+ double(
32
+ :operation,
33
+ name: :noop,
34
+ collection: collection,
35
+ value: "foo",
36
+ index: 0,
37
+ ),
38
+ double(
39
+ :operation,
40
+ name: :noop,
41
+ collection: collection,
42
+ value: ["one fish", "two fish"],
43
+ index: 1,
44
+ ),
45
+ double(
46
+ :operation,
47
+ name: :noop,
48
+ collection: collection,
49
+ value: "bar",
50
+ index: 2,
51
+ ),
52
+ ])
53
+
54
+ flattened_operation_tree = described_class.call(operation_tree)
55
+
56
+ expect(flattened_operation_tree).to match([
57
+ an_object_having_attributes(
58
+ type: :noop,
59
+ indentation_level: 0,
60
+ prefix: "",
61
+ value: %([),
62
+ collection_bookend: :open,
63
+ complete_bookend: :open,
64
+ add_comma: false,
65
+ ),
66
+ an_object_having_attributes(
67
+ type: :noop,
68
+ indentation_level: 1,
69
+ prefix: "",
70
+ value: %("foo"),
71
+ add_comma: true,
72
+ ),
73
+ an_object_having_attributes(
74
+ type: :noop,
75
+ indentation_level: 1,
76
+ prefix: "",
77
+ value: %([),
78
+ collection_bookend: :open,
79
+ complete_bookend: nil,
80
+ add_comma: false,
81
+ ),
82
+ an_object_having_attributes(
83
+ type: :noop,
84
+ indentation_level: 2,
85
+ prefix: "",
86
+ value: %("one fish"),
87
+ add_comma: true,
88
+ ),
89
+ an_object_having_attributes(
90
+ type: :noop,
91
+ indentation_level: 2,
92
+ prefix: "",
93
+ value: %("two fish"),
94
+ add_comma: false,
95
+ ),
96
+ an_object_having_attributes(
97
+ type: :noop,
98
+ indentation_level: 1,
99
+ prefix: "",
100
+ value: %(]),
101
+ collection_bookend: :close,
102
+ complete_bookend: nil,
103
+ add_comma: true,
104
+ ),
105
+ an_object_having_attributes(
106
+ type: :noop,
107
+ indentation_level: 1,
108
+ prefix: "",
109
+ value: %("bar"),
110
+ add_comma: false,
111
+ ),
112
+ an_object_having_attributes(
113
+ type: :noop,
114
+ indentation_level: 0,
115
+ prefix: "",
116
+ value: %(]),
117
+ collection_bookend: :close,
118
+ complete_bookend: :close,
119
+ add_comma: false,
120
+ ),
121
+ ])
122
+ end
123
+ end
124
+
125
+ context "given a one-dimensional tree of noops, inserts, and deletes" do
126
+ it "returns a series of lines from inspecting each value, creating multiple lines upon encountering inner data structures" do
127
+ expected = Array.new(3) { :some_value }
128
+ actual = Array.new(4) { :some_value }
129
+ operation_tree = SuperDiff::OperationTrees::Array.new([
130
+ double(
131
+ :operation,
132
+ name: :delete,
133
+ collection: expected,
134
+ value: "foo",
135
+ index: 0,
136
+ ),
137
+ double(
138
+ :operation,
139
+ name: :insert,
140
+ collection: actual,
141
+ value: "zoo",
142
+ index: 0,
143
+ ),
144
+ double(
145
+ :operation,
146
+ name: :noop,
147
+ collection: actual,
148
+ value: ["one fish", "two fish"],
149
+ index: 1,
150
+ ),
151
+ double(
152
+ :operation,
153
+ name: :noop,
154
+ collection: actual,
155
+ value: "bar",
156
+ index: 2,
157
+ ),
158
+ double(
159
+ :operation,
160
+ name: :insert,
161
+ collection: actual,
162
+ value: "baz",
163
+ index: 3,
164
+ ),
165
+ ])
166
+
167
+ flattened_operation_tree = described_class.call(operation_tree)
168
+
169
+ expect(flattened_operation_tree).to match([
170
+ an_object_having_attributes(
171
+ type: :noop,
172
+ indentation_level: 0,
173
+ prefix: "",
174
+ value: %([),
175
+ collection_bookend: :open,
176
+ complete_bookend: :open,
177
+ add_comma: false,
178
+ ),
179
+ an_object_having_attributes(
180
+ type: :delete,
181
+ indentation_level: 1,
182
+ prefix: "",
183
+ value: %("foo"),
184
+ add_comma: true,
185
+ ),
186
+ an_object_having_attributes(
187
+ type: :insert,
188
+ indentation_level: 1,
189
+ prefix: "",
190
+ value: %("zoo"),
191
+ add_comma: true,
192
+ ),
193
+ an_object_having_attributes(
194
+ type: :noop,
195
+ indentation_level: 1,
196
+ prefix: "",
197
+ value: %([),
198
+ collection_bookend: :open,
199
+ complete_bookend: nil,
200
+ add_comma: false,
201
+ ),
202
+ an_object_having_attributes(
203
+ type: :noop,
204
+ indentation_level: 2,
205
+ prefix: "",
206
+ value: %("one fish"),
207
+ add_comma: true,
208
+ ),
209
+ an_object_having_attributes(
210
+ type: :noop,
211
+ indentation_level: 2,
212
+ prefix: "",
213
+ value: %("two fish"),
214
+ add_comma: false,
215
+ ),
216
+ an_object_having_attributes(
217
+ type: :noop,
218
+ indentation_level: 1,
219
+ prefix: "",
220
+ value: %(]),
221
+ collection_bookend: :close,
222
+ complete_bookend: nil,
223
+ add_comma: true,
224
+ ),
225
+ an_object_having_attributes(
226
+ type: :noop,
227
+ indentation_level: 1,
228
+ prefix: "",
229
+ value: %("bar"),
230
+ add_comma: true,
231
+ ),
232
+ an_object_having_attributes(
233
+ type: :insert,
234
+ indentation_level: 1,
235
+ prefix: "",
236
+ value: %("baz"),
237
+ add_comma: false,
238
+ ),
239
+ an_object_having_attributes(
240
+ type: :noop,
241
+ indentation_level: 0,
242
+ prefix: "",
243
+ value: %(]),
244
+ collection_bookend: :close,
245
+ complete_bookend: :close,
246
+ add_comma: false,
247
+ ),
248
+ ])
249
+ end
250
+ end
251
+
252
+ context "given a multi-dimensional tree of operations" do
253
+ it "splits change operations into multiple lines" do
254
+ collection = Array.new(3) { :some_value }
255
+ subcollection = Array.new(2) { :some_value }
256
+ operation_tree = SuperDiff::OperationTrees::Array.new([
257
+ double(
258
+ :operation,
259
+ name: :noop,
260
+ collection: collection,
261
+ value: "foo",
262
+ index: 0,
263
+ ),
264
+ double(
265
+ :operation,
266
+ name: :change,
267
+ left_collection: collection,
268
+ left_index: 1,
269
+ right_collection: collection,
270
+ right_index: 1,
271
+ children: SuperDiff::OperationTrees::Array.new([
272
+ double(
273
+ :operation,
274
+ name: :noop,
275
+ collection: subcollection,
276
+ value: "one fish",
277
+ index: 0,
278
+ ),
279
+ double(
280
+ :operation,
281
+ name: :delete,
282
+ collection: subcollection,
283
+ value: "two fish",
284
+ index: 1,
285
+ ),
286
+ double(
287
+ :operation,
288
+ name: :insert,
289
+ collection: subcollection,
290
+ value: "blue fish",
291
+ index: 1,
292
+ ),
293
+ ]),
294
+ ),
295
+ double(
296
+ :operation,
297
+ name: :noop,
298
+ collection: collection,
299
+ value: "bar",
300
+ index: 2,
301
+ ),
302
+ ])
303
+
304
+ flattened_operation_tree = described_class.call(operation_tree)
305
+
306
+ expect(flattened_operation_tree).to match([
307
+ an_object_having_attributes(
308
+ type: :noop,
309
+ indentation_level: 0,
310
+ prefix: "",
311
+ value: %([),
312
+ collection_bookend: :open,
313
+ complete_bookend: :open,
314
+ add_comma: false,
315
+ ),
316
+ an_object_having_attributes(
317
+ type: :noop,
318
+ indentation_level: 1,
319
+ prefix: "",
320
+ value: %("foo"),
321
+ add_comma: true,
322
+ ),
323
+ an_object_having_attributes(
324
+ type: :noop,
325
+ indentation_level: 1,
326
+ prefix: "",
327
+ value: %([),
328
+ collection_bookend: :open,
329
+ complete_bookend: nil,
330
+ add_comma: false,
331
+ ),
332
+ an_object_having_attributes(
333
+ type: :noop,
334
+ indentation_level: 2,
335
+ prefix: "",
336
+ value: %("one fish"),
337
+ add_comma: true,
338
+ ),
339
+ an_object_having_attributes(
340
+ type: :delete,
341
+ indentation_level: 2,
342
+ prefix: "",
343
+ value: %("two fish"),
344
+ add_comma: false,
345
+ ),
346
+ an_object_having_attributes(
347
+ type: :insert,
348
+ indentation_level: 2,
349
+ prefix: "",
350
+ value: %("blue fish"),
351
+ add_comma: false,
352
+ ),
353
+ an_object_having_attributes(
354
+ type: :noop,
355
+ indentation_level: 1,
356
+ prefix: "",
357
+ value: %(]),
358
+ collection_bookend: :close,
359
+ complete_bookend: nil,
360
+ add_comma: true,
361
+ ),
362
+ an_object_having_attributes(
363
+ type: :noop,
364
+ indentation_level: 1,
365
+ prefix: "",
366
+ value: %("bar"),
367
+ add_comma: false,
368
+ ),
369
+ an_object_having_attributes(
370
+ type: :noop,
371
+ indentation_level: 0,
372
+ prefix: "",
373
+ value: %(]),
374
+ collection_bookend: :close,
375
+ complete_bookend: :close,
376
+ add_comma: false,
377
+ ),
378
+ ])
379
+ end
380
+ end
381
+
382
+ context "given a single-dimensional tree that contains a reference to itself" do
383
+ it "replaces the reference with a static placeholder" do
384
+ left_collection = Array.new(3) { :some_value }
385
+ right_collection = Array.new(2) { :some_value }
386
+ right_collection << right_collection
387
+
388
+ operation_tree = SuperDiff::OperationTrees::Array.new([
389
+ double(
390
+ :operation,
391
+ name: :noop,
392
+ collection: right_collection,
393
+ value: "foo",
394
+ index: 0,
395
+ ),
396
+ double(
397
+ :operation,
398
+ name: :noop,
399
+ collection: right_collection,
400
+ value: "bar",
401
+ index: 1,
402
+ ),
403
+ double(
404
+ :operation,
405
+ name: :delete,
406
+ collection: left_collection,
407
+ value: "baz",
408
+ index: 2,
409
+ ),
410
+ double(
411
+ :operation,
412
+ name: :insert,
413
+ collection: right_collection,
414
+ value: right_collection,
415
+ index: 2,
416
+ ),
417
+ ])
418
+
419
+ flattened_operation_tree = described_class.call(operation_tree)
420
+
421
+ expect(flattened_operation_tree).to match([
422
+ an_object_having_attributes(
423
+ type: :noop,
424
+ indentation_level: 0,
425
+ prefix: "",
426
+ value: %([),
427
+ collection_bookend: :open,
428
+ complete_bookend: :open,
429
+ add_comma: false,
430
+ ),
431
+ an_object_having_attributes(
432
+ type: :noop,
433
+ indentation_level: 1,
434
+ prefix: "",
435
+ value: %("foo"),
436
+ add_comma: true,
437
+ ),
438
+ an_object_having_attributes(
439
+ type: :noop,
440
+ indentation_level: 1,
441
+ prefix: "",
442
+ value: %("bar"),
443
+ add_comma: true,
444
+ ),
445
+ an_object_having_attributes(
446
+ type: :delete,
447
+ indentation_level: 1,
448
+ prefix: "",
449
+ value: %("baz"),
450
+ add_comma: false,
451
+ ),
452
+ an_object_having_attributes(
453
+ type: :insert,
454
+ indentation_level: 1,
455
+ prefix: "",
456
+ value: %(∙∙∙),
457
+ add_comma: false,
458
+ ),
459
+ an_object_having_attributes(
460
+ type: :noop,
461
+ indentation_level: 0,
462
+ prefix: "",
463
+ value: %(]),
464
+ collection_bookend: :close,
465
+ complete_bookend: :close,
466
+ add_comma: false,
467
+ ),
468
+ ])
469
+ end
470
+ end
471
+
472
+ context "given a multi-dimensional tree that contains a reference to itself in an inner level" do
473
+ it "replaces the reference with a static placeholder" do
474
+ collection = Array.new(3) { :some_value }
475
+ left_subcollection = Array.new(2) { :some_value }
476
+ right_subcollection = Array.new(1) { :some_value }
477
+ right_subcollection << right_subcollection
478
+
479
+ operation_tree = SuperDiff::OperationTrees::Array.new([
480
+ double(
481
+ :operation,
482
+ name: :noop,
483
+ collection: collection,
484
+ value: "foo",
485
+ index: 0,
486
+ ),
487
+ double(
488
+ :operation,
489
+ name: :change,
490
+ left_collection: collection,
491
+ left_index: 1,
492
+ right_collection: collection,
493
+ right_index: 1,
494
+ children: SuperDiff::OperationTrees::Array.new([
495
+ double(
496
+ :operation,
497
+ name: :noop,
498
+ collection: right_subcollection,
499
+ value: "bar",
500
+ index: 0,
501
+ ),
502
+ double(
503
+ :operation,
504
+ name: :delete,
505
+ collection: left_subcollection,
506
+ value: "baz",
507
+ index: 1,
508
+ ),
509
+ double(
510
+ :operation,
511
+ name: :insert,
512
+ collection: right_subcollection,
513
+ value: right_subcollection,
514
+ index: 1,
515
+ ),
516
+ ]),
517
+ ),
518
+ double(
519
+ :operation,
520
+ name: :noop,
521
+ collection: collection,
522
+ value: "qux",
523
+ index: 2,
524
+ ),
525
+ ])
526
+
527
+ flattened_operation_tree = described_class.call(operation_tree)
528
+
529
+ expect(flattened_operation_tree).to match([
530
+ an_object_having_attributes(
531
+ type: :noop,
532
+ indentation_level: 0,
533
+ prefix: "",
534
+ value: %([),
535
+ collection_bookend: :open,
536
+ complete_bookend: :open,
537
+ add_comma: false,
538
+ ),
539
+ an_object_having_attributes(
540
+ type: :noop,
541
+ indentation_level: 1,
542
+ prefix: "",
543
+ value: %("foo"),
544
+ add_comma: true,
545
+ ),
546
+ an_object_having_attributes(
547
+ type: :noop,
548
+ indentation_level: 1,
549
+ prefix: "",
550
+ value: %([),
551
+ collection_bookend: :open,
552
+ complete_bookend: nil,
553
+ add_comma: false,
554
+ ),
555
+ an_object_having_attributes(
556
+ type: :noop,
557
+ indentation_level: 2,
558
+ prefix: "",
559
+ value: %("bar"),
560
+ add_comma: true,
561
+ ),
562
+ an_object_having_attributes(
563
+ type: :delete,
564
+ indentation_level: 2,
565
+ prefix: "",
566
+ value: %("baz"),
567
+ add_comma: false,
568
+ ),
569
+ an_object_having_attributes(
570
+ type: :insert,
571
+ indentation_level: 2,
572
+ prefix: "",
573
+ value: %(∙∙∙),
574
+ add_comma: false,
575
+ ),
576
+ an_object_having_attributes(
577
+ type: :noop,
578
+ indentation_level: 1,
579
+ prefix: "",
580
+ value: %(]),
581
+ collection_bookend: :close,
582
+ complete_bookend: nil,
583
+ add_comma: true,
584
+ ),
585
+ an_object_having_attributes(
586
+ type: :noop,
587
+ indentation_level: 1,
588
+ prefix: "",
589
+ value: %("qux"),
590
+ add_comma: false,
591
+ ),
592
+ an_object_having_attributes(
593
+ type: :noop,
594
+ indentation_level: 0,
595
+ prefix: "",
596
+ value: %(]),
597
+ collection_bookend: :close,
598
+ complete_bookend: :close,
599
+ add_comma: false,
600
+ ),
601
+ ])
602
+ end
603
+ end
604
+ end