示例#1
0
    public void TestGenerateAttributes()
    {
        var gen = new CodeUnitGenerator("TestCodeGen");

        var attributeGen = new ClassGenerator("MyAttribute")
                           .AddBaseType("Attribute");

        var classGen = new ClassGenerator("TestClass");

        classGen.SetCustomAttribute("MyAttribute", new ParamBuilder());

        var field = new FieldGenerator(typeof(int), "MyField");

        field.SetCustomAttribute("MyAttribute", new ParamBuilder());
        var property = new AutoPropertyGenerator("TestClass", "MyProp");

        property.SetCustomAttribute("MyAttribute", new ParamBuilder());


        classGen.AddAutoProperty(property);
        classGen.AddField(field);
        gen.AddType(attributeGen);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Debug.Log(output);
        Assert.IsTrue(output.Contains("MyAttribute"));
    }
示例#2
0
    public void TestGenerateClassImplementation()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass")
                       .SetIsPartial(true)
                       .SetIsAbstract(true)
                       .AddBaseType("IComponent");
        var field       = new FieldGenerator(typeof(int), "MyField");
        var property    = new AutoPropertyGenerator("TestClass", "MyProp");
        var constructor = new ConstructorGenerator()
                          .AddBaseCall("BaseArg")
                          .AddParameter(field.FieldType, field.Name)
                          .AddParameter(property.Name, property.PropertyType)
                          .AddStatement(new StatementBuilder()
                                        .AddConstructorFieldAssignement(field.Name, field.Name)
                                        .AddConstructorPropertyAssignement(property.Name, property.Name));

        classGen.AddAutoProperty(property);
        classGen.AddField(field);
        classGen.AddMethod(constructor);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        Debug.Log(output);
        Assert.IsTrue(output.Contains("base("));
        Assert.IsTrue(output.Contains("BaseArg"));
    }
示例#3
0
    public void TestGenerateClassWithFieldPropertyMethod()
    {
        var gen      = new CodeUnitGenerator("TestCodeGen");
        var classGen = new ClassGenerator("TestClass");
        var field    = new FieldGenerator(typeof(int), "MyField");
        var property = new AutoPropertyGenerator("TestClass", "MyProp");
        var method   = new MethodReturnField(field);

        method.AddStatement("Debug.Log(\"This is a string\"");
        classGen.AddAutoProperty(property);
        classGen.AddField(field);
        classGen.AddMethod(method);
        gen.AddType(classGen);
        var ccu = gen.GenerateCompileUnit();

        var output = StringCompiler.CompileToString(ccu);

        //Debug.Log(output);
        Assert.IsTrue(output.Contains("MyProp"));
        Assert.IsTrue(output.Contains("MyField"));
        Assert.IsTrue(output.Contains("GetMyField"));
    }