public void GenerateParameterPropertyTest()
        {
            var parameter = new MockParameter() { Name = "Param" };
            var method = new MockMethod() { Name = "Method", Parameters = new Dictionary<string, IParameter>() };
            method.Parameters.Add("Param", parameter);
            var resource = new MockResource();
            resource.Methods.Add("Method", method);
            var resourceDecl = new CodeTypeDeclaration();

            // Confirm that two properties and two fields are generated.
            var decorator = new ParameterPropertyDecorator();
            CodeTypeMemberCollection newMembers = decorator.GenerateParameterProperty(
                parameter, method, resourceDecl, Enumerable.Empty<string>());
            Assert.AreEqual(2, newMembers.Count); // Property  + field.
            Assert.AreEqual(0, resourceDecl.Members.Count);

            // Check the generated property.
            Assert.IsInstanceOf<CodeMemberField>(newMembers[0]);

            CodeMemberProperty property = (CodeMemberProperty)newMembers[1];
            Assert.AreEqual("Param", property.Name);

            // Check that the property has a Key attribute.
            Assert.AreEqual(1, property.CustomAttributes.Count);
            Assert.AreEqual(
                typeof(RequestParameterAttribute).FullName, property.CustomAttributes[0].AttributeType.BaseType);
            Assert.AreEqual(
                "Param", ((CodePrimitiveExpression) property.CustomAttributes[0].Arguments[0].Value).Value);
        }
        public void TestGetParameterType()
        {
            Assert.Throws(typeof(ArgumentNullException), () => ResourceBaseGenerator.GetParameterType(null));

            MockParameter param = new MockParameter();

            // Null => string.
            param.ValueType = null;
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // "" => string.
            param.ValueType = "";
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // "string" => string.
            param.ValueType = "string";
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // "integer" => long.
            param.ValueType = "integer";
            Assert.AreEqual(typeof(long), ResourceBaseGenerator.GetParameterType(param));

            // "boolean" => bool.
            param.ValueType = "boolean";
            Assert.AreEqual(typeof(bool), ResourceBaseGenerator.GetParameterType(param));

            // "AnyOldRubbish" => string.
            param.ValueType = "AGreatBigFish";
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // repeatable "string" => Repeatable<string>.
            param.IsRepeatable = true;
            param.ValueType = "string";
            Assert.AreEqual(typeof(Repeatable<string>), ResourceBaseGenerator.GetParameterType(param));
        }
        public void AddRequiredParametersTest()
        {
            var parameterA = new MockParameter() { Name = "ParamA", IsRequired = false };
            var parameterB = new MockParameter() { Name = "ParamB", IsRequired = true};
            var method = new MockMethod() { Name = "Method", Parameters = new Dictionary<string, IParameter>() };
            method.Parameters.Add("ParamA", parameterA);
            method.Parameters.Add("ParamB", parameterB);
            var resource = new MockResource();
            resource.Methods.Add("Method", method);
            var resourceDecl = new CodeTypeDeclaration();
            var typeProvider = new DefaultObjectTypeProvider("Schema");

            // Confirm that the "service" parameter is added.
            var decorator = new RequestConstructorDecorator(typeProvider);
            CodeConstructor constructor = new CodeConstructor();
            decorator.AddRequestParameters(resourceDecl, method, constructor, false);

            Assert.AreEqual(1, constructor.Parameters.Count);
            Assert.AreEqual("paramB", constructor.Parameters[0].Name);
            Assert.AreEqual(1, constructor.Statements.Count);

            // Check that optional parameters are added when the appropriate flag is set.
            constructor = new CodeConstructor();
            decorator.AddRequestParameters(resourceDecl, method, constructor, true);

            Assert.AreEqual(2, constructor.Parameters.Count);
            Assert.AreEqual("paramB", constructor.Parameters[0].Name);
            Assert.AreEqual("paramA", constructor.Parameters[1].Name);
            Assert.AreEqual(2, constructor.Statements.Count);
        }
Esempio n. 4
0
        public void ValidateRegexTest()
        {
            var parameter = new MockParameter()
            {
                Pattern = ".+", Name = "test"
            };

            Assert.IsTrue(ParameterValidator.ValidateRegex(parameter, "Test"));
        }
Esempio n. 5
0
        public void ValidateRegexEmptyNeedsDataTest()
        {
            var parameter = new MockParameter()
            {
                Pattern = ".+", Name = "test"
            };

            Assert.IsFalse(ParameterValidator.ValidateRegex(parameter, ""));
        }
        public void CreateParameterComment()
        {
            var instance = new DefaultEnglishCommentCreator();
            var parameter = new MockParameter();
            var progamaticName = "testParameter";

            CodeCommentStatementCollection result = instance.CreateParameterComment(parameter, progamaticName);
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.IsNotNull(result[0].Comment);
            Assert.AreEqual("<param name=\"testParameter\">Optional</param>", result[0].Comment.Text);
        }
Esempio n. 7
0
        public void ValidateEnumNullTest()
        {
            var p = new MockParameter()
            {
                Name = "test"
            };

            // Confirm that the method validates enumerations correctly.
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, "one"));
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, "two"));
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, "three"));
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, "One"));
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, ""));
        }
Esempio n. 8
0
        public void ValidateEnumTest()
        {
            var p = new MockParameter
            {
                Name       = "test",
                EnumValues = new string[] { "one", "two", "three" }
            };

            // Confirm that the method validates enumerations correctly.
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, "one"));
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, "two"));
            Assert.IsTrue(ParameterValidator.ValidateEnum(p, "three"));
            Assert.IsFalse(ParameterValidator.ValidateEnum(p, "One"));
            Assert.IsFalse(ParameterValidator.ValidateEnum(p, ""));
            Assert.IsFalse(ParameterValidator.ValidateEnum(p, "FooBar"));
        }
        public void CreateConstructorTest()
        {
            var parameter = new MockParameter() { Name = "Param", IsRequired = true };
            var method = new MockMethod() { Name = "Method", Parameters = new Dictionary<string, IParameter>() };
            method.Parameters.Add("Param", parameter);
            var resource = new MockResource();
            resource.Methods.Add("Method", method);
            var resourceDecl = new CodeTypeDeclaration();
            var typeProvider = new DefaultObjectTypeProvider("Schema");

            // Confirm that the "service" parameter is added.
            var decorator = new RequestConstructorDecorator(typeProvider);
            CodeConstructor constructor = decorator.CreateRequiredConstructor(resourceDecl, method, false);
            Assert.AreEqual(2, constructor.Parameters.Count);
            Assert.AreEqual("service", constructor.Parameters[0].Name);
            Assert.AreEqual(1, constructor.BaseConstructorArgs.Count);
        }
        public void CreateParameterCommentValidateParams()
        {
            var instance = new DefaultEnglishCommentCreator();
            var parameter = new MockParameter();
            string progamaticName = null;

            Assert.Throws(
                typeof(ArgumentNullException), () => instance.CreateParameterComment(parameter, progamaticName));

            progamaticName = "";
            Assert.Throws(typeof(ArgumentException), () => instance.CreateParameterComment(parameter, progamaticName));

            progamaticName = "testParameter";
            parameter = null;
            Assert.Throws(
                typeof(ArgumentNullException), () => instance.CreateParameterComment(parameter, progamaticName));
        }
        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 GetParameterMetaData()
        {
            var instance = new DefaultEnglishCommentCreator();
            var parameter = new MockParameter();
            var progamaticName = "testParameter";
            parameter.Name = progamaticName;
            parameter.IsRequired = false;

            string result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Optional", result);

            parameter.IsRequired = true;
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required", result);

            parameter.Name = "test-parameters";
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("test-parameters - Required", result);

            parameter.Name = progamaticName;
            parameter.Minimum = "53";
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Minimum value of 53", result);

            parameter.Minimum = null;
            parameter.Maximum = "105";
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Maximum value of 105", result);

            parameter.Maximum = null;
            parameter.Pattern = ".*\\.java";
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Must match pattern .*\\.java", result);

            parameter.Pattern = null;
            parameter.EnumValues = new List<string> { "a", "b", "c", "d" };
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Must be one of the following values [a, b, c, d]", result);

            parameter.EnumValues = null;
            parameter.Description = "A Test Description";
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - A Test Description", result);
        }