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