From 34fa7765f550fc36df6d2c0fba122af23065a8b8 Mon Sep 17 00:00:00 2001 From: JD Ivey Date: Thu, 23 Jun 2022 07:54:46 -0700 Subject: [PATCH] add sunset year for visible terms (#2434) * add sunset year for visible terms * cover all the possible test cases --- lib/term_year.rb | 12 +++- spec/lib/term_year_spec.rb | 129 +++++++++++++++++++++++++++++-------- 2 files changed, 111 insertions(+), 30 deletions(-) diff --git a/lib/term_year.rb b/lib/term_year.rb index 63a41e39fa..dc30a696d7 100644 --- a/lib/term_year.rb +++ b/lib/term_year.rb @@ -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) @@ -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 } diff --git a/spec/lib/term_year_spec.rb b/spec/lib/term_year_spec.rb index f567a1c2dd..14378fbd4a 100644 --- a/spec/lib/term_year_spec.rb +++ b/spec/lib/term_year_spec.rb @@ -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