Пример #1
0
        public void CreateGenerator_GenericGeneratorWithNoTypeArguments_ThrowsError()
        {
            var attr = new GeneratorAttribute(typeof(TestGenericGenerator <,>));

            Assert.That(
                () => attr.CreateGenerator(GetDSLMethod(x => x.GetTestGenerator()), new Dictionary <string, object>()),
                Throws.ArgumentException);
        }
Пример #2
0
        public void CreateGenerator_NonGenericGeneratorFromGenericMethod_ReturnsGenerator()
        {
            var attr = new GeneratorAttribute(typeof(TestGenerator));

            var generator = attr.CreateGenerator(
                GetDSLMethod(x => x.GetTestGenericGenerator <int, string>()),
                new Dictionary <string, object>());

            Assert.That(generator, Is.Not.Null.And.InstanceOf <TestGenerator>());
        }
Пример #3
0
        public void CreateGenerator_GeneratorWithoutParameters_ReturnsGenerator()
        {
            var attr = new GeneratorAttribute(typeof(TestGenerator));

            dynamic generator = attr.CreateGenerator(GetDSLMethod(x => x.GetTestGenerator()), new Dictionary <string, object>());

            Assert.That(generator, Is.Not.Null.And.InstanceOf <TestGenerator>());
            Assert.That(generator.Length, Is.EqualTo(5));
            Assert.That(generator.Source, Is.EqualTo("The really long expression."));
        }
Пример #4
0
        public void CreateGenerator_IncorrectParameters_ThrowsError()
        {
            var attr = new GeneratorAttribute(typeof(TestGenerator));

            Assert.Throws <MissingMethodException>(
                () =>
                attr.CreateGenerator(
                    GetDSLMethod(x => x.GetTestGenerator(1, "adf")),
                    new Dictionary <string, object> {
                ["source1"] = 15, ["length"] = "The short one."
            }));
        }
Пример #5
0
        public void CreateGenerator_GenericGenerator_ReturnsGenerator()
        {
            var attr = new GeneratorAttribute(typeof(TestGenericGenerator <,>));

            dynamic generator = attr.CreateGenerator(
                GetDSLMethod(x => x.GetTestGenericGenerator("a", 33)),
                new Dictionary <string, object> {
                ["value1"] = "test", ["value2"] = 554
            });

            Assert.That(generator, Is.InstanceOf <TestGenericGenerator <string, int> >());
            Assert.That(generator.Value1, Is.EqualTo("test"));
            Assert.That(generator.Value2, Is.EqualTo(554));
        }
Пример #6
0
        public void CreateGenerator_GeneratorWithParameters_ReturnsGenerator()
        {
            var attr = new GeneratorAttribute(typeof(TestGenerator));

            dynamic generator = attr.CreateGenerator(
                GetDSLMethod(x => x.GetTestGenerator(1, "a")),
                new Dictionary <string, object> {
                ["length"] = 15, ["source"] = "The short one."
            });

            Assert.That(generator, Is.Not.Null.And.InstanceOf <TestGenerator>());
            Assert.That(generator.Length, Is.EqualTo(15));
            Assert.That(generator.Source, Is.EqualTo("The short one."));
        }
Пример #7
0
        public void CreateGenerator_WithDefaultParameters_ReturnsGenerator()
        {
            var attr = new GeneratorAttribute(typeof(TestGeneratorWithDefaultParameters));

            dynamic generator = attr.CreateGenerator(
                GetDSLMethod(x => x.GetTestGeneratorWithDefaultParameters(0, "")),
                new Dictionary <string, object> {
                ["numberInteger"] = 10, ["text2"] = "abc"
            });

            Assert.That(generator, Is.InstanceOf <TestGeneratorWithDefaultParameters>());
            Assert.That(generator.NumberInteger, Is.EqualTo(10));
            Assert.That(generator.Text1, Is.EqualTo("a"));
            Assert.That(generator.Text2, Is.EqualTo("abc"));
        }
Пример #8
0
        /// <summary> Write a Generator XML Element from attributes in a member. </summary>
        public virtual void WriteGenerator(System.Xml.XmlWriter writer, System.Reflection.MemberInfo member, GeneratorAttribute attribute, BaseAttribute parentAttribute, System.Type mappedClass)
        {
            writer.WriteStartElement( "generator" );
            // Attribute: <class>
            writer.WriteAttributeString("class", attribute.Class==null ? DefaultHelper.Get_Generator_Class_DefaultValue(member) : GetAttributeValue(attribute.Class, mappedClass));

            WriteUserDefinedContent(writer, member, null, attribute);

            System.Collections.ArrayList memberAttribs = GetSortedAttributes(member);
            int attribPos; // Find the position of the GeneratorAttribute (its <sub-element>s must be after it)
            for(attribPos=0; attribPos<memberAttribs.Count; attribPos++)
                if( memberAttribs[attribPos] is GeneratorAttribute
                    && ((BaseAttribute)memberAttribs[attribPos]).Position == attribute.Position )
                    break; // found
            int i = attribPos + 1;

            // Element: <param>
            for(; i<memberAttribs.Count; i++)
            {
                BaseAttribute memberAttrib = memberAttribs[i] as BaseAttribute;
                if( IsNextElement(memberAttrib, parentAttribute, attribute.GetType())
                    || IsNextElement(memberAttrib, attribute, typeof(ParamAttribute)) )
                    break; // next attributes are 'elements' of the same level OR for 'sub-elements'
                else
                {
                    if( memberAttrib is GeneratorAttribute )
                        break; // Following attributes are for this Generator
                    if( memberAttrib is ParamAttribute )
                        WriteParam(writer, member, memberAttrib as ParamAttribute, attribute, mappedClass);
                }
            }
            WriteUserDefinedContent(writer, member, typeof(ParamAttribute), attribute);

            writer.WriteEndElement();
        }