Ejemplo n.º 1
0
 public void Create_WhenCreatingAttributeWithNamedArgument_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test(with:1,value:2)]", AttributeGenerator.Create(new Attribute("Test", new List <IArgument>()
     {
         new ValueArgument(1, namedArgument: "with"), new ValueArgument(2, namedArgument: "value")
     })).ToString());
 }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
 public void Create_WhenCreatingAttributeWithArguments_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test(1,2)]", AttributeGenerator.Create(new Attribute("Test", new List <IArgument>()
     {
         new ValueArgument(1), new ValueArgument(2)
     })).ToString());
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Create the syntax for a class constructor.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="body">The generated body of the constructor.</param>
        /// <param name="parameters">A list with parameters.</param>
        /// <param name="modifiers">A list with modifiers.</param>
        /// <param name="attributes">A list with attributes.</param>
        /// <returns>The declaration syntax for a constructor.</returns>
        public static ConstructorDeclarationSyntax Create(
            string className,
            BlockSyntax body,
            IEnumerable <Parameter> parameters = null,
            IEnumerable <Modifiers> modifiers  = null,
            IEnumerable <Attribute> attributes = null)
        {
            if (className == null)
            {
                throw new ArgumentNullException(nameof(className));
            }

            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(className))
                              .WithBody(body);

            if (parameters != null)
            {
                constructor = constructor.WithParameterList(ParameterGenerator.Create(parameters.ToArray()));
            }

            if (attributes != null)
            {
                constructor = constructor.WithAttributeLists(AttributeGenerator.Create(attributes.ToArray()));
            }

            if (modifiers != null)
            {
                constructor = constructor.WithModifiers(ModifierGenerator.Create(modifiers.ToArray()));
            }

            return(constructor);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create the syntax for a property of a class.
        /// </summary>
        /// <param name="property">The property to create.</param>
        /// <returns>The declaration syntax for a property.</returns>
        public static PropertyDeclarationSyntax Create(Property property)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            PropertyDeclarationSyntax propertyDeclaration;

            if (property is AutoProperty)
            {
                propertyDeclaration = CreateAutoProperty((AutoProperty)property);
            }
            else if (property is BodyProperty)
            {
                propertyDeclaration = CreateBodyProperty((BodyProperty)property);
            }
            else
            {
                throw new ArgumentException($"Unknown property type: {property.Type}, could not generate code.");
            }

            if (property.Modifiers != null)
            {
                propertyDeclaration = propertyDeclaration.WithModifiers(ModifierGenerator.Create(property.Modifiers.ToArray()));
            }

            if (property.Attributes != null)
            {
                propertyDeclaration = propertyDeclaration.WithAttributeLists(AttributeGenerator.Create(property.Attributes.ToArray()));
            }

            return(propertyDeclaration);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Create the syntax for a class constructor.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="body">The generated body of the constructor.</param>
        /// <param name="parameters">A list with parameters.</param>
        /// <param name="modifiers">A list with modifiers.</param>
        /// <param name="attributes">A list with attributes.</param>
        /// <param name="constructorInitializer">The constructor initializer</param>
        /// <param name="summary">XML documentation summary</param>
        /// <returns>The declaration syntax for a constructor.</returns>
        public static ConstructorDeclarationSyntax Create(
            string className,
            BlockSyntax body,
            IEnumerable <Parameter> parameters            = null,
            IEnumerable <Modifiers> modifiers             = null,
            IEnumerable <Attribute> attributes            = null,
            ConstructorInitializer constructorInitializer = null,
            string summary = null)
        {
            if (className == null)
            {
                throw new ArgumentNullException(nameof(className));
            }

            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(className))
                              .WithBody(body);

            if (parameters != null)
            {
                constructor = constructor.WithParameterList(ParameterGenerator.Create(parameters.ToArray()));
            }

            if (constructorInitializer != null)
            {
                constructor = constructor.WithInitializer(
                    SyntaxFactory.ConstructorInitializer(
                        constructorInitializer.ConstructorInitializerType == ConstructorInitializerTypes.Base ? SyntaxKind.BaseConstructorInitializer : SyntaxKind.ThisConstructorInitializer,
                        constructorInitializer.Arguments == null ? null : ArgumentGenerator.Create(constructorInitializer.Arguments.ToArray())));
            }

            if (attributes != null)
            {
                constructor = constructor.WithAttributeLists(AttributeGenerator.Create(attributes.ToArray()));
            }

            if (modifiers != null)
            {
                constructor = constructor.WithModifiers(ModifierGenerator.Create(modifiers.ToArray()));
            }

            if (!string.IsNullOrEmpty(summary))
            {
                constructor = constructor.WithSummary(summary, parameters);
            }

            return(constructor);
        }
Ejemplo n.º 7
0
 public void Create_WhenCreatingWithMultipleAttributes_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test][TestCase]", AttributeGenerator.Create(new Attribute("Test"), new Attribute("TestCase")).ToString());
 }
Ejemplo n.º 8
0
 public void Create_WhenCreatingWithSingleAttrbute_ShouldGenerateCorrectCode()
 {
     Assert.AreEqual("[Test]", AttributeGenerator.Create(new Attribute("Test", new List <IArgument>())).ToString());
 }
Ejemplo n.º 9
0
 public void Create_WhenNotProvidingAnyAttributes_ShouldGetEmptyString()
 {
     Assert.AreEqual(string.Empty, AttributeGenerator.Create().ToString());
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Set type attributes.
 /// </summary>
 /// <param name="attributes">A set of wanted attributes.</param>
 /// <returns>The current builder</returns>
 public TBuilder WithAttributes(params Attribute[] attributes)
 {
     _attributes = AttributeGenerator.Create(attributes);
     return((TBuilder)this);
 }