将操作系统表示为枚举

发布于 2024-10-16 04:33:30 字数 450 浏览 2 评论 0原文

我希望在我们的自动化测试框架中添加测试编写者使用代表测试支持的最低操作系统版本的属性来标记其测试方法的能力。例如,测试编写者会标记测试方法支持 Windows Vista SP1 及更高版本,然后框架将知道在 Vista、7 等上运行测试。

我的直觉是使用枚举来表示此表示(例如 OS. VistaSp1、OS.Win7 等),但由于我们还支持多种操作系统类型(Mac、Linux 等),因此枚举不足以让框架了解最小值(例如,它无法轻易知道OS.Win7 高于 OS.VistaSp1,但与 OS.MacOsX 无关)

我的下一步是我可以使用枚举的较低位来表示操作系统类型,使用较高位来表示[相对]版本号这就能解决问题。它不是最干净的,但是使用它的测试编写者根本不需要意识到这一点,并且只需要解析出信息的少量代码,这些代码都可以封装在某个 Utils 类中。

对这个主题有什么想法吗?这是一个干净的解决方案吗?有更好的解决方案吗? 谢谢!

I am looking to add to our automated test framework the ability for test writers to mark their test methods with attributes representing the minimum OS version the test supports. For example, the test writer would mark that a test method supports Windows Vista SP1 and higher, and then the framework will know to run the test on Vista, 7, etc.

My gut was to use an enum for this representation (e.g. OS.VistaSp1, OS.Win7, etc.) but since we also support several OS types (Mac, Linux, etc.) an enum wouldn't be enough information for the framework to understand what the minimum is (e.g. it couldn't easily know that OS.Win7 is higher than OS.VistaSp1, but not related to OS.MacOsX)

My next though was that I could use the lower bits of the enum to represent the OS type and the higher bits to represent the [relative] version number and that would solve the issue. It's not the cleanest, but the test writers using it wouldn't need to be aware of this at all, and there would only small amount of code that would need to parse out the information, which can all be encapsulate in some Utils class.

Any thoughts on the subject? Is this a clean solution? Is there a better solution?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

零度° 2024-10-23 04:33:30

是否无法使用已经存在的Environment.OperatingSystem 类?

对于平台 ID,它包含 MacOSX、Unix、Win32S、Win32NT、Win32Windows、WinCE 和 XBox。
它还包含服务包的字符串和提供版本号的 Version 对象。

我不知道它自动检测这些的效果如何(我猜单声道支持其中的很多),因为我自己从未尝试过。

Is it not possible to use the already existing Environment.OperatingSystem class?

For platform id it contains MacOSX, Unix, Win32S, Win32NT, Win32Windows, WinCE and XBox.
It also contains a string for the service pack and a Version object to provide a version number.

I don't know how well it detects these automatically (i'm guessing mono supports a bunch of these) as i've never tried it myself.

扮仙女 2024-10-23 04:33:30

为此,我将(至少)使用两个不同的变量:

  • 一般操作系统类型(Windows、Linux、MacOS) - 枚举是此
  • 服务包/版本信息的理想选择(可以是字符串,或者甚至是两个单独的字符串 - 例如“ XP SP3”和“5.3.157”*)

这有两个好处:

  • 更容易对操作系统版本进行排序并决定兼容性问题,
  • 至少服务包版本可以跨操作系统重用,因此可以减少所需枚举值的数量。

* 不是真正的 XP 版本号,只是示例

I would use (at least) two distinct variables for this:

  • general OS type (Windows, Linux, MacOS) - an enum is ideal for this
  • service pack/version info (could be a string, or rather even two separate strings - e.g. "XP SP3" and "5.3.157"*)

This has two benefits:

  • it is easier to sort OS versions and decide compatibility issues,
  • at least the service pack versions are reusable across OSes, so you can reduce the number of required enum values.

* not a real XP version number, just example

瑶笙 2024-10-23 04:33:30

我会编写一个操作系统枚举并使用 Version 对象作为第二个参数。

I would write a OS enum and use a Version object as second parameter.

月隐月明月朦胧 2024-10-23 04:33:30

当您需要更多功能时,可以以与枚举类似的方式使用结构体。声明一堆 static readonly 字段,它们是结构的实例,可以像一组枚举值一样使用(当然不在 switch 语句中):

public struct OSVersion
{
    public readonly string Name;
    public readonly Version Version;

    public OSVersion( string name, Version version )
    {
        Name = name;
        Version = version;
    }

    public static readonly OSVersion WindowsXPSP3 = new OSVersion( "XP SP3", new Version(...) );
    public static readonly OSVersion WindowsVistaSP1 = new OSVersion( "Vista SP1", new Version(...) );
    public static readonly OSVersion Windows7 = new OSVersion( "Win7", new Version(...) );
}

显然,您需要在这里做更多的事情,就像可能添加更多字段并重载一些运算符(这里必须使用相等运算符),但这在很多方面都像枚举一样起作用,但提供了更多功能。

You can use a struct in a similar manner to an enum when you need a bit more functionality. Declare a bunch of static readonly fields which are instances of your struct to be used like a set of enumerated values (except of course not in a switch statement):

public struct OSVersion
{
    public readonly string Name;
    public readonly Version Version;

    public OSVersion( string name, Version version )
    {
        Name = name;
        Version = version;
    }

    public static readonly OSVersion WindowsXPSP3 = new OSVersion( "XP SP3", new Version(...) );
    public static readonly OSVersion WindowsVistaSP1 = new OSVersion( "Vista SP1", new Version(...) );
    public static readonly OSVersion Windows7 = new OSVersion( "Win7", new Version(...) );
}

Obviously you'll need to do more here, like possibly adding more fields and overloading some of the operators (equality operators are a must here), but this will act in many ways like an enum but provide much more power.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文