Skip to content

Commit 5f8fd43

Browse files
committed
feat(utility): 添加运行时平台检测工具类及测试
添加 RuntimePlatformHelper 工具类用于检测当前运行平台(Linux/macOS/Windows/FreeBSD) 同时添加对应的单元测试验证平台检测功能
1 parent 055e4a5 commit 5f8fd43

2 files changed

Lines changed: 316 additions & 0 deletions

File tree

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// ==========================================================================================
2+
// GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利
3+
// GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights
4+
// 均受中华人民共和国及相关国际法律法规保护。
5+
// are protected by the laws of the People's Republic of China and relevant international regulations.
6+
//
7+
// 使用本项目须严格遵守相应法律法规及开源许可证之规定。
8+
// Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses.
9+
//
10+
// 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发,
11+
// This project is dual-licensed under the MIT License and Apache License 2.0,
12+
// 完整许可证文本请参见源代码根目录下的 LICENSE 文件。
13+
// please refer to the LICENSE file in the root directory of the source code for the full license text.
14+
//
15+
// 禁止利用本项目实施任何危害国家安全、破坏社会秩序、
16+
// It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order,
17+
// 侵犯他人合法权益等法律法规所禁止的行为!
18+
// or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations!
19+
// 因基于本项目二次开发所产生的一切法律纠纷与责任,
20+
// Any legal disputes and liabilities arising from secondary development based on this project
21+
// 本项目组织与贡献者概不承担。
22+
// shall be borne solely by the developer; the project organization and contributors assume no responsibility.
23+
//
24+
// GitHub 仓库:https://github.com/GameFrameX
25+
// GitHub Repository: https://github.com/GameFrameX
26+
// Gitee 仓库:https://gitee.com/GameFrameX
27+
// Gitee Repository: https://gitee.com/GameFrameX
28+
// 官方文档:https://gameframex.doc.alianblank.com/
29+
// Official Documentation: https://gameframex.doc.alianblank.com/
30+
// ==========================================================================================
31+
32+
using System.Runtime.InteropServices;
33+
using GameFrameX.Foundation.Utility;
34+
using Xunit;
35+
36+
namespace GameFrameX.Foundation.Tests.Utility;
37+
38+
/// <summary>
39+
/// PlatformRuntimeHelper 类的单元测试
40+
/// </summary>
41+
public class PlatformRuntimeHelperTests
42+
{
43+
/// <summary>
44+
/// 测试 IsLinux 属性
45+
/// </summary>
46+
[Fact]
47+
public void IsLinux_ShouldReturnCorrectValue()
48+
{
49+
// Arrange & Act
50+
var isLinux = RuntimePlatformHelper.IsLinux;
51+
var expectedIsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
52+
53+
// Assert
54+
Assert.Equal(expectedIsLinux, isLinux);
55+
}
56+
57+
/// <summary>
58+
/// 测试 IsOsx 属性
59+
/// </summary>
60+
[Fact]
61+
public void IsOsx_ShouldReturnCorrectValue()
62+
{
63+
// Arrange & Act
64+
var isOsx = RuntimePlatformHelper.IsOsx;
65+
var expectedIsOsx = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
66+
67+
// Assert
68+
Assert.Equal(expectedIsOsx, isOsx);
69+
}
70+
71+
/// <summary>
72+
/// 测试 IsWindows 属性
73+
/// </summary>
74+
[Fact]
75+
public void IsWindows_ShouldReturnCorrectValue()
76+
{
77+
// Arrange & Act
78+
var isWindows = RuntimePlatformHelper.IsWindows;
79+
var expectedIsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
80+
81+
// Assert
82+
Assert.Equal(expectedIsWindows, isWindows);
83+
}
84+
85+
/// <summary>
86+
/// 测试 IsFreeBsd 属性
87+
/// </summary>
88+
[Fact]
89+
public void IsFreeBsd_ShouldReturnCorrectValue()
90+
{
91+
// Arrange & Act
92+
var isFreeBsd = RuntimePlatformHelper.IsFreeBsd;
93+
var expectedIsFreeBsd = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
94+
95+
// Assert
96+
Assert.Equal(expectedIsFreeBsd, isFreeBsd);
97+
}
98+
99+
/// <summary>
100+
/// 测试在任何时候只有一个平台为真
101+
/// </summary>
102+
[Fact]
103+
public void OnlyOnePlatformShouldBeTrue()
104+
{
105+
// Arrange & Act
106+
var platforms = new[]
107+
{
108+
RuntimePlatformHelper.IsLinux,
109+
RuntimePlatformHelper.IsOsx,
110+
RuntimePlatformHelper.IsWindows,
111+
RuntimePlatformHelper.IsFreeBsd
112+
};
113+
114+
var trueCount = 0;
115+
foreach (var platform in platforms)
116+
{
117+
if (platform)
118+
{
119+
trueCount++;
120+
}
121+
}
122+
123+
// Assert
124+
Assert.True(trueCount >= 1, "至少应该有一个平台为真");
125+
Assert.True(trueCount <= 1, "最多只能有一个平台为真");
126+
}
127+
128+
/// <summary>
129+
/// 测试属性的一致性 - 多次调用应该返回相同的值
130+
/// </summary>
131+
[Fact]
132+
public void PlatformProperties_ShouldBeConsistent()
133+
{
134+
// Arrange & Act
135+
var isLinux1 = RuntimePlatformHelper.IsLinux;
136+
var isLinux2 = RuntimePlatformHelper.IsLinux;
137+
138+
var isOsx1 = RuntimePlatformHelper.IsOsx;
139+
var isOsx2 = RuntimePlatformHelper.IsOsx;
140+
141+
var isWindows1 = RuntimePlatformHelper.IsWindows;
142+
var isWindows2 = RuntimePlatformHelper.IsWindows;
143+
144+
var isFreeBsd1 = RuntimePlatformHelper.IsFreeBsd;
145+
var isFreeBsd2 = RuntimePlatformHelper.IsFreeBsd;
146+
147+
// Assert
148+
Assert.Equal(isLinux1, isLinux2);
149+
Assert.Equal(isOsx1, isOsx2);
150+
Assert.Equal(isWindows1, isWindows2);
151+
Assert.Equal(isFreeBsd1, isFreeBsd2);
152+
}
153+
154+
/// <summary>
155+
/// 测试当前运行平台的检测
156+
/// </summary>
157+
[Fact]
158+
public void CurrentPlatform_ShouldBeDetectedCorrectly()
159+
{
160+
// Arrange & Act
161+
var currentPlatform = "Unknown";
162+
163+
if (RuntimePlatformHelper.IsWindows)
164+
{
165+
currentPlatform = "Windows";
166+
}
167+
else if (RuntimePlatformHelper.IsLinux)
168+
{
169+
currentPlatform = "Linux";
170+
}
171+
else if (RuntimePlatformHelper.IsOsx)
172+
{
173+
currentPlatform = "OSX";
174+
}
175+
else if (RuntimePlatformHelper.IsFreeBsd)
176+
{
177+
currentPlatform = "FreeBSD";
178+
}
179+
180+
// Assert
181+
Assert.NotEqual("Unknown", currentPlatform);
182+
}
183+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// ==========================================================================================
2+
// GameFrameX 组织及其衍生项目的版权、商标、专利及其他相关权利
3+
// GameFrameX organization and its derivative projects' copyrights, trademarks, patents, and related rights
4+
// 均受中华人民共和国及相关国际法律法规保护。
5+
// are protected by the laws of the People's Republic of China and relevant international regulations.
6+
//
7+
// 使用本项目须严格遵守相应法律法规及开源许可证之规定。
8+
// Usage of this project must strictly comply with applicable laws, regulations, and open-source licenses.
9+
//
10+
// 本项目采用 MIT 许可证与 Apache License 2.0 双许可证分发,
11+
// This project is dual-licensed under the MIT License and Apache License 2.0,
12+
// 完整许可证文本请参见源代码根目录下的 LICENSE 文件。
13+
// please refer to the LICENSE file in the root directory of the source code for the full license text.
14+
//
15+
// 禁止利用本项目实施任何危害国家安全、破坏社会秩序、
16+
// It is prohibited to use this project to engage in any activities that endanger national security, disrupt social order,
17+
// 侵犯他人合法权益等法律法规所禁止的行为!
18+
// or infringe upon the legitimate rights and interests of others, as prohibited by laws and regulations!
19+
// 因基于本项目二次开发所产生的一切法律纠纷与责任,
20+
// Any legal disputes and liabilities arising from secondary development based on this project
21+
// 本项目组织与贡献者概不承担。
22+
// shall be borne solely by the developer; the project organization and contributors assume no responsibility.
23+
//
24+
// GitHub 仓库:https://github.com/GameFrameX
25+
// GitHub Repository: https://github.com/GameFrameX
26+
// Gitee 仓库:https://gitee.com/GameFrameX
27+
// Gitee Repository: https://gitee.com/GameFrameX
28+
// 官方文档:https://gameframex.doc.alianblank.com/
29+
// Official Documentation: https://gameframex.doc.alianblank.com/
30+
// ==========================================================================================
31+
32+
using System.Runtime.InteropServices;
33+
34+
namespace GameFrameX.Foundation.Utility;
35+
36+
/// <summary>
37+
/// 平台运行时帮助类
38+
/// Runtime platform helper that detects the current operating system.
39+
/// </summary>
40+
/// <remarks>
41+
/// Provides simple boolean properties to query Linux, macOS (OSX), Windows, and FreeBSD.
42+
/// Values are evaluated via <see cref="System.Runtime.InteropServices.RuntimeInformation"/> with <see cref="System.Runtime.InteropServices.OSPlatform"/>.
43+
/// </remarks>
44+
/// <example>
45+
/// <code>
46+
/// if (RuntimePlatformHelper.IsWindows)
47+
/// {
48+
/// // Windows-specific logic
49+
/// }
50+
/// else if (RuntimePlatformHelper.IsLinux)
51+
/// {
52+
/// // Linux-specific logic
53+
/// }
54+
/// </code>
55+
/// </example>
56+
/// <seealso cref="System.Runtime.InteropServices.RuntimeInformation"/>
57+
/// <seealso cref="System.Runtime.InteropServices.OSPlatform"/>
58+
public static class RuntimePlatformHelper
59+
{
60+
/// <summary>
61+
/// 是否是Linux
62+
/// Indicates whether the current OS is Linux.
63+
/// </summary>
64+
/// <value>Returns <c>true</c> when running on Linux; otherwise <c>false</c>.</value>
65+
/// <remarks>Computed from <see cref="System.Runtime.InteropServices.RuntimeInformation"/> using <see cref="System.Runtime.InteropServices.OSPlatform.Linux"/>. This property is read-only.</remarks>
66+
/// <example>
67+
/// <code>
68+
/// bool onLinux = RuntimePlatformHelper.IsLinux;
69+
/// </code>
70+
/// </example>
71+
/// <seealso cref="System.Runtime.InteropServices.OSPlatform"/>
72+
public static bool IsLinux
73+
{
74+
get { return RuntimeInformation.IsOSPlatform(OSPlatform.Linux); }
75+
}
76+
77+
/// <summary>
78+
/// 是否是Mac
79+
/// Indicates whether the current OS is macOS (OSX).
80+
/// </summary>
81+
/// <value>Returns <c>true</c> when running on macOS; otherwise <c>false</c>.</value>
82+
/// <remarks>Computed from <see cref="System.Runtime.InteropServices.RuntimeInformation"/> using <see cref="System.Runtime.InteropServices.OSPlatform.OSX"/>. This property is read-only.</remarks>
83+
/// <example>
84+
/// <code>
85+
/// if (RuntimePlatformHelper.IsOsx)
86+
/// {
87+
/// // macOS specific behavior
88+
/// }
89+
/// </code>
90+
/// </example>
91+
/// <seealso cref="System.Runtime.InteropServices.OSPlatform"/>
92+
public static bool IsOsx
93+
{
94+
get { return RuntimeInformation.IsOSPlatform(OSPlatform.OSX); }
95+
}
96+
97+
/// <summary>
98+
/// 是否是Windows
99+
/// Indicates whether the current OS is Windows.
100+
/// </summary>
101+
/// <value>Returns <c>true</c> when running on Windows; otherwise <c>false</c>.</value>
102+
/// <remarks>Computed from <see cref="System.Runtime.InteropServices.RuntimeInformation"/> using <see cref="System.Runtime.InteropServices.OSPlatform.Windows"/>. This property is read-only.</remarks>
103+
/// <example>
104+
/// <code>
105+
/// if (RuntimePlatformHelper.IsWindows)
106+
/// {
107+
/// // Enable windows-specific features
108+
/// }
109+
/// </code>
110+
/// </example>
111+
/// <seealso cref="System.Runtime.InteropServices.OSPlatform"/>
112+
public static bool IsWindows
113+
{
114+
get { return RuntimeInformation.IsOSPlatform(OSPlatform.Windows); }
115+
}
116+
117+
/// <summary>
118+
/// 是否是FreeBSD
119+
/// Indicates whether the current OS is FreeBSD.
120+
/// </summary>
121+
/// <value>Returns <c>true</c> when running on FreeBSD; otherwise <c>false</c>.</value>
122+
/// <remarks>Computed from <see cref="System.Runtime.InteropServices.RuntimeInformation"/> using <see cref="System.Runtime.InteropServices.OSPlatform.FreeBSD"/>. This property is read-only.</remarks>
123+
/// <example>
124+
/// <code>
125+
/// bool onBsd = RuntimePlatformHelper.IsFreeBsd;
126+
/// </code>
127+
/// </example>
128+
/// <seealso cref="System.Runtime.InteropServices.OSPlatform"/>
129+
public static bool IsFreeBsd
130+
{
131+
get { return RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD); }
132+
}
133+
}

0 commit comments

Comments
 (0)