title: Checking, if all enum values were processed
body: <p>Suppose, that there's a piece of code:</p>
  
  <pre><code>enum Directions
  {
      North,
      South,
      East,
      West
  }
  
  // (...)
  
  switch (dir)
  {
      case North : // Do sth
      case South : // Do sth
      case East : // Do sth
      case West : // Do sth
  }
  </code></pre>
  
  <p>Notice especially the lack of <code>default:</code> directive. Now suppose, that later someone added four more values to that enum: <code>NorthEast</code>, <code>NorthWest</code>, <code>SouthEast</code>, <code>SouthWest</code>. In such case it is likely, that the quoted code will effect in undefined behavior, because there is no security measure to handle possible extensions to Directions enumeration type.</p>
  
  <p>I've got some questions regarding this matter.</p>
  
  <ol>
  <li><p>Shall a programmer implement a security measure here, eg:</p>
  
  <pre><code>default: throw new InvalidArgumentException("Unsupported enum value!");
  </code></pre></li>
  <li><p>Should this matter be tested in unit testing?</p>
  
  <pre><code>// Naïve implementation, for example purposes only
  [ExpectedException(typeof(InvalidArgumentException))]
  [Test]
  void SomeTestMethod()
  {
      SomeFunction((Direction)-1);
  }
  </code></pre>
  
  <p>If so, how would you automate such kind of test (eg. find ordinal value different from all other values in this enum, take into consideration enum backing type etc.)?</p></li>
  <li><p>Should this matter be verified during static code analysis? Which tool can provide such informations? I've written simple Proof-of-concept in VS 2012 Pro, run code analysis, but IDE reported, that 'No code analysis issues were detected'.</p>
  
  <p>The code:</p>
  
  <pre><code>namespace ManagedConsoleSketchbook
  {
      class Program
      {
          enum MyEnum
          {
              One,
              Two,
              Three
          }
  
          static void Main(string[] args)
          {
              MyEnum e = MyEnum.One;
  
              switch (e)
              {
                  case MyEnum.One:
                      break;
                  case MyEnum.Two:
                      break;
              }
          }
      }
  }
  </code></pre></li>
  <li><p>Is there a generic approach to this general problem? Eg. maybe I can write sophisticated test method in C# using reflection, but in C++ it's a lot harder to nail down such <em>potential</em> code integrity breach than in C# or other higher-level languages.</p></li>
  </ol>
  
  <hr />
  
  <p>Why do I ask about if it should be checked during unit testing? It's because - as far as I know - unit tests shall verify, <em>if method behaves as it is supposed (documented) to</em>. In this case it is supposed to accept any of four enum elements and it actually do it, so all tests should pass. Checking for <em>potential</em> problems seems to me not to be a concern of the unit testing process. If I'd like to verify all possibilities of <em>potential</em> problems in unit tests, I'd probably end up with writing a hundreds of tests for 3-line-of-code methods.</p>
  
  <p>On the other hand it seems like a quite cheap (in terms of spent resources) way of making sure, that the method won't cause any serious problems, when someone decides to add another value to an enum.</p>