Skip to content

Commit

Permalink
fix: parsing impossible dates (#56)
Browse files Browse the repository at this point in the history
* fix: parsing impossible dates

* fix: skip begin for method block rescue

* fix: extend compatibility to rubies 2.6 and 2.5

The `Date::Error` exception class was added with Ruby 2.7 but this gem still supports a couple of versions below that.

---------

Co-authored-by: Domenico Caiazza <[email protected]>
Co-authored-by: Matteo Rossi <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent ffac35c commit 4f5cc91
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/itax_code/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ class Parser
InvalidControlInternalNumberError = Class.new(Error)
InvalidTaxCodeError = Class.new(Error)
NoTaxCodeError = Class.new(Error)
DateTaxCodeError = Class.new(Error)
end
end
2 changes: 2 additions & 0 deletions lib/itax_code/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def day

def birthdate
@birthdate ||= Date.parse("#{year}-#{month}-#{day}").to_s
rescue ArgumentError
raise DateTaxCodeError
end

def birthplace(src = utils.cities, stop: false)
Expand Down
6 changes: 6 additions & 0 deletions test/itax_code/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class ParserTest < Minitest::Test
assert_raises(klass::InvalidTaxCodeError) { klass.new(wrong_length_tax_code) }
end

test "raises DateTaxCodeError with an invalid date, e.g. 1978-4-31" do
wrong_month_tax_code = "SPENTB78D71D612X"

assert_raises(klass::DateTaxCodeError) { klass.new(wrong_month_tax_code).decode }
end

test "raises InvalidControlInternalNumberError when the cin differs from the computed one" do
tax_code_with_wrong_cin = "CCCFBA85D03L219Z"

Expand Down

0 comments on commit 4f5cc91

Please sign in to comment.