Created
March 15, 2019 16:15
-
-
Save gugat/c90d16eca11b120f45cd52983e65687f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require 'minitest/spec' | |
#:nodoc: | |
class TestCompensation < Minitest::Test | |
def setup | |
@compensation = Compensation.new | |
end | |
describe 'when worked less than 12 months' do | |
it 'will not receive compensation' do | |
salary = 1000 | |
(1..11).each do |months| | |
assert_equal Compensation.calculate(salary, months), 0 | |
end | |
end | |
end | |
describe 'when worked more than 12 months' do | |
it 'will receive compensation' do | |
# exactly 1 year | |
assert_equal Compensation.calculate(1000, 12.0), 1000 | |
# > 1 year | |
assert_equal Compensation.calculate(1000, 13.0), 1000 | |
# > 1 year and < 1.5 years | |
assert_equal Compensation.calculate(1000, 17.0), 1000 | |
# exactly 1.5 years | |
assert_equal Compensation.calculate(1000, 18.0), 2000 | |
# > 1.5 years < 2 years | |
assert_equal Compensation.calculate(1000, 23.0), 2000 | |
# exactly 2 years | |
assert_equal Compensation.calculate(1000, 24.0), 2000 | |
end | |
end | |
end | |
#:nodoc: | |
class Compensation | |
# | |
# Calculate employee compensation based on salary and worked months. | |
# Receive compensation only if worked more than 12 months. After that, she | |
# receives a salary per year or any fraction of 6 months or more. | |
# | |
# @param [float] salary Employee salary | |
# @param [float] months Worked months | |
# | |
# @return [float] Compensation amount | |
# | |
def self.calculate(salary, months) | |
return 0 if months < 12.0 | |
(months / 12.0).round * salary | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment