public void ShouldReadWhenBIsPrioritaryAndIntersectType3() { InlineParser parser; Column column; Inline[] inlines; column = new Column() { Name = "C1" }; column.InlineFormats.Add("B"); column.InlineFormats.Add("A"); parser = new InlineParser(Utils.EmptyRegexBuilder); parser.Add("NS", new InlineFormat() { Name = "B", Pattern = "6789" }); parser.Add("NS", new InlineFormat() { Name = "A", Pattern = "2345678" }); inlines = parser.Parse("01234567890").ToArray(); Assert.AreEqual(3, inlines.Length); Assert.AreEqual("012345", inlines[0].Value); Assert.AreEqual("6789", inlines[1].Value); Assert.AreEqual("0", inlines[2].Value); }
public void ShouldReadHoleAtEnd() { InlineParser parser; Column column; Inline[] inlines; column = new Column() { Name = "C1" }; column.InlineFormats.Add("Red"); column.InlineFormats.Add("Green"); column.InlineFormats.Add("Blue"); column.InlineFormats.Add("White"); column.InlineFormats.Add("Black"); parser = new InlineParser(Utils.EmptyRegexBuilder); parser.Add("NS", new InlineFormat() { Name = "Red", Pattern = "Red" }); parser.Add("NS", new InlineFormat() { Name = "Green", Pattern = "Green" }); parser.Add("NS", new InlineFormat() { Name = "Blue", Pattern = "Blue" }); parser.Add("NS", new InlineFormat() { Name = "White", Pattern = "White" }); parser.Add("NS", new InlineFormat() { Name = "Black", Pattern = "Black" }); inlines = parser.Parse("Red_Green_Blue_Black_White__").ToArray(); Assert.AreEqual(10, inlines.Length); Assert.AreEqual("Red", inlines[0].Value); Assert.AreEqual("_", inlines[1].Value); Assert.AreEqual("Green", inlines[2].Value); Assert.AreEqual("_", inlines[3].Value); Assert.AreEqual("Blue", inlines[4].Value); Assert.AreEqual("_", inlines[5].Value); Assert.AreEqual("Black", inlines[6].Value); Assert.AreEqual("_", inlines[7].Value); Assert.AreEqual("White", inlines[8].Value); Assert.AreEqual("__", inlines[9].Value); }
public void ShouldIgnoreCase() { InlineParser parser; Column column; Inline[] inlines; column = new Column() { Name = "C1" }; column.InlineFormats.Add("Red"); column.InlineFormats.Add("Green"); column.InlineFormats.Add("Blue"); column.InlineFormats.Add("White"); column.InlineFormats.Add("Black"); parser = new InlineParser(Utils.EmptyRegexBuilder); parser.Add("NS", new InlineFormat() { Name = "Red", Pattern = "red", IgnoreCase = true }); parser.Add("NS", new InlineFormat() { Name = "Green", Pattern = "grEen", IgnoreCase = true }); parser.Add("NS", new InlineFormat() { Name = "Blue", Pattern = "blUE", IgnoreCase = true }); parser.Add("NS", new InlineFormat() { Name = "White", Pattern = "wHitE", IgnoreCase = true }); parser.Add("NS", new InlineFormat() { Name = "Black", Pattern = "BLACK", IgnoreCase = true }); inlines = parser.Parse("RedGreenBlueBlackWhite").ToArray(); Assert.AreEqual(5, inlines.Length); Assert.AreEqual("Red", inlines[0].Value); Assert.AreEqual("Green", inlines[1].Value); Assert.AreEqual("Blue", inlines[2].Value); Assert.AreEqual("Black", inlines[3].Value); Assert.AreEqual("White", inlines[4].Value); }
public void ShouldReadOrderedContiguous() { InlineParser parser; Column column; Inline[] inlines; column = new Column() { Name = "C1" }; column.InlineFormats.Add("Red"); column.InlineFormats.Add("Green"); column.InlineFormats.Add("Blue"); column.InlineFormats.Add("White"); column.InlineFormats.Add("Black"); parser = new InlineParser(Utils.EmptyRegexBuilder); parser.Add("NS", new InlineFormat() { Name = "Red", Pattern = "Red" }); parser.Add("NS", new InlineFormat() { Name = "Green", Pattern = "Green" }); parser.Add("NS", new InlineFormat() { Name = "Blue", Pattern = "Blue" }); parser.Add("NS", new InlineFormat() { Name = "White", Pattern = "White" }); parser.Add("NS", new InlineFormat() { Name = "Black", Pattern = "Black" }); inlines = parser.Parse("RedGreenBlueBlackWhite").ToArray(); Assert.AreEqual(5, inlines.Length); Assert.AreEqual("Red", inlines[0].Value); Assert.AreEqual("Green", inlines[1].Value); Assert.AreEqual("Blue", inlines[2].Value); Assert.AreEqual("Black", inlines[3].Value); Assert.AreEqual("White", inlines[4].Value); }
public void Text() { /* given */ var text = new StringRange("random"); var parser = new InlineParser(); /* when */ List<Span> result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(1); result.Should().ContainItemsAssignableTo<TextSpan>(); // verify span details var span = result.Single() as TextSpan; span.Length.ShouldBeEquivalentTo(text.Length); }
public void Codeblock() { /* given */ var text = new StringRange("`bool IsCode = true;`"); var parser = new InlineParser(); /* when */ var result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(1); result.Should().OnlyContain(span => span is CodeblockSpan); // verify link details var codeSpan = result.First() as CodeblockSpan; codeSpan.ToString().ShouldBeEquivalentTo("bool IsCode = true;"); }
public void Link() { /* given */ var text = new StringRange("[title](http://something.com)"); var parser = new InlineParser(); /* when */ var result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(1); result.Should().OnlyContain(span => span is LinkSpan); // verify link details var linkSpan = result.First() as LinkSpan; linkSpan.Title.ShouldBeEquivalentTo(new StringRange(text, 1, 5)); linkSpan.Url.ShouldBeEquivalentTo(new StringRange(text, 8, 27)); }
public void TextAndLink() { /* given */ var text = new StringRange("0123 [title](http://something.com)"); var parser = new InlineParser(); /* when */ var result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(2); result.First().Should().BeOfType<TextSpan>(); result.Last().Should().BeOfType<LinkSpan>(); // verify link details var linkSpan = result.Last() as LinkSpan; linkSpan.Title.ShouldBeEquivalentTo(new StringRange(text, 6, 10)); linkSpan.Url.ShouldBeEquivalentTo(new StringRange(text, 13, 32)); }
public static IEnumerable <TResult> Parse <TSource, TResult>( this IEnumerable <TSource> source, Func <ParserQueryContext <TSource, TSource, IParser <TSource, TSource> >, ParserQueryContext <TSource, TSource, IParser <TSource, TResult> > > grammarSelector) { Contract.Requires(source != null); Contract.Requires(grammarSelector != null); Contract.Ensures(Contract.Result <IEnumerable <TResult> >() != null); var parser = new InlineParser <TSource, TResult>(); var proxyParser = (IParser <TSource, TSource>)parser; // The proxy allows the grammar author to use base methods such as Success and Failure // while still allowing type inference to work correctly; i.e., the Parse method is unavailable // and thus the type of the proxy is based solely on TSource, without requiring TResult to // be in an input position in grammarSelector. var context = new ParserQueryContext <TSource, TSource, IParser <TSource, TSource> >( proxyParser, parser.Next); var grammar = grammarSelector(context); Contract.Assume(grammar != null); IParser <TSource, TResult> start = grammar.Value; Contract.Assume(start != null); // enableBranchOptimizations must be false: See the comments in the first Parse extension for details. using (var cursor = source.ToCursor(forwardOnly: true, enableBranchOptimizations: false)) { foreach (var result in parser.Parse(cursor, start)) { yield return(result); } } }
public void Image() { /* given */ var text = new StringRange("![title](http://something.com)"); var parser = new InlineParser(); /* when */ var result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(1); result.Should().OnlyContain(span => span is ImageSpan); // verify link details var image = result.First() as ImageSpan; image.Title.ShouldBeEquivalentTo(new StringRange(text, 2, 6)); image.Url.ShouldBeEquivalentTo(new StringRange(text, 9, 28)); }
public void Combined() { /* given */ var text = new StringRange("emphasis *1234*, **1234**, link [title](link), image ![title](http)"); var parser = new InlineParser(); /* when */ var result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(12); }
public void StrongEmphasis() { /* given */ var text = new StringRange("**something important here**"); var parser = new InlineParser(); /* when */ var result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(3); result.First().Should().BeOfType<StrongEmphasis>(); result.First().Start.ShouldBeEquivalentTo(0); result.ElementAt(1).Start.ShouldBeEquivalentTo(2); result.ElementAt(1).End.ShouldBeEquivalentTo(25); result.Last().Start.ShouldBeEquivalentTo(26); result.Last().End.ShouldBeEquivalentTo(27); result.Last().Should().BeOfType<StrongEmphasis>(); }
public void ReferenceLink() { /* given */ var text = new StringRange("[title][0]"); var parser = new InlineParser(); /* when */ var result = parser.Parse(text).ToList(); /* then */ result.Should().HaveCount(1); result.Should().OnlyContain(span => span is ReferenceLinkSpan); // verify link details var linkSpan = result.First() as ReferenceLinkSpan; linkSpan.Title.ShouldBeEquivalentTo(new StringRange(text, 1, 5)); linkSpan.Key.ShouldBeEquivalentTo(new StringRange(text, 8, 8)); }