Class: Nicetest::SuperdiffPlugin::AssertIncludesMessage

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/minitest/superdiff_plugin.rb

Instance Method Summary collapse

Methods included from Helpers

#inspect_styled

Constructor Details

#initialize(collection:, item:) ⇒ AssertIncludesMessage

Returns a new instance of AssertIncludesMessage.



52
53
54
55
# File 'lib/minitest/superdiff_plugin.rb', line 52

def initialize(collection:, item:)
  @collection = collection
  @item = item
end

Instance Method Details

#basic_diff(expected, actual) ⇒ Object



92
93
94
95
# File 'lib/minitest/superdiff_plugin.rb', line 92

def basic_diff(expected, actual)
  content = SuperDiff.diff(expected, actual)
  "\nDiff:\n\n#{content}"
end

#optional_diffObject



84
85
86
87
88
89
90
# File 'lib/minitest/superdiff_plugin.rb', line 84

def optional_diff
  case @collection
  when Array, Set
    collection_with_item = @collection + [@item].flatten(1)
    basic_diff(@collection, collection_with_item)
  end
end

#to_sObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/minitest/superdiff_plugin.rb', line 57

def to_s
  return @to_s if defined?(@to_s)

  content = if (diff = optional_diff)
    collection = inspect_styled(@collection, :expected)
    item = inspect_styled(@item, :actual)

    <<~OUTPUT.strip
      Expected #{collection} to include #{item}, but it did not.

      #{diff}
    OUTPUT
  else
    expected = inspect_styled(@collection, :expected, prefix: "  Collection: ")
    actual = inspect_styled(@item, :actual, prefix: "Missing item: ")

    <<~OUTPUT.strip
      Expected collection to include item but it did not.

      #{expected}
      #{actual}
    OUTPUT
  end

  @to_s ||= content
end