开发者

Representing OS as Enum

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 though开发者_开发知识库ts on the subject? Is this a clean solution? Is there a better solution? Thanks!


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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜