super_diff 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +117 -89
  3. data/lib/super_diff.rb +33 -47
  4. data/lib/super_diff/active_record.rb +41 -0
  5. data/lib/super_diff/active_record/diff_formatters.rb +10 -0
  6. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +23 -0
  7. data/lib/super_diff/active_record/differs.rb +10 -0
  8. data/lib/super_diff/active_record/differs/active_record_relation.rb +30 -0
  9. data/lib/super_diff/active_record/object_inspection.rb +14 -0
  10. data/lib/super_diff/active_record/object_inspection/inspectors.rb +16 -0
  11. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb +38 -0
  12. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb +18 -0
  13. data/lib/super_diff/active_record/object_inspection/map_extension.rb +18 -0
  14. data/lib/super_diff/active_record/operation_sequences.rb +10 -0
  15. data/lib/super_diff/active_record/operation_sequences/active_record_relation.rb +16 -0
  16. data/lib/super_diff/active_record/operational_sequencers.rb +14 -0
  17. data/lib/super_diff/active_record/operational_sequencers/active_record_model.rb +19 -0
  18. data/lib/super_diff/active_record/operational_sequencers/active_record_relation.rb +24 -0
  19. data/lib/super_diff/active_support.rb +33 -0
  20. data/lib/super_diff/active_support/diff_formatters.rb +10 -0
  21. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +36 -0
  22. data/lib/super_diff/active_support/differs.rb +10 -0
  23. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +36 -0
  24. data/lib/super_diff/active_support/object_inspection.rb +14 -0
  25. data/lib/super_diff/active_support/object_inspection/inspectors.rb +12 -0
  26. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +18 -0
  27. data/lib/super_diff/active_support/object_inspection/map_extension.rb +15 -0
  28. data/lib/super_diff/active_support/operation_sequences.rb +10 -0
  29. data/lib/super_diff/active_support/operation_sequences/hash_with_indifferent_access.rb +16 -0
  30. data/lib/super_diff/active_support/operational_sequencers.rb +10 -0
  31. data/lib/super_diff/active_support/operational_sequencers/hash_with_indifferent_access.rb +21 -0
  32. data/lib/super_diff/colorized_document_extensions.rb +17 -0
  33. data/lib/super_diff/csi.rb +45 -15
  34. data/lib/super_diff/csi/bold_sequence.rb +9 -0
  35. data/lib/super_diff/csi/color.rb +62 -0
  36. data/lib/super_diff/csi/color_sequence_block.rb +28 -0
  37. data/lib/super_diff/csi/colorized_document.rb +72 -0
  38. data/lib/super_diff/csi/document.rb +183 -0
  39. data/lib/super_diff/csi/eight_bit_color.rb +72 -26
  40. data/lib/super_diff/csi/four_bit_color.rb +63 -29
  41. data/lib/super_diff/csi/twenty_four_bit_color.rb +79 -18
  42. data/lib/super_diff/csi/uncolorized_document.rb +29 -0
  43. data/lib/super_diff/diff_formatter.rb +10 -15
  44. data/lib/super_diff/diff_formatters.rb +10 -1
  45. data/lib/super_diff/diff_formatters/base.rb +12 -17
  46. data/lib/super_diff/diff_formatters/collection.rb +81 -50
  47. data/lib/super_diff/diff_formatters/{object.rb → custom_object.rb} +12 -9
  48. data/lib/super_diff/diff_formatters/default_object.rb +48 -0
  49. data/lib/super_diff/diff_formatters/multiline_string.rb +31 -0
  50. data/lib/super_diff/differ.rb +35 -32
  51. data/lib/super_diff/differs.rb +16 -1
  52. data/lib/super_diff/differs/array.rb +2 -2
  53. data/lib/super_diff/differs/base.rb +11 -21
  54. data/lib/super_diff/differs/custom_object.rb +26 -0
  55. data/lib/super_diff/differs/default_object.rb +25 -0
  56. data/lib/super_diff/differs/empty.rb +1 -1
  57. data/lib/super_diff/differs/hash.rb +2 -2
  58. data/lib/super_diff/differs/{multi_line_string.rb → multiline_string.rb} +6 -5
  59. data/lib/super_diff/equality_matcher.rb +9 -22
  60. data/lib/super_diff/equality_matchers.rb +19 -1
  61. data/lib/super_diff/equality_matchers/array.rb +6 -4
  62. data/lib/super_diff/equality_matchers/base.rb +8 -16
  63. data/lib/super_diff/equality_matchers/default.rb +60 -0
  64. data/lib/super_diff/equality_matchers/hash.rb +6 -4
  65. data/lib/super_diff/equality_matchers/{multi_line_string.rb → multiline_string.rb} +9 -6
  66. data/lib/super_diff/equality_matchers/primitive.rb +34 -0
  67. data/lib/super_diff/equality_matchers/{single_line_string.rb → singleline_string.rb} +7 -5
  68. data/lib/super_diff/helpers.rb +17 -81
  69. data/lib/super_diff/no_differ_available_error.rb +22 -0
  70. data/lib/super_diff/{errors.rb → no_operational_sequencer_available_error.rb} +0 -0
  71. data/lib/super_diff/object_inspection.rb +24 -0
  72. data/lib/super_diff/object_inspection/inspection_tree.rb +144 -0
  73. data/lib/super_diff/object_inspection/inspector.rb +27 -0
  74. data/lib/super_diff/object_inspection/inspectors.rb +18 -0
  75. data/lib/super_diff/object_inspection/inspectors/array.rb +22 -0
  76. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +27 -0
  77. data/lib/super_diff/object_inspection/inspectors/default_object.rb +47 -0
  78. data/lib/super_diff/object_inspection/inspectors/hash.rb +22 -0
  79. data/lib/super_diff/object_inspection/inspectors/primitive.rb +13 -0
  80. data/lib/super_diff/object_inspection/inspectors/string.rb +13 -0
  81. data/lib/super_diff/object_inspection/map.rb +28 -0
  82. data/lib/super_diff/object_inspection/nodes.rb +49 -0
  83. data/lib/super_diff/object_inspection/nodes/base.rb +86 -0
  84. data/lib/super_diff/object_inspection/nodes/break.rb +15 -0
  85. data/lib/super_diff/object_inspection/nodes/inspection.rb +15 -0
  86. data/lib/super_diff/object_inspection/nodes/nesting.rb +16 -0
  87. data/lib/super_diff/object_inspection/nodes/text.rb +15 -0
  88. data/lib/super_diff/object_inspection/nodes/when_empty.rb +30 -0
  89. data/lib/super_diff/object_inspection/nodes/when_multiline.rb +22 -0
  90. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +30 -0
  91. data/lib/super_diff/object_inspection/nodes/when_singleline.rb +24 -0
  92. data/lib/super_diff/operation_sequences.rb +9 -0
  93. data/lib/super_diff/operation_sequences/base.rb +1 -1
  94. data/lib/super_diff/operation_sequences/{object.rb → custom_object.rb} +4 -3
  95. data/lib/super_diff/operation_sequences/default_object.rb +25 -0
  96. data/lib/super_diff/operational_sequencer.rb +23 -18
  97. data/lib/super_diff/operational_sequencers.rb +12 -1
  98. data/lib/super_diff/operational_sequencers/array.rb +65 -62
  99. data/lib/super_diff/operational_sequencers/base.rb +18 -26
  100. data/lib/super_diff/operational_sequencers/custom_object.rb +35 -0
  101. data/lib/super_diff/operational_sequencers/{object.rb → default_object.rb} +21 -11
  102. data/lib/super_diff/operational_sequencers/hash.rb +8 -5
  103. data/lib/super_diff/operational_sequencers/{multi_line_string.rb → multiline_string.rb} +11 -6
  104. data/lib/super_diff/operations.rb +6 -0
  105. data/lib/super_diff/operations/binary_operation.rb +14 -34
  106. data/lib/super_diff/operations/unary_operation.rb +11 -2
  107. data/lib/super_diff/rails.rb +1 -0
  108. data/lib/super_diff/recursion_guard.rb +47 -0
  109. data/lib/super_diff/rspec-rails.rb +2 -0
  110. data/lib/super_diff/rspec.rb +52 -8
  111. data/lib/super_diff/rspec/augmented_matcher.rb +98 -0
  112. data/lib/super_diff/rspec/configuration.rb +31 -0
  113. data/lib/super_diff/rspec/differ.rb +60 -16
  114. data/lib/super_diff/rspec/differs.rb +13 -0
  115. data/lib/super_diff/rspec/differs/collection_containing_exactly.rb +23 -0
  116. data/lib/super_diff/rspec/differs/partial_array.rb +22 -0
  117. data/lib/super_diff/rspec/differs/partial_hash.rb +22 -0
  118. data/lib/super_diff/rspec/differs/partial_object.rb +22 -0
  119. data/lib/super_diff/rspec/matcher_text_builders.rb +24 -0
  120. data/lib/super_diff/rspec/matcher_text_builders/base.rb +155 -0
  121. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +78 -0
  122. data/lib/super_diff/rspec/matcher_text_builders/contain_exactly.rb +14 -0
  123. data/lib/super_diff/rspec/matcher_text_builders/match.rb +23 -0
  124. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +13 -0
  125. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +99 -0
  126. data/lib/super_diff/rspec/matcher_text_template.rb +240 -0
  127. data/lib/super_diff/rspec/monkey_patches.rb +601 -98
  128. data/lib/super_diff/rspec/object_inspection.rb +8 -0
  129. data/lib/super_diff/rspec/object_inspection/inspectors.rb +24 -0
  130. data/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb +19 -0
  131. data/lib/super_diff/rspec/object_inspection/inspectors/partial_array.rb +22 -0
  132. data/lib/super_diff/rspec/object_inspection/inspectors/partial_hash.rb +21 -0
  133. data/lib/super_diff/rspec/object_inspection/inspectors/partial_object.rb +21 -0
  134. data/lib/super_diff/rspec/object_inspection/map_extension.rb +23 -0
  135. data/lib/super_diff/rspec/operational_sequencers.rb +22 -0
  136. data/lib/super_diff/rspec/operational_sequencers/collection_containing_exactly.rb +97 -0
  137. data/lib/super_diff/rspec/operational_sequencers/partial_array.rb +23 -0
  138. data/lib/super_diff/rspec/operational_sequencers/partial_hash.rb +32 -0
  139. data/lib/super_diff/rspec/operational_sequencers/partial_object.rb +64 -0
  140. data/lib/super_diff/version.rb +1 -1
  141. data/spec/examples.txt +328 -46
  142. data/spec/integration/rails/active_record_spec.rb +19 -0
  143. data/spec/integration/rails/hash_with_indifferent_access_spec.rb +19 -0
  144. data/spec/integration/rspec/be_falsey_matcher_spec.rb +53 -0
  145. data/spec/integration/rspec/be_matcher_spec.rb +565 -0
  146. data/spec/integration/rspec/be_nil_matcher_spec.rb +53 -0
  147. data/spec/integration/rspec/be_predicate_matcher_spec.rb +546 -0
  148. data/spec/integration/rspec/be_truthy_matcher_spec.rb +57 -0
  149. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +368 -0
  150. data/spec/integration/rspec/eq_matcher_spec.rb +874 -0
  151. data/spec/integration/rspec/have_attributes_matcher_spec.rb +299 -0
  152. data/spec/integration/rspec/include_matcher_spec.rb +350 -0
  153. data/spec/integration/rspec/match_matcher_spec.rb +1258 -0
  154. data/spec/integration/rspec/raise_error_matcher_spec.rb +350 -0
  155. data/spec/integration/rspec/respond_to_matcher_spec.rb +994 -0
  156. data/spec/integration/rspec/unhandled_errors_spec.rb +94 -0
  157. data/spec/spec_helper.rb +19 -4
  158. data/spec/support/colorizer.rb +9 -0
  159. data/spec/support/command_runner.rb +4 -0
  160. data/spec/support/integration/helpers.rb +179 -0
  161. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +79 -41
  162. data/spec/support/models/a.rb +11 -0
  163. data/spec/support/models/active_record/person.rb +26 -0
  164. data/spec/support/models/active_record/shipping_address.rb +29 -0
  165. data/spec/support/models/customer.rb +24 -0
  166. data/spec/support/models/empty_class.rb +6 -0
  167. data/spec/support/models/item.rb +10 -0
  168. data/spec/support/models/order.rb +9 -0
  169. data/spec/support/models/person.rb +20 -0
  170. data/spec/support/models/player.rb +33 -0
  171. data/spec/support/models/shipping_address.rb +34 -0
  172. data/spec/support/ruby_versions.rb +7 -0
  173. data/spec/support/shared_examples/active_record.rb +338 -0
  174. data/spec/support/shared_examples/hash_with_indifferent_access.rb +233 -0
  175. data/spec/unit/equality_matcher_spec.rb +579 -171
  176. data/spec/unit/object_inspection_spec.rb +1092 -0
  177. data/spec/unit/rspec/matchers/be_compared_to_spec.rb +23 -0
  178. data/spec/unit/rspec/matchers/be_falsey_spec.rb +9 -0
  179. data/spec/unit/rspec/matchers/be_nil_spec.rb +9 -0
  180. data/spec/unit/rspec/matchers/be_predicate_spec.rb +31 -0
  181. data/spec/unit/rspec/matchers/be_spec.rb +17 -0
  182. data/spec/unit/rspec/matchers/be_truthy_spec.rb +9 -0
  183. data/spec/unit/rspec/matchers/contain_exactly_spec.rb +11 -0
  184. data/spec/unit/rspec/matchers/eq_spec.rb +9 -0
  185. data/spec/unit/rspec/matchers/have_attributes_spec.rb +11 -0
  186. data/spec/unit/rspec/matchers/include_spec.rb +21 -0
  187. data/spec/unit/rspec/matchers/match_spec.rb +9 -0
  188. data/spec/unit/rspec/matchers/raise_error_spec.rb +29 -0
  189. data/spec/unit/rspec/matchers/respond_to_spec.rb +78 -0
  190. data/super_diff.gemspec +4 -2
  191. metadata +231 -34
  192. data/lib/super_diff/csi/color_helper.rb +0 -52
  193. data/lib/super_diff/csi/eight_bit_sequence.rb +0 -27
  194. data/lib/super_diff/csi/four_bit_sequence.rb +0 -24
  195. data/lib/super_diff/csi/sequence.rb +0 -22
  196. data/lib/super_diff/csi/twenty_four_bit_sequence.rb +0 -27
  197. data/lib/super_diff/diff_formatters/multi_line_string.rb +0 -31
  198. data/lib/super_diff/differs/object.rb +0 -68
  199. data/lib/super_diff/equality_matchers/object.rb +0 -18
  200. data/lib/super_diff/value_inspection.rb +0 -11
  201. data/spec/integration/rspec_spec.rb +0 -261
  202. data/spec/support/color_helper.rb +0 -49
  203. data/spec/support/person.rb +0 -23
  204. data/spec/support/person_diff_formatter.rb +0 -15
  205. data/spec/support/person_operation_sequence.rb +0 -14
  206. data/spec/support/person_operational_sequencer.rb +0 -19
@@ -1,52 +0,0 @@
1
- module SuperDiff
2
- module Csi
3
- module ColorHelper
4
- BLACK = Csi::FourBitColor.new(:white)
5
- # LIGHT_RED = Csi::TwentyFourBitColor.new(r: 63, g: 32, b: 32)
6
- LIGHT_RED = Csi::TwentyFourBitColor.new(r: 73, g: 62, b: 71)
7
- # RED = Csi::TwentyFourBitColor.new(r: 110, g: 56, b: 56)
8
- # RED = Csi::TwentyFourBitColor.new(r: 116, g: 78, b: 84)
9
- RED = Csi::FourBitColor.new(:red)
10
- # LIGHT_GREEN = Csi::TwentyFourBitColor.new(r: 40, g: 48, b: 39)
11
- LIGHT_GREEN = Csi::TwentyFourBitColor.new(r: 51, g: 81, b: 81)
12
- # GREEN = Csi::TwentyFourBitColor.new(r: 73, g: 87, b: 71)
13
- # GREEN = Csi::TwentyFourBitColor.new(r: 81, g: 115, b: 105)
14
- GREEN = Csi::FourBitColor.new(:green)
15
- DARK_GREY = Csi::TwentyFourBitColor.new(r: 134, g: 147, b: 149)
16
-
17
- def self.plain(text)
18
- text
19
- end
20
-
21
- def self.line(given_text = "")
22
- if block_given?
23
- text = ""
24
- yield text
25
- text + "\n"
26
- else
27
- given_text + "\n"
28
- end
29
- end
30
-
31
- def self.light_red_bg(text)
32
- Csi.colorize(text, fg: BLACK, bg: LIGHT_RED)
33
- end
34
-
35
- def self.red(text)
36
- Csi.colorize(text, fg: RED)
37
- end
38
-
39
- def self.light_green_bg(text)
40
- Csi.colorize(text, fg: BLACK, bg: LIGHT_GREEN)
41
- end
42
-
43
- def self.green(text)
44
- Csi.colorize(text, fg: GREEN)
45
- end
46
-
47
- def self.dark_grey(text)
48
- Csi.colorize(text, fg: BLACK, bg: DARK_GREY)
49
- end
50
- end
51
- end
52
- end
@@ -1,27 +0,0 @@
1
- module SuperDiff
2
- module Csi
3
- class EightBitSequence
4
- LEADING_CODES = { fg: 38, bg: 48 }.freeze
5
- SERIAL_CODE = 5
6
-
7
- def initialize(fg: nil, bg: nil)
8
- @fg = fg
9
- @bg = bg
10
- end
11
-
12
- def to_s
13
- [sequence_for(:fg, fg), sequence_for(:bg, bg)].compact.join
14
- end
15
-
16
- private
17
-
18
- attr_reader :fg, :bg
19
-
20
- def sequence_for(type, color)
21
- if color
22
- "\e[#{LEADING_CODES.fetch(type)};#{SERIAL_CODE};#{color.code}m"
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,24 +0,0 @@
1
- module SuperDiff
2
- module Csi
3
- class FourBitSequence
4
- def initialize(fg: nil, bg: nil)
5
- @fg = fg
6
- @bg = bg
7
- end
8
-
9
- def to_s
10
- [sequence_for(fg), sequence_for(bg)].compact.join
11
- end
12
-
13
- private
14
-
15
- attr_reader :fg, :bg
16
-
17
- def sequence_for(color)
18
- if color
19
- "\e[#{color.code}m"
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,22 +0,0 @@
1
- module SuperDiff
2
- module Csi
3
- module Sequence
4
- def self.for(color)
5
- case color
6
- when :reset
7
- Csi::ResetSequence.new
8
- when FourBitColor
9
- Csi::FourBitSequence.new(color)
10
- when EightBitColor
11
- Csi::EightBitSequence.new(color)
12
- when TwentyFourBitColor
13
- Csi::TwentyFourBitSequence.new(color)
14
- else
15
- raise ArgumentError.new(
16
- "Don't know how to interpret color: #{color.inspect}",
17
- )
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,27 +0,0 @@
1
- module SuperDiff
2
- module Csi
3
- class TwentyFourBitSequence
4
- LEADING_CODES = { fg: 38, bg: 48 }.freeze
5
- SERIAL_CODE = 2
6
-
7
- def initialize(fg: nil, bg: nil)
8
- @fg = fg
9
- @bg = bg
10
- end
11
-
12
- def to_s
13
- [sequence_for(:fg, fg), sequence_for(:bg, bg)].compact.join
14
- end
15
-
16
- private
17
-
18
- attr_reader :fg, :bg
19
-
20
- def sequence_for(type, color)
21
- if color
22
- "\e[#{LEADING_CODES.fetch(type)};#{SERIAL_CODE};#{color.code}m"
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,31 +0,0 @@
1
- module SuperDiff
2
- module DiffFormatters
3
- class MultiLineString < Base
4
- def self.applies_to?(operations)
5
- operations.is_a?(OperationSequences::MultiLineString)
6
- end
7
-
8
- def call
9
- lines.join("\n")
10
- end
11
-
12
- private
13
-
14
- def lines
15
- operations.reduce([]) do |array, operation|
16
- case operation.name
17
- when :change
18
- array << Helpers.style(:deleted, "- #{operation.left_value}")
19
- array << Helpers.style(:inserted, "+ #{operation.right_value}")
20
- when :delete
21
- array << Helpers.style(:deleted, "- #{operation.value}")
22
- when :insert
23
- array << Helpers.style(:inserted, "+ #{operation.value}")
24
- else
25
- array << Helpers.style(:normal, " #{operation.value}")
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,68 +0,0 @@
1
- module SuperDiff
2
- module Differs
3
- class Object < Base
4
- def self.applies_to?(value)
5
- value.is_a?(::Object)
6
- end
7
-
8
- def self.call(
9
- expected,
10
- actual,
11
- indent_level:,
12
- index_in_collection:,
13
- **rest
14
- )
15
- super(
16
- expected,
17
- actual,
18
- indent_level: indent_level,
19
- index_in_collection: index_in_collection,
20
- **rest
21
- )
22
- end
23
-
24
- def self.inspection_lines_for(value)
25
- inspected_value = value.inspect
26
- match = inspected_value.match(/\A#<([^ ]+)(.*)>\Z/)
27
-
28
- if match
29
- [
30
- "#<#{match.captures[0]} {",
31
- *match.captures[1].split(" ").map { |line| " " + line },
32
- "}>",
33
- ]
34
- else
35
- [inspected_value]
36
- end
37
- end
38
-
39
- def call
40
- lines.join("\n")
41
- end
42
-
43
- private
44
-
45
- attr_reader :expected, :actual, :indent_level, :index_in_collection
46
-
47
- def lines
48
- [
49
- styled_lines_for("-", :deleted, expected),
50
- styled_lines_for("+", :inserted, actual),
51
- ]
52
- end
53
-
54
- def styled_lines_for(icon, style_name, value)
55
- unstyled_lines_for(icon, value).map do |line|
56
- Helpers.style(style_name, line)
57
- end
58
- end
59
-
60
- def unstyled_lines_for(icon, value)
61
- lines = self.class.inspection_lines_for(value).
62
- map { |inspection_line| "#{icon} #{indentation}#{inspection_line}" }
63
-
64
- lines
65
- end
66
- end
67
- end
68
- end
@@ -1,18 +0,0 @@
1
- module SuperDiff
2
- module EqualityMatchers
3
- class Object < Base
4
- def self.applies_to?(value)
5
- value.class == ::Object
6
- end
7
-
8
- def fail
9
- <<~OUTPUT.strip
10
- Differing #{Helpers.plural_type_for(actual)}.
11
-
12
- #{Helpers.style :deleted, "Expected: #{expected.inspect}"}
13
- #{Helpers.style :inserted, " Actual: #{actual.inspect}"}
14
- OUTPUT
15
- end
16
- end
17
- end
18
- end
@@ -1,11 +0,0 @@
1
- module SuperDiff
2
- class ValueInspection
3
- attr_reader :beginning, :middle, :end
4
-
5
- def initialize(args)
6
- @beginning = args.fetch(:beginning)
7
- @middle = args.fetch(:middle)
8
- @end = args.fetch(:end)
9
- end
10
- end
11
- end
@@ -1,261 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe "Integration with RSpec", type: :integration do
4
- context "comparing two different integers" do
5
- it "produces the correct output" do
6
- test = <<~TEST
7
- expect(1).to eq(42)
8
- TEST
9
-
10
- expected_output = <<~OUTPUT
11
- expected: 42
12
- got: 1
13
- OUTPUT
14
-
15
- expect(test).to produce_output_when_run(expected_output)
16
- end
17
- end
18
-
19
- context "comparing two different symbols" do
20
- it "produces the correct output" do
21
- test = <<~TEST
22
- expect(:bar).to eq(:foo)
23
- TEST
24
-
25
- expected_output = <<~OUTPUT
26
- expected: :foo
27
- got: :bar
28
- OUTPUT
29
-
30
- expect(test).to produce_output_when_run(expected_output)
31
- end
32
- end
33
-
34
- context "comparing two different single-line strings" do
35
- it "produces the correct output" do
36
- test = <<~TEST
37
- expect("Jennifer").to eq("Marty")
38
- TEST
39
-
40
- expected_output = <<~OUTPUT
41
- expected: "Marty"
42
- got: "Jennifer"
43
- OUTPUT
44
-
45
- expect(test).to produce_output_when_run(expected_output)
46
- end
47
- end
48
-
49
- context "comparing two closely different multi-line strings" do
50
- it "produces the correct output" do
51
- test = <<~TEST
52
- expected = "This is a line\nAnd that's a line\nAnd there's a line too"
53
- actual = "This is a line\nSomething completely different\nAnd there's a line too"
54
- expect(actual).to eq(expected)
55
- TEST
56
-
57
- expected_output = <<~OUTPUT
58
- Diff:
59
-
60
- #{
61
- colored do
62
- plain_line %( This is a line⏎)
63
- red_line %(- And that's a line⏎)
64
- green_line %(+ Something completely different⏎)
65
- plain_line %( And there's a line too)
66
- end
67
- }
68
- OUTPUT
69
-
70
- expect(test).to produce_output_when_run(expected_output)
71
- end
72
- end
73
-
74
- context "comparing two completely different multi-line strings" do
75
- it "produces the correct output" do
76
- test = <<~TEST
77
- expected = "This is a line\nAnd that's a line\n"
78
- actual = "Something completely different\nAnd something else too\n"
79
- expect(actual).to eq(expected)
80
- TEST
81
-
82
- expected_output = <<~OUTPUT
83
- Diff:
84
-
85
- #{
86
- colored do
87
- red_line %(- This is a line⏎)
88
- red_line %(- And that's a line⏎)
89
- green_line %(+ Something completely different⏎)
90
- green_line %(+ And something else too⏎)
91
- end
92
- }
93
- OUTPUT
94
-
95
- expect(test).to produce_output_when_run(expected_output)
96
- end
97
- end
98
-
99
- context "comparing two arrays with other data structures inside" do
100
- it "produces the correct output" do
101
- test = <<~TEST
102
- expected = [
103
- [
104
- :h1,
105
- [:span, [:text, "Hello world"]],
106
- {
107
- class: "header",
108
- data: {
109
- "sticky" => true,
110
- person: SuperDiff::Test::Person.new(name: "Marty")
111
- }
112
- }
113
- ]
114
- ]
115
- actual = [
116
- [
117
- :h2,
118
- [:span, [:text, "Goodbye world"]],
119
- {
120
- id: "hero",
121
- class: "header",
122
- data: {
123
- "sticky" => false,
124
- role: "deprecated",
125
- person: SuperDiff::Test::Person.new(name: "Doc")
126
- }
127
- }
128
- ],
129
- :br
130
- ]
131
- expect(actual).to eq(expected)
132
- TEST
133
-
134
- expected_output = <<~OUTPUT
135
- Diff:
136
-
137
- #{
138
- colored do
139
- plain_line %( [)
140
- plain_line %( [)
141
- red_line %(- :h1,)
142
- green_line %(+ :h2,)
143
- plain_line %( [)
144
- plain_line %( :span,)
145
- plain_line %( [)
146
- plain_line %( :text,)
147
- red_line %(- "Hello world")
148
- green_line %(+ "Goodbye world")
149
- plain_line %( ])
150
- plain_line %( ],)
151
- plain_line %( {)
152
- plain_line %( class: "header",)
153
- plain_line %( data: {)
154
- red_line %(- "sticky" => true,)
155
- green_line %(+ "sticky" => false,)
156
- plain_line %( person: #<SuperDiff::Test::Person {)
157
- red_line %(- name: "Marty")
158
- green_line %(+ name: "Doc")
159
- plain_line %( }>,)
160
- green_line %(+ role: "deprecated")
161
- plain_line %( },)
162
- green_line %(+ id: "hero")
163
- plain_line %( })
164
- plain_line %( ],)
165
- green_line %(+ :br)
166
- plain_line %( ])
167
- end
168
- }
169
- OUTPUT
170
-
171
- expect(test).to produce_output_when_run(expected_output)
172
- end
173
- end
174
-
175
- context "comparing two hashes with other data structures inside" do
176
- it "produces the correct output" do
177
- test = <<~TEST
178
- expected = {
179
- customer: {
180
- person: SuperDiff::Test::Person.new(name: "Marty McFly"),
181
- shipping_address: {
182
- line_1: "123 Main St.",
183
- city: "Hill Valley",
184
- state: "CA",
185
- zip: "90382"
186
- }
187
- },
188
- items: [
189
- {
190
- name: "Fender Stratocaster",
191
- cost: 100_000,
192
- options: ["red", "blue", "green"]
193
- },
194
- { name: "Chevy 4x4" }
195
- ]
196
- }
197
- actual = {
198
- customer: {
199
- person: SuperDiff::Test::Person.new(name: "Marty McFly, Jr."),
200
- shipping_address: {
201
- line_1: "456 Ponderosa Ct.",
202
- city: "Hill Valley",
203
- state: "CA",
204
- zip: "90382"
205
- }
206
- },
207
- items: [
208
- {
209
- name: "Fender Stratocaster",
210
- cost: 100_000,
211
- options: ["red", "blue", "green"]
212
- },
213
- { name: "Mattel Hoverboard" }
214
- ]
215
- }
216
- expect(actual).to eq(expected)
217
- TEST
218
-
219
- expected_output = <<~OUTPUT
220
- Diff:
221
-
222
- #{
223
- colored do
224
- plain_line %( {)
225
- plain_line %( customer: {)
226
- plain_line %( person: #<SuperDiff::Test::Person {)
227
- red_line %(- name: "Marty McFly")
228
- green_line %(+ name: "Marty McFly, Jr.")
229
- plain_line %( }>,)
230
- plain_line %( shipping_address: {)
231
- red_line %(- line_1: "123 Main St.",)
232
- green_line %(+ line_1: "456 Ponderosa Ct.",)
233
- plain_line %( city: "Hill Valley",)
234
- plain_line %( state: "CA",)
235
- plain_line %( zip: "90382")
236
- plain_line %( })
237
- plain_line %( },)
238
- plain_line %( items: [)
239
- plain_line %( {)
240
- plain_line %( name: "Fender Stratocaster",)
241
- plain_line %( cost: 100000,)
242
- plain_line %( options: ["red", "blue", "green"])
243
- plain_line %( },)
244
- plain_line %( {)
245
- red_line %(- name: "Chevy 4x4")
246
- green_line %(+ name: "Mattel Hoverboard")
247
- plain_line %( })
248
- plain_line %( ])
249
- plain_line %( })
250
- end
251
- }
252
- OUTPUT
253
-
254
- expect(test).to produce_output_when_run(expected_output)
255
- end
256
- end
257
-
258
- def colored(&block)
259
- SuperDiff::Tests::Colorizer.call(&block).chomp
260
- end
261
- end