Builders - Testura/Testura.Code GitHub Wiki
Testura.Code provides two different builders - one for methods and one for classes.
- WithParameters
- WithReturnType
- WithAttributes
- WithBody
- WithSummary
- WithModifiers
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()
public static Main(string[] args)
{
Console.WriteLine("Hello world");
Console.ReadLine();
}
- WithUsings
- WithModifiers
- ThatInheritfrom
- WithAttributes
- WithFields
- WithConsturctor
- WithMethods
- WithProperties
- Build
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();
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;
}
}
}