protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.AnyShouldBeFalseSyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("Any"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeFalse", "NotContain", remove.Arguments))); } else if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.WhereShouldBeEmptySyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("Where"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeEmpty", "NotContain", remove.Arguments))); } /* * else if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.ShouldOnlyContainNotSyntaxVisitor)) * { * return GetNewExpression(expression, NodeReplacement.RenameAndNegateLambda("OnlyContain", "NotContain")); * } */ throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var remove = NodeReplacement.RemoveAndExtractArguments("EndsWith"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeTrue", "EndWith", remove.Arguments))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var remove = NodeReplacement.Remove("Length"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.Rename("Be", "HaveLength"))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(DictionaryShouldContainPairAnalyzer.ShouldContainKeyAndContainValueSyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("ContainValue"); var newExpression = GetNewExpression(expression, remove); var newArguments = GetArgumentsWithFirstAsPairIdentifierArgument(remove.Arguments); newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndRemoveFirstArgument("ContainKey", "Contain")); newExpression = GetNewExpression(newExpression, NodeReplacement.PrependArguments("Contain", newArguments)); return(newExpression); } else if (properties.VisitorName == nameof(DictionaryShouldContainPairAnalyzer.ShouldContainValueAndContainKeySyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("ContainKey"); var newExpression = GetNewExpression(expression, remove); var newArguments = GetArgumentsWithFirstAsPairIdentifierArgument(remove.Arguments); newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndRemoveFirstArgument("ContainValue", "Contain")); newExpression = GetNewExpression(newExpression, NodeReplacement.PrependArguments("Contain", newArguments)); return(newExpression); } else { throw new InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); } }
protected ExpressionSyntax GetNewExpression(ExpressionSyntax expression, NodeReplacement replacement) { var visitor = new MemberAccessExpressionsCSharpSyntaxVisitor(); expression.Accept(visitor); var members = new LinkedList <MemberAccessExpressionSyntax>(visitor.Members); var current = members.Last; while (current != null) { if (replacement.IsValidNode(current)) { // extract custom data into the replacement object replacement.ExtractValues(current.Value); var oldNode = replacement.ComputeOld(current); var newNode = replacement.ComputeNew(current); return(expression.ReplaceNode(oldNode, newNode)); } current = current.Previous; } throw new System.InvalidOperationException("should not get here"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { return(GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveInvocationOfMethodOnFirstArgument("NotBe", "NotHaveSameCount") )); }
private ExpressionSyntax ReplaceWithMessage(ExpressionSyntax expression, string whichOrAnd, string renameMethod, string prefix = "", string postfix = "") { var replacements = new[] { NodeReplacement.Remove(whichOrAnd), NodeReplacement.Remove("Message"), NodeReplacement.RemoveOccurrence("Should", occurrence: 2) }; var newExpression = GetNewExpression(expression, replacements); var rename = NodeReplacement.RenameAndExtractArguments(renameMethod, "WithMessage"); newExpression = GetNewExpression(newExpression, rename); ArgumentSyntax newArgument = null; switch (rename.Arguments[0].Expression) { case IdentifierNameSyntax identifier: newArgument = SF.Argument(SF.ParseExpression($"$\"{prefix}{{{identifier.Identifier.Text}}}{postfix}\"")); break; case LiteralExpressionSyntax literal: newArgument = SF.Argument(SF.ParseExpression($"\"{prefix}{literal.Token.ValueText}{postfix}\"")); break; } var replacement = NodeReplacement.WithArguments("WithMessage", rename.Arguments.Replace(rename.Arguments[0], newArgument)); return(GetNewExpression(newExpression, replacement)); }
private ExpressionSyntax GetCombinedAssertions(ExpressionSyntax expression, string removeMethod, string renameMethod) { var remove = NodeReplacement.RemoveAndExtractArguments(removeMethod); var newExpression = GetNewExpression(expression, NodeReplacement.RemoveMethodBefore(removeMethod), remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments(renameMethod, "NotBeNullOrEmpty", remove.Arguments))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(CollectionShouldHaveElementAtAnalyzer.ElementAtIndexShouldBeSyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("ElementAt"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("Be", "HaveElementAt", remove.Arguments))); } else if (properties.VisitorName == nameof(CollectionShouldHaveElementAtAnalyzer.IndexerShouldBeSyntaxVisitor)) { var remove = NodeReplacement.RemoveAndRetrieveIndexerArguments("Should"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("Be", "HaveElementAt", remove.Arguments))); } else if (properties.VisitorName == nameof(CollectionShouldHaveElementAtAnalyzer.SkipFirstShouldBeSyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("Skip"); var newExpression = GetNewExpression(expression, remove, NodeReplacement.Remove("First")); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("Be", "HaveElementAt", remove.Arguments))); } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(NumericShouldBeInRangeAnalyzer.BeGreaterOrEqualToAndBeLessOrEqualToSyntaxVisitor)) { var removeLess = NodeReplacement.RemoveAndExtractArguments("BeLessOrEqualTo"); var newExpression = GetNewExpression(expression, removeLess); var renameGreater = NodeReplacement.RenameAndExtractArguments("BeGreaterOrEqualTo", "BeInRange"); newExpression = GetNewExpression(newExpression, renameGreater); var arguments = renameGreater.Arguments.InsertRange(1, removeLess.Arguments); return(GetNewExpression(newExpression, NodeReplacement.WithArguments("BeInRange", arguments))); } else if (properties.VisitorName == nameof(NumericShouldBeInRangeAnalyzer.BeLessOrEqualToAndBeGreaterOrEqualToSyntaxVisitor)) { var removeGreater = NodeReplacement.RemoveAndExtractArguments("BeGreaterOrEqualTo"); var newExpression = GetNewExpression(expression, removeGreater); var renameLess = NodeReplacement.RenameAndExtractArguments("BeLessOrEqualTo", "BeInRange"); newExpression = GetNewExpression(newExpression, renameLess); var arguments = removeGreater.Arguments.InsertRange(1, renameLess.Arguments); return(GetNewExpression(newExpression, NodeReplacement.WithArguments("BeInRange", arguments))); } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(DictionaryShouldContainPairAnalyzer.ShouldContainKeyAndContainValueSyntaxVisitor)) { var renameKeyArguments = NodeReplacement.RenameAndExtractArguments("ContainKey", "Contain"); var removeValueArguments = NodeReplacement.RemoveAndExtractArguments("ContainValue"); var newExpression = GetNewExpression(expression, NodeReplacement.RemoveMethodBefore("ContainValue"), renameKeyArguments, removeValueArguments); var newArguments = MergeContainKeyAndContainValueArguments(renameKeyArguments.Arguments, removeValueArguments.Arguments); return(GetNewExpression(newExpression, NodeReplacement.WithArguments("Contain", newArguments))); } else if (properties.VisitorName == nameof(DictionaryShouldContainPairAnalyzer.ShouldContainValueAndContainKeySyntaxVisitor)) { var removeKeyArguments = NodeReplacement.RemoveAndExtractArguments("ContainKey"); var renameValueArguments = NodeReplacement.RenameAndExtractArguments("ContainValue", "Contain"); var newExpression = GetNewExpression(expression, NodeReplacement.RemoveMethodBefore("ContainKey"), removeKeyArguments, renameValueArguments); var newArguments = MergeContainKeyAndContainValueArguments(removeKeyArguments.Arguments, renameValueArguments.Arguments); return(GetNewExpression(newExpression, NodeReplacement.WithArguments("Contain", newArguments))); } else { throw new InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); } }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var remove = NodeReplacement.RemoveAndExtractArguments("Select"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.PrependArguments("NotContainNulls", remove.Arguments))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(StringShouldNotBeNullOrEmptyAnalyzer.StringShouldNotBeNullAndNotBeEmptySyntaxVisitor)) { return(GetCombinedAssertions(expression, "NotBeEmpty", "NotBeNull")); } else if (properties.VisitorName == nameof(StringShouldNotBeNullOrEmptyAnalyzer.StringShouldNotBeEmptyAndNotBeNullSyntaxVisitor)) { return(GetCombinedAssertions(expression, "NotBeNull", "NotBeEmpty")); } else if (properties.VisitorName == nameof(StringShouldNotBeNullOrEmptyAnalyzer.StringIsNullOrEmptyShouldBeFalseSyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("IsNullOrEmpty"); var newExpression = GetNewExpression(expression, remove); var rename = NodeReplacement.Rename("BeFalse", "NotBeNullOrEmpty"); newExpression = GetNewExpression(newExpression, rename); var stringKeyword = newExpression.DescendantNodes().OfType <PredefinedTypeSyntax>().Single(); var subject = remove.Arguments.First().Expression; return(newExpression.ReplaceNode(stringKeyword, subject.WithTriviaFrom(stringKeyword))); } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var remove = NodeReplacement.RemoveAndExtractArguments("OrderByDescending"); var newExpression = GetNewExpression(expression, remove); newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInDescendingOrder")); return(GetNewExpression(newExpression, NodeReplacement.PrependArguments("BeInDescendingOrder", remove.Arguments))); }
private ExpressionSyntax ReplaceBeMessage(ExpressionSyntax expression, string whichOrAnd) { var replacements = new[] { NodeReplacement.Remove(whichOrAnd), NodeReplacement.Remove("Message"), NodeReplacement.RemoveOccurrence("Should", occurrence: 2), NodeReplacement.Rename("Be", "WithMessage") }; return(GetNewExpression(expression, replacements)); }
private ExpressionSyntax ReplaceWithInnerException(ExpressionSyntax expression, string whichOrAnd, string renameFrom, string renameTo) { var replacements = new[] { NodeReplacement.Remove(whichOrAnd), NodeReplacement.Remove("InnerException"), NodeReplacement.RemoveOccurrence("Should", occurrence: 2) }; var newExpression = GetNewExpression(expression, replacements); var rename = NodeReplacement.RenameAndExtractArguments(renameFrom, renameTo); return(GetNewExpression(newExpression, rename)); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var remove = NodeReplacement.RemoveAndExtractArguments("IsNullOrEmpty"); var newExpression = GetNewExpression(expression, remove); var rename = NodeReplacement.Rename("BeTrue", "BeNullOrEmpty"); newExpression = GetNewExpression(newExpression, rename); var stringKeyword = newExpression.DescendantNodes().OfType <PredefinedTypeSyntax>().Single(); var subject = remove.Arguments.First().Expression; return(newExpression.ReplaceNode(stringKeyword, subject.WithTriviaFrom(stringKeyword))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { switch (properties.VisitorName) { case nameof(CollectionShouldBeEmptyAnalyzer.AnyShouldBeFalseSyntaxVisitor): return(GetNewExpression(expression, NodeReplacement.Remove("Any"), NodeReplacement.Rename("BeFalse", "BeEmpty"))); case nameof(CollectionShouldBeEmptyAnalyzer.ShouldHaveCount0SyntaxVisitor): return(GetNewExpression(expression, new HaveCountNodeReplacement())); default: throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); } }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var remove = NodeReplacement.RemoveAndExtractArguments("Abs"); var newExpression = GetNewExpression(expression, remove); var subtractExpression = (BinaryExpressionSyntax)remove.Arguments[0].Expression; var actual = subtractExpression.Right as IdentifierNameSyntax; var expected = subtractExpression.Left; newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeLessOrEqualTo", "BeApproximately", new SeparatedSyntaxList <ArgumentSyntax>().Add(SyntaxFactory.Argument(expected)))); newExpression = RenameIdentifier(newExpression, "Math", actual.Identifier.Text); return(newExpression); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.CountShouldBe0SyntaxVisitor)) { return(GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "BeEmpty"))); } else if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.CountShouldBe1SyntaxVisitor)) { return(GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "ContainSingle"))); } else if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.CountShouldBeSyntaxVisitor)) { return(GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"))); } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var newExpression = GetNewExpression(expression, NodeReplacement.RenameAndRemoveFirstArgument("HaveCount", "ContainSingle")); if (properties.VisitorName == nameof(CollectionShouldContainSingleAnalyzer.ShouldHaveCount1SyntaxVisitor)) { return(newExpression); } else if (properties.VisitorName == nameof(CollectionShouldContainSingleAnalyzer.WhereShouldHaveCount1SyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("Where"); newExpression = GetNewExpression(newExpression, remove); return(GetNewExpression(newExpression, NodeReplacement.PrependArguments("ContainSingle", remove.Arguments))); } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { if (properties.VisitorName == nameof(CollectionShouldNotBeNullOrEmptyAnalyzer.ShouldNotBeNullAndNotBeEmptySyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("NotBeEmpty"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("NotBeNull", "NotBeNullOrEmpty", remove.Arguments))); } else if (properties.VisitorName == nameof(CollectionShouldNotBeNullOrEmptyAnalyzer.ShouldNotBeEmptyAndNotBeNullSyntaxVisitor)) { var remove = NodeReplacement.RemoveAndExtractArguments("NotBeNull"); var newExpression = GetNewExpression(expression, remove); return(GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("NotBeEmpty", "NotBeNullOrEmpty", remove.Arguments))); } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { var removeMethodContainingFirstLambda = NodeReplacement.RemoveAndExtractArguments("Select"); var newExpression = GetNewExpression(expression, removeMethodContainingFirstLambda); var removeArgument = NodeReplacement.RemoveFirstArgument("Equal"); newExpression = GetNewExpression(newExpression, removeArgument); var argumentInvocation = (InvocationExpressionSyntax)removeArgument.Argument.Expression; var identifier = ((MemberAccessExpressionSyntax)argumentInvocation.Expression).Expression; var firstLambda = (SimpleLambdaExpressionSyntax)removeMethodContainingFirstLambda.Arguments[0].Expression; var secondLambda = (SimpleLambdaExpressionSyntax)argumentInvocation.ArgumentList.Arguments[0].Expression; var newArguments = SyntaxFactory.SeparatedList <ArgumentSyntax>() .Add(removeArgument.Argument.WithExpression(identifier)) .Add(removeArgument.Argument.WithExpression(CombineLambdas(firstLambda, secondLambda).NormalizeWhitespace() )); return(GetNewExpression(newExpression, NodeReplacement.PrependArguments("Equal", newArguments))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { return(GetNewExpression(expression, NodeReplacement.Remove("Any"), NodeReplacement.Rename("BeTrue", "NotBeEmpty"))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { return(GetNewExpression(expression, NodeReplacement.RenameAndRemoveFirstArgument("HaveSameCount", "OnlyHaveUniqueItems"))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { return(GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeLessThan", "HaveCountLessThan"))); }
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) { return(GetNewExpression(expression, NodeReplacement.RenameAndRemoveFirstArgument("BeGreaterThan", "BePositive"))); }