Skip to content

Commit

Permalink
fix bug to negatives params
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquesantosfinlytech committed Jul 15, 2024
1 parent c0b03ad commit 3468880
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## [1.2.2] - 2024-07-15
### Fixed
- Fixed the bug that caused an error when printing negative numbers in 'real_formatado'.
- Fixed the bug that caused an error when printing decimal numbers in 'numero'.
2 changes: 2 additions & 0 deletions extensobr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/rickmax/extensobr"
spec.license = "MIT"

# spec.required_ruby_version = ">= 2.3.0"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.

Expand Down
22 changes: 16 additions & 6 deletions lib/extensobr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ def self.numero(valor, genero = GENERO_MASC)
return 'Zero'
end

unless int?(valor) && !valor.nil?
"[Exceção em Extenso.numero] Parâmetro 'valor' não é numérico (recebido: '#{valor}')"
unless valor.is_a?(Integer) && !valor.nil?
raise "[Exceção em Extenso.numero] Parâmetro 'valor' deve ser um número inteiro, (recebido: #{valor})"
end

if valor <= 0
Expand Down Expand Up @@ -460,20 +460,30 @@ def self.ordinal(valor, genero = GENERO_MASC)

# Gera o valor em formato de Real
#
# Exemplo:
# Exemplo +:
# Extenso.real_formatado(10) - R$ 10,00
# Extenso.real_formatado(1.55) - R$ 1,55
#
# Exemplo -:
# Extenso.real_formatado(10.555) - R$ 10,55
# Extenso.real_formatado(1000.555) - R$ 1.000,55
# @params[Object]
def self.real_formatado(valor)
positive = true
if valor < 0
valor = valor.abs
positive = false
end
float_valor = format('%#0.02f', valor)
float_valor = float_valor.chars.reverse.insert(6, '.').reverse.join if float_valor.chars.count >= 7

float_valor = float_valor.chars.reverse.insert(10, '.').reverse.join if float_valor.chars.count >= 11

float_valor = float_valor.chars.reverse
float_valor[2] = ','

"R$ #{float_valor.reverse.join}"
if positive
"R$ #{float_valor.reverse.join}"
else
"R$ -#{float_valor.reverse.join}"
end
end
end
2 changes: 1 addition & 1 deletion lib/extensobr/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Extensobr
VERSION = "1.2.1"
VERSION = "1.2.2"
end
20 changes: 19 additions & 1 deletion spec/extensobr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,27 @@
expect(Extenso.real_formatado(1_300_570.82)).to eq('R$ 1.300.570,82')
expect(Extenso.real_formatado(100_300_570.82)).to eq('R$ 100.300.570,82')
end

it 'retorno de valores negativos para moéda brasileira R$' do
expect(Extenso.real_formatado(-0)).to eq('R$ 0,00')
expect(Extenso.real_formatado(-0.82)).to eq('R$ -0,82')
expect(Extenso.real_formatado(-1)).to eq('R$ -1,00')
expect(Extenso.real_formatado(-10)).to eq('R$ -10,00')
expect(Extenso.real_formatado(-10.5)).to eq('R$ -10,50')
expect(Extenso.real_formatado(-100.5)).to eq('R$ -100,50')
expect(Extenso.real_formatado(-3570.82)).to eq('R$ -3.570,82')
expect(Extenso.real_formatado(-30_570.82)).to eq('R$ -30.570,82')
expect(Extenso.real_formatado(-300_570.82)).to eq('R$ -300.570,82')
expect(Extenso.real_formatado(-1_300_570.82)).to eq('R$ -1.300.570,82')
expect(Extenso.real_formatado(-100_300_570.82)).to eq('R$ -100.300.570,82')
end
end

context 'valor de payload nil ou vazio' do
context 'valor de payload inválido' do
it 'retorno de erro para valores decimais em Extenso.numero' do
expect { Extenso.numero(0.1) }.to raise_error "[Exceção em Extenso.numero] Parâmetro 'valor' deve ser um número inteiro, (recebido: 0.1)"
end

it 'retorno quando envia nil com raise desligado' do
expect(Extenso.numero(nil)).to eq('Zero')
expect(Extenso.moeda(nil)).to eq('Zero Centavos')
Expand Down

0 comments on commit 3468880

Please sign in to comment.