Skip to content

Commit ba81b53

Browse files
committed
enhance: tweak ToFileSize for readability
1 parent d3d1377 commit ba81b53

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

.github/workflows/tests-check.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Format Check
2+
on:
3+
push:
4+
branches: [develop]
5+
pull_request:
6+
branches: [develop]
7+
workflow_dispatch:
8+
workflow_call:
9+
10+
jobs:
11+
tests-check:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Set up .NET
19+
uses: actions/setup-dotnet@v4
20+
with:
21+
dotnet-version: 9.0.x
22+
23+
- name: Run unit tests
24+
run: dotnet test

SourceGit.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{C54D
8686
build\scripts\package.windows.sh = build\scripts\package.windows.sh
8787
EndProjectSection
8888
EndProject
89+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}"
90+
EndProject
91+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceGit.Tests", "tests\SourceGit.Tests.csproj", "{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}"
92+
EndProject
8993
Global
9094
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9195
Debug|Any CPU = Debug|Any CPU
@@ -96,6 +100,10 @@ Global
96100
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
97101
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
98102
{2091C34D-4A17-4375-BEF3-4D60BE8113E4}.Release|Any CPU.Build.0 = Release|Any CPU
103+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
104+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
105+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
106+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC}.Release|Any CPU.Build.0 = Release|Any CPU
99107
EndGlobalSection
100108
GlobalSection(SolutionProperties) = preSolution
101109
HideSolutionNode = FALSE
@@ -116,6 +124,7 @@ Global
116124
{7802CD7A-591B-4EDD-96F8-9BF3F61692E4} = {9BA0B044-0CC9-46F8-B551-204F149BF45D}
117125
{5D125DD9-B48A-491F-B2FB-D7830D74C4DC} = {FD384607-ED99-47B7-AF31-FB245841BC92}
118126
{C54D4001-9940-477C-A0B6-E795ED0A3209} = {773082AC-D9C8-4186-8521-4B6A7BEE6158}
127+
{6BE6CF5B-2F49-4AA3-99D6-54701196B2DC} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
119128
EndGlobalSection
120129
GlobalSection(ExtensibilityGlobals) = postSolution
121130
SolutionGuid = {7FF1B9C6-B5BF-4A50-949F-4B407A0E31C9}

src/Converters/LongConverters.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ public static class LongConverters
66
{
77
public static readonly FuncValueConverter<long, string> ToFileSize = new(bytes =>
88
{
9+
var full = $"{bytes:N0} B";
910
if (bytes < KB)
10-
return $"{bytes} B";
11+
return full;
1112

1213
if (bytes < MB)
13-
return $"{(bytes / KB):F3} KB ({bytes} B)";
14+
return $"{(bytes / KB):G3} KB ({full})";
1415

1516
if (bytes < GB)
16-
return $"{(bytes / MB):F3} MB ({bytes} B)";
17+
return $"{(bytes / MB):G3} MB ({full})";
1718

18-
return $"{(bytes / GB):F3} GB ({bytes} B)";
19+
if (bytes < 1000 * GB)
20+
return $"{(bytes / GB):G3} GB ({full})";
21+
22+
return $"{(bytes / GB):N0} GB ({full})";
1923
});
2024

2125
private const double KB = 1024;
22-
private const double MB = 1024 * 1024;
23-
private const double GB = 1024 * 1024 * 1024;
26+
private const double MB = 1024 * KB;
27+
private const double GB = 1024 * MB;
2428
}
2529
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Globalization;
2+
using SourceGit.Converters;
3+
4+
namespace SourceGit.Tests.Converters
5+
{
6+
public class LongConvertersTests
7+
{
8+
[Theory]
9+
[InlineData(4, "4 B")]
10+
[InlineData(44, "44 B")]
11+
[InlineData(444, "444 B")]
12+
[InlineData(4444, "4.34 KB (4,444 B)")]
13+
[InlineData(44444, "43.4 KB (44,444 B)")]
14+
[InlineData(444444, "434 KB (444,444 B)")]
15+
[InlineData(4444444, "4.24 MB (4,444,444 B)")]
16+
[InlineData(44444444, "42.4 MB (44,444,444 B)")]
17+
[InlineData(444444444, "424 MB (444,444,444 B)")]
18+
[InlineData(4444444444, "4.14 GB (4,444,444,444 B)")]
19+
[InlineData(44444444444, "41.4 GB (44,444,444,444 B)")]
20+
[InlineData(444444444444, "414 GB (444,444,444,444 B)")]
21+
[InlineData(4444444444444, "4,139 GB (4,444,444,444,444 B)")]
22+
[InlineData(long.MinValue, "-9,223,372,036,854,775,808 B")]
23+
[InlineData(0, "0 B")]
24+
[InlineData(long.MaxValue, "8,589,934,592 GB (9,223,372,036,854,775,807 B)")]
25+
public void ToFileSize_ShouldReturnCorrectFormat(long bytes, string expected)
26+
{
27+
var actual = LongConverters.ToFileSize.Convert(bytes, typeof(string), null, CultureInfo.CurrentCulture) as string;
28+
Assert.Equal(expected, actual);
29+
}
30+
}
31+
}

tests/SourceGit.Tests.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net9.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
8+
<PackageReference Include="xunit" Version="2.9.3" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<Using Include="Xunit" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\src\SourceGit.csproj" />
18+
</ItemGroup>
19+
</Project>

0 commit comments

Comments
 (0)