Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion CodeLineCounter.Tests/CyclomaticComplexityCalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,48 @@ public void MyMethod()
var complexity = CyclomaticComplexityCalculator.Calculate(methodDeclaration, semanticModel);

// Assert
Assert.Equal(2, complexity);
Assert.Equal(3, complexity);
}

[Fact]
public void Calculate_Should_Return_Correct_Complexity_6()
{
// Arrange
var syntaxTree = CSharpSyntaxTree.ParseText(@"
class Program
{
static void Main(string[] args)
{
int x = 5;

switch (x)
{
case 1:
Console.WriteLine(1);
break;
case 2:
Console.WriteLine(2);
break;
case 3:
Console.WriteLine(3);
break;
case 4:
Console.WriteLine(4);
break;
}
}
}
");
var root = syntaxTree.GetRoot();
var methodDeclaration = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
var compilation = CSharpCompilation.Create("TestCompilation", new[] { syntaxTree });
var semanticModel = compilation.GetSemanticModel(syntaxTree);

// Act
var complexity = CyclomaticComplexityCalculator.Calculate(methodDeclaration, semanticModel);

// Assert
Assert.Equal(6, complexity);
}
}
}
6 changes: 5 additions & 1 deletion CodeLineCounter/Services/CyclomaticComplexityCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ private static int CalculateComplexity(SyntaxNode node)
switch (descendant.Kind())
{
case SyntaxKind.IfStatement:
case SyntaxKind.ElseClause:
case SyntaxKind.SwitchStatement:
case SyntaxKind.CaseSwitchLabel:
case SyntaxKind.DefaultSwitchLabel:
case SyntaxKind.ForStatement:
case SyntaxKind.ForEachStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.CaseSwitchLabel:
case SyntaxKind.TryStatement:
case SyntaxKind.CatchClause:
complexity++;
break;
Expand Down