protected override void AnalyzeAssertInvocation(OperationAnalysisContext context, IInvocationOperation assertOperation) { var negated = false; if (SupportedNegativeAssertMethods.Contains(assertOperation.TargetMethod.Name)) { negated = true; } else if (!SupportedPositiveAssertMethods.Contains(assertOperation.TargetMethod.Name)) { return; } var constraintExpression = assertOperation.GetArgumentOperation(NameOfExpressionParameter); // Constraint should be either absent, or Is.True, or Is.False if (constraintExpression != null) { if (!(constraintExpression is IPropertyReferenceOperation propertyReference && propertyReference.Property.ContainingType.Name == NameOfIs && (propertyReference.Property.Name == NameOfIsTrue || propertyReference.Property.Name == NameOfIsFalse))) { return; } if (propertyReference.Property.Name == NameOfIsFalse) { negated = !negated; } } var actual = assertOperation.GetArgumentOperation(NameOfActualParameter) ?? assertOperation.GetArgumentOperation(NameOfConditionParameter); if (actual == null) { return; } if (IsPrefixNotOperation(actual, out var unwrappedActual)) { negated = !negated; } var(descriptor, suggestedConstraint, swapOperands) = this.GetDiagnosticDataWithPossibleSwapOperands(context, unwrappedActual ?? actual, negated); if (descriptor != null && suggestedConstraint != null) { var properties = ImmutableDictionary.CreateBuilder <string, string?>(); properties.Add(SuggestedConstraintString, suggestedConstraint); properties.Add(SwapOperands, swapOperands.ToString()); var diagnostic = Diagnostic.Create(descriptor, actual.Syntax.GetLocation(), properties.ToImmutable(), suggestedConstraint); context.ReportDiagnostic(diagnostic); } }
protected override void AnalyzeAssertInvocation(OperationAnalysisContext context, IInvocationOperation assertOperation) { IOperation?actualOperation; IOperation?expectedOperation; if (assertOperation.TargetMethod.Name.Equals(NunitFrameworkConstants.NameOfAssertAreEqual, StringComparison.Ordinal) || assertOperation.TargetMethod.Name.Equals(NunitFrameworkConstants.NameOfAssertAreNotEqual, StringComparison.Ordinal)) { actualOperation = assertOperation.GetArgumentOperation(NunitFrameworkConstants.NameOfActualParameter); expectedOperation = assertOperation.GetArgumentOperation(NunitFrameworkConstants.NameOfExpectedParameter); CheckActualVsExpectedOperation(context, actualOperation, expectedOperation); } else { if (!AssertHelper.TryGetActualAndConstraintOperations(assertOperation, out actualOperation, out var constraintExpression)) { return; } foreach (var constraintPartExpression in constraintExpression.ConstraintParts) { if (constraintPartExpression.HasIncompatiblePrefixes() || HasCustomEqualityComparer(constraintPartExpression) || constraintPartExpression.HasUnknownExpressions()) { return; } var constraintMethod = constraintPartExpression.GetConstraintMethod(); if (constraintMethod?.Name != NunitFrameworkConstants.NameOfIsEqualTo || constraintMethod.ReturnType?.GetFullMetadataName() != NunitFrameworkConstants.FullNameOfEqualToConstraint) { continue; } expectedOperation = constraintPartExpression.GetExpectedArgument(); CheckActualVsExpectedOperation(context, actualOperation, expectedOperation); } } }