@@ -59,5 +59,49 @@ module SOF
5959 expect ( span . end_date_of_period ( "2022-01-15" . to_date ) ) . to eq Date . parse ( "2022-03-31" )
6060 end
6161 end
62+
63+ describe "#begin_date_of_period" do
64+ it "for a quarter period returns the beginning of the quarter" do
65+ span = described_class . for ( "1" , "Q" )
66+ expect ( span . begin_date_of_period ( "2022-02-15" . to_date ) ) . to eq Date . parse ( "2022-01-01" )
67+ end
68+
69+ it "for a week period returns the beginning of the week" do
70+ span = described_class . for ( "1" , "W" )
71+ # 2022-01-15 is a Saturday, beginning of week is Monday 2022-01-10
72+ expect ( span . begin_date_of_period ( "2022-01-15" . to_date ) ) . to eq Date . parse ( "2022-01-10" )
73+ end
74+
75+ it "for a day period returns the same date" do
76+ span = described_class . for ( "1" , "D" )
77+ expect ( span . begin_date_of_period ( "2022-01-15" . to_date ) ) . to eq Date . parse ( "2022-01-15" )
78+ end
79+ end
80+
81+ describe "#duration" do
82+ it "for quarters returns the duration in months" do
83+ span = described_class . for ( "2" , "Q" )
84+ expect ( span . duration ) . to eq 6 . months
85+ end
86+ end
87+
88+ describe "#final_date" do
89+ it "returns nil if there is no period" do
90+ span = described_class . for ( "" , "" )
91+ expect ( span . final_date ( Date . parse ( "2022-01-15" ) ) ) . to be_nil
92+ end
93+
94+ it "returns the end date for a valid period" do
95+ span = described_class . for ( "3" , "M" )
96+ expect ( span . final_date ( Date . parse ( "2022-01-15" ) ) ) . to eq Date . parse ( "2022-04-15" )
97+ end
98+ end
99+
100+ describe "#to_h" do
101+ it "returns a hash with period and period_count" do
102+ span = described_class . for ( "3" , "M" )
103+ expect ( span . to_h ) . to eq ( { period : :month , period_count : 3 } )
104+ end
105+ end
62106 end
63107end
0 commit comments