2015-10-08 01:28:45 -04:00
|
|
|
|
require 'shoulda/matchers/util/word_wrap'
|
|
|
|
|
|
2014-10-08 01:19:07 -04:00
|
|
|
|
module Shoulda
|
|
|
|
|
module Matchers
|
|
|
|
|
# @private
|
|
|
|
|
module Util
|
|
|
|
|
def self.deconstantize(path)
|
|
|
|
|
if defined?(ActiveSupport::Inflector) &&
|
|
|
|
|
ActiveSupport::Inflector.respond_to?(:deconstantize)
|
|
|
|
|
ActiveSupport::Inflector.deconstantize(path)
|
|
|
|
|
else
|
|
|
|
|
path.to_s[0...(path.to_s.rindex('::') || 0)]
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-11-05 13:07:29 -05:00
|
|
|
|
|
|
|
|
|
def self.safe_constantize(camel_cased_word)
|
|
|
|
|
if defined?(ActiveSupport::Inflector) &&
|
|
|
|
|
ActiveSupport::Inflector.respond_to?(:safe_constantize)
|
|
|
|
|
ActiveSupport::Inflector.safe_constantize(camel_cased_word)
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
camel_cased_word.constantize
|
|
|
|
|
rescue NameError
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-22 20:46:01 -05:00
|
|
|
|
|
|
|
|
|
def self.indent(string, width)
|
2017-06-29 03:10:21 -04:00
|
|
|
|
return if !string
|
2015-01-22 20:46:01 -05:00
|
|
|
|
indentation = ' ' * width
|
|
|
|
|
string.split(/[\n\r]/).map { |line| indentation + line }.join("\n")
|
|
|
|
|
end
|
2015-12-08 00:00:37 -05:00
|
|
|
|
|
|
|
|
|
def self.a_or_an(next_word)
|
2019-05-24 02:34:24 -04:00
|
|
|
|
if next_word =~ /\A[aeiou]/i && next_word != 'unique'
|
2015-12-08 00:00:37 -05:00
|
|
|
|
"an #{next_word}"
|
|
|
|
|
else
|
|
|
|
|
"a #{next_word}"
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-12-22 02:00:51 -05:00
|
|
|
|
|
|
|
|
|
def self.inspect_value(value)
|
2018-01-26 14:33:27 -05:00
|
|
|
|
case value
|
|
|
|
|
when Hash
|
|
|
|
|
inspect_hash(value)
|
|
|
|
|
when Range
|
|
|
|
|
inspect_range(value)
|
|
|
|
|
else
|
|
|
|
|
"‹#{value.inspect}›"
|
|
|
|
|
end
|
2015-12-22 02:00:51 -05:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.inspect_values(values)
|
|
|
|
|
values.map { |value| inspect_value(value) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.inspect_range(range)
|
|
|
|
|
"#{inspect_value(range.first)} to #{inspect_value(range.last)}"
|
|
|
|
|
end
|
2015-12-27 00:41:04 -05:00
|
|
|
|
|
2018-01-26 14:33:27 -05:00
|
|
|
|
def self.inspect_hash(hash)
|
|
|
|
|
output = '‹{'
|
|
|
|
|
|
|
|
|
|
output << hash.map { |key, value|
|
|
|
|
|
if key.is_a?(Symbol)
|
|
|
|
|
"#{key}: #{value.inspect}"
|
|
|
|
|
else
|
|
|
|
|
"#{key.inspect} => #{value.inspect}"
|
|
|
|
|
end
|
|
|
|
|
}.join(', ')
|
|
|
|
|
|
|
|
|
|
output << '}›'
|
|
|
|
|
end
|
|
|
|
|
|
2015-12-27 00:41:04 -05:00
|
|
|
|
def self.dummy_value_for(column_type, array: false)
|
|
|
|
|
if array
|
|
|
|
|
[dummy_value_for(column_type, array: false)]
|
|
|
|
|
else
|
|
|
|
|
case column_type
|
|
|
|
|
when :integer
|
|
|
|
|
0
|
|
|
|
|
when :date
|
|
|
|
|
Date.new(2100, 1, 1)
|
|
|
|
|
when :datetime, :timestamp
|
|
|
|
|
DateTime.new(2100, 1, 1)
|
|
|
|
|
when :time
|
|
|
|
|
Time.new(2100, 1, 1)
|
|
|
|
|
when :uuid
|
|
|
|
|
SecureRandom.uuid
|
|
|
|
|
when :boolean
|
|
|
|
|
true
|
|
|
|
|
else
|
|
|
|
|
'dummy value'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-10-08 01:19:07 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|