Ejemplo n.º 1
0
        private string GetMethodName(StepInstance stepInstance, AnalyzedStepText analyzedStepText, StepDefinitionSkeletonStyle style, ProgrammingLanguage language)
        {
            var keyword = LanguageHelper.GetDefaultKeyword(stepInstance.StepContext.Language, stepInstance.StepDefinitionType);

            switch (style)
            {
            case StepDefinitionSkeletonStyle.RegexAttribute:
                return(keyword.ToIdentifier() + string.Concat(analyzedStepText.TextParts.ToArray()).ToIdentifier());

            case StepDefinitionSkeletonStyle.MethodNameUnderscores:
                return(GetMatchingMethodName(keyword, analyzedStepText, stepInstance.StepContext.Language, AppendWordsUnderscored, "_{0}"));

            case StepDefinitionSkeletonStyle.MethodNamePascalCase:
                return(GetMatchingMethodName(keyword, analyzedStepText, stepInstance.StepContext.Language, AppendWordsPascalCase, "_{0}_"));

            case StepDefinitionSkeletonStyle.MethodNameRegex:
                if (language != ProgrammingLanguage.FSharp)
                {
                    goto case StepDefinitionSkeletonStyle.MethodNameUnderscores;
                }
                return("``" + GetRegex(analyzedStepText) + "``");

            default:
                throw new NotSupportedException();
            }
        }
Ejemplo n.º 2
0
        private string GetRegex(AnalyzedStepText stepText)
        {
            StringBuilder result = new StringBuilder();

            result.Append(EscapeRegex(stepText.TextParts[0]));
            for (int i = 1; i < stepText.TextParts.Count; i++)
            {
                result.AppendFormat("({0})", stepText.Parameters[i - 1].RegexPattern);
                result.Append(EscapeRegex(stepText.TextParts[i]));
            }

            return(result.ToString());
        }
Ejemplo n.º 3
0
        public AnalyzedStepText Analyze(string stepText, CultureInfo bindingCulture)
        {
            var result = new AnalyzedStepText();

            var paramMatches = RecognizeQuotedTexts(stepText)
                               .Concat(RecognizeDates(stepText))
                               .Concat(RecognizeIntegers(stepText))
                               .Concat(RecognizeDecimals(stepText, bindingCulture))
                               .OrderBy(m => m.Capture.Index)
                               .ThenByDescending(m => m.Capture.Length);

            int textIndex = 0;

            foreach (var paramMatch in paramMatches)
            {
                if (paramMatch.Capture.Index < textIndex)
                {
                    continue;
                }

                const string singleQuoteRegexPattern = "[^']*";
                const string doubleQuoteRegexPattern = "[^\"\"]*";
                const string defaultRegexPattern     = ".*";

                string regexPattern = defaultRegexPattern;
                string value        = paramMatch.Capture.Value;
                int    index        = paramMatch.Capture.Index;

                switch (value.Substring(0, Math.Min(value.Length, 1)))
                {
                case "\"":
                    regexPattern = doubleQuoteRegexPattern;
                    value        = value.Substring(1, value.Length - 2);
                    index++;
                    break;

                case "'":
                    regexPattern = singleQuoteRegexPattern;
                    value        = value.Substring(1, value.Length - 2);
                    index++;
                    break;
                }

                result.TextParts.Add(stepText.Substring(textIndex, index - textIndex));
                result.Parameters.Add(AnalyzeParameter(value, bindingCulture, result.Parameters.Count, regexPattern, paramMatch.ParameterType));
                textIndex = index + value.Length;
            }

            result.TextParts.Add(stepText.Substring(textIndex));
            return(result);
        }
        public void Setup()
        {
            templateProviderMock = new Mock<ISkeletonTemplateProvider>();
            templateProviderMock.Setup(tp => tp.GetStepDefinitionClassTemplate(It.IsAny<ProgrammingLanguage>()))
                .Returns("{namespace}/{className}/{bindings}");
            templateProviderMock.Setup(tp => tp.GetStepDefinitionTemplate(It.IsAny<ProgrammingLanguage>(), true))
                .Returns("{attribute}/{regex}/{methodName}/{parameters}");
            templateProviderMock.Setup(tp => tp.GetStepDefinitionTemplate(It.IsAny<ProgrammingLanguage>(), false))
                .Returns("{attribute}/{methodName}/{parameters}");

            analizeResult = new AnalyzedStepText();
            stepTextAnalyzerMock = new Mock<IStepTextAnalyzer>();
            stepTextAnalyzerMock.Setup(a => a.Analyze(It.IsAny<string>(), It.IsAny<CultureInfo>()))
                .Returns(analizeResult);
        }
Ejemplo n.º 5
0
        private string GetMatchingMethodName(string keyword, AnalyzedStepText analyzedStepText, CultureInfo language, Action <string, CultureInfo, StringBuilder> appendWords, string paramFormat)
        {
            StringBuilder result = new StringBuilder(keyword);

            appendWords(analyzedStepText.TextParts[0], language, result);
            for (int i = 1; i < analyzedStepText.TextParts.Count; i++)
            {
                result.AppendFormat(paramFormat, analyzedStepText.Parameters[i - 1].Name.ToUpper(CultureInfo.InvariantCulture));
                appendWords(analyzedStepText.TextParts[i], language, result);
            }

            if (result.Length > 0 && result[result.Length - 1] == '_')
            {
                result.Remove(result.Length - 1, 1);
            }

            return(result.ToString());
        }
Ejemplo n.º 6
0
        public AnalyzedStepText Analyze(string stepText, CultureInfo bindingCulture)
        {
            var result = new AnalyzedStepText();

            var paramMatches = RecognizeQuotedTexts(stepText).Concat(RecognizeIntegers(stepText)).Concat(RecognizeDecimals(stepText, bindingCulture))
                .OrderBy(m => m.Index).ThenByDescending(m => m.Length);

            int textIndex = 0;
            foreach (var paramMatch in paramMatches)
            {
                if (paramMatch.Index < textIndex)
                    continue;

                const string singleQuoteRegexPattern = "[^']*";
                const string doubleQuoteRegexPattern = "[^\"\"]*";
                const string defaultRegexPattern = ".*";

                string regexPattern = defaultRegexPattern;
                string value = paramMatch.Value;
                int index = paramMatch.Index;

                switch (value.Substring(0, 1))
                {
                    case "\"":
                        regexPattern = doubleQuoteRegexPattern;
                        value = value.Substring(1, value.Length - 2);
                        index++;
                        break;
                    case "'":
                        regexPattern = singleQuoteRegexPattern;
                        value = value.Substring(1, value.Length - 2);
                        index++;
                        break;
                }

                result.TextParts.Add(stepText.Substring(textIndex, index - textIndex));
                result.Parameters.Add(AnalyzeParameter(value, bindingCulture, result.Parameters.Count, regexPattern));
                textIndex = index + value.Length;
            }

            result.TextParts.Add(stepText.Substring(textIndex));
            return result;
        }
 private string GetMethodName(StepInstance stepInstance, AnalyzedStepText analyzedStepText, StepDefinitionSkeletonStyle style, ProgrammingLanguage language)
 {
     var keyword = LanguageHelper.GetDefaultKeyword(stepInstance.StepContext.Language, stepInstance.StepDefinitionType);
     switch (style)
     {
         case StepDefinitionSkeletonStyle.RegexAttribute:
             return keyword.ToIdentifier() + string.Concat(analyzedStepText.TextParts.ToArray()).ToIdentifier();
         case StepDefinitionSkeletonStyle.MethodNameUnderscores:
             return GetMatchingMethodName(keyword, analyzedStepText, stepInstance.StepContext.Language, AppendWordsUnderscored, "_{0}");
         case StepDefinitionSkeletonStyle.MethodNamePascalCase:
             return GetMatchingMethodName(keyword, analyzedStepText, stepInstance.StepContext.Language, AppendWordsPascalCase, "_{0}_");
         case StepDefinitionSkeletonStyle.MethodNameRegex:
             if (language != ProgrammingLanguage.FSharp)
                 goto case StepDefinitionSkeletonStyle.MethodNameUnderscores;
             return "``" + GetRegex(analyzedStepText) + "``";
         default:
             throw new NotSupportedException();
     }
 }
Ejemplo n.º 8
0
        public AnalyzedStepText Analyze(string stepText, CultureInfo bindingCulture)
        {
            var result = new AnalyzedStepText();

            var paramMatches = RecognizeQuotedTexts(stepText).Concat(RecognizeIntegers(stepText)).Concat(RecognizeDecimals(stepText, bindingCulture))
                .OrderBy(m => m.Index).ThenByDescending(m => m.Length);

            int textIndex = 0;
            foreach (var paramMatch in paramMatches)
            {
                if (paramMatch.Index < textIndex)
                    continue;

                result.TextParts.Add(stepText.Substring(textIndex, paramMatch.Index - textIndex));
                result.Parameters.Add(AnalyzeParameter(paramMatch.Value, bindingCulture, result.Parameters.Count));
                textIndex = paramMatch.Index + paramMatch.Length;
            }

            result.TextParts.Add(stepText.Substring(textIndex));
            return result;
        }
Ejemplo n.º 9
0
        public AnalyzedStepText Analyze(string stepText, CultureInfo bindingCulture)
        {
            var result = new AnalyzedStepText();

            var paramMatches = RecognizeQuotedTexts(stepText).Concat(RecognizeIntegers(stepText)).Concat(RecognizeDecimals(stepText, bindingCulture))
                               .OrderBy(m => m.Index).ThenByDescending(m => m.Length);

            int textIndex = 0;

            foreach (var paramMatch in paramMatches)
            {
                if (paramMatch.Index < textIndex)
                {
                    continue;
                }

                result.TextParts.Add(stepText.Substring(textIndex, paramMatch.Index - textIndex));
                result.Parameters.Add(AnalyzeParameter(paramMatch.Value, bindingCulture, result.Parameters.Count));
                textIndex = paramMatch.Index + paramMatch.Length;
            }

            result.TextParts.Add(stepText.Substring(textIndex));
            return(result);
        }
        private string GetMatchingMethodName(string keyword, AnalyzedStepText analyzedStepText, CultureInfo language, Action<string, CultureInfo, StringBuilder> appendWords, string paramFormat)
        {
            StringBuilder result = new StringBuilder(keyword);

            appendWords(analyzedStepText.TextParts[0], language, result);
            for (int i = 1; i < analyzedStepText.TextParts.Count; i++)
            {
                result.AppendFormat(paramFormat, analyzedStepText.Parameters[i - 1].Name.ToUpper(CultureInfo.InvariantCulture));
                appendWords(analyzedStepText.TextParts[i], language, result);
            }

            if (result.Length > 0 && result[result.Length - 1] == '_')
                result.Remove(result.Length - 1, 1);

            return result.ToString();
        }
        private string GetRegex(AnalyzedStepText stepText)
        {
            StringBuilder result = new StringBuilder();

            result.Append(EscapeRegex(stepText.TextParts[0]));
            for (int i = 1; i < stepText.TextParts.Count; i++)
            {
                result.AppendFormat("({0})", stepText.Parameters[i-1].RegexPattern);
                result.Append(EscapeRegex(stepText.TextParts[i]));
            }

            return result.ToString();
        }
        private List<string> AddStatements(CodeMemberMethod testMethod, MethodInfo methodInfo, List<string> classesDeclared, ScenarioStep step, AnalyzedStepText stepText, string regex)
        {
            if (!HasClassBeenDeclared(classesDeclared, methodInfo.DeclaringType.ToString()))
            {
                var testRunnerField = new CodeVariableDeclarationStatement(
                    methodInfo.DeclaringType, methodInfo.DeclaringType.Name.ToLower(),
                    new CodeObjectCreateExpression(methodInfo.DeclaringType));
                testMethod.Statements.Add(testRunnerField);
                classesDeclared.Add(methodInfo.DeclaringType.ToString());
            }

            ParameterInfo[] parameters = methodInfo.GetParameters();

            if (parameters.Any())
            {
                //declare any variables needed here
                //string value = step.Text
                int i = 0;
                foreach (ParameterInfo parameterInfo in parameters) //these are the parameters needed
                {
                    string parameterValue = stepText.TextParts[i];


                    Match match = Regex.Match(step.Text,regex);
                    if (match.Success)
                    {
                         parameterValue = match.Groups[1].Value;
                    }

                    bool paramAlreadyDeclared = false;

                    //check not in test case
                    foreach (CodeParameterDeclarationExpression parameter in testMethod.Parameters) //these are the ones specified by the test case attributes
                    {
                        if (parameterInfo.Name == parameter.Name)
                        {
                            paramAlreadyDeclared = true;
                        }
                    }

                    //check not already declared in body of method
                    foreach (var preDecalredParam in m_parametersDeclaredInMethod)
                    {
                        if (preDecalredParam == parameterInfo.Name)
                        {
                            paramAlreadyDeclared = true;
                        }
                    }

                    if (!paramAlreadyDeclared)
                    {
                        m_parametersDeclaredInMethod.Add(parameterInfo.Name);//record that param declared
                        CodeVariableDeclarationStatement variable = new CodeVariableDeclarationStatement(typeof(string), parameterInfo.Name, new CodePrimitiveExpression(parameterValue));
                        testMethod.Statements.Add(variable);
                    }

                }



                testMethod.Statements.Add(new CodeMethodInvokeExpression(
                                        new CodeTypeReferenceExpression(
                                            methodInfo.DeclaringType.Name.ToLower()),
                                        methodInfo.Name,
                                        GetParameters(testMethod, methodInfo)));
            }
            else
            {
                testMethod.Statements.Add(new CodeMethodInvokeExpression(
                                              new CodeTypeReferenceExpression(
                                                  methodInfo.DeclaringType.Name.ToLower()),
                                              methodInfo.Name));
            }
            return classesDeclared;
        }