Skip to content

Commit

Permalink
Merge pull request #3516 from alphagov/date-parse-bug
Browse files Browse the repository at this point in the history
`DateParser`: Allow nil input
  • Loading branch information
csutter authored Oct 18, 2024
2 parents e7817e0 + 1439bec commit f182c72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/parsers/date_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def parse
DateStringParser.new(date_param).parse
when Hash
DateHashParser.new(date_param).parse
else raise ArgumentError, "date_param must be a String or a Hash"
when nil
nil
else raise ArgumentError, "date_param must be a String, Hash, or nil"
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/parsers/date_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,21 @@
expect(date_parser.parse).to eq(Date.new(2024, 8, 17))
end
end

context "when given nil" do
let(:date_param) { nil }

it "returns nil" do
expect(date_parser.parse).to be_nil
end
end

context "when given an unexpected object" do
let(:date_param) { Object.new }

it "raises an error" do
expect { date_parser.parse }.to raise_error(ArgumentError, /be a String, Hash, or nil/)
end
end
end
end

0 comments on commit f182c72

Please sign in to comment.