Skip to content

Commit

Permalink
Added text normalization specs, truncate_title does not add page titl…
Browse files Browse the repository at this point in the history
…e when site title is too long
  • Loading branch information
kpumuk committed Aug 20, 2015
1 parent 1d70c2c commit 0de0093
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/meta_tags/text_normalizer.rb
Expand Up @@ -16,8 +16,14 @@ def self.normalize_title(site_title, title, separator, reverse = false)
separator = strip_tags(separator)

if MetaTags.config.title_limit
limit = MetaTags.config.title_limit - site_title.length - separator.length
title = truncate_array(title, limit, separator)
limit = MetaTags.config.title_limit - separator.length
if limit > site_title.length
title = truncate_array(title, limit - site_title.length, separator)
else
site_title = truncate(site_title, limit)
# Site title is too long, we have to skip page title
title = []
end
end

title.unshift(site_title) if site_title.present?
Expand Down Expand Up @@ -126,7 +132,7 @@ def self.truncate_array(string_array, limit = nil, separator = '', natural_separ
length = 0
result = []
string_array.each do |string|
limit_left = limit - length - separator.length
limit_left = limit - length - (result.any? ? separator.length : 0)
if string.length > limit_left
result << truncate(string, limit_left, natural_separator)
break
Expand Down
43 changes: 43 additions & 0 deletions spec/text_normalizer/normalize_title_spec.rb
@@ -0,0 +1,43 @@
require 'spec_helper'

describe MetaTags::TextNormalizer, '.normalize_title' do
context 'when site_title is blank' do
it 'should return title when site_title is blank' do
expect(subject.normalize_title(nil, 'title', '-')).to eq('title')
expect(subject.normalize_title('', 'title', '-')).to eq('title')
end

it 'should join title parts with separator' do
expect(subject.normalize_title('', %w[title subtitle], '-')).to eq('title-subtitle')
end

it 'should reverse title parts when reverse is true' do
expect(subject.normalize_title('', %w[title subtitle], '-', true)).to eq('subtitle-title')
end
end

context 'when site_title is specified' do
it 'should join title and site_title with separator' do
expect(subject.normalize_title('site', 'title', '-')).to eq('site-title')
end

it 'should join title parts and site_title with separator' do
expect(subject.normalize_title('site', %w[title subtitle], '-')).to eq('site-title-subtitle')
end

it 'should reverse title parts when reverse is true' do
expect(subject.normalize_title('site', %w[title subtitle], '-', true)).to eq('subtitle-title-site')
end

it 'should not add title when site title is longer than limit' do
site_title = 'a' * (MetaTags.config.title_limit - 2)
expect(subject.normalize_title(site_title, 'title', '---')).to eq(site_title[0..-2])
end

it 'should truncate title when limit is reached' do
site_title = 'a' * 20
title = 'b' * (MetaTags.config.title_limit + 10)
expect(subject.normalize_title(site_title, title, '-')).to eq("#{site_title}-#{'b' * (MetaTags.config.title_limit - 21)}")
end
end
end
60 changes: 60 additions & 0 deletions spec/text_normalizer/truncate_array_spec.rb
@@ -0,0 +1,60 @@
require 'spec_helper'

describe MetaTags::TextNormalizer, '.truncate_array' do
it 'should return array as is when limit is not specified' do
arr = %w[a]
expect(subject.truncate_array(arr, nil)).to be(arr)
expect(subject.truncate_array(arr, 0)).to be(arr)
end

it 'should return a new array when limit is specified' do
arr = %w[a]
expect(subject.truncate_array(arr, 1)).to_not be(arr)
end

context 'when separator is empty string' do
it 'should return the whole array when total size is less than or equal to limit' do
arr = %w[a a]
expect(subject.truncate_array(arr, 5)).to eq(arr)
expect(subject.truncate_array(arr, 2)).to eq(arr)
end

it 'should truncate array to specified limit' do
arr = %w[a a a a a]
expect(subject.truncate_array(arr, 3)).to eq(%w[a a a])
end

it 'should truncate last word to match the limit' do
arr = %w[a a aaaa aa]
expect(subject.truncate_array(arr, 4)).to eq(%w[a a aa])
end

it 'should use natural separator when truncating a long word' do
arr = ['a', 'aa aaaa', 'aa']
expect(subject.truncate_array(arr, 7)).to eq(%w[a aa])
end
end

context 'when separator is specified' do
it 'should return the whole array when total size is less than or equal to limit' do
arr = %w[a a]
expect(subject.truncate_array(arr, 5, '-')).to eq(arr)
expect(subject.truncate_array(arr, 3, '-')).to eq(arr)
end

it 'should truncate array to specified limit' do
arr = %w[a a a a a]
expect(subject.truncate_array(arr, 3, '-')).to eq(%w[a a])
end

it 'should truncate last word to match the limit' do
arr = %w[a a aaaa aa]
expect(subject.truncate_array(arr, 5, '-')).to eq(%w[a a a])
end

it 'should use natural separator when truncating a long word' do
arr = ['a', 'aa aaaa', 'aa']
expect(subject.truncate_array(arr, 7, '-')).to eq(%w[a aa])
end
end
end

0 comments on commit 0de0093

Please sign in to comment.