Ejemplo n.º 1
0
        public void should_url_encode(string inputText, string expectedOutput)
        {
            // given
            var processorType = VariablePostProcessorType.UrlEncode;

            // when
            var    processor  = new VariablePostProcessor();
            string outputText = processor.Process(inputText, processorType);

            // then
            Assert.That(outputText, Is.EqualTo(expectedOutput));
        }
Ejemplo n.º 2
0
        public void should_not_change_text_when_type_is_none()
        {
            // given
            var          processorType = VariablePostProcessorType.None;
            const string inputText     = "this&is&really_";

            // when
            var    processor  = new VariablePostProcessor();
            string outputText = processor.Process(inputText, processorType);

            // then
            Assert.That(outputText, Is.EqualTo(inputText));
        }
        /// <summary>
        /// Finds text in the content, returning them as variables, e.g. {capturedvariable1} = value
        /// </summary>
        public static List <Variable> MatchVariables(List <CapturedVariable> capturedVariables, string content, ITestFileRunnerLogger logger)
        {
            var variables             = new List <Variable>();
            var variablePostProcessor = new VariablePostProcessor();

            foreach (CapturedVariable regexItem in capturedVariables)
            {
                logger.WriteLine("Parsing captured variable '{{{0}}}'", regexItem.Name);
                logger.WriteLine("- Regex: {0}", regexItem.Regex);

                string capturedValue = "";
                try
                {
                    var regex = new Regex(regexItem.Regex, RegexOptions.IgnoreCase);
                    if (regex.IsMatch(content))
                    {
                        MatchCollection matches    = regex.Matches(content);
                        int             matchCount = 0;
                        foreach (Match match in matches)
                        {
                            if (match.Groups.Count > 1)
                            {
                                string detectedValue = match.Groups[1].Value;
                                logger.WriteLine($"- Detected value: {detectedValue}");

                                string transformedValue = variablePostProcessor.Process(detectedValue, regexItem.PostProcessorType);
                                logger.WriteLine($"- Transformed value: {detectedValue}");

                                capturedValue = transformedValue;
                                logger.WriteLine($"{++matchCount}. '{regexItem.Regex}' matched, updated variable to '{capturedValue}'");
                                break;
                            }

                            logger.WriteLine("- {0}. '{1}' matched, but the regex has no capture groups so the variable value wasn't updated.", ++matchCount, regexItem.Regex);
                        }
                    }
                    else
                    {
                        logger.WriteLine("- No match");
                    }
                }
                catch (ArgumentException e)
                {
                    logger.WriteLine("- Invalid regex: {0}", e.Message);
                }

                variables.Add(new Variable(regexItem.Name, capturedValue, ""));
            }

            return(variables);
        }