public void ShiftIndices()
        {
            var subject = new FormatterRequestCollection();
            subject.Add(
                new FormatterRequest(
                    new Literal(0, 9, 1, 1, new StringBuilder(new string('a', 10))), 
                    "test", 
                    "test", 
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(10, 19, 1, 1, new StringBuilder(new string('a', 10))), 
                    "test", 
                    "test", 
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(20, 29, 1, 1, new StringBuilder(new string('a', 10))), 
                    "test", 
                    "test", 
                    "test"));
            subject.ShiftIndices(1, 4);
            Assert.Equal(0, subject[0].SourceLiteral.StartIndex);
            Assert.Equal(10, subject[1].SourceLiteral.StartIndex);

            Assert.Equal(14, subject[2].SourceLiteral.StartIndex);
        }
        public void Clone()
        {
            var subject = new FormatterRequestCollection();
            subject.Add(
                new FormatterRequest(
                    new Literal(0, 9, 1, 1, new StringBuilder(new string('a', 10))), 
                    "test", 
                    "test", 
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(10, 19, 1, 1, new StringBuilder(new string('a', 10))), 
                    "test", 
                    "test", 
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(20, 29, 1, 1, new StringBuilder(new string('a', 10))), 
                    "test", 
                    "test", 
                    "test"));

            var cloned = subject.Clone();
            Assert.Equal(subject.Count, cloned.Count());

            foreach (var clonedReq in cloned)
            {
                Assert.False(subject.Any(x => ReferenceEquals(x, clonedReq)));
                Assert.False(subject.Any(x => x.SourceLiteral == clonedReq.SourceLiteral));
                Assert.True(subject.Any(x => x.SourceLiteral.StartIndex == clonedReq.SourceLiteral.StartIndex));
            }
        }
Exemple #3
0
        public void Clone()
        {
            var subject = new FormatterRequestCollection();

            subject.Add(
                new FormatterRequest(
                    new Literal(0, 9, 1, 1, new StringBuilder(new string('a', 10))),
                    "test",
                    "test",
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(10, 19, 1, 1, new StringBuilder(new string('a', 10))),
                    "test",
                    "test",
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(20, 29, 1, 1, new StringBuilder(new string('a', 10))),
                    "test",
                    "test",
                    "test"));

            var cloned = subject.Clone();

            Assert.Equal(subject.Count, cloned.Count());

            foreach (var clonedReq in cloned)
            {
                Assert.DoesNotContain(subject, x => ReferenceEquals(x, clonedReq));
                Assert.DoesNotContain(subject, x => x.SourceLiteral == clonedReq.SourceLiteral);
                Assert.Contains(subject, x => x.SourceLiteral.StartIndex == clonedReq.SourceLiteral.StartIndex);
            }
        }
Exemple #4
0
        public void ShiftIndices()
        {
            var subject = new FormatterRequestCollection();

            subject.Add(
                new FormatterRequest(
                    new Literal(0, 9, 1, 1, new StringBuilder(new string('a', 10))),
                    "test",
                    "test",
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(10, 19, 1, 1, new StringBuilder(new string('a', 10))),
                    "test",
                    "test",
                    "test"));
            subject.Add(
                new FormatterRequest(
                    new Literal(20, 29, 1, 1, new StringBuilder(new string('a', 10))),
                    "test",
                    "test",
                    "test"));
            subject.ShiftIndices(1, 4);
            Assert.Equal(0, subject[0].SourceLiteral.StartIndex);
            Assert.Equal(10, subject[1].SourceLiteral.StartIndex);

            Assert.Equal(14, subject[2].SourceLiteral.StartIndex);
        }
        /// <summary>
        ///     Parses the specified source.
        /// </summary>
        /// <param name="source">
        ///     The source.
        /// </param>
        /// <returns>
        ///     The <see cref="IFormatterRequestCollection" />.
        /// </returns>
        public IFormatterRequestCollection Parse(StringBuilder source)
        {
            var literals = this.literalParser.ParseLiterals(source).ToArray();
            if (literals.Length == 0)
            {
                return new FormatterRequestCollection();
            }

            var result = new FormatterRequestCollection();
            foreach (var literal in literals)
            {
                // The first token to follow an opening brace will be the variable name.
                int lastIndex;
                string variableName = ReadLiteralSection(literal, 0, false, out lastIndex);

                // The next (if any), is the formatter to use. Null is allowed.
                string formatterKey = null;

                // The rest of the string is what we pass into the formatter. Can be null.
                string formatterArgs = null;
                if (variableName.Length != literal.InnerText.Length)
                {
                    formatterKey = ReadLiteralSection(literal, variableName.Length + 1, true, out lastIndex);
                    if (formatterKey != null)
                    {
                        formatterArgs =
                            literal.InnerText.ToString(lastIndex + 1, literal.InnerText.Length - lastIndex - 1).Trim();
                    }
                }

                result.Add(new FormatterRequest(literal, variableName, formatterKey, formatterArgs));
            }

            return result;
        }