Ejemplo n.º 1
0
        public void TestMethod2()
        {
            var pool    = new IlInstanceHolderPool(typeof(int), 16);
            var item    = pool.Alloc();
            var astNode = ast.Statements(
                ast.Assign(ast.StaticFieldAccess(item.FieldInfo), ast.Argument <int>(0, "Value")),
                ast.Return()
                );

            _testOutputHelper.WriteLine(GeneratorCSharp.GenerateString <GeneratorCSharp>(astNode));
            var generatorIl = new GeneratorIl();
            var itemSet     = generatorIl.GenerateDelegate <Action <int> >("ItemSet", astNode);

            itemSet(10);
            Assert.Equal(10, item.Value);
        }
        public void TestWriteLineLoadString()
        {
            var ast = Ast.Statements(
                Ast.Statement(Ast.CallStatic((Action <string>)Console.WriteLine, Ast.Argument <string>(0))),
                Ast.Statement(Ast.CallStatic((Action <string>)Console.WriteLine, "Goodbye World!")),
                Ast.Return()
                );

            var generatorIl = new GeneratorIl();
            var method      = generatorIl.GenerateDelegate <Action <string> >("TestWriteLine", ast);

            Console.WriteLine(new GeneratorCSharp().GenerateRoot(ast).ToString());
            Console.WriteLine("{0}", new GeneratorIl().GenerateToString(method.Method, ast));

            var output = AstStringUtils.CaptureOutput(() => { method("Hello World!"); });

            Assert.Equal("Hello World!" + Environment.NewLine + "Goodbye World!" + Environment.NewLine, output);
        }
        public void TestAstSwitch()
        {
            var argument = AstArgument.Create <int>(0, "Value");
            var ast      = Ast.Statements(
                Ast.Switch(
                    Ast.Argument(argument),
                    Ast.Default(Ast.Return("-")),
                    Ast.Case(1, Ast.Return("One")),
                    Ast.Case(3, Ast.Return("Three"))
                    ),
                Ast.Return("Invalid!")
                );
            var generatorIl = new GeneratorIl();
            var method      = generatorIl.GenerateDelegate <Func <int, string> >("TestSwitch", ast);

            Assert.Equal("-", method(0));
            Assert.Equal("One", method(1));
            Assert.Equal("-", method(2));
            Assert.Equal("Three", method(3));
            Assert.Equal("-", method(4));
        }