Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parsing impossible dates #56

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 5 additions & 1 deletion lib/itax_code/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def day
end

def birthdate
@birthdate ||= Date.parse("#{year}-#{month}-#{day}").to_s
begin
@birthdate ||= Date.parse("#{year}-#{month}-#{day}").to_s
rescue Date::Error
raise DateTaxCodeError
end
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
Loading