Skip to content

Commit

Permalink
Truncate long values in descriptions. Fixes #1041
Browse files Browse the repository at this point in the history
  • Loading branch information
betesh authored and mcmire committed May 6, 2020
1 parent 390c2ca commit 8bf411f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/shoulda/matchers/util.rb
Expand Up @@ -4,6 +4,8 @@ module Shoulda
module Matchers
# @private
module Util
MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY = 500

def self.deconstantize(path)
if defined?(ActiveSupport::Inflector) &&
ActiveSupport::Inflector.respond_to?(:deconstantize)
Expand Down Expand Up @@ -47,7 +49,12 @@ def self.inspect_value(value)
when Range
inspect_range(value)
else
"‹#{value.inspect}›"
inspected_value = value.inspect
if inspected_value.length > MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY
"‹#{inspected_value[0, MAXIMUM_LENGTH_OF_VALUE_TO_DISPLAY]}...›"
else
"‹#{inspected_value}›"
end
end
end

Expand Down
Expand Up @@ -41,6 +41,12 @@
)
end
end

it 'truncates the description when long' do
matcher = allow_value("A" * 10000).for(:baz)

expect(matcher.description).to eq "allow :baz to be ‹\"#{"A" * 499}...›"
end
end

describe '#_after_setting_value' do
Expand Down

0 comments on commit 8bf411f

Please sign in to comment.