示例#1
0
        public void TestOpenCloseTagComplexChildren()
        {
            var children = SyntaxFactory.List(new CsxNodeSyntax[]
            {
                RoslynTestHelper.GenerateCsxTextNode("abc"),
                SyntaxFactory.CsxOpenCloseTagElement("Child",
                                                     SyntaxFactory.CsxCloseTag("Child")),
                RoslynTestHelper.GenerateCsxTextNode("def"),
                RoslynTestHelper.GenerateCsxTextNode("123"),
                SyntaxFactory.CsxSelfClosingTagElement("Child"),
            });

            var parent = SyntaxFactory.CsxOpenCloseTagElement(
                SyntaxFactory.Identifier("Parent"),
                SyntaxFactory.List <CsxAttributeSyntax>(),
                children,
                SyntaxFactory.CsxCloseTag("Parent"));

            var csxCodeBlock = @"<Parent>""abc""<Child></Child>""def""""123""<Child/></Parent>";

            Assert.AreEqual(parent.ToFullString(), csxCodeBlock);


            var tree     = CSharpSyntaxTree.ParseText(RoslynTestHelper.GenerateCodeForExpression(csxCodeBlock));
            var root     = (CompilationUnitSyntax)tree.GetRoot();
            var rewriter = new CsxRewriter();
            var result   = rewriter.Visit(root);

            var expected = RoslynTestHelper.GenerateCodeForExpression(@"ComponentBlueprint.From<Parent, ParentProps>(new ParentProps {Children=new Blueprint[]{""abc"",ComponentBlueprint.From<Child, ChildProps>(new ChildProps {}),""def"",""123"",ComponentBlueprint.From<Child, ChildProps>(new ChildProps {})}})");

            Assert.AreEqual(expected, result.ToFullString());
        }
示例#2
0
        public void TestNDepthFamilyWithAttributes()
        {
            var csxCodeBlock = "";
            var compiledCode = "";

            for (var i = 0; i < 10; i += 1)
            {
                var tagName = $"Name_{i}";
                var id      = i.ToString();
                csxCodeBlock = $@"<{tagName} Id=""{id}"">{csxCodeBlock}</{tagName}>";

                var tree     = CSharpSyntaxTree.ParseText(RoslynTestHelper.GenerateCodeForExpression(csxCodeBlock));
                var root     = (CompilationUnitSyntax)tree.GetRoot();
                var rewriter = new CsxRewriter();
                var result   = rewriter.Visit(root);

                compiledCode = i == 0
                    ? $@"ComponentBlueprint.From<{tagName}, {tagName}Props>(new {tagName}Props {{Id=""{id}""}})"
                    : $@"ComponentBlueprint.From<{tagName}, {tagName}Props>(new {tagName}Props {{Id=""{id}"",Children=new Blueprint[]{{{compiledCode}}}}})";

                var expected = RoslynTestHelper.GenerateCodeForExpression(compiledCode);

                Assert.AreEqual(expected, result.ToFullString());
            }
        }
示例#3
0
        public void TestParsingOneOpenCloseTagTextChild()
        {
            var csxCodeBlock = @"<Parent>""textChild""</Parent>";

            var tree     = CSharpSyntaxTree.ParseText(RoslynTestHelper.GenerateCodeForExpression(csxCodeBlock));
            var root     = (CompilationUnitSyntax)tree.GetRoot();
            var rewriter = new CsxRewriter();
            var result   = rewriter.Visit(root);

            var expected = RoslynTestHelper.GenerateCodeForExpression(@"ComponentBlueprint.From<Parent, ParentProps>(new ParentProps {Children=new Blueprint[]{""textChild""}})");

            Assert.AreEqual(expected, result.ToFullString());
        }
示例#4
0
        public void TestCsxSelfClosingTagElement_Multiple_Attributes()
        {
            var csxCodeBlock = @"<MyComponent Id=""abc"" ClassName=""clicked""/>";

            var tree     = CSharpSyntaxTree.ParseText(RoslynTestHelper.GenerateCodeForExpression(csxCodeBlock));
            var root     = (CompilationUnitSyntax)tree.GetRoot();
            var rewriter = new CsxRewriter();
            var result   = rewriter.Visit(root);

            var expected = RoslynTestHelper.GenerateCodeForExpression(@"ComponentBlueprint.From<MyComponent, MyComponentProps>(new MyComponentProps {Id=""abc"",ClassName=""clicked""})");

            Assert.AreEqual(expected, result.ToFullString());
        }
示例#5
0
        public void TestCsxOpenCloseTagElement_NoAttributes()
        {
            var csxCodeBlock = @"<MyComponent></MyComponent>";

            var tree     = CSharpSyntaxTree.ParseText(RoslynTestHelper.GenerateCodeForExpression(csxCodeBlock));
            var root     = (CompilationUnitSyntax)tree.GetRoot();
            var rewriter = new CsxRewriter();
            var result   = rewriter.Visit(root);

            var expected = RoslynTestHelper.GenerateCodeForExpression(@"ComponentBlueprint.From<MyComponent, MyComponentProps>(new MyComponentProps {})");

            Assert.AreEqual(expected, result.ToFullString());
        }
        public void TestOpenCloseTagComplexChildren()
        {
            var csxCodeBlock = @"<Parent Str=""abc"" Attr={$""hello {value}""}>""abc""<Child src={source + 1}></Child></Parent>";

            var children = SyntaxFactory.List(new CsxNodeSyntax[]
            {
                RoslynTestHelper.GenerateCsxTextNode("abc"),
                SyntaxFactory.CsxOpenCloseTagElement(
                    SyntaxFactory.Identifier("Child"),
                    SyntaxFactory.List(new CsxAttributeSyntax[] {
                    SyntaxFactory.CsxBraceAttribute("src", "source + 1"),
                }),
                    SyntaxFactory.List <CsxNodeSyntax>(),
                    SyntaxFactory.CsxCloseTag("Child")),
            });

            var parent = SyntaxFactory.CsxOpenCloseTagElement(
                SyntaxFactory.Identifier("Parent"),
                SyntaxFactory.List(new CsxAttributeSyntax[] {
                SyntaxFactory.CsxStringAttribute("Str", "abc"),
                SyntaxFactory.CsxBraceAttribute("Attr", @"$""hello {value}"""),
            }),
                children,
                SyntaxFactory.CsxCloseTag("Parent"));

            Assert.AreEqual(csxCodeBlock, parent.ToFullString());


            var tree     = CSharpSyntaxTree.ParseText(RoslynTestHelper.GenerateCodeForExpression(csxCodeBlock));
            var root     = (CompilationUnitSyntax)tree.GetRoot();
            var rewriter = new CsxRewriter();
            var result   = rewriter.Visit(root);

            var expected = RoslynTestHelper.GenerateCodeForExpression(@"ComponentBlueprint.From<Parent, ParentProps>(new ParentProps {Str=""abc"",Attr=$""hello {value}"",Children=new Blueprint[]{""abc"",ComponentBlueprint.From<Child, ChildProps>(new ChildProps {src=source + 1})}})");

            Assert.AreEqual(expected, result.ToFullString());
        }
        public void TestBraceNodeChild_float()
        {
            var children = SyntaxFactory.List(new CsxNodeSyntax[]
            {
                SyntaxFactory.CsxBraceNode(SyntaxFactory.ParseExpression("123.456")),
            });

            var parent = SyntaxFactory.CsxOpenCloseTagElement(
                SyntaxFactory.Identifier("DivComponent"),
                SyntaxFactory.List <CsxAttributeSyntax>(),
                children,
                SyntaxFactory.CsxCloseTag("DivComponent"));

            var csxCodeBlock = @"<DivComponent>{123.456}</DivComponent>";

            Assert.AreEqual(csxCodeBlock, parent.ToFullString());

            var tree     = CSharpSyntaxTree.ParseText(RoslynTestHelper.GenerateCodeForExpression(csxCodeBlock));
            var root     = (CompilationUnitSyntax)tree.GetRoot();
            var rewriter = new CsxRewriter();
            var result   = rewriter.Visit(root);

            var expectedCsCode = RoslynTestHelper.GenerateCodeForExpression(@"ComponentBlueprint.From<DivComponent, DivComponentProps>(new DivComponentProps {Children=new Blueprint[]{123.456}})");

            Assert.AreEqual(expectedCsCode, result.ToFullString());

            var rootBlueprint = RoslynTestHelper.GetGeneratedExpressionCodeReturnValue <ComponentBlueprint>(expectedCsCode);

            var document    = new TestHtmlDocument();
            var renderer    = new DomRenderer(document);
            var htmlElement = renderer.Mount(rootBlueprint).RootNodes[0];

            var expected = @"<div>123.456</div>";

            Assert.AreEqual(htmlElement.ToString(), expected);
        }