Exemple #1
0
        private static void CompareGroups(PcreMatch actualMatch, ExpectedMatch expectedMatch)
        {
            var actualGroups   = actualMatch.ToList();
            var expectedGroups = expectedMatch.Groups.ToList();

            Assert.That(actualGroups.Count, Is.GreaterThanOrEqualTo(expectedGroups.Count));

            for (var groupIndex = 0; groupIndex < actualGroups.Count; ++groupIndex)
            {
                var actualGroup   = actualGroups[groupIndex];
                var expectedGroup = groupIndex < expectedGroups.Count
                    ? expectedGroups[groupIndex]
                    : ExpectedGroup.Unset;

                Console.WriteLine("    Group #{0}: {1}", groupIndex, expectedGroup.Value);

                Assert.That(actualGroup.Success, Is.EqualTo(expectedGroup.IsMatch));

                if (expectedGroup.IsMatch)
                {
                    Assert.That(actualGroup.Value, Is.EqualTo(expectedGroup.Value.UnescapeGroup()));
                }
            }
        }
Exemple #2
0
 private static void CompareRemainingString(PcreMatch actualMatch, ExpectedMatch expectedMatch)
 {
     Assert.That(actualMatch.Subject.Substring(actualMatch.Index + actualMatch.Length), Is.EqualTo(expectedMatch.RemainingString.UnescapeGroup()));
 }
Exemple #3
0
 private static void CompareMark(PcreMatch actualMatch, ExpectedMatch expectedMatch)
 {
     Assert.That(actualMatch.Mark, Is.EqualTo(expectedMatch.Mark.UnescapeGroup()));
 }
Exemple #4
0
 private static void CompareMark(PcreRefMatch actualMatch, ExpectedMatch expectedMatch)
 => Assert.That(actualMatch.Mark.ToString(), Is.EqualTo(expectedMatch.Mark.UnescapeGroup() ?? string.Empty));
Exemple #5
0
        private static void CompareGroups(TestPattern pattern, PcreRefMatch actualMatch, ExpectedMatch expectedMatch)
        {
            var expectedGroups = expectedMatch.Groups.ToList();

            Assert.That(actualMatch.Groups.Count, Is.GreaterThanOrEqualTo(expectedGroups.Count));

            for (var groupIndex = 0; groupIndex < actualMatch.Groups.Count; ++groupIndex)
            {
                var actualGroup   = actualMatch.Groups[groupIndex];
                var expectedGroup = groupIndex < expectedGroups.Count
                    ? expectedGroups[groupIndex]
                    : ExpectedGroup.Unset;

                Assert.That(actualGroup.Success, Is.EqualTo(expectedGroup.IsMatch));

                if (expectedGroup.IsMatch)
                {
                    var expectedValue = pattern.SubjectLiteral
                        ? expectedGroup.Value
                        : expectedGroup.Value.UnescapeGroup();

                    Assert.That(actualGroup.Value.ToString(), Is.EqualTo(expectedValue));
                }
            }
        }
        public IEnumerable <TestOutput> ReadTestOutputs()
        {
            while (true)
            {
                var pattern = ReadNextPattern();

                if (pattern == null)
                {
                    yield break;
                }

                var testOutput = new TestOutput
                {
                    Pattern = pattern
                };

                ExpectedResult currentResult = null;
                ExpectedMatch  currentMatch  = null;

                while (true)
                {
                    var line = ReadLine();

                    if (line == _separator)
                    {
                        do
                        {
                            line = ReadLine();
                        } while (line != _separator && line != null);
                        line = ReadLine();
                    }

                    if (string.IsNullOrWhiteSpace(line))
                    {
                        break;
                    }

                    if (pattern.ReplaceWith != null) // Not supported yet
                    {
                        while (!string.IsNullOrWhiteSpace(line))
                        {
                            line = ReadLine();
                        }
                        break;
                    }

                    if (currentResult != null)
                    {
                        if (line.StartsWith("No match"))
                        {
                            currentResult = null;
                            currentMatch  = null;
                            continue;
                        }

                        // Capture
                        var match = Regex.Match(line, @"^\s*(\d+): (.*)$");
                        if (match.Success)
                        {
                            var groupIndex = int.Parse(match.Groups[1].Value);

                            if (groupIndex == 0)
                            {
                                currentMatch = new ExpectedMatch();
                                currentResult.Matches.Add(currentMatch);
                            }

                            if (currentMatch == null)
                            {
                                throw InvalidInputException("Group outside of match");
                            }

                            if (groupIndex != currentMatch.Groups.Count)
                            {
                                throw InvalidInputException("Invalid group index");
                            }

                            currentMatch.Groups.Add(match.Groups[2].Value == "<unset>"
                                ? ExpectedGroup.Unset
                                : new ExpectedGroup(match.Groups[2].Value));

                            continue;
                        }

                        if (pattern.GetRemainingString)
                        {
                            match = Regex.Match(line, @"^\s*(\d+)\+ (.*)$");
                            if (match.Success && currentMatch != null)
                            {
                                currentMatch.RemainingString = match.Groups[2].Value;
                                continue;
                            }
                        }

                        if (pattern.ExtractMarks)
                        {
                            match = Regex.Match(line, @"^\s*MK: (.*)$");
                            if (match.Success && currentMatch != null)
                            {
                                currentMatch.Mark = match.Groups[1].Value;
                                continue;
                            }
                        }
                    }

                    currentResult = new ExpectedResult
                    {
                        SubjectLine = line.Trim()
                    };

                    currentMatch = null;

                    testOutput.ExpectedResults.Add(currentResult);
                    continue;
                }

                yield return(testOutput);
            }
        }