diff --git a/DFe.Classes/Valor.cs b/DFe.Classes/Extensoes/Valor.cs similarity index 100% rename from DFe.Classes/Valor.cs rename to DFe.Classes/Extensoes/Valor.cs diff --git a/DFe.Testes/DFe.Testes.csproj b/DFe.Testes/DFe.Testes.csproj index c4d819d8c..1efc8325b 100644 --- a/DFe.Testes/DFe.Testes.csproj +++ b/DFe.Testes/DFe.Testes.csproj @@ -17,8 +17,13 @@ + + + + + diff --git a/DFe.Testes/Valores/DadosDeTeste/ValorDadosDeTeste.cs b/DFe.Testes/Valores/DadosDeTeste/ValorDadosDeTeste.cs deleted file mode 100644 index a1adc3345..000000000 --- a/DFe.Testes/Valores/DadosDeTeste/ValorDadosDeTeste.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; - -namespace DFe.Testes.Valores.DadosDeTeste -{ - public class ValorDadosDeTeste - { - public static IEnumerable ObterValoresDecimaisParaArredondar() - { - return new List - { - 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 }, - }; - } - } -} diff --git a/DFe.Testes/Valores/ValorTesteUnitario.cs b/DFe.Testes/Valores/ValorTesteUnitario.cs index 77995952f..7faa587eb 100644 --- a/DFe.Testes/Valores/ValorTesteUnitario.cs +++ b/DFe.Testes/Valores/ValorTesteUnitario.cs @@ -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); } } } diff --git a/DadosDeTestes/DadosDeTestes.csproj b/DadosDeTestes/DadosDeTestes.csproj new file mode 100644 index 000000000..eb2460e91 --- /dev/null +++ b/DadosDeTestes/DadosDeTestes.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/DadosDeTestes/ValorDadosDeTeste.cs b/DadosDeTestes/ValorDadosDeTeste.cs new file mode 100644 index 000000000..48ae11b40 --- /dev/null +++ b/DadosDeTestes/ValorDadosDeTeste.cs @@ -0,0 +1,54 @@ +namespace DadosDeTestes +{ + public class ValorDadosDeTeste + { + public static IEnumerable ObterValoresDecimaisParaArredondar() + { + return new List + { + 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 ObterValoresParaArredondamentoSegundoNormativaAbnt() + { + return new List + { + 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 ObterValoresParaArredondamentoParaBaixo() + { + return new List + { + 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 } + }; + } + } +} diff --git a/NFe.Classes.Testes/NFe.Classes.Testes.csproj b/NFe.Classes.Testes/NFe.Classes.Testes.csproj new file mode 100644 index 000000000..28bff0da0 --- /dev/null +++ b/NFe.Classes.Testes/NFe.Classes.Testes.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + + + + + diff --git a/NFe.Classes.Testes/ValorTesteUnitario.cs b/NFe.Classes.Testes/ValorTesteUnitario.cs new file mode 100644 index 000000000..167ce6ee8 --- /dev/null +++ b/NFe.Classes.Testes/ValorTesteUnitario.cs @@ -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); + } +} \ No newline at end of file diff --git a/NFe.Classes/Valor.cs b/NFe.Classes/Valor.cs index 5a86a7381..0bbc8ead0 100644 --- a/NFe.Classes/Valor.cs +++ b/NFe.Classes/Valor.cs @@ -1,5 +1,4 @@ using System; -using System.Globalization; namespace NFe.Classes { @@ -7,9 +6,8 @@ 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) diff --git a/Zeus NFe.sln b/Zeus NFe.sln index 187e4eef2..6fe0e1af3 100644 --- a/Zeus NFe.sln +++ b/Zeus NFe.sln @@ -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 @@ -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 @@ -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}