-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81f6b25
commit 69679eb
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/JiuLing.CommonLibs.UnitTests/ExtensionMethods/VersionExtensionTests.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,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); | ||
} | ||
} | ||
} |
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,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
53
src/JiuLing.CommonLibs/ExtensionMethods/VersionExtension.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,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; | ||
} | ||
} | ||
} |