public void Test_CreateModelClass() { var classBuilder = new ClassBuilder("Cat", "Models"); var @class = classBuilder .WithUsings("System") .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 })) .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 }))) .Build(); Assert.AreEqual( "usingSystem;namespaceModels{publicclassCat{publicCat(stringname,intage){Name=name;Age=age;}publicstringName{get;set;}publicintAge{get;set;}}}", @class.ToString()); }
/// <summary> /// Get the correct equals value clause for a specific type /// </summary> /// <param name="value">Value we want to put the variable equal as</param> /// <returns>The correct equals value clause</returns> internal static EqualsValueClauseSyntax GetEqualsValueClause(object value) { if (value is int) { return(EqualsValueClause( LiteralExpression( SyntaxKind.NumericLiteralExpression, Literal(TriviaList(), value.ToString(), (int)value, TriviaList())))); } if (value is string) { return(EqualsValueClause( LiteralExpression( SyntaxKind.StringLiteralExpression, Literal(TriviaList(), value.ToString(), (string)value, TriviaList())))); } if (value is VariableReference) { return(EqualsValueClause(ReferenceGenerator.Create((VariableReference)value))); } throw new NotSupportedException("Not a supported value"); }
/// <summary> /// Create the return statement syntax to return of a reference. /// </summary> /// <param name="variableReference">Reference that we should return.</param> /// <returns>The declared return statement syntax.</returns> public ReturnStatementSyntax Return(VariableReference variableReference) { if (variableReference == null) { throw new ArgumentNullException(nameof(variableReference)); } return(ReturnStatement(ReferenceGenerator.Create(variableReference))); }
/// <summary> /// Create the expression statement syntax to assign a variable to another reference. /// </summary> /// <param name="name">Name of variable.</param> /// <param name="reference">Reference value to assign to the variable.</param> /// <param name="castTo">If we should do a cast while assign the variable.</param> /// <returns>The generated assign declaration syntax.</returns> public ExpressionStatementSyntax Assign(string name, VariableReference reference, Type castTo = null) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Value cannot be null or empty.", nameof(name)); } if (reference == null) { throw new ArgumentNullException(nameof(reference)); } return(Assign(name, ReferenceGenerator.Create(reference), castTo)); }
public void Test_CreateModelClassWithBodyProperties() { var classBuilder = new ClassBuilder("Cat", "Models"); var @class = classBuilder .WithUsings("System") .WithFields( new Field("_name", typeof(string), new List <Modifiers>() { Modifiers.Private }), new Field("_age", typeof(int), new List <Modifiers>() { Modifiers.Private })) .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 })) .WithProperties( PropertyGenerator.Create( new BodyProperty( "Name", typeof(string), BodyGenerator.Create(Statement.Jump.Return(new VariableReference("_name"))), BodyGenerator.Create(Statement.Declaration.Assign("_name", new ValueKeywordReference())), new List <Modifiers> { Modifiers.Public })), PropertyGenerator.Create( new BodyProperty( "Age", typeof(int), BodyGenerator.Create(Statement.Jump.Return(new VariableReference("_age"))), BodyGenerator.Create(Statement.Declaration.Assign("_age", new ValueKeywordReference())), new List <Modifiers> { Modifiers.Public }))) .Build(); Assert.AreEqual( "usingSystem;namespaceModels{publicclassCat{privatestring_name;privateint_age;publicCat(stringname,intage){Name=name;Age=age;}publicstringName{get{return_name;}set{_name=value;}}publicintAge{get{return_age;}set{_age=value;}}}}", @class.ToString()); }
/// <summary> /// Create the expression statement syntax to invoke a method in a chain (for example <c>myVariable.myProperty.MyMethod()</c>) /// </summary> /// <param name="reference">The reference chain.</param> /// <returns>A invocation object with both statement and expression.</returns> public Invocation Invoke(VariableReference reference) { if (reference == null) { throw new ArgumentNullException(nameof(reference)); } if (!(reference is MethodReference)) { var member = reference.GetLastMember(); if (!(member is MethodReference)) { throw new ArgumentException($"{nameof(reference)} or last member in chain must be a method reference"); } } return(new Invocation((InvocationExpressionSyntax)ReferenceGenerator.Create(reference))); }
/// <summary> /// Create the expression statement syntax to assign a reference to a binary expression. /// </summary> /// <param name="reference">Reference that should be assigned.</param> /// <param name="binaryExpression">The binary expression.</param> /// <returns>The generated assign declaration syntax.</returns> public ExpressionStatementSyntax Assign(VariableReference reference, IBinaryExpression binaryExpression) { if (reference == null) { throw new ArgumentNullException(nameof(reference)); } if (binaryExpression == null) { throw new ArgumentNullException(nameof(binaryExpression)); } return (ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, ReferenceGenerator.Create(reference), binaryExpression.GetBinaryExpression()))); }
/// <summary> /// Initializes a new instance of the <see cref="ConditionalBinaryExpression"/> class. /// </summary> /// <param name="leftReference">The left reference.</param> /// <param name="rightReference">The right reference.</param> /// <param name="conditional">The conditional statement between two expressions.</param> public ConditionalBinaryExpression( VariableReference leftReference, VariableReference rightReference, ConditionalStatements conditional) { if (leftReference == null) { throw new ArgumentNullException(nameof(leftReference)); } if (rightReference == null) { throw new ArgumentNullException(nameof(rightReference)); } _leftExpression = ReferenceGenerator.Create(leftReference); _rightExpression = ReferenceGenerator.Create(rightReference); _conditional = conditional; }
/// <summary> /// Create the expression statement syntax to assign a reference to another reference. For example a property to a property. /// </summary> /// <param name="reference">Reference that should be assigned.</param> /// <param name="valueReference">Reference that we should assign to another reference.</param> /// <param name="castTo">If we should do a cast while assign the variabl.</param> /// <returns>The generated assign declaration syntax.</returns> public ExpressionStatementSyntax Assign(VariableReference reference, VariableReference valueReference, Type castTo = null) { if (reference == null) { throw new ArgumentNullException(nameof(reference)); } if (valueReference == null) { throw new ArgumentNullException(nameof(valueReference)); } if (reference is MethodReference || reference.GetLastMember() is MethodReference) { throw new ArgumentException($"{nameof(reference)} to assign can't be a method"); } return(Assign(reference, ReferenceGenerator.Create(valueReference), castTo)); }
/// <summary> /// Initializes a new instance of the <see cref="MathBinaryExpression"/> class. /// </summary> /// <param name="leftReference">The left reference.</param> /// <param name="rightBinaryExpression">The right other binary expression.</param> /// <param name="mathOperator">The math operator to generate.</param> /// <param name="useParenthes">If we should generate with paranthes surounding the the binary expression.</param> public MathBinaryExpression( VariableReference leftReference, MathBinaryExpression rightBinaryExpression, MathOperators mathOperator, bool useParenthes = false) { if (leftReference == null) { throw new ArgumentNullException(nameof(leftReference)); } if (rightBinaryExpression == null) { throw new ArgumentNullException(nameof(rightBinaryExpression)); } _leftExpression = ReferenceGenerator.Create(leftReference); _rightExpression = rightBinaryExpression.GetBinaryExpression(); _mathOperator = mathOperator; _useParenthes = useParenthes; }
/// <summary> /// Create the foreach statement syntax for a foreach loop with a reference as enumerable. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="varialeType">The variable type.</param> /// <param name="enumerableReference">Reference to the enumerable.</param> /// <param name="body">Body of the foreach loop.</param> /// <param name="useVar">If we should use the var keyword instead of the type.</param> /// <returns>The declared foreach statement syntax.</returns> public ForEachStatementSyntax ForEach(string variableName, Type varialeType, VariableReference enumerableReference, BlockSyntax body, bool useVar = true) { if (string.IsNullOrEmpty(variableName)) { throw new ArgumentException("Value cannot be null or empty.", nameof(variableName)); } if (varialeType == null) { throw new ArgumentNullException(nameof(varialeType)); } if (enumerableReference == null) { throw new ArgumentNullException(nameof(enumerableReference)); } return(ForEachStatement( useVar ? IdentifierName("var") : TypeGenerator.Create(varialeType), Identifier(variableName), ReferenceGenerator.Create(enumerableReference), body)); }
/// <summary> /// Create the expression statement syntax to assign a reference to another expression. /// </summary> /// <param name="reference">Reference that should be assigned.</param> /// <param name="expressionSyntax">Expression that we should assign to reference.</param> /// <param name="castTo">If we should do a cast while assign the variable.</param> /// <returns>The generated assign declaration syntax.</returns> public ExpressionStatementSyntax Assign(VariableReference reference, ExpressionSyntax expressionSyntax, Type castTo = null) { if (reference == null) { throw new ArgumentNullException(nameof(reference)); } if (expressionSyntax == null) { throw new ArgumentNullException(nameof(expressionSyntax)); } if (castTo != null && castTo != typeof(void)) { expressionSyntax = CastExpression(TypeGenerator.Create(castTo), expressionSyntax); } return (ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, ReferenceGenerator.Create(reference), expressionSyntax))); }
/// <summary> /// Create the for statement syntax for a for loop with a reference for start and stop. /// </summary> /// <param name="start">Reference for start.</param> /// <param name="end">Reference for end.</param> /// <param name="variableName">Variable name in loop.</param> /// <param name="body">Body inside loop.</param> /// <returns>The declared for statement syntax.</returns> public ForStatementSyntax For(VariableReference start, VariableReference end, string variableName, BlockSyntax body) { if (start == null) { throw new ArgumentNullException(nameof(start)); } if (end == null) { throw new ArgumentNullException(nameof(end)); } if (string.IsNullOrEmpty(variableName)) { throw new ArgumentException("Value cannot be null or empty.", nameof(variableName)); } return(ForStatement( VariableDeclaration( PredefinedType(Token(SyntaxKind.IntKeyword)), SeparatedList(new[] { VariableDeclarator( Identifier(variableName), null, EqualsValueClause(ReferenceGenerator.Create(start))) })), SeparatedList <ExpressionSyntax>(), BinaryExpression( SyntaxKind.LessThanExpression, IdentifierName(variableName), ReferenceGenerator.Create(end)), SeparatedList <ExpressionSyntax>(new[] { PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, IdentifierName(variableName)) }), body)); }
public void Create_WhenCreatingVariableRefernceWithChainedMembers_ShouldGenerateCorrectCode() { Assert.AreEqual("myVariable.MyMethod().MyProperty", ReferenceGenerator.Create(new VariableReference("myVariable", new MethodReference("MyMethod", new MemberReference("MyProperty"), new List <IArgument>()))).ToString()); }
public void Create_WhenCreatingVariableRefernceWithPropertyMember_ShouldGenerateCorrectCode() { Assert.AreEqual("myVariable.Property", ReferenceGenerator.Create(new VariableReference("myVariable", new MemberReference("Property"))).ToString()); }
public void Create_WhenCreatingVariableRefernceWithMethodMemberThatHasArgument_ShouldGenerateCorrectCode() { Assert.AreEqual("myVariable.MyMethod(1,\"test\")", ReferenceGenerator.Create(new VariableReference("myVariable", new MethodReference("MyMethod", new List <IArgument> { new ValueArgument(1), new ValueArgument("test") }))).ToString()); }
public void Create_WhenCreatingVariableRefernce_ShouldGenerateCorrectCode() { Assert.AreEqual("myVariable", ReferenceGenerator.Create(new VariableReference("myVariable")).ToString()); }
public void Create_WhenCreatingFieldWithInitializer_ShouldGenerateCorrectCode() { Assert.AreEqual("ILogger_logger=LoggerService.Logger();", FieldGenerator.Create(new Field("_logger", CustomType.Create("ILogger"), initializeWith: ReferenceGenerator.Create(new VariableReference("LoggerService", new MethodReference("Logger"))))).ToString()); }
public void Test_CreateClassWithRegionWithMultipleMembers() { var classBuilder = new ClassBuilder("Cat", "Models"); var @class = classBuilder .WithUsings("System") .WithRegions(new RegionBuilder("MyRegion") .WithFields( new Field("_name", typeof(string), new List <Modifiers>() { Modifiers.Private }), new Field("_age", typeof(int), new List <Modifiers>() { Modifiers.Private })) .WithProperties(PropertyGenerator.Create(new AutoProperty("Name", typeof(string), 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()) .Build(); Assert.AreEqual( "usingSystem;namespaceModels{publicclassCat{#region MyRegion \nprivatestring_name;privateint_age;publicstringName{get;set;}publicCat(stringname,intage){Name=name;Age=age;}#endregion}}", @class.ToString()); }
public void Test_CreateClassWithMethodThatHaveMultipleSUmmarysAndSingleLineComments() { var classBuilder = new ClassBuilder("Cat", "Models"); var @class = classBuilder .WithUsings("System") .WithSummary("My class summary") .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), xmlDocumentation: "My parameter") }, new List <Modifiers> { Modifiers.Public }, summary: "MyConstructor summary")) .WithProperties(new AutoProperty("MyProperty", typeof(int), PropertyTypes.GetAndSet, summary: "MyPropertySummary")) .WithFields( new Field("_name", typeof(string), new List <Modifiers>() { Modifiers.Private }, summary: "My field summary")) .WithMethods(new MethodBuilder("MyMethod") .WithParameters(new Parameter("MyParameter", typeof(string))) .WithBody( BodyGenerator.Create( Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment above").WithComment("hej"), Statement.Declaration.Declare("hello", typeof(int)).WithComment("My comment to the side", CommentPosition.Right) )) .Build()) .Build(); Assert.AreEqual( "usingSystem;namespaceModels{/// <summary>\n/// My class summary\n/// </summary>\npublicclassCat{/// <summary>\n/// MyConstructor summary\n/// </summary>\n/// <param name=\"age\">My parameter</param>\npublicCat(stringname,intage){Name=name;Age=age;}/// <summary>\n/// MyPropertySummary\n/// </summary>\nintMyProperty{get;set;}/// <summary>\n/// My field summary\n/// </summary>\nprivatestring_name;voidMyMethod(stringMyParameter){//hej\ninthello;inthello; //My comment to the side\n}}}", @class.ToString()); }
protected override ArgumentSyntax CreateArgumentSyntax() { return(SyntaxFactory.Argument(ReferenceGenerator.Create(_reference))); }