From e03d2c603793ed0ae8df55a403fd97f5f7805b00 Mon Sep 17 00:00:00 2001 From: Matteo Date: Fri, 29 May 2026 22:50:50 +0200 Subject: [PATCH] fix(parser): raise InvalidTaxCodeError when birthplace not found in cities or countries Previously the birthplace method returned nil implicitly when the stop: true recursive call found nothing, masking invalid tax codes silently. Co-Authored-By: Claude Sonnet 4.6 --- AGENTS.md | 2 +- lib/itax_code/parser.rb | 8 +++++--- test/itax_code/parser_test.rb | 5 ++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0b846ab..ee5a94c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -78,7 +78,7 @@ ItaxCode.decode(tax_code) #=> Hash: # code: String, # the input code, upcased # gender: "M" | "F", # birthdate: String, # "YYYY-MM-DD" -# birthplace: { # nil if not found in either CSV +# birthplace: { # raises InvalidTaxCodeError if not found in either CSV # code: String, # e.g. "F205" # province: String, # e.g. "MI" # name: String, # e.g. "MILANO" diff --git a/lib/itax_code/parser.rb b/lib/itax_code/parser.rb index 97c1ee0..e110025 100644 --- a/lib/itax_code/parser.rb +++ b/lib/itax_code/parser.rb @@ -92,10 +92,12 @@ def birthplace(src = utils.cities, stop: false) place = src.find { |item| item["code"] == birthplace_code && in_dates?(item) } if place.nil? - birthplace(utils.countries, stop: true) unless stop - else - place.to_h.transform_keys(&:to_sym) + return birthplace(utils.countries, stop: true) unless stop + + raise InvalidTaxCodeError end + + place.to_h.transform_keys(&:to_sym) end def birthplace_code diff --git a/test/itax_code/parser_test.rb b/test/itax_code/parser_test.rb index ba12176..b0d0d17 100644 --- a/test/itax_code/parser_test.rb +++ b/test/itax_code/parser_test.rb @@ -29,9 +29,8 @@ class ParserTest < Minitest::Test end end - # FIXME: This behaviour needs to be fixed, maybe by raising an InvalidTaxCodeError. - test "returns nil birthplace when the lookup on both cities and countries fails" do - assert_nil klass.new("BRRDRN70M41ZXXXE").decode[:birthplace] + test "raises InvalidTaxCodeError when birthplace code is not found in cities or countries" do + assert_raises(klass::InvalidTaxCodeError) { klass.new("BRRDRN70M41ZXXXE").decode } end test "raises NoTaxCodeError with an empty tax code" do