Skip to content

Commit

Permalink
Fixes #28154 - random name/description test failure fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Oct 30, 2019
1 parent 1345b63 commit 90164d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions lib/core_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ def to_utf8
def contains_erb?
self =~ /<%.*%>/
end

# TODO Remove me after Rails 6 upgrade: https://github.com/rails/rails/commit/4940cc49ddb361d584d51bc3eb4675ff8ece4a2b
def truncate_bytes(truncate_at, omission: "…")
omission ||= ""

if bytesize <= truncate_at
dup
elsif omission.bytesize > truncate_at
raise ArgumentError, "Omission #{omission.inspect} is #{omission.bytesize}, larger than the truncation length of #{truncate_at} bytes"
elsif omission.bytesize == truncate_at
omission.dup
else
self.class.new.tap do |cut|
cut_at = truncate_at - omission.bytesize

scan(/\X/) do |grapheme|
if cut.bytesize + grapheme.bytesize <= cut_at
cut << grapheme
else
break
end
end

cut << omission
end
end
end
end

class Object
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def valid_name_list
[
RFauxFactory.gen_alpha(1),
RFauxFactory.gen_alpha(255),
*RFauxFactory.gen_strings(1..255, exclude: [:html]).values,
*RFauxFactory.gen_strings(1..255, exclude: [:html]).values.map {|x| x.truncate_bytes(255, omission: '')},
RFauxFactory.gen_html(rand((1..230))),
]
end
Expand Down

0 comments on commit 90164d7

Please sign in to comment.