NCover: exclude non-executable line of code from scope

The switch statement in the following code has a sentence defaultthat the compiler needs and good protection, but which is never executed. After writing tests for everything else, I cannot (or should) check this line. I do not care that I did not consider this line with the tests, but my TestWrans.net NCover code coverage report shows an unchecked line, and this reduces the class coverage to 86%. Is there a way to allow NCover to exclude only one row?

public static class OperandTypeExtensions
{
    public static string ToShortName(this OperandType type)
    {
        #region Contract
        Contract.Requires<InvalidEnumArgumentException>(Enum.IsDefined(typeof(OperandType), type));
        #endregion

        switch (type)
        {
            case OperandType.None: return "<none>";
            case OperandType.Int32: return "i32";
            case OperandType.Int64: return "i64";
            default:
                throw new NotSupportedException();
        }
    }
}

My question is similar to this question , but none of the answers help in my specific situation.

+3
source share
2 answers

, , OperandType OperandType:

Assert.Throws<InvalidEnumArgumentException>(delegate { ((OperandType)Int32.MaxValue).ToShortName(); } );

BTW 86%

UPDATE: Contract. , .

public static class OperandTypeExtensions
{
    public static string ToShortName(this OperandType type)
    {
        switch (type)
        {
            case OperandType.None: return "<none>";
            case OperandType.Int32: return "i32";
            case OperandType.Int64: return "i64";
            default:
                throw new NotSupportedException();
        }
    }
}

default, OperandType enum, Contract , .

UPDATE2: 100% - , OperandType.None :

public static class OperandTypeExtensions
{
    public static string ToShortName(this OperandType type)
    {
        Contract.Requires<InvalidEnumArgumentException>(Enum.IsDefined(typeof(OperandType), type));

        switch (type)
        {
            case OperandType.Int32: return "i32";
            case OperandType.Int64: return "i64";
            default:
                return "<none>";
        }
    }
}

:

CollectionAssert.AreEquivalent(Enum.GetValues(typeof(OperandType)), 
                               new OperandType[] { OperandType.Int32,
                                                   OperandType.Int64, 
                                                   OperandType.None });
+2

100% , %, , Cover Coverage .

IMO, , , - :

Assert.Throws<NotSupportedException>( OperandTypeExtensions.ToShortName() );

,

, , throws Debug.Assert . . Assert.Throws.

, , . , , , . #.

private string ToShortName(this OperandType type)
{
    var result = "";
    switch (type)
    {
        case OperandType.Int32: result = "i32";
        case OperandType.Int64: result = "i64";
    }
    Debug.Assert(result != "", "Invalid type.");
    return result;
}

, ( Debug), Code Coverage Debug.Assert .

P.S. , , , -, .

-1

All Articles