Skip to content

Commit

Permalink
Version 8.0.0:
Browse files Browse the repository at this point in the history
Enumberation - Get enum by description fix.
NUnit upgrade version and tests fixes.
.NET 8 Support.
  • Loading branch information
Bruno de Souza Melo committed Jan 28, 2024
1 parent 019eb9c commit 67a91be
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 107 deletions.
2 changes: 1 addition & 1 deletion docs/Publishing.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nuget.exe push -Source "nuvtools" -ApiKey az NuvTools.Common.7.0.0.nupkg
nuget.exe push -Source "nuvtools" -ApiKey az NuvTools.Common.8.0.0.nupkg
7 changes: 4 additions & 3 deletions src/NuvTools.Common/Enums/Enumeration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;

namespace NuvTools.Common.Enums;
/// <summary>
Expand Down Expand Up @@ -119,9 +120,9 @@ private static TEnum GetEnumByProperties<TEnum>(string shortName = null, string

var enumList = ToList<TEnum>();

var enumerator = enumList.FirstOrDefault(e => e.ShortName == shortName
|| e.Name == name
|| e.Description == description);
var enumerator = enumList.FirstOrDefault(e => (shortName != null && e.ShortName == shortName)
|| (name != null && e.Name == name)
|| (description != null && e.Description == description));

if (enumerator == null)
return (TEnum)Enum.Parse(typeof(TEnum), enumList[0].Id.ToString(), true);
Expand Down
56 changes: 28 additions & 28 deletions src/NuvTools.Common/Numbers/Portuguese/NumberToWords.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class NumberToWords
};

private static readonly string[] dozen = {
string.Empty, "dez", "vinte", "trinta", "quarenta", "cinqüenta", "sessenta"
string.Empty, "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta"
, "setenta", "oitenta", "noventa"
};

Expand All @@ -31,10 +31,10 @@ public static class NumberToWords
private const decimal maxValue = 999999999999999.99M;
private const string currency = " real ";
private const string currencyPlural = " reais ";
private const string centesimo = " centavo ";
private const string centesimoPlural = " centavos ";
private const string hundredth = " centavo ";
private const string hundredthPlural = " centavos ";

private static string Converter(long value)
private static string Convert(long value)
{
long result;

Expand All @@ -56,7 +56,7 @@ private static string Converter(long value)
value >= 71 && value <= 79 ||
value >= 81 && value <= 89 ||
value >= 91 && value <= 99)
return dozen[result] + " e " + Converter(remainder);
return dozen[result] + " e " + Convert(remainder);

result = NumbersHelper.DivRem(value, 100, out remainder);

Expand All @@ -66,7 +66,7 @@ private static string Converter(long value)
return hundreds[result] + ", ";

if (value >= 101 && value <= 199)
return " cento e " + Converter(remainder);
return " cento e " + Convert(remainder);

if (value >= 201 && value <= 299 ||
value >= 301 && value <= 399 ||
Expand All @@ -77,64 +77,64 @@ private static string Converter(long value)
value >= 801 && value <= 899 ||
value >= 901 && value <= 999)
return hundreds[result] + " e " +
Converter(remainder);
Convert(remainder);

result = NumbersHelper.DivRem(value, 1000, out remainder);

if (value >= 1000 && value <= 999999)
return Converter(result) + " mil " +
Converter(remainder);
return Convert(result) + " mil " +
Convert(remainder);

result = NumbersHelper.DivRem(value, 1000000, out remainder);

if (value >= 1000000 && value <= 1999999)
return Converter(result) + " milhão " +
Converter(remainder);
return Convert(result) + " milhão " +
Convert(remainder);

if (value >= 2000000 && value <= 999999999)
return Converter(result) + " milhões " +
Converter(remainder);
return Convert(result) + " milhões " +
Convert(remainder);

result = NumbersHelper.DivRem(value, 1000000000, out remainder);

if (value >= 1000000000 && value <= 1999999999)
return Converter(result) + " bilhão " +
Converter(remainder);
return Convert(result) + " bilhão " +
Convert(remainder);

if (value >= 2000000000 && value <= 999999999999)
return Converter(result) + " bilhões " +
Converter(remainder);
return Convert(result) + " bilhões " +
Convert(remainder);

result = NumbersHelper.DivRem(value, 1000000000000, out remainder);

if (value >= 1000000000000 && value <= 1999999999999)
return Converter(result) + " trilhão " +
Converter(remainder);
return Convert(result) + " trilhão " +
Convert(remainder);

if (value >= 2000000000000 && value <= 999999999999999)
return Converter(result) + " trilhões " +
Converter(remainder);
return Convert(result) + " trilhões " +
Convert(remainder);

return string.Empty;
}

public static string ToWords(decimal value)
{
string texto = string.Empty;
string result = string.Empty;

if (value >= minValue && value <= maxValue)
{
long inteiro = Convert.ToInt64(Math.Truncate(value));
long centavos = Convert.ToInt64(Math.Truncate((value - Math.Truncate(value)) * 100));
long intPart = System.Convert.ToInt64(Math.Truncate(value));
long decimalPart = System.Convert.ToInt64(Math.Truncate((value - Math.Truncate(value)) * 100));

texto += Converter(inteiro) + (inteiro <= 1 ? currency : currencyPlural);
result += Convert(intPart) + (intPart <= 1 ? currency : currencyPlural);

if (centavos > 0)
texto += " e " + Converter(centavos) + (centavos == 1 ? centesimo : centesimoPlural);
if (decimalPart > 0)
result += " e " + Convert(decimalPart) + (decimalPart == 1 ? hundredth : hundredthPlural);
}
else
throw new Exception(Messages.ResourceManager.GetString(nameof(Messages.ValueOutsideRange), CultureInfo.GetCultureInfo("pt-BR")));

return NumbersHelper.CleanupSpaces(texto);
return NumbersHelper.CleanupSpaces(result);
}
}
8 changes: 4 additions & 4 deletions src/NuvTools.Common/NuvTools.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6;net7</TargetFrameworks>
<TargetFrameworks>net6;net7;net8</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Nuv Tools</Authors>
<Copyright>Copyright © 2023 Nuv Tools</Copyright>
<Copyright>Copyright © 2024 Nuv Tools</Copyright>
<PackageProjectUrl>https://nuv.tools</PackageProjectUrl>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>NuvTools.Common.snk</AssemblyOriginatorKeyFile>
<Description>Common library for Web, Desktop and Mobile (MAUI) applications.</Description>
<Version>7.1.5</Version>
<Version>8.0.0</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageIcon>icon.png</PackageIcon>
Expand Down Expand Up @@ -42,7 +42,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 13 additions & 13 deletions tests/NuvTools.Common.Test/Enums/EnumerationExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,82 +13,82 @@ public class EnumerationExtensionsTests
public void GetGroupNameLongTest()
{
var value = FormatTypeLong.PowerPoint.GetGroupName();
Assert.AreEqual("Microsoft Office", value);
Assert.That("Microsoft Office" == value);

var value2 = FormatTypeLong.Word.GetGroupName();
Assert.AreNotEqual("Microsoft Office", value2);
Assert.That("Microsoft Office" != value2);
}

[Test()]
public void GetShortNameLongTest()
{
var value = FormatTypeLong.PowerPoint.GetShortName();
Assert.AreEqual("ms", value);
Assert.That("ms" == value);
}

[Test()]
public void GetNameLongTest()
{
var value = FormatTypeLong.Word.GetName();
Assert.AreEqual("word", value);
Assert.That("word" == value);
}

[Test()]
public void GetDescriptionLongTest()
{
var value = FormatTypeLong.Word.GetDescription();
Assert.AreEqual("word", value);
Assert.That("word" == value);
}

[Test()]
public void GetDescriptionShortTest()
{
var value = FormatTypeShort.Word.GetDescription();
Assert.AreEqual("word", value);
Assert.That("word" == value);
}

[Test()]
public void GetDescriptionTest()
{
var value = FormatType.Word.GetDescription();
Assert.AreEqual("word", value);
Assert.That("word" == value);
}

[Test()]
public void GetValueStringTest()
{
var value = FormatType.Word.GetValueAsString();
Assert.AreEqual("1", value);
Assert.That("1" == value);
}

[Test()]
public void GetValueStringShortTest()
{
var value = FormatTypeShort.Word.GetValueAsString();
Assert.AreEqual("1", value);
Assert.That("1" == value);
}

[Test()]
public void GetStringShortTest()
{
var value = FormatTypeShort.Word.GetValueAsString();
Assert.AreEqual("1", value);
Assert.That("1" == value);
}

[Test()]
public void GetStringTest()
{
var list = new List<Enum> { FormatTypeShort.Word, FormatTypeLong.Excel };
var value = list.ToStringSeparatorDelimited(',');
Assert.AreEqual(value, "1,2");
Assert.That(value == "1,2");
}

[Test()]
public void GetListEnumBySeparatorDelimitedTest()
{
var value = "1,2,20,40,3".ToListEnumFromSeparatorDelimited<FormatTypeShort>(',').ToList();
var value = "1,2,3,4".ToListEnumFromSeparatorDelimited<FormatTypeShort>(',').ToList();

Assert.AreEqual(value[0], FormatTypeShort.Word);
Assert.That(value[0] == FormatTypeShort.Word);

//Assert.IsTrue(value.Any(e => e == (short)20))
}
Expand Down
53 changes: 26 additions & 27 deletions tests/NuvTools.Common.Test/Enums/EnumerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using NUnit.Framework;
using NuvTools.Common.Enums;
using System;

public enum FormatType
{
Expand Down Expand Up @@ -73,37 +74,35 @@ public void GetEnumTest1()
{
var value = Enumeration.GetEnum<FormatType>(3);

Assert.AreEqual(FormatType.PowerPoint, value);
Assert.That(FormatType.PowerPoint == value);
}

[Test()]
public void GetEnumTest2()
public void GetEnumNotExisting()
{
var value = Enumeration.GetEnum<FormatType>(20);

Assert.AreEqual(FormatType.PowerPoint, value);
Assert.Throws<IndexOutOfRangeException>(() => Enumeration.GetEnum<FormatType>(20));
}

[Test()]
public void GetEnumByDescriptionTest()
{
var value = Enumeration.GetEnumByDescription<FormatType>("excel");
Assert.AreEqual(FormatType.Excel, value);
Assert.That(FormatType.Excel == value);
}

[Test()]
public void ToListTest()
{
var list = Enumeration.ToList<FormatType>(true);

Assert.IsNotNull(list);
Assert.AreEqual("excel", list[0].Description);
Assert.That(list is not null);
Assert.That("excel" == list[0].Description);

//Using display
Assert.AreEqual("More than one", list[2].Description);
Assert.AreEqual("Should take this", list[2].GroupName);
Assert.That("More than one" == list[2].Description);
Assert.That("Should take this" == list[2].GroupName);

Assert.AreEqual("word", list[3].Name);
Assert.That("word" == list[3].Name);
}

[Test()]
Expand All @@ -124,45 +123,45 @@ public void ToListByteTest()
{
var list = Enumeration.ToList<FormatTypeByte, byte>(false);

Assert.AreEqual(list[0].Id.GetType(), typeof(byte));
Assert.AreEqual("word", list[0].Description); //sorted
Assert.That(list[0].Id.GetType() == typeof(byte));
Assert.That("word" == list[0].Description); //sorted

//Using display
Assert.AreEqual("Microsoft tools", list[2].Description);
Assert.AreEqual("Microsoft Office", list[2].GroupName);
Assert.That("Microsoft tools" == list[2].Description);
Assert.That("Microsoft Office" == list[2].GroupName);

Assert.AreEqual("duplo", list[3].Name);
Assert.That("duplo" == list[3].Name);
}

[Test()]
public void ToListShortTest()
{
var list = Enumeration.ToList<FormatTypeShort, short>(false);

Assert.IsNotNull(list);
Assert.AreEqual(list[0].Id.GetType(), typeof(short));
Assert.AreEqual("word", list[0].Description); //sorted
Assert.That(list != null);
Assert.That(list[0].Id.GetType() == typeof(short));
Assert.That("word" == list[0].Description); //sorted

//Using display
Assert.AreEqual("Microsoft tools", list[2].Description);
Assert.AreEqual("Microsoft Office", list[2].GroupName);
Assert.That("Microsoft tools" == list[2].Description);
Assert.That("Microsoft Office" == list[2].GroupName);

Assert.AreEqual("duplo", list[3].Name);
Assert.That("duplo" == list[3].Name);
}

[Test()]
public void ToListLongTest()
{
var list = Enumeration.ToList<FormatTypeLong, long>(false);

Assert.AreEqual(list[0].Id.GetType(), typeof(long));
Assert.AreEqual("word", list[0].Description); //sorted
Assert.That(list[0].Id.GetType() == typeof(long));
Assert.That("word" == list[0].Description); //sorted

//Using display
Assert.AreEqual("Microsoft tools", list[2].Description);
Assert.AreEqual("Microsoft Office", list[2].GroupName);
Assert.That("Microsoft tools" == list[2].Description);
Assert.That("Microsoft Office" == list[2].GroupName);

Assert.AreEqual("duplo", list[3].Name);
Assert.That("duplo" == list[3].Name);
}

[Test()]
Expand Down
Loading

0 comments on commit 67a91be

Please sign in to comment.