public void FindFittingEnumerationTest()
        {
            CodeTypeDeclaration  decl        = new CodeTypeDeclaration();
            IEnumerable <string> enumValues  = new[] { "foo", "bar", "42" };
            IEnumerable <string> enumValues2 = new[] { "foo", "bar", "cake" };
            IEnumerable <string> enumValues3 = new[] { "foo", "bar" };

            // Test parameter validation.
            Assert.Throws <ArgumentNullException>(() => DecoratorUtil.FindFittingEnumeration(decl, null, null));
            Assert.Throws <ArgumentNullException>(() => DecoratorUtil.FindFittingEnumeration(null, enumValues, null));

            // Test with empty class.
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues, null));

            // Add an enum, and check whether it can be found.
            CodeTypeDeclaration testEnum = EnumResourceDecorator.GenerateEnum(
                decl, "SomeName", "SomeDescription", enumValues, null);

            decl.Members.Add(testEnum);
            Assert.AreEqual(testEnum.Name, DecoratorUtil.FindFittingEnumeration(decl, enumValues, null).BaseType);

            // Confirm that the other values are not found.
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues2, null));
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues3, null));

            // Check that the code also works with two enumerations
            CodeTypeDeclaration testEnum3 = EnumResourceDecorator.GenerateEnum(
                decl, "SomeOtherName", "SomeDescription", enumValues3, null);

            decl.Members.Add(testEnum3);

            Assert.AreEqual(testEnum.Name, DecoratorUtil.FindFittingEnumeration(decl, enumValues, null).BaseType);
            Assert.AreEqual(testEnum3.Name, DecoratorUtil.FindFittingEnumeration(decl, enumValues3, null).BaseType);
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues2, null));
        }
        public void TestGetParameterTypeReference()
        {
            MockParameter param = new MockParameter()
            {
                Name = "Parameter"
            };
            CodeTypeDeclaration decl = new CodeTypeDeclaration();
            CodeTypeReference   refType;

            param.IsRequired = true;

            // Normal string.
            param.ValueType = "string";
            refType         = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(string).FullName, refType.BaseType);

            // Normal int
            param.ValueType = "integer";
            refType         = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(long).FullName, refType.BaseType);

            // optional int
            param.IsRequired = false;
            param.ValueType  = "integer";
            refType          = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(Nullable <>).FullName, refType.BaseType);
            Assert.AreEqual(1, refType.TypeArguments.Count);
            Assert.AreEqual(typeof(Int64).FullName, refType.TypeArguments[0].BaseType);

            // Enumeration
            param.IsRequired            = true;
            param.ValueType             = "string";
            param.EnumValues            = new[] { "TestA", "TestB" };
            param.EnumValueDescriptions = new[] { "DescA", "DescB" };
            decl.Members.Add(EnumResourceDecorator.GenerateEnum(
                                 decl, "TestEnum", null, param.EnumValues, param.EnumValueDescriptions));
            refType = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual("TestEnum", refType.BaseType);

            // Optional enumeration
            param.IsRequired            = false;
            param.ValueType             = "string";
            param.EnumValues            = new[] { "TestA", "TestB" };
            param.EnumValueDescriptions = new[] { "DescA", "DescB" };
            refType = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(Nullable <>).FullName, refType.BaseType);
            Assert.AreEqual(1, refType.TypeArguments.Count);
            Assert.AreEqual("TestEnum", refType.TypeArguments[0].BaseType);
        }
        public void IsFittingEnumCommectTest()
        {
            var decl = new CodeTypeDeclaration();
            IEnumerable <string> enumValues    = new[] { "foo", "bar", "42" };
            IEnumerable <string> enumCommentsA = new[] { "fish", "dolphins", "mice" };
            IEnumerable <string> enumCommentsB = new[] { "fishy", "dolphiny", "micy" };
            IEnumerable <string> enumCommentsC = null;

            // Add an enum, and check whether it can be found.
            CodeTypeDeclaration testEnum = EnumResourceDecorator.GenerateEnum(
                decl, "SomeName", "SomeDescription", enumValues, enumCommentsA);

            decl.Members.Add(testEnum);
            Assert.IsTrue(DecoratorUtil.IsFittingEnum(testEnum, enumValues, enumCommentsA));
            Assert.IsFalse(DecoratorUtil.IsFittingEnum(testEnum, enumValues, enumCommentsB));
            Assert.IsFalse(DecoratorUtil.IsFittingEnum(testEnum, enumValues, enumCommentsC));
        }
        public void IsFittingEnumTest()
        {
            var decl = new CodeTypeDeclaration();
            IEnumerable <string> enumValues  = new[] { "foo", "bar", "42" };
            IEnumerable <string> enumValues2 = new[] { "foo", "bar", "cake" };
            IEnumerable <string> enumValues3 = new[] { "foo", "bar" };

            // Add an enum, and check whether it can be found.
            CodeTypeDeclaration testEnum = EnumResourceDecorator.GenerateEnum(
                decl, "SomeName", "SomeDescription", enumValues, null);

            decl.Members.Add(testEnum);
            Assert.IsTrue(DecoratorUtil.IsFittingEnum(testEnum, enumValues, null));
            Assert.IsFalse(DecoratorUtil.IsFittingEnum(testEnum, enumValues2, null));
            Assert.IsFalse(DecoratorUtil.IsFittingEnum(testEnum, enumValues3, null));
            Assert.IsFalse(DecoratorUtil.IsFittingEnum(testEnum, Enumerable.Empty <string>(), null));
        }
Exemplo n.º 5
0
        public void GenerateEnumTest()
        {
            const string name = "TestEnum";
            const string desc = "TestDescription";

            string[] elems            = new[] { "ElemA", "ElemB", "ElemC" };
            string[] elemDescriptions = new[] { "DescA", "DescB", "DescC" };
            var      typeDecl         = new CodeTypeDeclaration();

            // Check that the method validates its inputs.
            Assert.Throws <ArgumentNullException>(
                () => EnumResourceDecorator.GenerateEnum(null, name, desc, elems, elemDescriptions));
            Assert.Throws <ArgumentNullException>(
                () => EnumResourceDecorator.GenerateEnum(typeDecl, null, desc, elems, elemDescriptions));
            Assert.Throws <ArgumentNullException>(
                () => EnumResourceDecorator.GenerateEnum(typeDecl, name, desc, null, elemDescriptions));

            // Check that the generated enumeration contains all elements.
            CodeTypeDeclaration decl = EnumResourceDecorator.GenerateEnum(typeDecl, name, desc, elems, elemDescriptions);

            Assert.AreEqual(name, decl.Name);
            Assert.IsTrue(decl.IsEnum);
            Assert.AreEqual(1, decl.Comments.Count);

            Assert.AreEqual(3, decl.Members.Count);
            Assert.AreEqual("ElemA", decl.Members[0].Name);
            Assert.AreEqual("ElemB", decl.Members[1].Name);
            Assert.AreEqual("ElemC", decl.Members[2].Name);

            Assert.AreEqual(1, decl.Members[0].Comments.Count);
            Assert.AreEqual(1, decl.Members[1].Comments.Count);
            Assert.AreEqual(1, decl.Members[2].Comments.Count);

            // Check that the code does not fail when no comments are provided
            decl = EnumResourceDecorator.GenerateEnum(typeDecl, name, desc, elems, null);
            Assert.IsNotNull(decl);
        }