Created
January 25, 2018 08:13
-
-
Save jonathascosta/a8e81f27046519866c470dce4c717311 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis.CSharp; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
using Microsoft.CodeAnalysis; | |
namespace UnitTestGenerator | |
{ | |
public class CyclomaticComplexityCalculator : CSharpSyntaxWalker | |
{ | |
private int cyclomaticComplexity; | |
public int Calculate(SyntaxNode node) | |
{ | |
this.cyclomaticComplexity = 1; | |
this.Visit(node); | |
return this.cyclomaticComplexity; | |
} | |
public override void VisitWhileStatement(WhileStatementSyntax node) | |
{ | |
base.VisitWhileStatement(node); | |
this.cyclomaticComplexity++; | |
} | |
public override void VisitIfStatement(IfStatementSyntax node) | |
{ | |
base.VisitIfStatement(node); | |
this.cyclomaticComplexity++; | |
} | |
public override void VisitForStatement(ForStatementSyntax node) | |
{ | |
base.VisitForStatement(node); | |
this.cyclomaticComplexity++; | |
} | |
public override void VisitForEachStatement(ForEachStatementSyntax node) | |
{ | |
base.VisitForEachStatement(node); | |
this.cyclomaticComplexity++; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis.CSharp; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
using Microsoft.CodeAnalysis; | |
namespace UnitTestGenerator | |
{ | |
public class CyclomaticComplexityWalker : CSharpSyntaxWalker | |
{ | |
public IList<string> UnitTests { get; private set; } | |
public CyclomaticComplexityWalker(MethodDeclarationSyntax node) | |
{ | |
this.UnitTests = new List<string>() { node.Identifier.Text }; | |
Visit(node); | |
var classDeclaration = SyntaxFactory.ClassDeclaration("Class1Tests"); | |
foreach (var test in UnitTests) | |
{ | |
var methodDeclaration = SyntaxFactory | |
.MethodDeclaration(SyntaxFactory.ParseTypeName("void"), test) | |
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)) | |
.AddBodyStatements( | |
node.ParameterList.Parameters.Select(x => | |
SyntaxFactory.LocalDeclarationStatement( | |
SyntaxFactory.VariableDeclaration(x.Type, | |
SyntaxFactory.SingletonSeparatedList( | |
SyntaxFactory.VariableDeclarator(x.Identifier.Text)))) as StatementSyntax).ToArray()) | |
.AddBodyStatements( | |
SyntaxFactory.LocalDeclarationStatement( | |
SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("Class1"), | |
SyntaxFactory.SingletonSeparatedList( | |
SyntaxFactory.VariableDeclarator("target"))))); | |
classDeclaration = classDeclaration.AddMembers(methodDeclaration); | |
} | |
var code = classDeclaration.NormalizeWhitespace().ToFullString(); | |
} | |
public override void VisitIfStatement(IfStatementSyntax node) | |
{ | |
base.VisitIfStatement(node); | |
} | |
public override void VisitBinaryExpression(BinaryExpressionSyntax node) | |
{ | |
if (node.IsKind(SyntaxKind.GreaterThanExpression)) | |
{ | |
var clone = UnitTests.ToList(); | |
for (int i = 0; i < UnitTests.Count; i++) | |
{ | |
UnitTests[i] += $"_When_{node.Left}_GreaterThan_{node.Right}"; | |
clone[i] += $"_When_{node.Left}_LessThanOrEqual_{node.Right}"; | |
} | |
UnitTests = UnitTests.Concat(clone).ToList(); | |
} | |
base.VisitBinaryExpression(node); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment