Add tests for StreamEntriesHelper#rtl? (#2286)

We used some random Arabic characters to test that the various RTL
conditions got triggered.
This commit is contained in:
Joël Quenneville 2017-04-21 18:13:37 -04:00 committed by Eugen
parent af7d02da5d
commit 974ac467de
2 changed files with 29 additions and 9 deletions

View File

@ -37,20 +37,22 @@ module StreamEntriesHelper
end end
def rtl?(text) def rtl?(text)
return false if text.empty? rtl_characters = /[\p{Hebrew}|\p{Arabic}|\p{Syriac}|\p{Thaana}|\p{Nko}]+/m.match(text)
matches = /[\p{Hebrew}|\p{Arabic}|\p{Syriac}|\p{Thaana}|\p{Nko}]+/m.match(text) if rtl_characters.present?
total_size = text.strip.size.to_f
return false unless matches rtl_size(rtl_characters.to_a) / total_size > 0.3
else
rtl_size = matches.to_a.reduce(0) { |acc, elem| acc + elem.size }.to_f false
ltr_size = text.strip.size.to_f end
rtl_size / ltr_size > 0.3
end end
private private
def rtl_size(characters)
characters.reduce(0) { |acc, elem| acc + elem.size }.to_f
end
def embedded_view? def embedded_view?
params[:controller] == 'stream_entries' && params[:action] == 'embed' params[:controller] == 'stream_entries' && params[:action] == 'embed'
end end

View File

@ -14,4 +14,22 @@ RSpec.describe StreamEntriesHelper, type: :helper do
expect(helper.display_name(account)).to eq "Username" expect(helper.display_name(account)).to eq "Username"
end end
end end
describe '#rtl?' do
it 'is false if text is empty' do
expect(helper).not_to be_rtl ''
end
it 'is false if there are no right to left characters' do
expect(helper).not_to be_rtl 'hello world'
end
it 'is false if right to left characters are fewer than 1/3 of total text' do
expect(helper).not_to be_rtl 'hello ݟ world'
end
it 'is true if right to left characters are greater than 1/3 of total text' do
expect(helper).to be_rtl 'aaݟ'
end
end
end end