Пример #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]);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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());
        }
Пример #5
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);
            }
        }
Пример #6
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");
        }
Пример #7
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);
        }