Skip to content

Commit

Permalink
chore: add quality badges + enable branch coverage (#41)
Browse files Browse the repository at this point in the history
* chore: add code-climate badges

* chore: add gem version badge

* chore: publish test coverage to code climate

* chore: enable SimpleCov branch coverage

* chore: add test coverage for missing branch conditions
  • Loading branch information
matteoredz authored Oct 23, 2023
1 parent 99b00f1 commit cd3a950
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
release-type: ruby
token: ${{secrets.GITHUB_TOKEN}}
version-file: "lib/itax_code/version.rb"
- uses: actions/checkout@v3
- uses: actions/checkout@v4
if: ${{steps.release.outputs.release_created}}
- uses: ruby/setup-ruby@v1
with:
Expand Down
20 changes: 18 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [2.5, 2.6, 2.7, '3.0', 3.1, head]
ruby: [2.5, 2.6, 2.7, '3.0', 3.1, 3.2, head]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: ${{ matrix.ruby }}
- run: bundle install
- run: bundle exec rake
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: 3.2
- run: bundle install
- name: Test & publish code coverage
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
coverageCommand: bundle exec rake
debug: true
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ gem "rubocop-minitest"
gem "rubocop-performance"
gem "rubocop-rake"
gem "simplecov"
gem "timecop"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ItaxCode

[![Gem Version](https://badge.fury.io/rb/itax_code.svg)](https://badge.fury.io/rb/itax_code) [![Maintainability](https://api.codeclimate.com/v1/badges/6ce5347018f4b448098f/maintainability)](https://codeclimate.com/github/matteoredz/itax-code/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/6ce5347018f4b448098f/test_coverage)](https://codeclimate.com/github/matteoredz/itax-code/test_coverage)

`ItaxCode` is a Ruby Gem to encode and decode Italian tax code (Codice Fiscale).

It handles common operations on Italian tax code like, for instance, encoding, decoding and validation.
Expand Down
5 changes: 3 additions & 2 deletions lib/itax_code/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ def gender
# value could be wrong. E.g. a person born on 1920 would have birthdate_year = 20 meaning
# that both 1920 and 2020 could be valid born years.
def year
val = (Date.today.year.to_s[0..1] + utils.omocodia_decode(raw[:birthdate_year])).to_i
val > Date.today.year ? val - 100 : val
current_year = Date.today.year
val = (current_year.to_s[0..1] + utils.omocodia_decode(raw[:birthdate_year])).to_i
val > current_year ? val - 100 : val
end

def month
Expand Down
28 changes: 20 additions & 8 deletions test/itax_code/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,34 @@ class ParserTest < ActiveSupport::TestCase
assert_equal decoded_f, klass.new("RSSMRA80A41F205B").decode
end

test "#decode returns invalid code on invalid birthplace" do
test "#decode when foreign" do
assert_equal decoded_foreign, klass.new("BRRDRN70M41Z602D").decode
end

test "raises NoTaxCodeError when no tax code is given" do
assert_raises klass::NoTaxCodeError do
klass.new("")
# NOTE: This tests the branch where the computed date is before the current year.
test "decodes the birthdate without manipulating the original value" do
Timecop.freeze(Date.parse("1990-01-01")) do
assert_equal "1985-04-03", klass.new("CCCFBA85D03L219P").decode[:birthdate]
end
end

test "raises InvalidTaxCodeError when the tax code is invalid" do
# 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]
end

test "raises NoTaxCodeError with an empty tax code" do
assert_raises(klass::NoTaxCodeError) { klass.new("") }
end

test "raises NoTaxCodeError with a nil tax code" do
assert_raises(klass::NoTaxCodeError) { klass.new(nil) }
end

test "raises InvalidTaxCodeError when the tax code has wrong length" do
wrong_length_tax_code = "CCCFBA85D03L219PXXX"

assert_raises klass::InvalidTaxCodeError do
klass.new(wrong_length_tax_code)
end
assert_raises(klass::InvalidTaxCodeError) { klass.new(wrong_length_tax_code) }
end

test "raises InvalidControlInternalNumberError when the cin differs from the computed one" do
Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "simplecov"
SimpleCov.start do
add_filter "test"
enable_coverage :branch
minimum_coverage line: 100, branch: 100
end

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
Expand All @@ -11,3 +13,4 @@
require "byebug"
require "minitest/autorun"
require "mocha/minitest"
require "timecop"

0 comments on commit cd3a950

Please sign in to comment.