# frozen_string_literal: true
require 'spec_helper'
module Calagator
describe TimeRangeHelper do
let(:start_time) { Time.utc(2008, 4, 1, 9, 0o0) }
# Test all permutations of
# - context-date: with vs without
# - format: hcal vs html (tags stripped) vs text (tags stripped, '–' --> '-')
tests = [
# comment, end_time, results_without_context, results_with_context
['start time only', nil,
'',
''],
['same day & am-pm', Time.utc(2008, 4, 1, 11, 0o0),
'–',
'–'],
['same day, different am-pm', Time.utc(2008, 4, 1, 13, 30),
'–',
'–'],
['different days', Time.utc(2009, 4, 1, 13, 30),
' through ',
' through ']
]
[nil, Date.new(2008, 4, 1)].each do |context_date|
describe "with#{context_date.nil? ? 'out' : ''} context date" do
%i[text hcal html].each do |format|
tests.each do |label, end_time, expected_without_context, expected_with_context|
expected = context_date ? expected_with_context : expected_without_context
expected = expected.gsub(/\<[^\>]*\>/, '') if format != :hcal
expected = expected.gsub('–', '-') if format == :text
it "formats #{label} in #{format} format as '#{expected}'" do
actual = helper.normalize_time(start_time, end_time, format: format, context: context_date)
expect(actual).to eq expected
end
end
end
end
end
describe 'with objects' do
it 'formats from objects that respond to just start_time' do
event = Event.new(start_time: Time.zone.parse('2008-04-01 13:30'))
actual = helper.normalize_time(event, format: :text)
expect(actual).to eq 'Tuesday, April 1, 2008 at 1:30pm'
end
it 'formats from objects that respond to both start_time and end_time' do
event = Event.new(start_time: Time.zone.parse('2008-04-01 13:30'),
end_time: Time.zone.parse('2008-04-01 15:30'))
actual = helper.normalize_time(event, format: :text)
expect(actual).to eq 'Tuesday, April 1, 2008 from 1:30-3:30pm'
end
end
end
end