public override SyntaxNode VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node) { var variableDeclarationSyntax = node.Declaration; VariableDeclaratorSyntax variableDeclarator = variableDeclarationSyntax.Variables[0]; if (variableDeclarator.Initializer != null) { var nodeSemanticModel = _semanticModel.Compilation.GetSemanticModel(node.SyntaxTree); var symbolInfo = nodeSemanticModel.GetSymbolInfo(variableDeclarationSyntax.Type); var typeSymbol = symbolInfo.Symbol; ExpressionSyntax replaceValueSyntaxNode = _randomTypeGenerator.ResolveType(typeSymbol.ToString()); //if (typeSymbol.IsAbstract) //{ // //get a type that implements that interface // string toBeResolvedType = // _randomTypeGenerator.GetTypeForInterface(typeSymbol.ToString()); // replaceValueSyntaxNode = toBeResolvedType != null ? // _randomTypeGenerator.ResolveType(toBeResolvedType) : null; //} //else //{ // replaceValueSyntaxNode = // _randomTypeGenerator.ResolveType(typeSymbol.ToString()); //} if (replaceValueSyntaxNode != null) { var newLocalVariableDeclarationNode = SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( variableDeclarationSyntax.Type) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( variableDeclarator.Identifier) .WithInitializer( SyntaxFactory.EqualsValueClause(replaceValueSyntaxNode))))) .NormalizeWhitespace(); var mutatedClassRoot = _classRootNode.ReplaceNode(node, newLocalVariableDeclarationNode); _mutantCreator.CreateNewMutant(mutatedClassRoot, false); } } return(node); }
public override SyntaxNode VisitAssignmentExpression(AssignmentExpressionSyntax node) { var nodeSemanticModel = _semanticModel.Compilation.GetSemanticModel(node.SyntaxTree); var typeInfo = nodeSemanticModel.GetTypeInfo(node); ExpressionSyntax replaceValueSyntaxNode = _randomTypeGenerator.ResolveType(typeInfo.Type.ToString()); //if (typeInfo.Type.IsAbstract) // TODO: rethink this, might be abstract class, not interface //{ // //get a type that implements that interface // string toBeResolvedType = // _randomTypeGenerator.GetTypeForInterface(typeInfo.Type.Name); // replaceValueSyntaxNode = toBeResolvedType != null ? // _randomTypeGenerator.ResolveType(toBeResolvedType) : null; //} //else //{ // replaceValueSyntaxNode = // _randomTypeGenerator.ResolveType(typeInfo.Type.ToString()); //} if (replaceValueSyntaxNode != null) { var newAssignmentNode = SyntaxFactory.AssignmentExpression( node.Kind(), node.Left, replaceValueSyntaxNode).NormalizeWhitespace(); var mutatedClassRoot = _classRootNode.ReplaceNode(node, newAssignmentNode); _mutantCreator.CreateNewMutant(mutatedClassRoot, false); } return(node); }
public override SyntaxNode VisitReturnStatement(ReturnStatementSyntax node) { var nodeSemanticModel = _semanticModel.Compilation.GetSemanticModel(node.SyntaxTree); var typeInfo = nodeSemanticModel.GetTypeInfo(node.Expression); ExpressionSyntax replaceValueSyntaxNode; if (typeInfo.Type != null) { replaceValueSyntaxNode = _randomTypeGenerator.ResolveType(typeInfo.Type.ToString()); //if (typeInfo.Type.IsAbstract) // TODO: rethink this, might be abstract class, not interface //{ // //get a type that implements that interface // string toBeResolvedType = // _randomTypeGenerator.GetTypeForInterface(typeInfo.Type.Name); // replaceValueSyntaxNode = toBeResolvedType != null ? // _randomTypeGenerator.ResolveType(toBeResolvedType) : null; //} //else //{ // replaceValueSyntaxNode = // _randomTypeGenerator.ResolveType(typeInfo.Type.ToString()); //} } else { replaceValueSyntaxNode = null; } //replace with random value if (replaceValueSyntaxNode != null) { var newReturnStatemenNode = SyntaxFactory.ReturnStatement(replaceValueSyntaxNode) .NormalizeWhitespace(); var mutatedClassRoot = _classRootNode.ReplaceNode(node, newReturnStatemenNode); _mutantCreator.CreateNewMutant(mutatedClassRoot, false); } else { if (typeInfo.Type == null) { return(node); } if (typeInfo.Type != null && typeInfo.Type.Name == "T") { return(node); } if (typeInfo.Type.BaseType != null && typeInfo.Type.BaseType.Name == "Enum") { return(node); } // replace with return null; var nullReturnStatementNode = SyntaxFactory.ReturnStatement( SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)) .NormalizeWhitespace(); var mutatedClassRoot = _classRootNode.ReplaceNode(node, nullReturnStatementNode); _mutantCreator.CreateNewMutant(mutatedClassRoot, false); } return(node); }