Skip to content

Switch statement weird coverage #171

@bjornbouetsmith

Description

@bjornbouetsmith

Hi,

I have the following simple class:

internal static class StateParser
{
	public static State Parse(string stateText)
	{
		if (string.IsNullOrWhiteSpace(stateText))
		{
			return State.Unknown;
		}

		switch (stateText)
		{
			case "ONLINE":
				return State.Online;
			case "DEGRADED":
				return State.Degraded;
			case "OFFLINE":
				return State.Offline;
			case "FAULTED":
				return State.Faulted;
			case "REMOVED":
				return State.Removed;
			case "UNAVAIL":
				return State.Unavailable;
			case "AVAIL":
				return State.Available;
			default:
				return State.Unknown;
		}
	}
}

No matter how I test it - I get coverage of 77.8% - with the message stating that the line of the switch(stateText) is only partially covered by tests (12 of 20 conditions).

At first I thought it was because I did not have the if (string.IsNullOrWhiteSpace(stateText)) { return State.Unknown; }

Even though the default case works just fine for null and empty strings.

But even though I have tests like this:

[TestMethod]
[DataRow("ONLINE",State.Online)]
[DataRow("OFFLINE", State.Offline)]
[DataRow("DEGRADED", State.Degraded)]
[DataRow("FAULTED", State.Faulted)]
[DataRow("REMOVED", State.Removed)]
[DataRow("AVAIL", State.Available)]
[DataRow("UNAVAIL", State.Unavailable)]
[DataRow("HOTSTUFF", State.Unknown)]
[DataRow(null, State.Unknown)]
[DataRow("", State.Unknown)]
public void ParseStateTest(string stateText, State expected)
{
	var state = StateParser.Parse(stateText);
	Assert.AreEqual(expected, state);
}

Which hits every single line of the StateParser - I still only get 77.8% coverage.

Do you have any explanation of why this is happening - and is it a bug in altcover - or something weird going on?

To have the full picture, in case you want to reproduce it - this is the State enum:

public enum State
{
	Unknown = 0,
	Online = 1,
	Degraded = 2,
	Faulted = 3,
	Offline = 4,
	Removed = 5,
	Unavailable = 6,
	Available = 7
}

Thanks in advance for any hints you can give.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions