protected override ArgumentSyntax CreateArgumentSyntax() { var syntaxNodeOrTokens = new List <SyntaxNodeOrToken>(); foreach (var dictionaryValue in _dictionary) { syntaxNodeOrTokens.Add( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName(dictionaryValue.Key), dictionaryValue.Value.GetArgumentSyntax().Expression)); syntaxNodeOrTokens.Add(Token(SyntaxKind.CommaToken)); } if (syntaxNodeOrTokens.Any()) { syntaxNodeOrTokens.RemoveAt(syntaxNodeOrTokens.Count - 1); } return(Argument( ObjectCreationExpression(TypeGenerator.Create(type)).WithInitializer( InitializerExpression( SyntaxKind.ObjectInitializerExpression, SeparatedList <ExpressionSyntax>(syntaxNodeOrTokens.ToArray()))))); }
/// <summary> /// Create the local declaration statement syntax to declare a local variable and assign it an already created expression. /// </summary> /// <param name="name">Name of the variable.</param> /// <param name="type">Type of the variable.</param> /// <param name="expressionSyntax">An already created expression to assign to the variable.</param> /// <param name="castTo">Cast the expression to this type.</param> /// <param name="useVarKeyword">Will use "var" keyword if true, otherwise the type.</param> /// <returns>The generated local declaration statement.</returns> public LocalDeclarationStatementSyntax DeclareAndAssign(string name, Type type, ExpressionSyntax expressionSyntax, Type castTo = null, bool useVarKeyword = true) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Value cannot be null or empty.", nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } if (expressionSyntax == null) { throw new ArgumentNullException(nameof(expressionSyntax)); } if (castTo != null && castTo != typeof(void)) { expressionSyntax = CastExpression(TypeGenerator.Create(castTo), expressionSyntax); } return(LocalDeclarationStatement(VariableDeclaration(useVarKeyword ? IdentifierName("var") : TypeGenerator.Create(type)) .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(name)) .WithInitializer(EqualsValueClause(expressionSyntax)))))); }
/// <summary> /// Create the syntax for a field of a class. /// </summary> /// <param name="field">Field to create.</param> /// <returns>The declaration syntax for a field.</returns> public static FieldDeclarationSyntax Create(Field field) { if (field == null) { throw new ArgumentNullException(nameof(field)); } var variableDeclarator = VariableDeclarator(Identifier(field.Name)); if (field.InitializeWith != null) { variableDeclarator = variableDeclarator.WithInitializer(EqualsValueClause(field.InitializeWith)); } var fieldDeclaration = FieldDeclaration(VariableDeclaration(TypeGenerator.Create(field.Type), SeparatedList(new[] { variableDeclarator }))); if (field.Modifiers != null) { fieldDeclaration = fieldDeclaration.WithModifiers(ModifierGenerator.Create(field.Modifiers.ToArray())); } if (field.Attributes != null) { fieldDeclaration = fieldDeclaration.WithAttributeLists(AttributeGenerator.Create(field.Attributes.ToArray())); } return(fieldDeclaration); }
protected override ArgumentSyntax CreateArgumentSyntax() { if (_castTo != typeof(void)) { return(SyntaxFactory.Argument(SyntaxFactory.CastExpression(TypeGenerator.Create(_castTo), _invocation))); } return(SyntaxFactory.Argument(_invocation)); }
protected override ArgumentSyntax CreateArgumentSyntax() { if (_genericTypes != null && _genericTypes.Any()) { return(SyntaxFactory.Argument(SyntaxFactory.ObjectCreationExpression(GenericGenerator.Create(_type.Name, _genericTypes.ToArray())).WithArgumentList(ArgumentGenerator.Create(_arguments.ToArray())))); } return(SyntaxFactory.Argument(SyntaxFactory.ObjectCreationExpression(TypeGenerator.Create(_type)).WithArgumentList(ArgumentGenerator.Create(_arguments.ToArray())))); }
protected BaseListSyntax CreateBaseList() { if (!_inheritance.Any()) { return(null); } return(BaseList( SeparatedList <BaseTypeSyntax>( _inheritance.Select(i => SimpleBaseType(TypeGenerator.Create(i))), _inheritance.Select(i => Token(SyntaxKind.CommaToken))))); }
public void CreateNonTypeReturnsCorrectResult(object request) { // Arrange var sut = new TypeGenerator(); // Act var dummyContext = new DelegatingSpecimenContext(); var result = sut.Create(request, dummyContext); // Assert var expected = new NoSpecimen(); Assert.Equal(expected, result); }
private MethodDeclarationSyntax BuildMethodBase() { if (_returnType != null) { return(MethodDeclaration(TypeGenerator.Create(_returnType), Identifier(_name))); } else { return(MethodDeclaration( PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(_name))); } }
public void CreateNonTypeReturnsCorrectResult(object request) { // Fixture setup var sut = new TypeGenerator(); // Exercise system var dummyContext = new DelegatingSpecimenContext(); var result = sut.Create(request, dummyContext); // Verify outcome var expected = new NoSpecimen(); Assert.Equal(expected, result); // Teardown }
private static PropertyDeclarationSyntax CreateAutoProperty(AutoProperty property) { var propertyDeclaration = PropertyDeclaration( TypeGenerator.Create(property.Type), Identifier(property.Name)) .AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.GetAccessorDeclaration, null, property.GetModifiers)); if (property.PropertyType == PropertyTypes.GetAndSet) { propertyDeclaration = propertyDeclaration.AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.SetAccessorDeclaration, null, property.SetModifiers)); } return(propertyDeclaration); }
private static PropertyDeclarationSyntax CreateBodyProperty(BodyProperty property) { var propertyDeclaration = PropertyDeclaration( TypeGenerator.Create(property.Type), Identifier(property.Name)) .AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.GetAccessorDeclaration, property.GetBody, property.GetModifiers)); if (property.SetBody != null) { propertyDeclaration = propertyDeclaration.AddAccessorListAccessors(CreateAccessDeclaration(SyntaxKind.SetAccessorDeclaration, property.SetBody, property.SetModifiers)); } return(propertyDeclaration); }
private static PropertyDeclarationSyntax CreateAutoProperty(AutoProperty property) { var propertyDeclaration = PropertyDeclaration( TypeGenerator.Create(property.Type), Identifier(property.Name)) .AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.GetAccessorDeclaration). WithSemicolonToken(Token(SyntaxKind.SemicolonToken))); if (property.PropertyType == PropertyTypes.GetAndSet) { propertyDeclaration = propertyDeclaration.AddAccessorListAccessors(AccessorDeclaration(SyntaxKind.SetAccessorDeclaration). WithSemicolonToken(Token(SyntaxKind.SemicolonToken))); } return(propertyDeclaration); }
/// <summary> /// Create the expression statement syntax to declare a local variable. /// </summary> /// <param name="variableName">Name of the variable.</param> /// <param name="type">Type of the variable.</param> /// <returns>The declared expression statement syntax.</returns> public LocalDeclarationStatementSyntax Declare(string variableName, Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (string.IsNullOrEmpty(variableName)) { throw new ArgumentException("Value cannot be null or empty.", nameof(variableName)); } return(LocalDeclarationStatement( VariableDeclaration(TypeGenerator.Create(type)) .WithVariables(SingletonSeparatedList <VariableDeclaratorSyntax>( VariableDeclarator(Identifier(variableName)))))); }
/// <summary> /// Create the expression statement syntax to assign a variable a new instance with the "new" keyword. /// </summary> /// <param name="name">Name of the variable.</param> /// <param name="type">Type of the variable.</param> /// <param name="arguments">Arguments in the class constructor.</param> /// <returns>The generated assign declaration statement.</returns> public ExpressionStatementSyntax Assign(string name, Type type, ArgumentListSyntax arguments) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Value cannot be null or empty.", nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } return(ExpressionStatement( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, IdentifierName(name), ObjectCreationExpression(TypeGenerator.Create(type)).WithArgumentList(arguments).WithNewKeyword(Token(SyntaxKind.NewKeyword))))); }
private ClassDeclarationSyntax BuildClassBase() { if (_inheritance.Any()) { var syntaxNodeOrToken = new SyntaxNodeOrToken[(_inheritance.Count * 2) - 1]; for (int n = 0; n < (_inheritance.Count * 2) - 1; n += 2) { syntaxNodeOrToken[n] = SyntaxFactory.SimpleBaseType(TypeGenerator.Create(_inheritance[n])); if (n + 1 < _inheritance.Count - 1) { syntaxNodeOrToken[n + 1] = SyntaxFactory.Token(SyntaxKind.CommaToken); } } return(SyntaxFactory.ClassDeclaration(_name).WithBaseList(SyntaxFactory.BaseList(SyntaxFactory.SeparatedList <BaseTypeSyntax>(syntaxNodeOrToken))).WithModifiers(ModifierGenerator.Create(_modifiers.ToArray()))); } return(SyntaxFactory.ClassDeclaration(_name).WithModifiers(ModifierGenerator.Create(_modifiers.ToArray()))); }
protected BaseListSyntax CreateBaseList() { if (!_inheritance.Any()) { return(null); } var syntaxNodeOrToken = new SyntaxNodeOrToken[(_inheritance.Count * 2) - 1]; for (int n = 0; n < (_inheritance.Count * 2) - 1; n += 2) { syntaxNodeOrToken[n] = SyntaxFactory.SimpleBaseType(TypeGenerator.Create(_inheritance[n])); if (n + 1 < _inheritance.Count - 1) { syntaxNodeOrToken[n + 1] = SyntaxFactory.Token(SyntaxKind.CommaToken); } } return(SyntaxFactory.BaseList(SyntaxFactory.SeparatedList <BaseTypeSyntax>(syntaxNodeOrToken))); }
protected override ArgumentSyntax CreateArgumentSyntax() { SyntaxNodeOrToken[] syntaxNodeOrTokens = new SyntaxNodeOrToken[0]; if (_arguments.Any()) { syntaxNodeOrTokens = new SyntaxNodeOrToken[(_arguments.Count * 2) - 1]; var argumentIndex = 0; for (int i = 0; i < syntaxNodeOrTokens.Length; i += 2) { syntaxNodeOrTokens[i] = _arguments[argumentIndex].GetArgumentSyntax().Expression; if ((i + 1) < syntaxNodeOrTokens.Length) { syntaxNodeOrTokens[i + 1] = Token(SyntaxKind.CommaToken); } argumentIndex++; } } return(Argument(ArrayCreationExpression(ArrayType(TypeGenerator.Create(_type)) .WithRankSpecifiers(SingletonList <ArrayRankSpecifierSyntax>(ArrayRankSpecifier(SingletonSeparatedList <ExpressionSyntax>(OmittedArraySizeExpression()))))) .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression, SeparatedList <ExpressionSyntax>(syntaxNodeOrTokens))))); }
/// <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)); }
protected override ArgumentSyntax CreateArgumentSyntax() { var syntaxNodeOrTokens = new List <SyntaxNodeOrToken>(); foreach (var dictionaryValue in _dictionary) { syntaxNodeOrTokens.Add( AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, ImplicitElementAccess() .WithArgumentList( BracketedArgumentList( SingletonSeparatedList <ArgumentSyntax>( Argument( IdentifierName( typeof(TKey) == typeof(string) ? $"\"{dictionaryValue.Key}\"" : dictionaryValue.Key.ToString()))))), dictionaryValue.Value.GetArgumentSyntax().Expression)); syntaxNodeOrTokens.Add(Token(SyntaxKind.CommaToken)); } if (syntaxNodeOrTokens.Any()) { syntaxNodeOrTokens.RemoveAt(syntaxNodeOrTokens.Count - 1); } return(Argument(ObjectCreationExpression(GenericName(Identifier("Dictionary")) .WithTypeArgumentList(TypeArgumentList( SeparatedList <TypeSyntax>( new SyntaxNodeOrToken[] { TypeGenerator.Create(typeof(TKey)), Token(SyntaxKind.CommaToken), TypeGenerator.Create(typeof(TValue)) })))).WithInitializer(InitializerExpression( SyntaxKind.ObjectInitializerExpression, SeparatedList <ExpressionSyntax>(syntaxNodeOrTokens.ToArray()))))); }
/// <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))); }
public void Create_WhenCreatingWithNoPredfinedGenericType_ShouldGenerateCode() { Assert.AreEqual("List<string>", TypeGenerator.Create(typeof(List <string>)).ToString()); }
public void Create_WhenCreatingCustomType_ShouldGenerateCode() { Assert.AreEqual("MyNewClass", TypeGenerator.Create(CustomType.Create("MyNewClass")).ToString()); }
/// <summary> /// Create the local declaration statement syntax to declare a local variable and assign it a new class instance. /// </summary> /// <param name="name">Name of variable.</param> /// <param name="type">Type of the variable.</param> /// <param name="arguments">Arguments to use when creating the variable.</param> /// <param name="useVarKeyword">Will use "var" keyword if true, otherwise the type.</param> /// <returns>The generated local declaration statement.</returns> public LocalDeclarationStatementSyntax DeclareAndAssign(string name, Type type, ArgumentListSyntax arguments, bool useVarKeyword = true) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Value cannot be null or empty.", nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } return(LocalDeclarationStatement(VariableDeclaration(useVarKeyword ? IdentifierName("var") : TypeGenerator.Create(type)) .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(name)) .WithInitializer( EqualsValueClause( ObjectCreationExpression(TypeGenerator.Create(type)).WithArgumentList(arguments) .WithNewKeyword(Token(SyntaxKind.NewKeyword)))))))); }
/// <summary> /// Create a local declaration statement which declare a local variable and assign it a complex reference. /// </summary> /// <param name="name">Name of variable.</param> /// <param name="type">Type of the variable.</param> /// <param name="reference">Value of the variable.</param> /// <param name="useVarKeyword">Will use "var" keyword if true, otherwise the type.</param> /// <returns>The generated local declaration statement.</returns> public LocalDeclarationStatementSyntax DeclareAndAssign(string name, Type type, VariableReference reference, bool useVarKeyword = true) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Value cannot be null or empty.", nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } if (reference == null) { throw new ArgumentNullException(nameof(reference)); } return(LocalDeclarationStatement(VariableDeclaration(useVarKeyword ? IdentifierName("var") : TypeGenerator.Create(type)) .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(name)) .WithInitializer(EqualsValueClauseFactory.GetEqualsValueClause(reference).WithEqualsToken(Token(SyntaxKind.EqualsToken))))))); }
/// <summary> /// Create the local declaration statement syntax to declare a local variable and assign it a string value. /// </summary> /// <param name="name">Name of variable.</param> /// <param name="value">Value to assign variable.</param> /// <param name="useVarKeyword">Will use "var" keyword if true, otherwise the type.</param> /// <returns>The generated local declaration statement.</returns> public LocalDeclarationStatementSyntax DeclareAndAssign(string name, string value, bool useVarKeyword = true) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } return(LocalDeclarationStatement(VariableDeclaration(useVarKeyword ? IdentifierName("var") : TypeGenerator.Create(typeof(string))) .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(name)) .WithInitializer(EqualsValueClauseFactory.GetEqualsValueClause($@"""{value}""").WithEqualsToken(Token(SyntaxKind.EqualsToken))))))); }
public void Create_WhenCreatingArrayWithPredefinedType_ShouldGenerateCode() { Assert.AreEqual("int[]", TypeGenerator.Create(typeof(int[])).ToString()); }
public void Create_WhenCreatingArrayWithClassType_ShouldGenerateCode() { Assert.AreEqual("TypeGeneratorTests[]", TypeGenerator.Create(typeof(TypeGeneratorTests[])).ToString()); }
/// <summary> /// Create the local declaration statement syntax to declare a local variable and assign it to a binary expression. /// </summary> /// <param name="name">Name of the variable.</param> /// <param name="type">Type of the variable.</param> /// <param name="binaryExpression">The binary expression.</param> /// <param name="useVarKeyword">Will use "var" keyword if true, otherwise the type.</param> /// <returns>The generated local declaration statement.</returns> public LocalDeclarationStatementSyntax DeclareAndAssign(string name, Type type, IBinaryExpression binaryExpression, bool useVarKeyword = true) { if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Value cannot be null or empty.", nameof(name)); } if (type == null) { throw new ArgumentNullException(nameof(type)); } if (binaryExpression == null) { throw new ArgumentNullException(nameof(binaryExpression)); } return(LocalDeclarationStatement(VariableDeclaration(useVarKeyword ? IdentifierName("var") : TypeGenerator.Create(type)) .WithVariables(SingletonSeparatedList(VariableDeclarator(Identifier(name)) .WithInitializer(EqualsValueClause(binaryExpression.GetBinaryExpression())))))); }
public void Create_WhenCreatingArrayWithGenericClassType_ShouldGenerateCode() { Assert.AreEqual("List<string>[]", TypeGenerator.Create(typeof(List <string>[])).ToString()); }
protected override ArgumentSyntax CreateArgumentSyntax() { return(SyntaxFactory.Argument(SyntaxFactory.TypeOfExpression(TypeGenerator.Create(_type)))); }