模仿枚举继承:最好选择与放大器;做法 [英] Emulate enum inheritance: best choice & practices

查看:152
本文介绍了模仿枚举继承:最好选择与放大器;做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计通过网页测试应用程序(使用NUnit的),将有导航(通过硒的webdriver API)。我想用枚举到能够告诉通过此枚举的方法,其中导航(尤其是NUnit的方法)来modelize网站结构。

I am designing a test application (Using NUnit) that will have to navigate (through Selenium Webdriver API) through webpages. I'd like to modelize the site structure using enums to be able to tell methods where to navigate (especially NUnit methods) via this enum.

this.NavigateTo(Page.HomePage);

这是一个强烈的要求(有一个枚举类型的地方),主要是因为NUnit的不能传递非基本类型。例如,这个语法是不可能的NUnit的,因为页面的必须的原始或枚举:

This is a strong requirement (to have an enum type somewhere) mostly because NUnit cannot be passed non-primitive types. For instance, this syntax is impossible to NUnit because Page has to be primitive or enum:

static class Page{
  public static Page HomePage;
  public static Page UserAccountPage;
}

/* ... later ... */
[TestCase(Page.HomePage)]
void TestMethod(Page p) { ...

我也想用相同的枚举或最喜欢做基本的继承,像

I'd also like to use the same enum-or-the-like to make basic inheritance, like

interface IPage{}
interface IPageNavigatable : Page {}
interface IPageUserRelated : Page {}
interface IWildcardPage : Page {}

enum Page{
  HomePage : IPageNavigatable,
  UserAccountPage : IPageNavigatable IPageUserRelated,
  ErrorPage : /* none */,
  AnyPage : IWildcardPage 
}

然后,我将能够做出非常有趣的方法,如

And then, I would be able to make very fun methods like

bool IsCurrentPage(IWildcardPage p);
void LoginThenNavigate(String user, String pw, IPageUserRelated p);
Page WhereAmI(); /* and use this value with IsAssignableFrom */



我知道C#禁止枚举继承(我仍不很这个值不懂这样的选择,但现在这不是我的问题),我读过一些解决方法,包括再编码的从头开始的枚举机制的一类;并没有看起来令人满意。

I know C# forbids enum inheritance (I still don't understand this choice but this is not my question now) and I've read a few workarounds, including a class that recodes from scratch the enum mechanism; and none looks satisfactory.

最好的主意,我使用的是复杂的类内枚举和一些公共布尔值。不是很satisfctory ...

The best Idea I have is using a complex class with an inner enum and a few public booleans. Not very satisfctory...

public class Page{
  enum Pages { /* you know them */}
  public Page.Pages InnerPage;
  public bool isWildcard, isUserRelated, ...;

  public Page(Page.Pages p){
    this.InnerPage = p;
    switch (p){
      case ...:
      case ...:
        this.isWildcard = true;
        break;
      /* ... */
    }
  }
}

/* NUnit calls */
[TestCase(Page.Pages.HomePage)]
void TestMethod(Page.Pages p) { ...

另外在一个完美的世界,我很乐意链接我的枚举或那么输入用绳子或URI的值,这样我就可以写的东西,如:

Also, in a perfect world, I'd love to "link" my enum-or-so typed values with Strings or URIs, so that I could write something like:

enum Page{
  HomePage : IPageNavigatable = "/index.php",
  UserAccountPage : IPageNavigatable IPageUserRelated = "/user.php",
  ErrorPage : /* none */ = (String) null,
  AnyPage : IWildcardPage = (String) null
}

/* ... */
Page p;
Selenium.Webdriver drv;
drv.Navigate().GoToUrl(new Url(p.ToString()));

这是一个棘手的问题给我。任何人都可以帮助

This is a tough problem to me. Anyone can help?

编辑:一条线索,克里斯下面指出的是,NUnit的(通过属性)可以采取Object类型的参数。用这种方法,有可能是一个方法来实现近乎无代码与我的继承结构对象,并枚举机制(并链接到URL字符串)重新编码的部分。

A clue, as Chris points out below, is that NUnit (through attributes) can take an argument of type Object. Using this, there is probably a way to implement nearly-no-code objects with my inheritance structure, and recode part of the enum mechanism (and link to the URL strings).

不是我梦想的解决方案,但可能工作...

Not my dream solution but might work...

推荐答案

您确信,他们必须是原语(顺便说一下,我们调用.NET世界的值类型或结构)? 本页面似乎在暗示,你是有限的原因是因为固有的局限性在.NET属性,而这又是这里描述

Are you sure that they must be primitives (by the way, we call those Value Types or structs in the .net world)? This page seems to imply that the reason that you are limited is because of the inherent limitations in .NET attributes, which in turn are described here:

属性参数仅限于下列类型的
的常数值:

Attribute parameters are restricted to constant values of the following types:

简单类型(BOOL,BYTE,CHAR,short,int和长,float和双)

Simple types (bool, byte, char, short, int, long, float, and double)

字符串

System.Type的

System.Type

枚举

对象(参数属性类型的对象的参数必须是上述类型之一的恒定值。)

object (The argument to an attribute parameter of type object must be a constant value of one of the above types.)

上述任何类型的

这表明,我认为以下将工作:

This suggests to me that the following would work:

static class Pages {
  public const String HomePage = "Home Page";
  public const String UserAccountPage = "User Account Page";
}

[TestCase(Pages.HomePage)]
void TestMethod(Page p) { ...

这篇关于模仿枚举继承:最好选择与放大器;做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆