Example #1
0
        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());
        }
Example #2
0
        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());
        }
Example #3
0
        public void IfWithEmptyBlock()
        {
            JSBuilder builder = new JSBuilder();

            builder.If("true", ifBlock => { });
            AssertEx.EqualLines(
                new[]
            {
                "if (true) {",
                "}"
            },
                builder);
            Assert.IsTrue(builder.WriteNewLineBeforeNextText);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        public void IfBlockWithSurroundingEmptyLines()
        {
            JSBuilder builder = new JSBuilder();

            builder.Line();
            builder.If("true", ifBlock =>
            {
            });
            builder.Line();
            AssertEx.EqualLines(
                new[]
            {
                "",
                "if (true) {",
                "}",
                ""
            },
                builder.ToString());
        }
Example #7
0
        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());
        }
Example #8
0
        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());
        }
Example #9
0
        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);
        }
Example #10
0
 public JSIfBlock If(string condition, Action <JSBlock> thenAction)
 {
     SetCurrentState(State.If);
     return(builder.If(condition, thenAction));
 }