Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void AddBodyParameterTest()
        {
            var method = new MockMethod()
            {
                Name = "Method", Parameters = new Dictionary <string, IParameter>()
            };
            var typeProvider = new DefaultObjectTypeProvider("Schema");

            // Confirm that no body parameter is added.
            var             decorator   = new RequestConstructorDecorator(typeProvider);
            CodeConstructor constructor = new CodeConstructor();

            method.HasBody = false;
            decorator.AddBodyParameter(constructor, method);

            Assert.AreEqual(0, constructor.Parameters.Count);
            Assert.AreEqual(0, constructor.Statements.Count);

            // Confirm that a required body parameter is added.
            method.RequestType = "MySchema";
            method.HasBody     = true;
            constructor        = new CodeConstructor();
            decorator.AddBodyParameter(constructor, method);

            Assert.AreEqual(1, constructor.Parameters.Count);
            Assert.AreEqual("body", constructor.Parameters[0].Name);
            Assert.AreEqual("Schema.MySchema", constructor.Parameters[0].Type.BaseType);
            Assert.AreEqual(1, constructor.Statements.Count);
        }
Ejemplo n.º 3
0
        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, IDiscoveryParameter>()
            };

            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);
        }
Ejemplo n.º 4
0
        public void DecorateClassTest()
        {
            var method = new MockMethod()
            {
                Name       = "Method",
                Parameters = new Dictionary <string, IDiscoveryParameter>()
            };

            method.Parameters.Add("Param", new MockParameter()
            {
                Name = "Param"
            });
            var resource = new MockResource();

            resource.Methods.Add("Method", method);
            var resourceDecl = new CodeTypeDeclaration();
            var requestDecl  = new CodeTypeDeclaration();
            var typeProvider = new DefaultObjectTypeProvider("Schema");

            // Confirm that the decorator has run correctly.
            var decorator = new RequestConstructorDecorator(typeProvider)
            {
                CreateOptionalConstructor = true
            };

            decorator.DecorateClass(resource, method, requestDecl, resourceDecl);
            Assert.AreEqual(2, requestDecl.Members.Count); // 2 Constructors.
            Assert.IsInstanceOf <CodeConstructor>(requestDecl.Members[0]);
            Assert.AreEqual(0, resourceDecl.Members.Count);

            // Test the decorator without optional parameters.
            requestDecl = new CodeTypeDeclaration();
            decorator.CreateOptionalConstructor = false;
            decorator.DecorateClass(resource, method, requestDecl, resourceDecl);
            Assert.AreEqual(1, requestDecl.Members.Count); // 1 Constructor.
            Assert.IsInstanceOf <CodeConstructor>(requestDecl.Members[0]);
            Assert.AreEqual(0, resourceDecl.Members.Count);
        }