views:

35

answers:

1

I have the following code:

private void SetControlNumbers()
    {
        string controlString = "";
        int numberLength = PersonNummer.Length;

        switch (numberLength)
        {
            case (10) :
                controlString = PersonNummer.Substring(6, 4);
                break;
            case (11) :
                controlString = PersonNummer.Substring(7, 4);
                break;
            case (12) :
                controlString = PersonNummer.Substring(8, 4);
                break;
            case (13) : 
                controlString = PersonNummer.Substring(9, 4);
                break;
        }

        ControlNumbers = Convert.ToInt32(controlString);
    }

Which is tested using the following test methods:

[TestMethod()]
    public void SetControlNumbers_Length10()
    {
        string pNummer = "9999999999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

    [TestMethod()]
    public void SetControlNumbers_Length11()
    {
        string pNummer = "999999-9999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

    [TestMethod()]
    public void SetControlNumbers_Length12()
    {
        string pNummer = "199999999999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

    [TestMethod()]
    public void SetControlNumbers_Length13()
    {
        string pNummer = "1999999-9999";
        Personnummer target = new Personnummer(pNummer);
        Assert.AreEqual(9999, target.ControlNumbers);
    }

For some reason Visual Studio says that I have 1 block that is not tested despite showing all code in the method under test in blue (ie. the code is covered in my unit tests). Is this because of the fact that I don't have a default value defined in the switch? When the SetControlNumbers() method is called, the string on which it operates have already been validated and checked to see that it conforms to the specification and that the various Substring calls in the switch will generate a string containing 4 chars. I'm just curious as to why it says there is 1 untested block. I'm no unit test guru at all, so I'd love some feedback on this.

Also, how can I improve on the conversion after the switch to make it safer other than adding a try-catch block and check for FormatExceptions and OverflowExceptions?

+3  A: 

you need to add a default section to your switch statement to catch the cases that not explicitly defined.

private void SetControlNumbers()
{
    string controlString = "";
    int numberLength = PersonNummer.Length;

    switch (numberLength)
    {
        case (10) :
            controlString = PersonNummer.Substring(6, 4);
            break;
        case (11) :
            controlString = PersonNummer.Substring(7, 4);
            break;
        case (12) :
            controlString = PersonNummer.Substring(8, 4);
            break;
        case (13) : 
            controlString = PersonNummer.Substring(9, 4);
            break;
        default:
            //decide what to do here, 
            //could do this or something more appropriate for your situation
            throw new NotSupportedException("An invalid PersonNummer.Length value was encountered.")
    }

    ControlNumbers = Convert.ToInt32(controlString);
}
Sam Holder
Correct! See my comment to Ardman and sbi. Thanks guys!
daft