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")); }
public void TestGenerateDelegateEvents() { var gen = new CodeUnitGenerator("TestCodeGen"); var classGen = new ClassGenerator("TestClass"); var delegateGen = new DelegateGenerator("MyEventHandler") .AddParameter("TestClass", "myRef") .AddReturnType(typeof(bool)); var eventGen = new EventGenerator("OnSomeTrigger", delegateGen.delegateType); classGen.AddEvent(eventGen); var fireEventMethod = new MethodGenerator("FireEvent") .AddStatement(new StatementBuilder() //.AddSnippetExpression("Debug.Log();DoMoreStuff();")); .InvokeEvent(eventGen, new ParamBuilder() .AddPrimitiveExpression("new TestClass()"))); classGen.AddMethod(fireEventMethod); gen.AddType(delegateGen); gen.AddType(classGen); var classSubscriber = new ClassGenerator("MySubscribeClass"); var field = new FieldGenerator("TestClass", "eventSource"); classSubscriber.AddField(field); var constructor = new ConstructorGenerator(classSubscriber.classType); classSubscriber.AddMethod(constructor); var eventHandler = new MethodGenerator("OnSomeTrigger", delegateGen) .AddStatement(new StatementBuilder() .AddSnippet("Debug.Log(\"Expression1\");") .AddSnippet("Debug.Log(\"Expression2\");")); var subscribeMethod = new MethodGenerator("AddListener") .AddStatement(new StatementBuilder() .AttachEvent(eventHandler, new FieldTarget(field), eventGen)); classSubscriber.AddMethod( new MethodGenerator("Unsubscribe").AddStatement( new StatementBuilder() .DetachEvent(eventHandler, new FieldTarget(field), eventGen))); classSubscriber.AddMethod(eventHandler); classSubscriber.AddMethod(subscribeMethod); gen.AddType(classSubscriber); var ccu = gen.GenerateCompileUnit(); var output = StringCompiler.CompileToString(ccu); //Debug.Log(output); Assert.IsTrue(output.Contains("OnSomeTrigger")); Assert.IsTrue(output.Contains("FireEvent")); Assert.IsTrue(output.Contains("+=")); Assert.IsTrue(output.Contains("-=")); Assert.IsTrue(output.Contains("delegate")); Assert.IsTrue(output.Contains("event")); }
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")); }
public static void GenerateCode2() { FieldGenerator field = new FieldGenerator(); field.SetAuthority(AuthorityType.Private); field.SetFieldName("m_xxxx"); field.SetFieldType("bool"); FieldGenerator field1 = new FieldGenerator(); field1.SetAuthority(AuthorityType.Private); field1.SetFieldName("m_xxxx"); field1.SetFieldType("bool"); MethodGenerator method = new MethodGenerator("Test"); method.SetAuthority(MethodAuthorityType.Internal); method.SetDecorate(MethodDecorateType.Virtual); method.SetParms(new string[] { "int", "string" }); method.SetReturn("bool"); MethodGenerator method1 = new MethodGenerator("Test1"); method1.SetAuthority(MethodAuthorityType.Internal); method1.SetDecorate(MethodDecorateType.Virtual); method1.SetParms(new string[] { "int", "string" }); method1.SetReturn("bool"); ClassGenerator textClass = new ClassGenerator("TestClass"); //textClass.SetUsingName(new string[] { "xxxx", "aaaa" }); //textClass.SetNamespace("masdjf"); textClass.SetBaseClass("xxcvsdf"); textClass.SetDecorate(ClassDecorateType.Abstract); textClass.SetAuthority(AuthorityType.Public); textClass.SetInterfaces(new string[] { "asdfsadf", "asdfasdf" }); textClass.AddMethod(method); textClass.AddMethod(method1); textClass.AddField(field); textClass.AddField(field1); string classValue = textClass.ToString(); TxtUtility.StringToFile(classValue); //Debug.Log(classValue); }
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")); }
public ClassData ToFieldClass() { ClassData data = new ClassData(); data.className = sheetName; ClassGenerator generator = new ClassGenerator(sheetName); generator.SetAuthority(AuthorityType.Public); for (int i = 0; i < memberNames.Length; i++) { FieldGenerator field = new FieldGenerator(); field.SetAnnotation(annotations[i]); field.SetFieldName(memberNames[i]); field.SetFieldType(fieldTypes[i]); field.SetAuthority(AuthorityType.Public); generator.AddField(field); Type filterType = GetFilterType(fieldTypes[i]); if (filterType != null) { MethodInfo usingMethod = filterType.GetMethod("GetUsings"); if (usingMethod == null) { continue; } object filterObj = Activator.CreateInstance(filterType); string[] usingNames = usingMethod.Invoke(filterObj, null) as string[]; if (usingNames == null) { continue; } generator.SetUsingName(usingNames); } } data.classContent = generator.ToString(); return(data); }