Example #1
0
        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)
            });
        }