Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions PowerKit.Tests/VersionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using FluentAssertions;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

public class VersionExtensionsTests
{
[Fact]
public void ToSemanticString_Test()
{
Comment thread
Tyrrrz marked this conversation as resolved.
new Version(1, 2).ToSemanticString().Should().Be("1.2");
new Version(1, 2, 3).ToSemanticString().Should().Be("1.2.3");
new Version(1, 2, 3, 4).ToSemanticString().Should().Be("1.2.3.4");
new Version(1, 2, 3, 0).ToSemanticString().Should().Be("1.2.3");
}
Comment thread
Tyrrrz marked this conversation as resolved.
}
22 changes: 22 additions & 0 deletions PowerKit/Extensions/VersionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;

namespace PowerKit.Extensions;

#if !POWERKIT_INCLUDE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
internal static class VersionExtensions
{
extension(Version version)
{
/// <summary>
/// Formats the version as a semantic version string, omitting the revision component if it is not set or is zero.
/// </summary>
public string ToSemanticString() =>
version.Build < 0 ? version.ToString(2)
: version.Revision <= 0 ? version.ToString(3)
: version.ToString();
}
}