public void TransformedText_IsParsedCorrectly(string input, string expected)
        {
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility(input);
            Assert.Equal(expected, HttpUtility.HtmlDecode(parser.ToHtml(bbCode, uid)));
        }
        public void IdentityModifiedTreesAreEqual()
        {
            var tree  = BBCodeTestUtil.GetAnyTree();
            var tree2 = new IdentitiyModificationSyntaxTreeVisitor().Visit(tree);

            Assert.True(tree == tree2);
        }
        public void EscapeHtml_IsCorrect(string input, string expected, string uid)
        {
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, _, _) = parser.TransformForBackwardsCompatibility(input, uid);
            Assert.Equal(expected, bbCode);
        }
        public void PlainTextLinks_AreNotTransformed(string input, string expected, string uid)
        {
            var parser = BBCodeTestUtil.GetCustomParser();

            var(result, _, _) = parser.TransformForBackwardsCompatibility(input, uid);
            Assert.Equal(expected, result);
        }
        public void ReplaceTextSpans_WhenEverythingIsConvertedToX_OutputContainsOnlyX_CheckedWithTreeWalk()
        {
            var tree1 = BBCodeTestUtil.GetAnyTree();
            var tree2 = BBCode.ReplaceTextSpans(tree1, txt => new[] { new TextSpanReplaceInfo(0, txt.Length, new TextNode("x")), }, null);

            new TextAssertVisitor(str => Assert.True(str == "x")).Visit(tree2);
        }
 public void ReplaceTextSpans_ArbitraryTextSpans_NoCrash()
 {
     for (int i = 0; i < RandomValue.Int(100, 10); i++)
     {
         var tree1       = BBCodeTestUtil.GetAnyTree();
         var chosenTexts = new List <string>();
         var tree2       = BBCode.ReplaceTextSpans(tree1, txt =>
         {
             var count   = RandomValue.Int(3, 0);
             var indexes = new List <int>();
             for (int i = 0; i < count; i++)
             {
                 indexes.Add(RandomValue.Int(txt.Length, 0));
             }
             indexes.Sort();
             _output.WriteLine(string.Join(", ", indexes));
             return
             (Enumerable.Range(0, count)
              .Select(i =>
             {
                 var maxIndex = i == count - 1 ? txt.Length : indexes[i + 1];
                 var text = RandomValue.String();
                 chosenTexts.Add(text);
                 return new TextSpanReplaceInfo(indexes[i], RandomValue.Int(indexes[i] - maxIndex + 1, 0), new TextNode(text));
             })
              .ToArray());
         }, null);
         var bbCode = tree2.ToBBCode();
         if (!chosenTexts.All(s => bbCode.Contains(s)))
         {
         }
         Assert.All(chosenTexts, s => Assert.Contains(s, bbCode));
     }
 }
        static void ReplaceTextSpans_ManualTestCases_TestCase(string bbCode, string expected, Func <string, IList <TextSpanReplaceInfo> > getTextSpansToReplace, Func <TagNode, bool> tagFilter)
        {
            var tree1 = BBCodeTestUtil.GetParserForTest(ErrorMode.Strict, false, BBTagClosingStyle.AutoCloseElement, false).ParseSyntaxTree(bbCode);
            var tree2 = BBCode.ReplaceTextSpans(tree1, getTextSpansToReplace ?? (txt => Array.Empty <TextSpanReplaceInfo>()), tagFilter);

            Assert.Equal(expected, tree2.ToBBCode());
        }
        public void DefaultVisitorModifiesNothing()
        {
            var tree  = BBCodeTestUtil.GetAnyTree();
            var tree2 = new SyntaxTreeVisitor().Visit(tree);

            Assert.True(ReferenceEquals(tree, tree2));
        }
        public void WhenNoFormatting_IsCorrect()
        {
            var text   = "some plain text";
            var parser = BBCodeTestUtil.GetCustomParser();

            Assert.Equal(text, parser.ToHtml(text));
        }
        public void TextModifiedTreesAreNotEqual()
        {
            var tree  = BBCodeTestUtil.GetAnyTree();
            var tree2 = new TextModificationSyntaxTreeVisitor().Visit(tree);

            Assert.True(tree != tree2);
        }
        public void ReplaceTextSpans_WhenNoModifications_TreeIsPreserved()
        {
            var tree1 = BBCodeTestUtil.GetAnyTree();
            var tree2 = BBCode.ReplaceTextSpans(tree1, txt => Array.Empty <TextSpanReplaceInfo>(), null);

            Assert.Equal(tree1, tree2);
        }
Пример #12
0
        public void ContentTransformer_EmptyAttribute_CanChooseValueFromAttributeRenderingContext()
        {
            var parser = BBCodeTestUtil.GetParserForTest(ErrorMode.Strict, true, BBTagClosingStyle.AutoCloseElement, false);

            Assert.Equal(@"<a href=""http://codekicker.de"">http://codekicker.de</a>", parser.ToHtml(@"[url2]http://codekicker.de[/url2]"));
            Assert.Equal(@"<a href=""http://codekicker.de"">http://codekicker.de</a>", parser.ToHtml(@"[url2=http://codekicker.de]http://codekicker.de[/url2]"));
        }
        public void ReplaceTextSpans_WhenEmptyModifications_TreeIsPreserved()
        {
            var tree1 = BBCodeTestUtil.GetAnyTree();
            var tree2 = BBCode.ReplaceTextSpans(tree1, txt => new[] { new TextSpanReplaceInfo(0, 0, null), }, null);

            Assert.Equal(tree1.ToBBCode(), tree2.ToBBCode());
        }
        public void ReplaceTextSpans_WhenEverythingIsConvertedToX_OutputContainsOnlyX_CheckedWithContains()
        {
            var tree1 = BBCodeTestUtil.GetAnyTree();
            var tree2 = BBCode.ReplaceTextSpans(tree1, txt => new[] { new TextSpanReplaceInfo(0, txt.Length, new TextNode("x")), }, null);

            Assert.True(!tree2.ToBBCode().Contains("a"));
        }
Пример #15
0
        public void NoHtmlChars_AnyInput()
        {
            var output = BBCodeTestUtil.SimpleBBEncodeForTest(RandomValue.String(), RandomValue.Object <ErrorMode>());

            Assert.DoesNotContain('<', output);
            Assert.DoesNotContain('>', output);
        }
Пример #16
0
        public void NoScript_AnyInput_Tree()
        {
            var parser = BBCodeTestUtil.GetParserForTest(ErrorMode.ErrorFree, true, BBTagClosingStyle.AutoCloseElement, false);
            var tree   = BBCodeTestUtil.CreateRootNode(parser.Tags.ToArray());
            var output = tree.ToHtml();

            Assert.True(!output.Contains("<script"));
        }
        public void Bitfield_WithUid_IsCorrect()
        {
            var parser = BBCodeTestUtil.GetCustomParser();
            var input  = "[color=#0000FF:2xgytwj6]Lorem ipsum [url=https%3A%2F%2Fgoogle.com:2xgytwj6]dolor sit amet[/url:2xgytwj6][/color:2xgytwj6], consectetur adipiscing elit";

            var(_, _, bitfield) = parser.TransformForBackwardsCompatibility(input, "2xgytwj6");
            Assert.Equal("Eg==", bitfield);
        }
        public void WhenSimpleFormatting_IsCorrect()
        {
            var text   = "[b]simple formatting[/b]";
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility(text);
            Assert.Equal($"[b:{uid}]simple formatting[/b:{uid}]", bbCode);
        }
Пример #19
0
        public void ToTextDoesNotCrash()
        {
            var input  = RandomValue.String();
            var parser = BBCodeTestUtil.GetParserForTest(ErrorMode.ErrorFree, true, BBTagClosingStyle.AutoCloseElement, false);
            var text   = parser.ParseSyntaxTree(input).ToText();

            Assert.True(text.Length <= input.Length);
        }
        public void WhenComplexFormatting_URL_IsCorrect()
        {
            var text   = "[url=https://google.com]simple formatting[/url]";
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility(text);
            Assert.Equal($"[url=https://google.com:{uid}]simple formatting[/url:{uid}]", bbCode);
        }
Пример #21
0
        public void EqualTreesHaveEqualBBCode()
        {
            var tree1   = BBCodeTestUtil.GetAnyTree();
            var tree2   = BBCodeTestUtil.GetAnyTree();
            var bbCode1 = tree1.ToBBCode();
            var bbCode2 = tree2.ToBBCode();

            Assert.Equal(tree1 == tree2, bbCode1 == bbCode2);
        }
        public void WhenWrongFormatting_Typo_IsCorrect(string text)
        {
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility(text);
            Assert.Equal(text, bbCode);
            Assert.NotNull(uid);
            Assert.Empty(uid);
        }
        public void CarriageReturns_AreRemoved()
        {
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility("[url=\"https://google.com\"]\r\n[b]a link[/b]\r\n[/url]");
            Assert.Equal($"[url=&quot;https://google.com&quot;:{uid}]\n[b:{uid}]a link[/b:{uid}]\n[/url:{uid}]", bbCode);
            Assert.NotNull(uid);
            Assert.NotEmpty(uid);
        }
        public void InlineAttachment_IsCorrect()
        {
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility("[attachment=7]01.PNG[/attachment]");
            Assert.Equal($"[attachment=7:{uid}]<!-- ia7 -->01.PNG<!-- ia7 -->[/attachment:{uid}]", bbCode);
            Assert.NotNull(uid);
            Assert.NotEmpty(uid);
        }
Пример #25
0
        public void Roundtrip()
        {
            var parser = BBCodeTestUtil.GetParserForTest(RandomValue.Object <ErrorMode>(), false, BBTagClosingStyle.AutoCloseElement, false);
            var tree   = BBCodeTestUtil.CreateRootNode(parser.Tags.ToArray());
            var bbcode = tree.ToBBCode();
            var tree2  = parser.ParseSyntaxTree(bbcode);

            Assert.True(tree == tree2);
        }
        public void WhenWrongFormatting_Nesting_IsCorrect()
        {
            var text   = "[b]simple [i]formatting[/b][/i]";
            var parser = BBCodeTestUtil.GetCustomParser();

            var(bbCode, uid, _) = parser.TransformForBackwardsCompatibility(text);
            Assert.Equal(text, bbCode);
            Assert.NotNull(uid);
            Assert.Empty(uid);
        }
Пример #27
0
        public void CorrectingErrorMode()
        {
            Assert.True(BBCodeTestUtil.IsValid(@"", ErrorMode.TryErrorCorrection));
            Assert.True(BBCodeTestUtil.IsValid(@"[b]abc[/b]", ErrorMode.TryErrorCorrection));
            Assert.True(BBCodeTestUtil.IsValid(@"[b]abc", ErrorMode.TryErrorCorrection));

            Assert.Equal(@"\", BBEncodeForTest(@"\", ErrorMode.TryErrorCorrection));
            Assert.Equal(@"\x", BBEncodeForTest(@"\x", ErrorMode.TryErrorCorrection));
            Assert.Equal(@"\", BBEncodeForTest(@"\\", ErrorMode.TryErrorCorrection));
        }
Пример #28
0
 public void StrictErrorMode()
 {
     Assert.True(BBCodeTestUtil.IsValid(@"", ErrorMode.Strict));
     Assert.True(BBCodeTestUtil.IsValid(@"[b]abc[/b]", ErrorMode.Strict));
     Assert.False(BBCodeTestUtil.IsValid(@"[b]abc", ErrorMode.Strict));
     Assert.False(BBCodeTestUtil.IsValid(@"abc[0]def", ErrorMode.Strict));
     Assert.False(BBCodeTestUtil.IsValid(@"\", ErrorMode.Strict));
     Assert.False(BBCodeTestUtil.IsValid(@"\x", ErrorMode.Strict));
     Assert.False(BBCodeTestUtil.IsValid(@"[", ErrorMode.Strict));
     Assert.False(BBCodeTestUtil.IsValid(@"]", ErrorMode.Strict));
 }
Пример #29
0
        public void UnequalTexthasUnequalTrees()
        {
            var tree1 = BBCodeTestUtil.GetAnyTree();
            var tree2 = BBCodeTestUtil.GetAnyTree();
            var text1 = tree1.ToText();
            var text2 = tree2.ToText();

            if (text1 != text2)
            {
                Assert.True(tree1 != tree2);
            }
        }
Пример #30
0
        public void TextNodesCannotBeSplit()
        {
            var          parser = BBCodeTestUtil.GetParserForTest(RandomValue.Object <ErrorMode>(), true, BBTagClosingStyle.AutoCloseElement, false);
            SequenceNode tree;

            try
            {
                tree = parser.ParseSyntaxTree(RandomValue.String());
            }
            catch (BBCodeParsingException)
            {
                return;
            }

            AssertTextNodesNotSplit(tree);
        }