spec/lib/dotiw_spec.rb in dotiw-5.0.0 vs spec/lib/dotiw_spec.rb in dotiw-5.1.0

- old
+ new

@@ -5,10 +5,12 @@ describe 'A better distance_of_time_in_words' do if defined?(ActionView) include ActionView::Helpers::DateHelper include ActionView::Helpers::TextHelper include ActionView::Helpers::NumberHelper + + require 'action_controller' else include DOTIW::Methods end START_TIME = '01-08-2009'.to_time @@ -17,11 +19,11 @@ I18n.locale = :en allow(Time).to receive(:now).and_return(START_TIME) allow(Time.zone).to receive(:now).and_return(START_TIME) end - describe 'distance of time' do + describe '#distance_of_time' do [ [0.5.minutes, '30 seconds'], [4.5.minutes, '4 minutes and 30 seconds'], [5.minutes, '5 minutes'], [10.minutes, '10 minutes'], @@ -46,11 +48,11 @@ expect(distance_of_time(1.2.minute, include_seconds: true, except: 'seconds')).to eq('1 minute') end end end - describe 'hash version' do + describe '#distance_of_time_in_words_hash' do describe 'giving correct numbers of' do %i[years months weeks days minutes seconds].each do |name| describe name do it 'exactly' do hash = distance_of_time_in_words_hash(START_TIME, START_TIME + 1.send(name)) @@ -78,19 +80,54 @@ expect(hash[:seconds]).to eq(7) end end end - describe 'real version' do - it 'debe hablar español' do - expect(distance_of_time_in_words(START_TIME, START_TIME + 1.days, locale: :es)).to eq('un día') - expect(distance_of_time_in_words(START_TIME, START_TIME + 5.days, locale: :es)).to eq('5 días') + describe '#time_ago_in_words' do + it 'aliases to distance_of_time_in_words' do + expect(time_ago_in_words(Time.now - 3.days - 14.minutes)).to eq('3 days and 14 minutes') end + end - it "deve parlare l'italiano" do - expect(distance_of_time_in_words(START_TIME, START_TIME + 1.days, true, locale: :it)).to eq('un giorno') - expect(distance_of_time_in_words(START_TIME, START_TIME + 5.days, true, locale: :it)).to eq('5 giorni') + describe '#distance_of_time_in_words' do + context 'locale' do + it 'includes known languages' do + expect(DOTIW.languages).to include :en + expect(DOTIW.languages).to include :ru + end + + it 'includes all the languages in specs' do + languages = Dir[File.join(File.dirname(__FILE__), 'i18n', '*.yml')].map { |f| File.basename(f, '.yml') } + expect(DOTIW.languages.map(&:to_s).sort).to eq languages.sort + end + + DOTIW.languages.each do |lang| + context lang do + YAML.safe_load( + File.read( + File.join( + File.dirname(__FILE__), 'i18n', "#{lang}.yml" + ) + ) + ).each_pair do |category, fixtures| + context category do + fixtures.each_pair do |k, v| + it v do + expect( + distance_of_time_in_words( + START_TIME, + START_TIME + eval(k), + true, + locale: lang + ) + ).to eq(v) + end + end + end + end + end + end end [ [START_TIME, START_TIME + 5.days + 3.minutes, '5 days and 3 minutes'], [START_TIME, START_TIME + 1.minute, '1 minute'], @@ -143,11 +180,11 @@ it "should be #{output}" do expect(distance_of_time_in_words(start, finish)).to eq(output) end end - describe 'accumulate on' do + describe 'accumulate_on:' do [ [START_TIME, START_TIME + 10.minute, :seconds, '600 seconds'], @@ -296,21 +333,40 @@ ].each do |start, finish, options, output| it "should be #{output}" do expect(distance_of_time_in_words(start, finish, true, options)).to eq(output) end end + + context 'via ActionController::Base.helpers' do + it '#distance_of_time_in_words' do + end_time = START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds + expected = '1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds' + actual = ActionController::Base.helpers.distance_of_time_in_words(START_TIME, end_time, true, { vague: false }) + expect(actual).to eq(expected) + end + + it '#time_ago_in_words' do + expected = '3 days and 14 minutes' + actual = ActionController::Base.helpers.time_ago_in_words(Time.now - 3.days - 14.minutes) + expect(actual).to eq(expected) + end + end end end describe 'include_seconds' do it 'is ignored if only seconds have passed' do expect(distance_of_time_in_words(START_TIME, START_TIME + 1.second, false)).to eq('1 second') end it 'removes seconds in all other cases' do - expect(distance_of_time_in_words(START_TIME, - START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, - false)).to eq('1 year, 2 months, 3 weeks, 4 days, 5 hours, and 6 minutes') + expect( + distance_of_time_in_words( + START_TIME, + START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds, + false + ) + ).to eq('1 year, 2 months, 3 weeks, 4 days, 5 hours, and 6 minutes') end end # include_seconds end if defined?(ActionView)