コード例 #1
0
        public void ShouldTranslateAMultipleLineNonVoidIfElseStatements()
        {
            var intVariable           = Variable(typeof(int), "i");
            var zero                  = Constant(0);
            var intVariableEqualsZero = Equal(intVariable, zero);
            var writeHello            = CreateLambda(() => Console.WriteLine("Hello"));
            var writeGoodbye          = CreateLambda(() => Console.WriteLine("Goodbye"));
            var helloThenGoodbye      = Block(writeHello.Body, writeGoodbye.Body, intVariable);
            var goodbyeThenHello      = Block(writeGoodbye.Body, writeHello.Body, intVariable);
            var writeHelloAndGoodbye  = IfThenElse(intVariableEqualsZero, helloThenGoodbye, goodbyeThenHello);

            var translated = writeHelloAndGoodbye.ToReadableString();

            const string EXPECTED = @"
if (i == 0)
{
    Console.WriteLine(""Hello"");
    Console.WriteLine(""Goodbye"");

    return i;
}

Console.WriteLine(""Goodbye"");
Console.WriteLine(""Hello"");

return i;
";

            translated.ShouldBe(EXPECTED.Trim());
        }
コード例 #2
0
        public void ShouldFormatGotoStatements()
        {
            var labelTargetOne = Label(typeof(void), "One");
            var labelOne       = Label(labelTargetOne);
            var writeOne       = CreateLambda(() => Console.Write("One"));
            var gotoOne        = Goto(labelTargetOne);

            var labelTargetTwo = Label(typeof(void), "Two");
            var labelTwo       = Label(labelTargetTwo);
            var writeTwo       = CreateLambda(() => Console.Write("Two"));
            var gotoTwo        = Goto(labelTargetTwo);

            var intVariable  = Variable(typeof(int), "i");
            var intEqualsOne = Equal(intVariable, Constant(1));
            var intEqualsTwo = Equal(intVariable, Constant(2));

            var ifTwoGotoTwo = IfThen(intEqualsTwo, gotoTwo);
            var gotoOneOrTwo = IfThenElse(intEqualsOne, gotoOne, ifTwoGotoTwo);

            var writeNeither    = CreateLambda(() => Console.Write("Neither"));
            var returnFromBlock = Return(Label());

            var block = Block(
                gotoOneOrTwo,
                writeNeither.Body,
                returnFromBlock,
                labelOne,
                writeOne.Body,
                labelTwo,
                writeTwo.Body);

            var translated = ToReadableHtmlString(block);

            const string EXPECTED = @"
<span class=""cs"">if </span>(<span class=""vb"">i</span> == <span class=""nm"">1</span>)
{
    <span class=""cs"">goto </span>One;
}
<span class=""cs"">else</span> <span class=""cs"">if </span>(<span class=""vb"">i</span> == <span class=""nm"">2</span>)
{
    <span class=""cs"">goto </span>Two;
}

<span class=""tn"">Console</span>.<span class=""mn"">Write</span>(<span class=""tx"">""Neither""</span>);
<span class=""cs"">return</span>;

One:
<span class=""tn"">Console</span>.<span class=""mn"">Write</span>(<span class=""tx"">""One""</span>);

Two:
<span class=""tn"">Console</span>.<span class=""mn"">Write</span>(<span class=""tx"">""Two""</span>);
";

            translated.ShouldBe(EXPECTED.Trim());
        }
コード例 #3
0
        public void ShouldTranslateGotoStatements()
        {
            var labelTargetOne = Label(typeof(void), "One");
            var labelOne       = Label(labelTargetOne);
            var writeOne       = CreateLambda(() => Console.Write("One"));
            var gotoOne        = Goto(labelTargetOne);

            var labelTargetTwo = Label(typeof(void), "Two");
            var labelTwo       = Label(labelTargetTwo);
            var writeTwo       = CreateLambda(() => Console.Write("Two"));
            var gotoTwo        = Goto(labelTargetTwo);

            var intVariable  = Variable(typeof(int), "i");
            var intEqualsOne = Equal(intVariable, Constant(1));
            var intEqualsTwo = Equal(intVariable, Constant(2));

            var ifTwoGotoTwo = IfThen(intEqualsTwo, gotoTwo);
            var gotoOneOrTwo = IfThenElse(intEqualsOne, gotoOne, ifTwoGotoTwo);

            var writeNeither    = CreateLambda(() => Console.Write("Neither"));
            var returnFromBlock = Return(Label());

            var block = Block(
                gotoOneOrTwo,
                writeNeither.Body,
                returnFromBlock,
                labelOne,
                writeOne.Body,
                labelTwo,
                writeTwo.Body);

            var translated = block.ToReadableString();

            const string EXPECTED = @"
if (i == 1)
{
    goto One;
}
else if (i == 2)
{
    goto Two;
}

Console.Write(""Neither"");
return;

One:
Console.Write(""One"");

Two:
Console.Write(""Two"");
";

            translated.ShouldBe(EXPECTED.Trim());
        }