Пример #1
0
        public void TryParsePassesResult(bool innerSuccess, string innerRemaining, string result)
        {
            var inner = StringParserTestUtils.MockParser("stat", innerSuccess, innerRemaining, result).Object;
            var sut   = new ValidatingParser <string>(inner);

            var(_, _, actual) = sut.Parse("stat");

            Assert.AreEqual(result, actual);
        }
Пример #2
0
        public string TryParseReturnsCorrectRemaining(string innerRemaining)
        {
            var inner = StringParserTestUtils.MockParser("stat", true, innerRemaining, "").Object;
            var sut   = new ValidatingParser <string>(inner);

            var(_, actual, _) = sut.Parse("stat");

            return(actual);
        }
Пример #3
0
        public void TryParseReturnsCorrectUnchangedRemaining(string remaining)
        {
            var inner = StringParserTestUtils.MockParser("stat", true, remaining, "").Object;
            var sut   = new ValidatingParser <string>(inner);

            var(_, actual, _) = sut.Parse("stat");

            Assert.AreEqual(remaining, actual);
        }
Пример #4
0
        public void TryParseReturnsSuccessIfRemainingIsOnlyWhiteSpaceAndEndsWithHidden()
        {
            const string remaining = " \n\t\r" + ItemConstants.HiddenStatSuffix;
            var          inner     = StringParserTestUtils.MockParser("stat", true, remaining, "").Object;
            var          sut       = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.True(actual);
        }
Пример #5
0
        public void TryParseReturnsFailureIfRemainingStartsWithHidden()
        {
            const string remaining = ItemConstants.HiddenStatSuffix + " ";
            var          inner     = StringParserTestUtils.MockParser("stat", true, remaining, "").Object;
            var          sut       = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.False(actual);
        }
Пример #6
0
        public void TryParseReturnsFailureIfRemainingContainsNonWhiteSpace()
        {
            const string remaining = " r ";
            var          inner     = StringParserTestUtils.MockParser("stat", true, remaining, "").Object;
            var          sut       = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.False(actual);
        }
Пример #7
0
        public void TryParseReturnsFailureIfInnerReturnsFailure()
        {
            const string remaining = " ";
            var          inner     = StringParserTestUtils.MockParser("stat", false, remaining, "").Object;
            var          sut       = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.False(actual);
        }
Пример #8
0
        public void TryParseCleansIfInnerReturnsFalse()
        {
            const string innerRemaining = " \ntest\t\r " + ItemConstants.HiddenStatSuffix;
            var          inner          = StringParserTestUtils.MockParser("stat", true, innerRemaining, "").Object;
            var          sut            = new ValidatingParser <string>(inner);

            var(_, actual, _) = sut.Parse("stat");

            Assert.AreEqual("test", actual);
        }
Пример #9
0
        public void TryParseHiddenTestIsCaseInsensitive()
        {
            var remaining = ItemConstants.HiddenStatSuffix.ToLowerInvariant();
            var inner     = StringParserTestUtils.MockParser("stat", true, remaining, "").Object;
            var sut       = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.True(actual);
        }
Пример #10
0
        public void TryParsePassesResult(bool innerSuccess, string innerRemaining, string result)
        {
            var inner = Mock.Of <IParser <string> >(p =>
                                                    p.Parse("stat") == new ParseResult <string>(innerSuccess, innerRemaining, result));
            var sut = new ValidatingParser <string>(inner);

            var(_, _, actual) = sut.Parse("stat");

            Assert.AreEqual(result, actual);
        }
Пример #11
0
        public string TryParseReturnsCorrectRemaining(string innerRemaining)
        {
            var inner = Mock.Of <IParser <string> >(p =>
                                                    p.Parse("stat") == new ParseResult <string>(true, innerRemaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(_, actual, _) = sut.Parse("stat");

            return(actual);
        }
Пример #12
0
        public void TryParseReturnsCorrectUnchangedRemaining(string remaining)
        {
            var inner = Mock.Of <IParser <string> >(p =>
                                                    p.Parse("stat") == new ParseResult <string>(true, remaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(_, actual, _) = sut.Parse("stat");

            Assert.AreEqual(remaining, actual);
        }
Пример #13
0
        public void TryParseReturnsFailureIfRemainingStartsWithHidden()
        {
            const string remaining = ItemConstants.HiddenStatSuffix + " ";
            var          inner     = Mock.Of <IParser <string> >(p =>
                                                                 p.Parse("stat") == new ParseResult <string>(true, remaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.False(actual);
        }
Пример #14
0
        public void TryParseReturnsFailureIfRemainingContainsNonWhiteSpace()
        {
            const string remaining = " r ";
            var          inner     = Mock.Of <IParser <string> >(p =>
                                                                 p.Parse("stat") == new ParseResult <string>(true, remaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.False(actual);
        }
Пример #15
0
        public void TryParseReturnsFailureIfInnerReturnsFailure()
        {
            const string remaining = " ";
            var          inner     = Mock.Of <IParser <string> >(p =>
                                                                 p.Parse("stat") == new ParseResult <string>(false, remaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.False(actual);
        }
Пример #16
0
        public void TryParseCleansIfInnerReturnsFalse()
        {
            const string innerRemaining = " \ntest\t\r " + ItemConstants.HiddenStatSuffix;
            var          inner          = Mock.Of <IParser <string> >(p =>
                                                                      p.Parse("stat") == new ParseResult <string>(true, innerRemaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(_, actual, _) = sut.Parse("stat");

            Assert.AreEqual("test", actual);
        }
Пример #17
0
        public void TryParseReturnsSuccessIfRemainingIsOnlyWhiteSpaceAndEndsWithHidden()
        {
            const string remaining = " \n\t\r" + ItemConstants.HiddenStatSuffix;
            var          inner     = Mock.Of <IParser <string> >(p =>
                                                                 p.Parse("stat") == new ParseResult <string>(true, remaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.True(actual);
        }
Пример #18
0
        public void TryParseHiddenTestIsCaseInsensitive()
        {
            var remaining = ItemConstants.HiddenStatSuffix.ToLowerInvariant();
            var inner     = Mock.Of <IParser <string> >(p =>
                                                        p.Parse("stat") == new ParseResult <string>(true, remaining, default));
            var sut = new ValidatingParser <string>(inner);

            var(actual, _, _) = sut.Parse("stat");

            Assert.True(actual);
        }
Пример #19
0
        private Parser CreateParser()
        {
            var referenceService  = new ReferenceService(_parsingData.ReferencedMatchers, _parsingData.StatMatchers);
            var regexGroupService = new RegexGroupService(_builderFactories.ValueBuilders);

            // The parsing pipeline using one IStatMatchers instance to parse a part of the stat.
            IStringParser <IIntermediateModifier> CreateInnerParser(IStatMatchers statMatchers) =>
            new CachingStringParser <IIntermediateModifier>(
                new StatNormalizingParser <IIntermediateModifier>(
                    new ResolvingParser(
                        MatcherDataParser.Create(
                            statMatchers.Data,
                            new StatMatcherRegexExpander(
                                referenceService, regexGroupService, statMatchers.MatchesWholeLineOnly).Expand),
                        referenceService,
                        new IntermediateModifierResolver(ModifierBuilder.Empty),
                        regexGroupService
                        )
                    )
                );

            Parallel.ForEach(_parsingData.StatMatchers, s => { _ = s.Data; });
            var innerParserCache = _parsingData.StatMatchers.ToDictionary(Funcs.Identity, CreateInnerParser);

            // The steps define the order in which the inner parsers, and by extent the IStatMatchers, are executed.
            IStringParser <IIntermediateModifier> StepToParser(TStep step)
            => innerParserCache[_parsingData.SelectStatMatcher(step)];

            // The full parsing pipeline.
            IStringParser <IReadOnlyList <IReadOnlyList <IIntermediateModifier> > > parser =
                new ValidatingParser <IReadOnlyList <IReadOnlyList <IIntermediateModifier> > >(
                    new StatNormalizingParser <IReadOnlyList <IReadOnlyList <IIntermediateModifier> > >(
                        new StatReplacingParser <IReadOnlyList <IIntermediateModifier> >(
                            new CompositeParser <IIntermediateModifier, TStep>(_parsingData.Stepper, StepToParser),
                            _parsingData.StatReplacers
                            )
                        )
                    );

            return(ps => AggregateAndBuild(ps, parser.Parse(ps.ModifierLine)));
        }