Esempio n. 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);
        }
Esempio n. 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);
        }
        /// <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);
        }