Example #1
0
    public void TestGenerateForLoop()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass")
                       .SetIsSealed(true);
        var method = new MethodGenerator("IterateStuff");

        method.AddStatement(new StatementBuilder()
                            .AddVariable(typeof(string[]), "myStuff", true)
                            .AddVariable(typeof(bool), "myBool", true)
                            .AddVariable(typeof(int), "myInt", 1)
                            .AddSnippet("//Snippet")
                            .AddForLoop("i", 10, new StatementBuilder()
                                        .AddSnippet("Debug.Log()")
                                        .AddAssignement(new VariableTarget("myInt"), new VariableTarget("i"))
                                        .InvokeMethod(new VariableTarget("myStuff"),
                                                      new ClassTarget("UnityEngine"),
                                                      "GetAllStuff",
                                                      new ParamBuilder()
                                                      .AddVariable("myInt"))));
        classGen.AddMethod(method);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Debug.Log(output);
        Assert.IsTrue(output.Contains("for (i = 0"));
        Assert.IsTrue(output.Contains("(i < 10"));
        Assert.IsTrue(output.Contains("myStuff = UnityEngine.GetAllStuff("));
    }
Example #2
0
    public void TestWhileLoop()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass")
                       .SetIsSealed(true);
        var method = new MethodGenerator("IterateStuff");

        method.AddStatement(new StatementBuilder()
                            .AddVariable(typeof(string[]), "myStuff", true)
                            .AddVariable(typeof(bool), "myBool", true)
                            .AddWhile(new CodeSnippetExpression("iterator.MoveNext(myBool)"), new StatementBuilder()
                                      .AddSnippet("Debug.Log(myBool)")));

        classGen.AddMethod(method);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Debug.Log(output);
    }