Skip to content

Commit

Permalink
新增 Version 对象输出指定格式的扩展方法
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuLing-zhang committed Sep 6, 2024
1 parent 81f6b25 commit 69679eb
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using JiuLing.CommonLibs.Enums;
using JiuLing.CommonLibs.ExtensionMethods;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace JiuLing.CommonLibs.UnitTests.ExtensionMethods
{
[TestClass()]
public class VersionExtensionTests
{
[TestMethod()]
[DataRow("1.2", VersionFormatEnum.Major, "1")]
[DataRow("1.2", VersionFormatEnum.MajorMinor, "1.2")]
[DataRow("1.2", VersionFormatEnum.MajorMinorBuild, "1.2.0")]
[DataRow("1.2", VersionFormatEnum.MajorMinorBuildRevision, "1.2.0.0")]
[DataRow("1.2.3", VersionFormatEnum.Major, "1")]
[DataRow("1.2.3", VersionFormatEnum.MajorMinor, "1.2")]
[DataRow("1.2.3", VersionFormatEnum.MajorMinorBuild, "1.2.3")]
[DataRow("1.2.3", VersionFormatEnum.MajorMinorBuildRevision, "1.2.3.0")]
[DataRow("1.2.3.4", VersionFormatEnum.Major, "1")]
[DataRow("1.2.3.4", VersionFormatEnum.MajorMinor, "1.2")]
[DataRow("1.2.3.4", VersionFormatEnum.MajorMinorBuild, "1.2.3")]
[DataRow("1.2.3.4", VersionFormatEnum.MajorMinorBuildRevision, "1.2.3.4")]
public void ToFormatVersionTest1(string input, VersionFormatEnum format, string result)
{
var version = new Version(input);
var value = version.ToFormatString(format);
Assert.AreEqual(value, result);
}

[TestMethod()]
public void ToFormatVersionTest1_ArgumentOutOfRangeException()
{
var exception = Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
{
(new Version("1.2")).ToFormatString((VersionFormatEnum)0);
});

Assert.AreEqual("format", exception.ParamName);
Assert.IsTrue(exception.Message.IndexOf("无效的版本格式") >= 0);
}
}
}
29 changes: 29 additions & 0 deletions src/JiuLing.CommonLibs/Enums/VersionFormatEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace JiuLing.CommonLibs.Enums
{
/// <summary>
/// 版本号展示格式
/// </summary>
public enum VersionFormatEnum
{
/// <summary>
/// 主版本(1)
/// 例如:1
/// </summary>
Major = 1,
/// <summary>
/// 主版本.次版本
/// 例如:1.2
/// </summary>
MajorMinor = 2,
/// <summary>
/// 主版本.次版本.构建版本
/// 例如:1.2.3
/// </summary>
MajorMinorBuild = 3,
/// <summary>
/// 主版本.次版本.构建版本.修订版本
/// 例如:1.2.3.4
/// </summary>
MajorMinorBuildRevision = 4
}
}
53 changes: 53 additions & 0 deletions src/JiuLing.CommonLibs/ExtensionMethods/VersionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using JiuLing.CommonLibs.Enums;

namespace JiuLing.CommonLibs.ExtensionMethods
{
/// <summary>
/// 版本号的扩展方法
/// </summary>
public static class VersionExtension
{
/// <summary>
/// 格式化版本号
/// </summary>
/// <param name="version">版本号</param>
/// <param name="format">格式化</param>
/// <returns>格式化后的版本号字符串</returns>
public static string ToFormatString(this Version version, VersionFormatEnum format)
{
switch (format)
{
case VersionFormatEnum.Major:
return version.Major.ToString();
case VersionFormatEnum.MajorMinor:
return $"{version.Major}.{version.Minor}";
case VersionFormatEnum.MajorMinorBuild:
return $"{version.Major}.{version.Minor}.{GetVersionBuild(version)}";
case VersionFormatEnum.MajorMinorBuildRevision:
return $"{version.Major}.{version.Minor}.{GetVersionBuild(version)}.{GetVersionRevision(version)}";
default:
throw new ArgumentOutOfRangeException(nameof(format), "无效的版本格式");
}
}

private static int GetVersionBuild(Version version)
{
var build = version.Build;
if (build == -1)
{
build = 0;
}
return build;
}
private static int GetVersionRevision(Version version)
{
var revision = version.Revision;
if (revision == -1)
{
revision = 0;
}
return revision;
}
}
}

0 comments on commit 69679eb

Please sign in to comment.