Skip to content

Commit

Permalink
Add support for culture-specific ByteSize unit expression
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-z committed May 1, 2021
1 parent 1285835 commit 17e9884
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 62 deletions.
12 changes: 10 additions & 2 deletions src/Humanizer.Tests.Shared/Bytes/ToFullWordsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
namespace Humanizer.Tests.Bytes
{
public class ToFullWordsTests
{
{
[Fact]
public void ReturnsSingularBit()
{
Assert.Equal("1 bit", ByteSize.FromBits(1).ToFullWords());
}

[Fact]
public void ReturnsPluralBits()
{
Expand Down Expand Up @@ -98,5 +98,13 @@ public void ReturnsPluralTerabytes()
{
Assert.Equal("10 terabytes", ByteSize.FromTerabytes(10).ToFullWords());
}

[Theory]
[InlineData(229376, "B", "229376 bytes")]
[InlineData(229376, "# KB", "224 kilobytes")]
public void ToFullWordsFormatted(double input, string format, string expectedValue)
{
Assert.Equal(expectedValue, ByteSize.FromBytes(input).ToFullWords(format));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Xunit;

namespace Humanizer.Tests.Localisation.fr.Bytes
{
[UseCulture("fr-FR")]
public class ByteSizeExtensionsTests
{
[Theory]
[InlineData(2, null, "2 To")]
[InlineData(2, "GB", "2048 Go")]
[InlineData(2.123, "#.#", "2,1 To")]
public void HumanizesTerabytes(double input, string format, string expectedValue)
{
Assert.Equal(expectedValue, input.Terabytes().Humanize(format));
}

[Theory]
[InlineData(0, null, "0 b")]
[InlineData(0, "GB", "0 Go")]
[InlineData(2, null, "2 Go")]
[InlineData(2, "MB", "2048 Mo")]
[InlineData(2.123, "#.##", "2,12 Go")]
public void HumanizesGigabytes(double input, string format, string expectedValue)
{
Assert.Equal(expectedValue, input.Gigabytes().Humanize(format));
}

[Theory]
[InlineData(0, null, "0 b")]
[InlineData(0, "MB", "0 Mo")]
[InlineData(2, null, "2 Mo")]
[InlineData(2, "KB", "2048 Ko")]
[InlineData(2.123, "#", "2 Mo")]
public void HumanizesMegabytes(double input, string format, string expectedValue)
{
Assert.Equal(expectedValue, input.Megabytes().Humanize(format));
}

[Theory]
[InlineData(0, null, "0 b")]
[InlineData(0, "KB", "0 Ko")]
[InlineData(2, null, "2 Ko")]
[InlineData(2, "B", "2048 o")]
[InlineData(2.123, "#.####", "2,123 Ko")]
public void HumanizesKilobytes(double input, string format, string expectedValue)
{
Assert.Equal(expectedValue, input.Kilobytes().Humanize(format));
}

[Theory]
[InlineData(0, null, "0 b")]
[InlineData(0, "#.##", "0 b")]
[InlineData(0, "#.## B", "0 o")]
[InlineData(0, "B", "0 o")]
[InlineData(2, null, "2 o")]
[InlineData(2000, "KB", "1,95 Ko")]
[InlineData(2123, "#.##", "2,07 Ko")]
[InlineData(10000000, "KB", "9765,63 Ko")]
[InlineData(10000000, "#,##0 KB", "9 766 Ko")]
[InlineData(10000000, "#,##0.# KB", "9 765,6 Ko")]
public void HumanizesBytes(double input, string format, string expectedValue)
{
Assert.Equal(expectedValue, input.Bytes().Humanize(format));
}

[Theory]
[InlineData(0, null, "0 b")]
[InlineData(0, "b", "0 b")]
[InlineData(2, null, "2 b")]
[InlineData(12, "B", "1,5 o")]
[InlineData(10000, "#.# KB", "1,2 Ko")]
public void HumanizesBits(long input, string format, string expectedValue)
{
Assert.Equal(expectedValue, input.Bits().Humanize(format));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Humanizer.Bytes;
using Xunit;

namespace Humanizer.Tests.Localisation.fr.Bytes
{
[UseCulture("fr-FR")]
public class ToFullWordsTests
{
[Fact]
public void ReturnsSingularBit()
{
Assert.Equal("1 bit", ByteSize.FromBits(1).ToFullWords());
}

[Fact]
public void ReturnsPluralBits()
{
Assert.Equal("2 bits", ByteSize.FromBits(2).ToFullWords());
}

[Fact]
public void ReturnsSingularByte()
{
Assert.Equal("1 octet", ByteSize.FromBytes(1).ToFullWords());
}

[Fact]
public void ReturnsPluralBytes()
{
Assert.Equal("10 octets", ByteSize.FromBytes(10).ToFullWords());
}

[Fact]
public void ReturnsSingularKiloByte()
{
Assert.Equal("1 kilooctet", ByteSize.FromKilobytes(1).ToFullWords());
}

[Fact]
public void ReturnsPluralKilobytes()
{
Assert.Equal("10 kilooctets", ByteSize.FromKilobytes(10).ToFullWords());
}

[Fact]
public void ReturnsSingularMegabyte()
{
Assert.Equal("1 mégaoctet", ByteSize.FromMegabytes(1).ToFullWords());
}

[Fact]
public void ReturnsPluralMegabytes()
{
Assert.Equal("10 mégaoctets", ByteSize.FromMegabytes(10).ToFullWords());
}

[Fact]
public void ReturnsSingularGigabyte()
{
Assert.Equal("1 gigaoctet", ByteSize.FromGigabytes(1).ToFullWords());
}

[Fact]
public void ReturnsPluralGigabytes()
{
Assert.Equal("10 gigaoctets", ByteSize.FromGigabytes(10).ToFullWords());
}

[Fact]
public void ReturnsSingularTerabyte()
{
Assert.Equal("1 téraoctet", ByteSize.FromTerabytes(1).ToFullWords());
}

[Fact]
public void ReturnsPluralTerabytes()
{
Assert.Equal("10 téraoctets", ByteSize.FromTerabytes(10).ToFullWords());
}

[Theory]
[InlineData(229376, "B", "229376 octets")]
[InlineData(229376, "# KB", "224 kilooctets")]
public void ToFullWordsFormatted(double input, string format, string expectedValue)
{
Assert.Equal(expectedValue, ByteSize.FromBytes(input).ToFullWords(format));
}
}
}
87 changes: 87 additions & 0 deletions src/Humanizer.Tests.Shared/Localisation/fr/Bytes/ToStringTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Humanizer.Bytes;
using Xunit;

namespace Humanizer.Tests.Localisation.fr.Bytes
{
[UseCulture("fr-FR")]
public class ToStringTests
{
[Fact]
public void ReturnsLargestMetricSuffix()
{
Assert.Equal("10,5 Ko", ByteSize.FromKilobytes(10.5).ToString());
}

[Fact]
public void ReturnsDefaultNumberFormat()
{
Assert.Equal("10,5 Ko", ByteSize.FromKilobytes(10.5).ToString("KB"));
}

[Fact]
public void ReturnsProvidedNumberFormat()
{
Assert.Equal("10,1234 Ko", ByteSize.FromKilobytes(10.1234).ToString("#.#### KB"));
}

[Fact]
public void ReturnsBits()
{
Assert.Equal("10 b", ByteSize.FromBits(10).ToString("##.#### b"));
}

[Fact]
public void ReturnsBytes()
{
Assert.Equal("10 o", ByteSize.FromBytes(10).ToString("##.#### B"));
}

[Fact]
public void ReturnsKilobytes()
{
Assert.Equal("10 Ko", ByteSize.FromKilobytes(10).ToString("##.#### KB"));
}

[Fact]
public void ReturnsMegabytes()
{
Assert.Equal("10 Mo", ByteSize.FromMegabytes(10).ToString("##.#### MB"));
}

[Fact]
public void ReturnsGigabytes()
{
Assert.Equal("10 Go", ByteSize.FromGigabytes(10).ToString("##.#### GB"));
}

[Fact]
public void ReturnsTerabytes()
{
Assert.Equal("10 To", ByteSize.FromTerabytes(10).ToString("##.#### TB"));
}

[Fact]
public void ReturnsSelectedFormat()
{
Assert.Equal("10,0 To", ByteSize.FromTerabytes(10).ToString("0.0 TB"));
}

[Fact]
public void ReturnsLargestMetricPrefixLargerThanZero()
{
Assert.Equal("512 Ko", ByteSize.FromMegabytes(.5).ToString("#.#"));
}

[Fact]
public void ReturnsLargestMetricPrefixLargerThanZeroForNegativeValues()
{
Assert.Equal("-512 Ko", ByteSize.FromMegabytes(-.5).ToString("#.#"));
}

[Fact]
public void ReturnsBytesViaGeneralFormat()
{
Assert.Equal("10 o", $"{ByteSize.FromBytes(10)}");
}
}
}
Loading

0 comments on commit 17e9884

Please sign in to comment.