Example #1
0
        private static string?FindTypeNameForIdentifierExpressionInTestMethodScope(AstNode astNode, string parameterName)
        {
            var s             = astNode.ToString();
            var stopRecursive = astNode.Parent == null ||
                                s.StartsWith("[Fact]", StringComparison.Ordinal) ||
                                s.StartsWith("[Theory]", StringComparison.Ordinal);

            var index = s.IndexOf(" " + parameterName, StringComparison.Ordinal);

            if (index != -1 &&
                (index <= parameterName.Length + 1 ||
                 !AllowedCharsBeforeParameterNameSpace.Contains(s[index - 1])))
            {
                var ix = index + 1 + parameterName.Length;
                if (ix < s.Length)
                {
                    var c = s[ix];
                    if (AllowedCharsAfterParameterName.Contains(c))
                    {
                        var astNodeForParameter = DecompilerMethodHelper.GetAstNodeForParameter(astNode, parameterName);
                        if (astNodeForParameter != null)
                        {
                            var tmp = astNodeForParameter.Parent.FirstChild.ToString().Trim();
                            if (tmp.StartsWith('[') && tmp.EndsWith(']'))
                            {
                                // FirstChild is a DataAnnotation attribute, then take the next one
                                tmp = astNodeForParameter.Parent.Children.ToArray()[1].ToString().Trim();
                            }
                            else if (string.Equals(tmp, parameterName, StringComparison.Ordinal))
                            {
                                tmp = astNodeForParameter.Parent.Parent.FirstChild.ToString().Trim();
                            }

                            return(tmp);
                        }
                    }
                }
            }

            if (stopRecursive)
            {
                return(null);
            }

            if (astNode.Parent == null)
            {
                return(null);
            }

            return(FindTypeNameForIdentifierExpressionInTestMethodScope(astNode.Parent, parameterName));
        }
        private static string?FindTypeNameForIdentifierExpressionInTestMethodScope(AstNode astNode, string parameterName)
        {
            var s             = astNode.ToString();
            var stopRecursive = astNode.Parent == null ||
                                s.StartsWith("[Fact]", StringComparison.Ordinal) ||
                                s.StartsWith("[Theory]", StringComparison.Ordinal);

            var index = s.IndexOf(" " + parameterName, StringComparison.Ordinal);

            if (index != -1 &&
                (index <= parameterName.Length + 1 ||
                 !AllowedCharsBeforeParameterNameSpace.Contains(s[index - 1])))
            {
                var ix = index + 1 + parameterName.Length;
                if (ix < s.Length)
                {
                    var c = s[ix];
                    if (AllowedCharsAfterParameterName.Contains(c))
                    {
                        var astNodeForParameter = DecompilerMethodHelper.GetAstNodeForParameter(astNode, parameterName);
                        if (astNodeForParameter != null)
                        {
                            var tmp = astNodeForParameter.Parent.FirstChild.ToString();
                            if (tmp == parameterName)
                            {
                                tmp = astNodeForParameter.Parent.Parent.FirstChild.ToString();
                            }

                            return(tmp);
                        }
                    }
                }
            }

            if (stopRecursive)
            {
                return(null);
            }

            if (astNode.Parent == null)
            {
                return(null);
            }

            return(FindTypeNameForIdentifierExpressionInTestMethodScope(astNode.Parent, parameterName));
        }