Ejemplo n.º 1
0
        public void DecorateClassTest()
        {
            var          dec         = new SubresourceClassDecorator();
            var          resource    = new MockResource();
            const string className   = "TestClass";
            var          decl        = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            var          decorators  = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen  = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen         = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Add subresources.
            resource.Resources.Add("Sub1", new MockResource {
                Name = "Sub1"
            });
            resource.Resources.Add("Sub2", new MockResource {
                Name = "Sub2"
            });

            // Test decoration.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
            Assert.AreEqual(2, decl.Members.Count);
            Assert.IsInstanceOf <CodeTypeDeclaration>(decl.Members[0]);
            Assert.IsInstanceOf <CodeTypeDeclaration>(decl.Members[1]);
        }
Ejemplo n.º 2
0
        public void GenerateRequestClassesTest()
        {
            var decorator = new CountingRequestDecorator();
            var generator = new RequestClassGenerator(new IRequestDecorator[] { decorator });
            var decl      = new CodeTypeDeclaration();

            // Create a test resource.
            var resource = new MockResource();

            resource.Methods.Add("MethodA", new MockMethod()
            {
                Name = "MethodA"
            });
            resource.Methods.Add("MethodB", new MockMethod()
            {
                Name = "MethodB"
            });

            // Run the generator
            CodeTypeMemberCollection collection = generator.GenerateRequestClasses(decl, resource);

            Assert.AreEqual(2, decorator.TimesCalled);
            Assert.AreEqual(2, collection.Count);
            Assert.IsInstanceOf <CodeTypeDeclaration>(collection[0]);
            Assert.IsInstanceOf <CodeTypeDeclaration>(collection[1]);
            CollectionAssert.AreEqual(
                new[]
            {
                string.Format(RequestClassGenerator.RequestClassNamingScheme, "MethodA"),
                string.Format(RequestClassGenerator.RequestClassNamingScheme, "MethodB"),
            },
                from CodeTypeMember m in collection select m.Name);
        }
Ejemplo n.º 3
0
        public void DecorateClassRenamingTest()
        {
            var          dec         = new SubresourceClassDecorator();
            var          resource    = new MockResource();
            const string className   = "TestClass";
            var          decl        = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            var          decorators  = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen  = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen         = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Add subresources.
            resource.Resources.Add("Sub1", new MockResource {
                Name = "Sub1"
            });
            resource.Resources.Add("Sub2", new MockResource {
                Name = "Sub2"
            });

            // Add existing members
            decl.Members.Add(new CodeMemberField {
                Name = "Sub1Resource"
            });

            // Test decoration.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
            Assert.AreEqual(3, decl.Members.Count);
            Assert.AreEqual("Sub1Resource", decl.Members[0].Name);
            Assert.AreEqual("Sub1Res", decl.Members[1].Name);
            Assert.AreEqual("Sub2Resource", decl.Members[2].Name);
        }
Ejemplo n.º 4
0
        public void DecorateClassValidationTest()
        {
            var          dec         = new SubresourceClassDecorator();
            var          resource    = new MockResource();
            const string className   = "TestClass";
            var          decl        = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            IEnumerable <IResourceDecorator> decorators =
                GoogleServiceGenerator.GetSchemaAwareResourceDecorators("Generated.Data.");

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(
                GoogleServiceGenerator.StandardResourceContainerDecorator);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen        = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Validate method:
            Assert.Throws <ArgumentNullException>(
                () => dec.DecorateClass(null, className, decl, gen, serviceName, decorators));
            Assert.Throws <ArgumentNullException>(
                () => dec.DecorateClass(resource, null, decl, gen, serviceName, decorators));
            Assert.Throws <ArgumentNullException>(
                () => dec.DecorateClass(resource, className, null, gen, serviceName, decorators));
            Assert.Throws <ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, null, serviceName, decorators));
            Assert.Throws <ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, gen, null, decorators));
            Assert.Throws <ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, gen, serviceName, null));

            // Test normal use case.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
        }
Ejemplo n.º 5
0
        internal CodeTypeDeclaration GenerateSubresource(IResource subresource,
                                                         string serviceClassName,
                                                         IEnumerable <IResourceDecorator> allDecorators,
                                                         RequestClassGenerator requestClassGenerator,
                                                         ResourceContainerGenerator containerGenerator,
                                                         IEnumerable <string> forbiddenWords)
        {
            // Create a new generator for this subresource
            var subgenerator = new ResourceClassGenerator(
                subresource, serviceClassName, allDecorators, requestClassGenerator, containerGenerator,
                forbiddenWords);

            // Generate and add the subresource.
            return(subgenerator.CreateClass());
        }
Ejemplo n.º 6
0
 public void GetProposedNameTest()
 {
     Assert.AreEqual("TestRequest", RequestClassGenerator.GetProposedName(
                         new MockMethod()
     {
         Name = "Test"
     },
                         RequestClassGenerator.RequestClassNamingScheme));
     Assert.AreEqual("AbcRequest", RequestClassGenerator.GetProposedName(
                         new MockMethod()
     {
         Name = "abc"
     },
                         RequestClassGenerator.RequestClassNamingScheme));
 }
Ejemplo n.º 7
0
        private void CreateResources(CodeNamespace clientNamespace,
                                     string serviceClassName,
                                     IResource resource,
                                     RequestClassGenerator requestClassGenerator,
                                     ResourceContainerGenerator resourceContainerGenerator)
        {
            foreach (var res in resource.Resources.Values.Concat(resource))
            {
                // Create the current list of used names.
                IEnumerable <string> usedNames = resource.Resources.Keys;

                // Create a class for the resource.
                logger.Debug("Adding Resource {0}", res.Name);
                var resourceGenerator = new ResourceClassGenerator(
                    res, serviceClassName, resourceDecorators, requestClassGenerator, resourceContainerGenerator,
                    usedNames);
                var generatedClass = resourceGenerator.CreateClass();
                clientNamespace.Types.Add(generatedClass);
            }
        }
Ejemplo n.º 8
0
        public void GenerateSubresourceTest()
        {
            var          dec         = new SubresourceClassDecorator();
            const string serviceName = "TestService";
            var          decorators  = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen  = new RequestClassGenerator(new IRequestDecorator[0]);

            // Test generation of the nested class
            var subresource = new MockResource {
                Name = "Sub"
            };
            CodeTypeDeclaration decl = dec.GenerateSubresource(
                subresource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            Assert.IsNotNull(decl);
            Assert.AreEqual(decl.Name, "SubResource");
        }
Ejemplo n.º 9
0
        internal CodeNamespace GenerateClientCode()
        {
            var clientNamespace = CreateNamespace(codeClientNamespace);

            ResourceContainerGenerator resourceContainerGenerator =
                new ResourceContainerGenerator(resourceContainerDecorators);
            var requestClassGenerator = new RequestClassGenerator(
                GetSchemaAwareCommonRequestDecorators(DataNamespace(codeClientNamespace), service),
                GetSchemaAwareRequestDecorators(DataNamespace(codeClientNamespace), service),
                GetSchemaAwareUploadDecorators(DataNamespace(codeClientNamespace), service));

            var serviceClass =
                new ServiceClassGenerator(service, serviceDecorators, resourceContainerGenerator).CreateServiceClass();
            string serviceClassName = serviceClass.Name;

            clientNamespace.Types.Add(serviceClass);
            CreateResources(
                clientNamespace, serviceClassName, service, requestClassGenerator, resourceContainerGenerator);

            return(clientNamespace);
        }
Ejemplo n.º 10
0
        public void GenerateRequestClassDecoratorOrderTest()
        {
            var generator =
                new RequestClassGenerator(
                    new IRequestDecorator[]
            {
                new MockRequestDecorator("One"), new MockRequestDecorator("Two"),
                new MockRequestDecorator("Three")
            });
            var decl     = new CodeTypeDeclaration();
            var resource = new MockResource();
            var method   = new MockMethod();

            // Check the order of the generated members.
            var genClass = generator.GenerateRequestClass(resource, method, decl, Enumerable.Empty <string>());

            Assert.AreEqual(3, genClass.Members.Count);
            Assert.AreEqual("One", genClass.Members[0].Name);
            Assert.AreEqual("Two", genClass.Members[1].Name);
            Assert.AreEqual("Three", genClass.Members[2].Name);
        }
Ejemplo n.º 11
0
        public void GenerateRequestClassTest()
        {
            var decorator = new CountingRequestDecorator();
            var generator = new RequestClassGenerator(new IRequestDecorator[] { decorator });
            var decl      = new CodeTypeDeclaration();

            // Create a test resource.
            var        resource = new MockResource();
            MockMethod method   = new MockMethod()
            {
                Name = "MethodA"
            };

            resource.Methods.Add("MethodA", method);

            // Run the generator
            CodeTypeDeclaration newClass = generator.GenerateRequestClass(resource, method, decl, new string[0]);

            Assert.IsNotNull(newClass);
            Assert.AreEqual(1, decorator.TimesCalled);
            Assert.AreEqual(0, newClass.Members.Count);
        }
            public CodeMemberMethod CreateMethod(CodeTypeDeclaration classDeclaration,
                                                 IResource resource,
                                                 IMethod method,
                                                 bool addOptionalParameters,
                                                 MethodType methodType)
            {
                // Create a new method and make it public.
                var member = new CodeMemberMethod();

                member.Name       = GeneratorUtils.GetMethodName(method, resource.Methods.Keys.Without(method.Name));
                member.Attributes = MemberAttributes.Public;
                if (commentCreator != null)
                {
                    member.Comments.AddRange(commentCreator.CreateMethodComment(method));
                }

                // Check if this method has a body.
                CodeExpressionCollection constructorParameters = new CodeExpressionCollection();

                constructorParameters.Add(new CodeVariableReferenceExpression(ServiceFieldName));
                if (method.HasBody)
                {
                    // If so, add a body parameter.
                    ResourceCallAddBodyDeclaration(method, member, GetBodyType(method), false);
                }

                // If a body parameter or similar parameters were added, also add them to the constructor.
                foreach (CodeParameterDeclarationExpression existingParameter in member.Parameters)
                {
                    constructorParameters.Add(new CodeVariableReferenceExpression(existingParameter.Name));
                }

                // Add all request parameters to this method.
                AddDeclaredParameters(
                    classDeclaration, method, member, constructorParameters, addOptionalParameters);

                string requestClassNamingScheme = RequestClassGenerator.RequestClassNamingScheme;

                // If this is the media-upload convenience method, add the stream and content-type
                // parameters.
                if (methodType == MethodType.Media)
                {
                    member.Parameters.Add(new CodeParameterDeclarationExpression(
                                              typeof(System.IO.Stream), StreamParameterName));
                    member.Parameters.Add(new CodeParameterDeclarationExpression(
                                              typeof(System.String), ContentTypeParameterName));

                    constructorParameters.Add(new CodeVariableReferenceExpression(StreamParameterName));
                    constructorParameters.Add(new CodeVariableReferenceExpression(ContentTypeParameterName));

                    requestClassNamingScheme = RequestClassGenerator.MediaUploadClassNamingScheme;
                }

                // new ...Request(paramOne, paramTwo, paramThree)
                // TODO(mlinder): add method signature collision checking here.
                CodeTypeReference requestType = new CodeTypeReference(
                    RequestClassGenerator.GetProposedName(
                        method, requestClassNamingScheme));

                member.ReturnType = requestType;
                var newRequest = new CodeObjectCreateExpression(requestType);

                newRequest.Parameters.AddRange(constructorParameters);

                // return ...;
                var returnStatment = new CodeMethodReturnStatement(newRequest);

                member.Statements.Add(returnStatment);

                return(member);
            }