public void NestedElseBlocks()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("a", ifBlock1 =>
            {
                ifBlock1.If("b", ifBlock2 =>
                {
                })
                .Else(elseBlock2 =>
                {
                });
            })
            .Else(elseBlock1 =>
            {
            });;
            AssertEx.EqualLines(
                new[]
            {
                "if (a) {",
                "  if (b) {",
                "  } else {",
                "  }",
                "} else {",
                "}",
            },
                builder.ToString());
        }
        public void NestedElseIfBlocks()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("a1", ifBlock1 =>
            {
                ifBlock1.If("b1", ifBlock2 =>
                {
                })
                .ElseIf("b2", elseIfBlock2 =>
                {
                });
            })
            .ElseIf("a2", elseIfBlock1 =>
            {
            });;
            AssertEx.EqualLines(
                new[]
            {
                "if (a1) {",
                "  if (b1) {",
                "  } else if (b2) {",
                "  }",
                "} else if (a2) {",
                "}",
            },
                builder.ToString());
        }
        public void IfWithEmptyBlock()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("true", ifBlock => { });
            AssertEx.EqualLines(
                new[]
            {
                "if (true) {",
                "}"
            },
                builder);
            Assert.IsTrue(builder.WriteNewLineBeforeNextText);
        }
        public void IfWithLineAfterwards()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("true", ifBlock => { });
            builder.Line("Test");
            AssertEx.EqualLines(
                new[]
            {
                "if (true) {",
                "}",
                "Test"
            },
                builder);
            Assert.IsTrue(builder.WriteNewLineBeforeNextText);
        }
        public void NestedIfBlocks()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("a", ifBlock1 =>
            {
                Assert.IsTrue(builder.WriteNewLineBeforeNextText);
                AssertEx.EqualLines(
                    "if (a) {",
                    builder);

                ifBlock1.If("b", ifBlock2 =>
                {
                    Assert.IsTrue(builder.WriteNewLineBeforeNextText);
                    AssertEx.EqualLines(
                        new[]
                    {
                        "if (a) {",
                        "  if (b) {"
                    },
                        builder);
                });

                Assert.IsTrue(builder.WriteNewLineBeforeNextText);
                AssertEx.EqualLines(
                    new[]
                {
                    "if (a) {",
                    "  if (b) {",
                    "  }"
                },
                    builder);
            });

            Assert.IsTrue(builder.WriteNewLineBeforeNextText);
            AssertEx.EqualLines(
                new[]
            {
                "if (a) {",
                "  if (b) {",
                "  }",
                "}"
            },
                builder);
        }
        public void IfBlockWithSurroundingEmptyLines()
        {
            JSBuilder builder = new JSBuilder();

            builder.Line();
            builder.If("true", ifBlock =>
            {
            });
            builder.Line();
            AssertEx.EqualLines(
                new[]
            {
                "",
                "if (true) {",
                "}",
                ""
            },
                builder.ToString());
        }
        public void IfBlockWithSurroundingLines()
        {
            JSBuilder builder = new JSBuilder();

            builder.Line("const x = 5;");
            builder.If("true", ifBlock =>
            {
            });
            builder.Line("const y = 6;");
            AssertEx.EqualLines(
                new[]
            {
                "const x = 5;",
                "if (true) {",
                "}",
                "const y = 6;"
            },
                builder.ToString());
        }
        public void TryBlockInIfBlock()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("true", ifBlock =>
            {
                ifBlock.Try(tryBlock =>
                {
                })
                .Catch("error", catchBlock2 =>
                {
                });
            });
            AssertEx.EqualLines(
                "if (true) {" + Environment.NewLine +
                "  try {" + Environment.NewLine +
                "  } catch (error) {" + Environment.NewLine +
                "  }" + Environment.NewLine +
                "}",
                builder.ToString());
        }
        public void IfElseIfWithTextBlocks()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("true", ifBlock =>
            {
                ifBlock.Text("Hello");
            })
            .ElseIf("false", elseBlock =>
            {
                elseBlock.Text("World");
            });
            AssertEx.EqualLines(
                new[]
            {
                "if (true) {",
                "  Hello",
                "} else if (false) {",
                "  World",
                "}"
            },
                builder);
            Assert.IsTrue(builder.WriteNewLineBeforeNextText);
        }
示例#10
0
 public JSIfBlock If(string condition, Action <JSBlock> thenAction)
 {
     SetCurrentState(State.If);
     return(builder.If(condition, thenAction));
 }