-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced UserOutputService for output with user settings
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Gibbon.Git.Server.Tests/Services/UserOutputServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Gibbon.Git.Server.Services; | ||
|
||
namespace Gibbon.Git.Server.Tests.Services; | ||
|
||
[TestClass] | ||
public class UserOutputServiceTests | ||
{ | ||
private IUserOutputService _userOutputService = null!; | ||
|
||
[TestInitialize] | ||
public void Init() | ||
{ | ||
_userOutputService = new UserOutputService(); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow(0, "0 B")] | ||
[DataRow(2, "2 B")] | ||
[DataRow(512, "512 B")] | ||
[DataRow(100, "100 B")] | ||
[DataRow(2000, "1,95 kB")] | ||
[DataRow(1024, "1 kB")] | ||
[DataRow(1500, "1,46 kB")] | ||
[DataRow(2000000, "1,91 MB")] | ||
[DataRow(1048576, "1 MB")] | ||
[DataRow(2000000000, "1,86 GB")] | ||
[DataRow(1073741824, "1 GB")] | ||
[DataRow(2000000000000, "1,82 TB")] | ||
[DataRow(2000000000000000, "1819 TB")] | ||
[DataRow(-2000000, "1,91 MB")] | ||
[DataRow(999, "999 B")] | ||
[DataRow(10240, "10 kB")] | ||
[DataRow(1023999, "1000 kB")] | ||
[DataRow(1099511627776, "1 TB")] | ||
public void TestGetFileSizeString(long size, string expected) | ||
{ | ||
Assert.AreEqual(expected, _userOutputService.GetFileSizeString(size)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace Gibbon.Git.Server.Services; | ||
|
||
public interface IUserOutputService | ||
{ | ||
/// <summary> | ||
/// <para>Returns the human-readable file size for an arbitrary, 64-bit file size</para> | ||
/// <para>The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"</para> | ||
/// </summary> | ||
string GetFileSizeString(long size); | ||
} | ||
|
||
public class UserOutputService : IUserOutputService | ||
{ | ||
/// <summary> | ||
/// <para>Returns the human-readable file size for an arbitrary, 64-bit file size</para> | ||
/// <para>The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"</para> | ||
/// </summary> | ||
public string GetFileSizeString(long size) | ||
{ | ||
if (size == 0) | ||
{ | ||
return "0 B"; | ||
} | ||
|
||
string[] suffixes = ["B", "kB", "MB", "GB", "TB"]; | ||
var readable = Math.Abs((double)size); | ||
var suffixIndex = 0; | ||
|
||
while (readable >= 1024 && suffixIndex < suffixes.Length - 1) | ||
{ | ||
readable /= 1024; | ||
suffixIndex++; | ||
} | ||
|
||
return readable switch | ||
{ | ||
>= 100 => $"{readable:0} {suffixes[suffixIndex]}", | ||
> 10 => $"{readable:0.#} {suffixes[suffixIndex]}", | ||
_ => $"{readable:0.##} {suffixes[suffixIndex]}" | ||
}; | ||
} | ||
} |