Skip to content

Commit

Permalink
add sunset year for visible terms (#2434)
Browse files Browse the repository at this point in the history
* add sunset year for visible terms

* cover all the possible test cases
  • Loading branch information
jivey authored Jun 23, 2022
1 parent da4ca27 commit 34fa776
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 30 deletions.
12 changes: 10 additions & 2 deletions lib/term_year.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

const_set 'VISIBLE_TERMS', [:spring, :summer, :fall, :winter]

SUNSET_YEAR = 2023
SUNSET_YEAR_VISIBLE_TERMS = [:spring]

attr_reader :starts_at, :ends_at

def initialize(term, year)
Expand All @@ -45,14 +48,19 @@ def initialize(term, year)
@ends_at = end_proc.call(year) - 1.second
end

def self.visible_terms(year)
return [] if year > SUNSET_YEAR
year == SUNSET_YEAR ? SUNSET_YEAR_VISIBLE_TERMS : TermYear::VISIBLE_TERMS
end

def self.visible_term_years(current_time = Time.current)
current_year = current_time.year

current_year_visible_term_years = TermYear::VISIBLE_TERMS.map do |term|
current_year_visible_term_years = visible_terms(current_year).map do |term|
TermYear.new(term, current_year)
end.select { |term_year| term_year.ends_at > current_time }

next_year_visible_term_years = TermYear::VISIBLE_TERMS.map do |term|
next_year_visible_term_years = visible_terms(current_year + 1).map do |term|
TermYear.new(term, current_year + 1)
end.select { |term_year| term_year.ends_at <= current_time + 1.year }

Expand Down
129 changes: 101 additions & 28 deletions spec/lib/term_year_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,106 @@
end
end

it 'returns the correct visible_term_years' do
current_year = Time.current.year

spring_date_time = DateTime.parse("March 1st, #{current_year}" )
spring_summer_date_time = DateTime.parse("June 1st, #{current_year}" )
summer_fall_date_time = DateTime.parse("July 1st, #{current_year}" )
fall_date_time = DateTime.parse("November 1st, #{current_year}")

expect(TermYear.visible_term_years(spring_date_time)).to eq [
TermYear.new(:spring, current_year), TermYear.new(:summer, current_year ),
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year)
]

expect(TermYear.visible_term_years(spring_summer_date_time)).to eq [
TermYear.new(:spring, current_year ), TermYear.new(:summer, current_year ),
TermYear.new(:fall, current_year ), TermYear.new(:winter, current_year )
]

expect(TermYear.visible_term_years(summer_fall_date_time)).to eq [
TermYear.new(:summer, current_year ), TermYear.new(:fall, current_year ),
TermYear.new(:winter, current_year ), TermYear.new(:spring, current_year + 1)
]

expect(TermYear.visible_term_years(fall_date_time)).to eq [
TermYear.new(:fall, current_year ), TermYear.new(:winter, current_year ),
TermYear.new(:spring, current_year + 1), TermYear.new(:summer, current_year + 1),
]
describe 'visible terms' do
let(:current_year) { Time.current.year }
let(:spring_date_time) { DateTime.parse "March 1st, #{current_year}" }
let(:spring_summer_date_time) { DateTime.parse "June 1st, #{current_year}" }
let(:summer_fall_date_time) { DateTime.parse "July 1st, #{current_year}" }
let(:fall_date_time) { DateTime.parse "November 1st, #{current_year}" }

context 'sunset is in two years' do
before { stub_const('SUNSET_YEAR', current_year + 2) }

it 'returns the correct visible_term_years' do
expect(TermYear.visible_term_years(spring_date_time)).to eq [
TermYear.new(:spring, current_year),
TermYear.new(:summer, current_year),
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year)
]

expect(TermYear.visible_term_years(spring_summer_date_time)).to eq [
TermYear.new(:spring, current_year),
TermYear.new(:summer, current_year),
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year)
]

expect(TermYear.visible_term_years(summer_fall_date_time)).to eq [
TermYear.new(:summer, current_year),
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year),
TermYear.new(:spring, current_year + 1)
]

expect(TermYear.visible_term_years(fall_date_time)).to eq [
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year),
TermYear.new(:spring, current_year + 1),
TermYear.new(:summer, current_year + 1)
]
end
end

context 'sunset is next year' do
before { stub_const('SUNSET_YEAR', current_year + 1) }

it 'returns the correct visible_term_years' do
expect(TermYear.visible_term_years(spring_date_time)).to eq [
TermYear.new(:spring, current_year),
TermYear.new(:summer, current_year),
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year)
]

expect(TermYear.visible_term_years(spring_summer_date_time)).to eq [
TermYear.new(:spring, current_year),
TermYear.new(:summer, current_year),
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year)
]

expect(TermYear.visible_term_years(summer_fall_date_time)).to eq [
TermYear.new(:summer, current_year),
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year),
TermYear.new(:spring, current_year + 1)
]

expect(TermYear.visible_term_years(fall_date_time)).to eq [
TermYear.new(:fall, current_year),
TermYear.new(:winter, current_year),
TermYear.new(:spring, current_year + 1)
]
end
end

context 'sunset is current year' do
before { stub_const('SUNSET_YEAR', current_year) }

it 'returns the correct visible_term_years' do
expect(TermYear.visible_term_years(spring_date_time)).to eq [
TermYear.new(:spring, current_year),
]

expect(TermYear.visible_term_years(spring_summer_date_time)).to eq [
TermYear.new(:spring, current_year),
]

expect(TermYear.visible_term_years(summer_fall_date_time)).to eq []
expect(TermYear.visible_term_years(fall_date_time)).to eq []
end
end

context 'sunset is in the past' do
before { stub_const('SUNSET_YEAR', current_year - 1) }

it 'returns the correct visible_term_years' do
expect(TermYear.visible_term_years(spring_date_time)).to eq []
expect(TermYear.visible_term_years(spring_summer_date_time)).to eq []
expect(TermYear.visible_term_years(summer_fall_date_time)).to eq []
expect(TermYear.visible_term_years(fall_date_time)).to eq []
end
end
end
end

0 comments on commit 34fa776

Please sign in to comment.