private static IEnumerable<SyntaxNode> GetReferencedNodes(SyntaxNode node, SutInfo sutInfo) { var symbolModel = node.GetModelFromNode(sutInfo.SemanticModel.Compilation); if (symbolModel == null) return new SyntaxNode[0]; var symbol = symbolModel.GetSymbolInfo(node).Symbol; if (symbol == null || symbol.Locations.All(x => !x.IsInSource)) return new SyntaxNode[0]; var sutType = sutInfo.SymbolInfo.Symbol as INamedTypeSymbol; var symbolType = symbol as INamedTypeSymbol; //constraint to get nodes for current sut type or base type of it if (sutType != null && !IsTypesEquals(symbolType, sutType) && !IsTypesEquals(symbol.ContainingType, sutType) && !IsTypesEquals(symbol.ContainingType?.ConstructedFrom, sutType.ConstructedFrom) && !IsTypesEquals(symbolType, sutType.BaseType) && !IsTypesEquals(symbol.ContainingType, sutType.BaseType) && !IsTypesEquals(symbol.ContainingType?.ConstructedFrom, sutType.BaseType?.ConstructedFrom)) { return new SyntaxNode[0]; } return symbol.DeclaringSyntaxReferences .SelectMany(z => z.GetSyntax() .DescendantNodes()); }
private static Func<Fields, bool> DontHaveSetups(SutInfo suitableSut) { return syntax => !IsExistInSetups(syntax, suitableSut.InjectedFields); }
private static IEnumerable<Fields> GetUsings(ExpressionSyntax expression, ISymbol symbol, SutInfo suitableSut, SemanticModel semanticModel, Dictionary<string, ITypeSymbol> sutSubstitutions) { var syntax = expression.Parents(x => (x as UsingStatementSyntax)?.Declaration .Variables .Any(y => y.DescendantNodes() .Any(z => z == expression)) == true) as UsingStatementSyntax; if (syntax == null) return Enumerable.Empty<Fields>(); var methodSymbol = symbol as IMethodSymbol; var propertySymbol = symbol as IPropertySymbol; if (methodSymbol == null && propertySymbol == null) return Enumerable.Empty<Fields>(); var containingType = (methodSymbol?.ReturnType ?? propertySymbol?.GetMethod?.ReturnType); var disposable = containingType?.Interfaces.FirstOrDefault(x => x.Name == "IDisposable"); if(disposable == null) return Enumerable.Empty<Fields>(); var disposeMethod = disposable.GetMembers("Dispose").First(); return syntax.Declaration.Variables.Select(x => new Fields { Expression = expression, MethodOrPropertySymbol = disposeMethod, FieldsToSetup = GetFieldsToSetup(suitableSut, semanticModel, containingType, sutSubstitutions) }); }
private static IEnumerable<FieldsSetups> GetFieldsToSetup(SutInfo suitableSut, SemanticModel semanticModel, ISymbol symbol, Dictionary<string, ITypeSymbol> sutSubstitutions) { return suitableSut.InjectedFields.Find(IsCorrespondingField(semanticModel, symbol, sutSubstitutions)) .Select(z => new FieldsSetups { Field = z.Data.Field.Declaration.Variables.Select(f => f.Identifier.ValueText), Substitutions = (z.Data.Field.Declaration.Type as GenericNameSyntax)?.TypeArgumentList .Arguments .Select(y => GetSubstitutions(semanticModel, y)) .SelectMany(s => s) .ToDictionary(s => s.Key, s => s.Value), SutSubstitutions = sutSubstitutions }).ToArray(); }
private static IEnumerable<Fields> GetInvokedMethodsOfMocks( IEnumerable<ExpressionSyntax> methodsAndPropertyInvokations, SemanticModel model, SutInfo suitableSut, SemanticModel semanticModel, Dictionary<string, ITypeSymbol> sutSubstitutions) { var invokedMethodsOfMocks = methodsAndPropertyInvokations.Select(x => new { x.GetModelFromExpression(model)?.GetSymbolInfo(x).Symbol, Expression = x }).SelectMany(x => new[] {new Fields { Expression = x.Expression, MethodOrPropertySymbol = x.Symbol, FieldsToSetup = GetFieldsToSetup(suitableSut, semanticModel, x.Symbol, sutSubstitutions) }}.Concat(GetUsings(x.Expression, x.Symbol, suitableSut, semanticModel, sutSubstitutions))) .Where(x => x.FieldsToSetup.Any()) .ToArray(); return invokedMethodsOfMocks; }