Skip to content

Commit c219310

Browse files
authored
Merge pull request #1579 from ZeusAutomacao/Branch_22341
Unificado algoritmo de arredondamento das classes `NFe.Classes.Valor` e `DFe.Classes.Valor`
2 parents 2326b8a + aab5460 commit c219310

File tree

10 files changed

+173
-35
lines changed

10 files changed

+173
-35
lines changed
File renamed without changes.

DFe.Testes/DFe.Testes.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20+
<ProjectReference Include="..\DadosDeTestes\DadosDeTestes.csproj" />
2021
<ProjectReference Include="..\NFe.Classes\NFe.Classes.csproj" />
2122
<ProjectReference Include="..\NFe.Utils\NFe.Utils.csproj" />
2223
</ItemGroup>
2324

25+
<ItemGroup>
26+
<Folder Include="Valores\DadosDeTeste\" />
27+
</ItemGroup>
28+
2429
</Project>

DFe.Testes/Valores/DadosDeTeste/ValorDadosDeTeste.cs

-23
This file was deleted.
+18-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
using DFe.Testes.Valores.DadosDeTeste;
1+
using DadosDeTestes;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3-
using NFe.Classes;
3+
using Valor = DFe.Classes.Valor;
44

55
namespace DFe.Testes.Valores
66
{
77
[TestClass]
88
public class ValorTesteUnitario
99
{
10-
[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")]
11-
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresDecimaisParaArredondar), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
12-
public void DadoUmValorEUmaQuantidadeDeCasasDecimaisQuandoOArredondamentoForFeitoUtilizandoOsMetodosDaDfeENfeEntaoOValorArredondadoDeveSerIgualEmAmbosOsCasos(decimal valor, int casasDecimais)
10+
// Normativa ABNT NBR5891
11+
[TestMethod(displayName: "Dado valor para arredondamento, quando o arredondar, então deve retornar o valor arredondado seguindo as normas da ABNT.")]
12+
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresParaArredondamentoSegundoNormativaAbnt), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
13+
public void DadoValorParaArredondamentoQuandoArredondarEntaoDeveRetornarOValorArredondadoSeguindoAsNormasDaAbnt(decimal valor, decimal valorEsperado)
1314
{
14-
var valorArredondadoDfe = Classes.Valor.Arredondar(valor, casasDecimais);
15-
var valorArredondadoNfe = Valor.Arredondar(valor, casasDecimais);
15+
var casasDecimaisParaArredondamento = 2;
16+
var valorArredondadoDfe = Valor.Arredondar(valor,casasDecimaisParaArredondamento);
1617

17-
Assert.AreEqual(valorArredondadoDfe, valorArredondadoNfe);
18+
Assert.AreEqual(valorArredondadoDfe, valorEsperado);
19+
}
20+
21+
[TestMethod(displayName: "Dado valor para arredondamento nulo, quando o arredondar, então deve retornar nulo.")]
22+
public void DadoValorParaArredondamentoQuandoArredondarEntaoOValorArredondadoDeveSeguirAsNormasDaAbnt()
23+
{
24+
var casasDecimaisParaArredondamento = 2;
25+
var valorArredondadoDfe = Valor.Arredondar(null,casasDecimaisParaArredondamento);
26+
27+
Assert.IsNull(valorArredondadoDfe);
1828
}
1929
}
2030
}

DadosDeTestes/DadosDeTestes.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

DadosDeTestes/ValorDadosDeTeste.cs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace DadosDeTestes
2+
{
3+
public class ValorDadosDeTeste
4+
{
5+
public static IEnumerable<object[]> ObterValoresDecimaisParaArredondar()
6+
{
7+
return new List<object[]>
8+
{
9+
new object[] { 20.35m * 15.90m, 2 },
10+
new object[] { 0.35m * 15.90m, 2 },
11+
new object[] { 3.665m, 2 },
12+
new object[] { 4.775m, 2 },
13+
14+
new object[] { 20.35m * 15.90m, 3 },
15+
new object[] { 0.35m * 15.90m, 3 },
16+
new object[] { 4.77575m, 4 },
17+
new object[] { 5.445545m, 5 }
18+
};
19+
}
20+
21+
public static IEnumerable<object[]> ObterValoresParaArredondamentoSegundoNormativaAbnt()
22+
{
23+
return new List<object[]>
24+
{
25+
new object[] { 0.342m, 0.34m },
26+
new object[] { 0.346m, 0.35m },
27+
new object[] { 0.3452m, 0.35m },
28+
new object[] { 0.3450m, 0.34m },
29+
new object[] { 0.332m, 0.33m },
30+
new object[] { 0.336m, 0.34m },
31+
new object[] { 0.3352m, 0.34m },
32+
new object[] { 0.3350m, 0.34m },
33+
new object[] { 0.3050m, 0.30m },
34+
new object[] { 0.3150m, 0.32m }
35+
};
36+
}
37+
38+
public static IEnumerable<object[]> ObterValoresParaArredondamentoParaBaixo()
39+
{
40+
return new List<object[]>
41+
{
42+
new object[] { 123.4567m, 2, 123.45m },
43+
new object[] { 123.4599m, 2, 123.45m },
44+
new object[] { 123.451m, 2, 123.45m },
45+
new object[] { 123.4001m, 3, 123.400m },
46+
new object[] { 123.0009m, 3, 123.000m },
47+
new object[] { 123.0m, 2, 123.00m },
48+
new object[] { 0.9999m, 3, 0.999m },
49+
new object[] { 0m, 2, 0.00m },
50+
new object[] { 987.654m, 0, 987m }
51+
};
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
13+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8"/>
14+
<PackageReference Include="MSTest.TestFramework" Version="2.2.8"/>
15+
<PackageReference Include="coverlet.collector" Version="3.1.2"/>
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\DadosDeTestes\DadosDeTestes.csproj" />
20+
<ProjectReference Include="..\DFe.Classes\DFe.Classes.csproj" />
21+
<ProjectReference Include="..\NFe.Classes\NFe.Classes.csproj" />
22+
</ItemGroup>
23+
24+
</Project>
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using DadosDeTestes;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using DFeArredondar = DFe.Classes.Valor;
4+
5+
namespace NFe.Classes.Testes;
6+
7+
[TestClass]
8+
public class ValorTesteUnitario
9+
{
10+
[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.")]
11+
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresDecimaisParaArredondar), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
12+
public void DadoUmValorEUmaQuantidadeDeCasasDecimaisQuandoOArredondamentoForFeitoUtilizandoOsMetodosDaDfeENfeEntaoOValorArredondadoDeveSerIgualEmAmbosOsCasos(decimal valor, int casasDecimais)
13+
{
14+
var valorArredondadoDfe = DFeArredondar.Arredondar(valor,casasDecimais);
15+
var valorArredondadoNfe = valor.Arredondar(casasDecimais);
16+
17+
Assert.AreEqual(valorArredondadoDfe, valorArredondadoNfe);
18+
}
19+
20+
// Normativa ABNT NBR5891
21+
[TestMethod(displayName: "Dado valor para arredondamento, quando o arredondar, então deve retornar o valor arredondado seguindo as normas da ABNT.")]
22+
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresParaArredondamentoSegundoNormativaAbnt), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
23+
public void DadoValorParaArredondamentoQuandoArredondarEntaoDeveRetornarOValorArredondadoSeguindoAsNormasDaAbnt(decimal valor, decimal valorEsperado)
24+
{
25+
var casasDecimaisParaArredondamento = 2;
26+
var valorArredondadoNfe = valor.Arredondar(casasDecimaisParaArredondamento);
27+
28+
Assert.AreEqual(valorArredondadoNfe, valorEsperado);
29+
}
30+
31+
[TestMethod(displayName: "Dado valor para arredondamento, quando o arredondar para baixo, então deve retornar o valor arredondado para baixo.")]
32+
[DynamicData(nameof(ValorDadosDeTeste.ObterValoresParaArredondamentoParaBaixo), typeof(ValorDadosDeTeste), DynamicDataSourceType.Method)]
33+
public void DadoValorParaArredondamentoQuandoArredondarParaBaixoEntaoDeveRetornarOValorArredondadoParaBaixo(decimal valor, int casasDecimais, decimal valorEsperado)
34+
{
35+
var valorArredondadoParaBaixoNfe = valor.ArredondarParaBaixo(casasDecimais);
36+
37+
Assert.AreEqual(valorArredondadoParaBaixoNfe, valorEsperado);
38+
}
39+
}

NFe.Classes/Valor.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
using System;
2-
using System.Globalization;
32

43
namespace NFe.Classes
54
{
65
public static class Valor
76
{
87
public static decimal Arredondar(this decimal valor, int casasDecimais)
98
{
10-
var valorArredondado = decimal.Round(valor, casasDecimais, MidpointRounding.ToEven);
11-
var valorArredondadoFormatado = valorArredondado.ToString("F" + casasDecimais, CultureInfo.CurrentCulture);
12-
return decimal.Parse(valorArredondadoFormatado);
9+
var valorArredondado = DFe.Classes.Valor.Arredondar(valor, casasDecimais);
10+
return valorArredondado;
1311
}
1412

1513
public static decimal? Arredondar(this decimal? valor, int casasDecimais)

Zeus NFe.sln

+22
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFe.Danfe.PdfClown", "NFe.D
123123
EndProject
124124
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFe.Danfe.QuestPdf", "NFe.Danfe.QuestPdf\NFe.Danfe.QuestPdf.csproj", "{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}"
125125
EndProject
126+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFe.Classes.Testes", "NFe.Classes.Testes\NFe.Classes.Testes.csproj", "{7A6BAFEA-D159-41CD-A706-EF05E17914E3}"
127+
EndProject
128+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DadosDeTestes", "DadosDeTestes\DadosDeTestes.csproj", "{9E2125F8-2367-444A-B5D5-B7C61FCB456B}"
129+
EndProject
126130
Global
127131
GlobalSection(SolutionConfigurationPlatforms) = preSolution
128132
Debug|Any CPU = Debug|Any CPU
@@ -483,6 +487,22 @@ Global
483487
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}.Release|Any CPU.Build.0 = Release|Any CPU
484488
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}.Release|x86.ActiveCfg = Release|Any CPU
485489
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B}.Release|x86.Build.0 = Release|Any CPU
490+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
491+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
492+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|x86.ActiveCfg = Debug|Any CPU
493+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Debug|x86.Build.0 = Debug|Any CPU
494+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
495+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|Any CPU.Build.0 = Release|Any CPU
496+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|x86.ActiveCfg = Release|Any CPU
497+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3}.Release|x86.Build.0 = Release|Any CPU
498+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
499+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|Any CPU.Build.0 = Debug|Any CPU
500+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|x86.ActiveCfg = Debug|Any CPU
501+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Debug|x86.Build.0 = Debug|Any CPU
502+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|Any CPU.ActiveCfg = Release|Any CPU
503+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|Any CPU.Build.0 = Release|Any CPU
504+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|x86.ActiveCfg = Release|Any CPU
505+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B}.Release|x86.Build.0 = Release|Any CPU
486506
EndGlobalSection
487507
GlobalSection(SolutionProperties) = preSolution
488508
HideSolutionNode = FALSE
@@ -540,6 +560,8 @@ Global
540560
{18429710-19E2-480F-8B3A-39FB6FC7AF19} = {3AC1A3D4-91D0-471A-AF9C-5EF8442F3F27}
541561
{2A235861-B8FC-463A-A774-0C179529881A} = {003B008B-F388-4FB5-A081-12C0FBA84B57}
542562
{C3EB9A85-92D6-4201-96AB-959CBAE4BF1B} = {003B008B-F388-4FB5-A081-12C0FBA84B57}
563+
{7A6BAFEA-D159-41CD-A706-EF05E17914E3} = {164EF13E-7F27-4BC8-92A4-A82B1696B279}
564+
{9E2125F8-2367-444A-B5D5-B7C61FCB456B} = {164EF13E-7F27-4BC8-92A4-A82B1696B279}
543565
EndGlobalSection
544566
GlobalSection(ExtensibilityGlobals) = postSolution
545567
SolutionGuid = {C8C4D9F7-EF86-49A2-83AF-13FA5D4E8DBB}

0 commit comments

Comments
 (0)