Example #1
0
        public void can_gen_enum_from_scratch()
        {
            var @enum = new CodeTypeDeclaration();
            @enum.IsEnum = true;
            @enum.Name = "TransactionTypes";
            var field = new CodeMemberField();
            field.InitExpression = new CodeSnippetExpression("1");
            field.Name = "Rent";

            @enum.Members.Add(field);
            var builder = new CodeBuilder();
            builder.GenerateCode(Console.Out, @enum);
        }
Example #2
0
        public void Can_gen_from_scratch()
        {
            var type = new CodeTypeDeclaration("AccountTypes");
            type.BaseTypes.Add(new CodeTypeReference(typeof(Enumeration)));

            CodeTypeReference fieldType = new CodeTypeReference(typeof(EnumerationField));

            var f1 = new CodeMemberField(fieldType, "_AccountType1");
            f1.Attributes = MemberAttributes.Private | MemberAttributes.Static;
            f1.InitExpression = new CodeObjectCreateExpression(fieldType, new CodeSnippetExpression("1"), new CodeSnippetExpression("\"AccountType1\""));
            type.Members.Add(f1);

            var p1 = new CodeMemberProperty();
            p1.Type = fieldType;
            p1.Name = "AccountType1";
            p1.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            p1.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(null, "_AccountType1")));
            type.Members.Add(p1);

            var builder = new CodeBuilder();
            builder.GenerateCode(Console.Out, type);
        }
Example #3
0
 /// <summary>
 /// Compiles a type.
 /// </summary>
 public void Compile()
 {
     //compile to assembly
     CodeBuilder builder = new CodeBuilder();
     CodeCompileUnit compunit = builder.CreateCodeCompileUnit(this._nsdecl);
     CodeDomProvider compiler = new CSharpCodeProvider();
     CompilerParameters options = new CompilerParameters();
     options.GenerateInMemory = true;
     options.WarningLevel = 4;
     options.TreatWarningsAsErrors = true;
     options.IncludeDebugInformation = true;
     options.ReferencedAssemblies.Add(@"C:\lib\Castle\bin\Castle.ActiveRecord.dll");
     CompilerResults results = compiler.CompileAssemblyFromDom(options, compunit);
     _isCompiled = results.Errors.Count == 0;
     _compiled = results.CompiledAssembly;
     if (results.Errors.Count > 0)
     {
         foreach(CompilerError error in results.Errors)
         {
             Console.WriteLine(error.ErrorText);
         }
     }
 }
Example #4
0
        public void CanCreateActiveRecordClass()
        {
            NamespaceDeclaration nsdecl = new NamespaceDeclaration("My.Data").Imports("Castle.ActiveRecord");
            ClassDeclaration cdecl = nsdecl.AddClass("Customer").IsAbstract().InheritsFrom("ActiveRecordBase", "My.Data.Customer");

            //compile to assembly
            CodeBuilder builder = new CodeBuilder();
            CodeCompileUnit compunit = builder.CreateCodeCompileUnit(nsdecl);
            CodeDomProvider compiler = new CSharpCodeProvider();
            CompilerParameters options = new CompilerParameters();
            options.GenerateInMemory = true;
            options.WarningLevel = 4;
            options.TreatWarningsAsErrors = true;
            options.IncludeDebugInformation = true;
            options.ReferencedAssemblies.Add(@"C:\lib\Castle\bin\Castle.ActiveRecord.dll");
            CompilerResults results = compiler.CompileAssemblyFromDom(options, compunit);

                foreach (CompilerError error in results.Errors)
                {
                    Console.WriteLine(error.ErrorText);
                }

            Assembly compiled = results.CompiledAssembly;

            //load assembly in it's own app domain
            AppDomain testdomain = AppDomain.CreateDomain("testdomain");
            testdomain.Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            testdomain.Load(compiled.GetName());

            //test the assembly
            Type testtype = compiled.GetType("My.Data.Customer");
            Assert.IsNotNull(testtype);
            Assert.AreEqual("ActiveRecordBase`1", testtype.BaseType.Name);
            Assert.AreEqual("Customer", testtype.BaseType.GetGenericArguments()[0].Name);

            AppDomain.Unload(testdomain);
        }
        /// <summary>
        /// Generates the test class for a table.
        /// </summary>
        /// <param name="writer">The writer to which the code is written.</param>
        /// <param name="namespaceName">The namespace containing the test class.</param>
        /// <param name="table">The table being tested.</param>
        public void Generate(TextWriter writer, string namespaceName, TableSchema table)
        {
            if (table == null)
                throw new ArgumentNullException("table");

            NamespaceDeclaration nstest = new NamespaceDeclaration(namespaceName + ".Tests");

            //declare the test class
            string typeName = this.NameProvider.GetClassName(table.Name);
            ClassDeclaration testclass = nstest.AddClass(typeName + "Tests", true);
            string targetTypeName = namespaceName + "." + typeName;
            testclass.InheritsFrom(new ClassDeclaration("ActiveRecordBaseTest", new CodeDomTypeReference(targetTypeName)));

            //create the test method
            MethodDeclaration testmeth = testclass.AddMethod(String.Format("Verify{0}", typeName)).Public();
            testmeth.AddAttribute("NUnit.Framework.TestAttribute");
            string fullName = namespaceName + "." + typeName;

            string fieldName = this.NameProvider.GetClassName(table.Name).Substring(0, 1).ToLower() + this.NameProvider.GetClassName(table.Name).Substring(1);
            testmeth.Declare(fieldName).New(new CodeDomTypeReference(fullName));//generates: Customer customer = new Customer();
            foreach (ColumnSchema column in table.NonKeyColumns)
            {
                string propertyName = this.NameProvider.GetPropertyName(column);
                string propertyValue = this.ValueProvider.GetValue(column);
                testmeth.Assign(fieldName).Property(propertyName).To(propertyValue);
            }
            testmeth.Call(fieldName, "Save");
            nstest.Imports("System");
            CodeBuilder builder = new CodeBuilder();
            builder.GenerateCode(writer, nstest);
        }
Example #6
0
        public void CanCreateClass()
        {
            //create dom
            NamespaceDeclaration nsdecl = new NamespaceDeclaration("My.Data");
            ClassDeclaration cdecl = nsdecl.AddClass("Customer");
            cdecl.AddProperty("ID", "_id", "System.Int32");

            //compile to assembly
            CodeBuilder builder = new CodeBuilder();
            CodeCompileUnit compunit = builder.CreateCodeCompileUnit(nsdecl);
            CodeDomProvider compiler = new CSharpCodeProvider();
            CompilerParameters options = new CompilerParameters();
            options.GenerateInMemory = true;
            options.WarningLevel = 4;
            options.TreatWarningsAsErrors = true;
            options.IncludeDebugInformation = true;
            CompilerResults results = compiler.CompileAssemblyFromDom(options, compunit);
            Assembly compiled = results.CompiledAssembly;

            //load assembly in it's own app domain
            AppDomain testdomain = AppDomain.CreateDomain("testdomain");
            testdomain.Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            testdomain.Load(compiled.GetName());

            //test the assembly
            Type testtype = compiled.GetType("My.Data.Customer");
            Assert.IsNotNull(testtype);
            PropertyInfo id = testtype.GetProperty("ID");
            Assert.AreEqual(typeof(int), id.PropertyType);
            AppDomain.Unload(testdomain);
        }