Note that there are some explanatory texts on larger screens.

plurals
  1. POEmulate enum inheritance: best choice & practices
    text
    copied!<p>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. </p> <pre><code>this.NavigateTo(Page.HomePage); </code></pre> <p>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 <em>has to be</em> primitive or enum:</p> <pre><code>static class Page{ public static Page HomePage; public static Page UserAccountPage; } /* ... later ... */ [TestCase(Page.HomePage)] void TestMethod(Page p) { ... </code></pre> <p>I'd also like to use the same enum-or-the-like to make basic inheritance, like </p> <pre><code>interface IPage{} interface IPageNavigatable : Page {} interface IPageUserRelated : Page {} interface IWildcardPage : Page {} enum Page{ HomePage : IPageNavigatable, UserAccountPage : IPageNavigatable IPageUserRelated, ErrorPage : /* none */, AnyPage : IWildcardPage } </code></pre> <p>And then, I would be able to make very fun methods like </p> <pre><code>bool IsCurrentPage(IWildcardPage p); void LoginThenNavigate(String user, String pw, IPageUserRelated p); Page WhereAmI(); /* and use this value with IsAssignableFrom */ </code></pre> <p>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 <em>recodes</em> from scratch the enum mechanism; and none looks satisfactory.</p> <p>The best Idea I have is using a complex class with an inner enum and a few public booleans. Not very satisfctory...</p> <pre><code>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) { ... </code></pre> <p>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:</p> <pre><code>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())); </code></pre> <p>This is a tough problem to me. Anyone can help?</p> <p>EDIT: 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).</p> <p>Not my dream solution but might work...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload