Skip to content
Jeff Ward edited this page Jan 6, 2022 · 3 revisions

Builders

Testura.Code provides two different builders - one for methods and one for classes.

Method builder

Methods

  • WithParameters
  • WithReturnType
  • WithAttributes
  • WithBody
  • WithSummary
  • WithModifiers

Example

Usage

new MethodBuilder("Main")
			.WithParameters(new Parameter("args", typeof(string[])))
			.WithBody(
				BodyGenerator.Create(
					Statement.Expression.Invoke("Console", "WriteLine", new List<IArgument>() { new ValueArgument("Hello world") }).AsStatement(),
					Statement.Expression.Invoke("Console", "ReadLine").AsStatement()
					))
			.WithModifiers(Modifiers.Public, Modifiers.Static)
			.Build()

Result

public static Main(string[] args)
{
	Console.WriteLine("Hello world");
	Console.ReadLine();
}

Class builder

Methods

  • WithUsings
  • WithModifiers
  • ThatInheritfrom
  • WithAttributes
  • WithFields
  • WithConsturctor
  • WithMethods
  • WithProperties
  • Build

Example

Usage

var classBuilder = new ClassBuilder("Cat", "Models");
            var @class = classBuilder
                .WithUsings("System")
                .WithProperties(
                    PropertyGenerator.Create(new AutoProperty("Name", typeof(string), PropertyTypes.GetAndSet, new List<Modifiers> { Modifiers.Public } )), 
                    PropertyGenerator.Create(new AutoProperty("Age", typeof(int), PropertyTypes.GetAndSet, new List<Modifiers> { Modifiers.Public })))
                .WithConstructor(
                    ConstructorGenerator.Create(
                        "Cat",
                        BodyGenerator.Create(
                            Statement.Declaration.Assign("Name", ReferenceGenerator.Create(new VariableReference("name"))),
                            Statement.Declaration.Assign("Age", ReferenceGenerator.Create(new VariableReference("age")))),
                        new List<Parameter> { new Parameter("name", typeof(string)), new Parameter("age", typeof(int)) },
                        new List<Modifiers> { Modifiers.Public }))
                       .Build();

Result

using System;

namespace Models
{
    public class Cat
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public Cat(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }
}
Clone this wiki locally