Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unificado algoritmo de arredondamento das classes NFe.Classes.Valor e DFe.Classes.Valor #1579

Merged
merged 4 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
5 changes: 5 additions & 0 deletions DFe.Testes/DFe.Testes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DadosDeTestes\DadosDeTestes.csproj" />
<ProjectReference Include="..\NFe.Classes\NFe.Classes.csproj" />
<ProjectReference Include="..\NFe.Utils\NFe.Utils.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Valores\DadosDeTeste\" />
</ItemGroup>

</Project>
23 changes: 0 additions & 23 deletions DFe.Testes/Valores/DadosDeTeste/ValorDadosDeTeste.cs

This file was deleted.

26 changes: 18 additions & 8 deletions DFe.Testes/Valores/ValorTesteUnitario.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
using DFe.Testes.Valores.DadosDeTeste;
using DadosDeTestes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NFe.Classes;
using Valor = DFe.Classes.Valor;

namespace DFe.Testes.Valores
{
[TestClass]
public class ValorTesteUnitario
{
[TestMethod(displayName: "Dado um valor e uma quantidade de casas decimais, quando o arredondamento for feito utilizando os métodos da DFe e NFe, então o valor arredondado deve ser igual em ambos os casos")]
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresDecimaisParaArredondar), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
public void DadoUmValorEUmaQuantidadeDeCasasDecimaisQuandoOArredondamentoForFeitoUtilizandoOsMetodosDaDfeENfeEntaoOValorArredondadoDeveSerIgualEmAmbosOsCasos(decimal valor, int casasDecimais)
// Normativa ABNT NBR5891
[TestMethod(displayName: "Dado valor para arredondamento, quando o arredondar, então deve retornar o valor arredondado seguindo as normas da ABNT.")]
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresParaArredondamentoSegundoNormativaAbnt), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
public void DadoValorParaArredondamentoQuandoArredondarEntaoDeveRetornarOValorArredondadoSeguindoAsNormasDaAbnt(decimal valor, decimal valorEsperado)
{
var valorArredondadoDfe = Classes.Valor.Arredondar(valor, casasDecimais);
var valorArredondadoNfe = Valor.Arredondar(valor, casasDecimais);
var casasDecimaisParaArredondamento = 2;
var valorArredondadoDfe = Valor.Arredondar(valor,casasDecimaisParaArredondamento);

Assert.AreEqual(valorArredondadoDfe, valorArredondadoNfe);
Assert.AreEqual(valorArredondadoDfe, valorEsperado);
}

[TestMethod(displayName: "Dado valor para arredondamento nulo, quando o arredondar, então deve retornar nulo.")]
public void DadoValorParaArredondamentoQuandoArredondarEntaoOValorArredondadoDeveSeguirAsNormasDaAbnt()
{
var casasDecimaisParaArredondamento = 2;
var valorArredondadoDfe = Valor.Arredondar(null,casasDecimaisParaArredondamento);

Assert.IsNull(valorArredondadoDfe);
}
}
}
9 changes: 9 additions & 0 deletions DadosDeTestes/DadosDeTestes.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
54 changes: 54 additions & 0 deletions DadosDeTestes/ValorDadosDeTeste.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace DadosDeTestes
{
public class ValorDadosDeTeste
{
public static IEnumerable<object[]> ObterValoresDecimaisParaArredondar()
{
return new List<object[]>
{
new object[] { 20.35m * 15.90m, 2 },
new object[] { 0.35m * 15.90m, 2 },
new object[] { 3.665m, 2 },
new object[] { 4.775m, 2 },

new object[] { 20.35m * 15.90m, 3 },
new object[] { 0.35m * 15.90m, 3 },
new object[] { 4.77575m, 4 },
new object[] { 5.445545m, 5 }
};
}

public static IEnumerable<object[]> ObterValoresParaArredondamentoSegundoNormativaAbnt()
{
return new List<object[]>
{
new object[] { 0.342m, 0.34m },
new object[] { 0.346m, 0.35m },
new object[] { 0.3452m, 0.35m },
new object[] { 0.3450m, 0.34m },
new object[] { 0.332m, 0.33m },
new object[] { 0.336m, 0.34m },
new object[] { 0.3352m, 0.34m },
new object[] { 0.3350m, 0.34m },
new object[] { 0.3050m, 0.30m },
new object[] { 0.3150m, 0.32m }
};
}

public static IEnumerable<object[]> ObterValoresParaArredondamentoParaBaixo()
{
return new List<object[]>
{
new object[] { 123.4567m, 2, 123.45m },
new object[] { 123.4599m, 2, 123.45m },
new object[] { 123.451m, 2, 123.45m },
new object[] { 123.4001m, 3, 123.400m },
new object[] { 123.0009m, 3, 123.000m },
new object[] { 123.0m, 2, 123.00m },
new object[] { 0.9999m, 3, 0.999m },
new object[] { 0m, 2, 0.00m },
new object[] { 987.654m, 0, 987m }
};
}
}
}
24 changes: 24 additions & 0 deletions NFe.Classes.Testes/NFe.Classes.Testes.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8"/>
<PackageReference Include="MSTest.TestFramework" Version="2.2.8"/>
<PackageReference Include="coverlet.collector" Version="3.1.2"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DadosDeTestes\DadosDeTestes.csproj" />
<ProjectReference Include="..\DFe.Classes\DFe.Classes.csproj" />
<ProjectReference Include="..\NFe.Classes\NFe.Classes.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions NFe.Classes.Testes/ValorTesteUnitario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using DadosDeTestes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DFeArredondar = DFe.Classes.Valor;

namespace NFe.Classes.Testes;

[TestClass]
public class ValorTesteUnitario
{
[TestMethod(displayName: "Dado um valor e uma quantidade de casas decimais, quando o arredondamento for feito utilizando os métodos da DFe e NFe, então o valor arredondado deve ser igual em ambos os casos.")]
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresDecimaisParaArredondar), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
public void DadoUmValorEUmaQuantidadeDeCasasDecimaisQuandoOArredondamentoForFeitoUtilizandoOsMetodosDaDfeENfeEntaoOValorArredondadoDeveSerIgualEmAmbosOsCasos(decimal valor, int casasDecimais)
{
var valorArredondadoDfe = DFeArredondar.Arredondar(valor,casasDecimais);
var valorArredondadoNfe = valor.Arredondar(casasDecimais);

Assert.AreEqual(valorArredondadoDfe, valorArredondadoNfe);
}

// Normativa ABNT NBR5891
[TestMethod(displayName: "Dado valor para arredondamento, quando o arredondar, então deve retornar o valor arredondado seguindo as normas da ABNT.")]
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresParaArredondamentoSegundoNormativaAbnt), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
public void DadoValorParaArredondamentoQuandoArredondarEntaoDeveRetornarOValorArredondadoSeguindoAsNormasDaAbnt(decimal valor, decimal valorEsperado)
{
var casasDecimaisParaArredondamento = 2;
var valorArredondadoNfe = valor.Arredondar(casasDecimaisParaArredondamento);

Assert.AreEqual(valorArredondadoNfe, valorEsperado);
}

[TestMethod(displayName: "Dado valor para arredondamento, quando o arredondar para baixo, então deve retornar o valor arredondado para baixo.")]
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresParaArredondamentoParaBaixo), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
public void DadoValorParaArredondamentoQuandoArredondarParaBaixoEntaoDeveRetornarOValorArredondadoParaBaixo(decimal valor, int casasDecimais, decimal valorEsperado)
{
var valorArredondadoParaBaixoNfe = valor.ArredondarParaBaixo(casasDecimais);

Assert.AreEqual(valorArredondadoParaBaixoNfe, valorEsperado);
}
}
6 changes: 2 additions & 4 deletions NFe.Classes/Valor.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System;
using System.Globalization;

namespace NFe.Classes
{
public static class Valor
{
public static decimal Arredondar(this decimal valor, int casasDecimais)
{
var valorArredondado = decimal.Round(valor, casasDecimais, MidpointRounding.ToEven);
var valorArredondadoFormatado = valorArredondado.ToString("F" + casasDecimais, CultureInfo.CurrentCulture);
return decimal.Parse(valorArredondadoFormatado);
var valorArredondado = DFe.Classes.Valor.Arredondar(valor, casasDecimais);
return valorArredondado;
}

public static decimal? Arredondar(this decimal? valor, int casasDecimais)
Expand Down
22 changes: 22 additions & 0 deletions Zeus NFe.sln
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFe.Danfe.PdfClown", "NFe.D
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFe.Danfe.QuestPdf", "NFe.Danfe.QuestPdf\NFe.Danfe.QuestPdf.csproj", "{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFe.Classes.Testes", "NFe.Classes.Testes\NFe.Classes.Testes.csproj", "{7A6BAFEA-D159-41CD-A706-EF05E17914E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DadosDeTestes", "DadosDeTestes\DadosDeTestes.csproj", "{9E2125F8-2367-444A-B5D5-B7C61FCB456B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -483,6 +487,22 @@ Global
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}.Release|Any CPU.Build.0 = Release|Any CPU
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}.Release|x86.ActiveCfg = Release|Any CPU
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}.Release|x86.Build.0 = Release|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|x86.ActiveCfg = Debug|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|x86.Build.0 = Debug|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|Any CPU.Build.0 = Release|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|x86.ActiveCfg = Release|Any CPU
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|x86.Build.0 = Release|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|x86.ActiveCfg = Debug|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|x86.Build.0 = Debug|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|Any CPU.Build.0 = Release|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|x86.ActiveCfg = Release|Any CPU
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -540,6 +560,8 @@ Global
{18429710-19E2-480F-8B3A-39FB6FC7AF19} = {3AC1A3D4-91D0-471A-AF9C-5EF8442F3F27}
{2A235861-B8FC-463A-A774-0C179529881A} = {003B008B-F388-4FB5-A081-12C0FBA84B57}
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B} = {003B008B-F388-4FB5-A081-12C0FBA84B57}
{7A6BAFEA-D159-41CD-A706-EF05E17914E3} = {164EF13E-7F27-4BC8-92A4-A82B1696B279}
{9E2125F8-2367-444A-B5D5-B7C61FCB456B} = {164EF13E-7F27-4BC8-92A4-A82B1696B279}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C8C4D9F7-EF86-49A2-83AF-13FA5D4E8DBB}
Expand Down
Loading