2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-09-24 15:44:32 +00:00
|
|
|
RSpec::Matchers.define :include_assets_of do
|
|
|
|
match do |actual|
|
|
|
|
expected_array.all? { |elem| find_assets_of(elem, actual) }
|
|
|
|
end
|
|
|
|
|
|
|
|
match_when_negated do |actual|
|
|
|
|
expected_array.none? { |elem| find_assets_of(elem, actual) }
|
|
|
|
end
|
|
|
|
|
|
|
|
description do
|
|
|
|
"include assets of #{expected_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |actual|
|
|
|
|
list = expected_array.reject { |elem| find_assets_of(elem, actual) }
|
2020-09-30 09:07:01 +00:00
|
|
|
"Expected hash to include, but not included:\n#{items_for_message(list)}"
|
2019-09-24 15:44:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
failure_message_when_negated do |actual|
|
|
|
|
list = expected_array.select { |elem| find_assets_of(elem, actual) }
|
2020-09-30 09:07:01 +00:00
|
|
|
"Expected hash to not include, but was included:\n#{items_for_message(list)}"
|
2019-09-24 15:44:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def items_for_message(items)
|
|
|
|
items
|
|
|
|
.map { |elem| "- #{item_name(elem)}" }
|
|
|
|
.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def expected_name
|
|
|
|
expected_array
|
|
|
|
.map { |elem| item_name(elem) }
|
|
|
|
.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def item_name(item)
|
|
|
|
"#{item.class.name}##{item.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def expected_array
|
|
|
|
Array(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Finds corresponding object's data in assets hash
|
|
|
|
#
|
|
|
|
# @param [ActiveRecord::Base] object to look for
|
|
|
|
# @param [Hash] assets hash to use
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# assets = Ticket.first.assets
|
|
|
|
# find_assets_of(Ticket.first, assets)
|
|
|
|
#
|
|
|
|
# @return [Hash, nil]
|
|
|
|
def find_assets_of(object, actual)
|
2022-02-24 11:15:19 +00:00
|
|
|
actual
|
|
|
|
.deep_stringify_keys
|
|
|
|
.dig(object.class.name.gsub(%r{::}, ''), object.id.to_s)
|
2019-09-24 15:44:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec::Matchers.define_negated_matcher :not_include_assets_of, :include_assets_of
|