Example #1
0
        private bool ValidateParameterNames(Action <Diagnostic> reportError, List <string> parameters)
        {
            if (parameters.Count <= 1)
            {
                // 0 or 1 parameter: the parameter name is not mandatory
                if (CasesAndOr.CasesAnd.Count == 1 &&
                    CasesAndOr.CasesAnd[0].Cases.Count == 1 &&
                    CasesAndOr.CasesAnd.First().Cases.Keys.First() == Case.NoParameter)
                {
                    return(true);
                }
            }

            var result = true;

            foreach (var casesAnd in CasesAndOr.CasesAnd)
            {
                var unusedParameters = parameters.ToList();
                var hasAnonymousCase = false;
                foreach (var aCase in casesAnd.Cases.Values)
                {
                    if (aCase.ParameterName == Case.NoParameter)
                    {
                        hasAnonymousCase = true;
                        continue;
                    }

                    if (unusedParameters.Remove(aCase.ParameterName))
                    {
                        continue;
                    }

                    // This parameter name does not exist
                    result = false;
                    reportError(SmartTestsDiagnostics.CreateWrongParameterName(TestedMember, aCase.ParameterName, aCase.ParameterNameExpression));
                }

                if (casesAnd.HasError)
                {
                    continue;
                }

                // Remaining parameters have no Case!
                if (parameters.Count == 1 &&
                    hasAnonymousCase)
                {
                    // When 1 parameter, parameter name is not mandatory
                    continue;
                }

                foreach (var parameter in unusedParameters)
                {
                    result = false;
                    reportError(SmartTestsDiagnostics.CreateMissingParameterCase(TestedMember, parameter, casesAnd.Cases.First().Value.CaseExpressions.First()));
                }
            }

            return(result);
        }
Example #2
0
        private bool ValidateParameters(Action <Diagnostic> reportError, List <Tuple <string, ITypeSymbol> > testedParameters)
        {
            if (testedParameters.Count <= 1)
            {
                // 0 or 1 parameter: the parameter name is not mandatory
                if (CasesAndOr.CasesAnd.Count == 1 &&
                    CasesAndOr.CasesAnd[0].Cases.Count == 1 &&
                    CasesAndOr.CasesAnd.First().Cases.Values.First().TestedParameter.Name == Case.NoParameter)
                {
                    return(true);
                }
            }

            var result = true;

            foreach (var casesAnd in CasesAndOr.CasesAnd)
            {
                var untestedParameters = testedParameters.ToDictionary(p => p.Item1);
                var hasAnonymousCase   = false;
                foreach (var aCase in casesAnd.Cases.Values)
                {
                    if (aCase.TestedParameter.Name == Case.NoParameter)
                    {
                        hasAnonymousCase = true;
                        continue;
                    }

                    if (!untestedParameters.TryGetValue(aCase.TestedParameter.Name, out var testedParameter))
                    {
                        // This parameter name does not exist
                        result = false;
                        reportError(SmartTestsDiagnostics.CreateWrongParameterName(TestedMember, aCase.ParameterName, aCase.TestedParameter.Expression));
                        continue;
                    }

                    // This parameter is tested
                    if (aCase.TestedParameter.IsLambda)
                    {
                        if (testedParameter.Item2 != null &&
                            !testedParameter.Item2.Equals(aCase.TestedParameter.Type))
                        {
                            // This parameter type is not the right one
                            result = false;
                            reportError(SmartTestsDiagnostics.CreateWrongParameterType(TestedMember, aCase.ParameterName, aCase.TestedParameter.Type.ToString(), aCase.TestedParameter.Expression));
                        }

                        if (aCase.TestedParameter.PathHasErrors)
                        {
                            result = false;
                            reportError(SmartTestsDiagnostics.CreateWrongParameterPath(TestedMember, aCase.ParameterName, aCase.TestedParameter.Path, aCase.TestedParameter.PathExpression));
                        }
                    }

                    untestedParameters.Remove(aCase.TestedParameter.Name);
                }

                if (casesAnd.HasError)
                {
                    continue;
                }

                // Remaining parameters have no Case!
                if (testedParameters.Count == 1 &&
                    hasAnonymousCase)
                {
                    // When 1 parameter, parameter name is not mandatory
                    continue;
                }

                foreach (var parameter in untestedParameters)
                {
                    result = false;
                    reportError(SmartTestsDiagnostics.CreateMissingParameterCase(TestedMember, parameter.Key, casesAnd.Cases.First().Value.CaseExpressions.First()));
                }
            }

            return(result);
        }