Ejemplo n.º 1
0
        public ODataT4CodeGenerator Create()
        {
            var generator = new TestODataT4CodeGenerator();

            LastCreatedInstance = generator;
            return(generator);
        }
Ejemplo n.º 2
0
        public void TestAddGenerateClientCode_GeneratesMultipleFiles()
        {
            var serviceName = "MyService";
            ServiceConfiguration serviceConfig = new ServiceConfigurationV4()
            {
                ServiceName             = serviceName,
                GeneratedFileNamePrefix = "Main",
                GenerateMultipleFiles   = true
            };

            var codeGen           = new TestODataT4CodeGenerator();
            var codeGenFactory    = new TestODataT4CodeGeneratorFactory(codeGen);
            var handlerHelper     = new TestConnectedServiceHandlerHelper();
            var codeGenDescriptor = SetupCodeGenDescriptor(serviceConfig, serviceName,
                                                           codeGenFactory, handlerHelper);

            var template = new StringBuilder();

            codeGen.MultipleFilesManager = ODataT4CodeGenerator.FilesManager.Create(null, template);
            codeGen.MultipleFilesManager.StartNewFile("File1.cs", false);
            template.Append("Contents1");
            codeGen.MultipleFilesManager.EndBlock();

            codeGen.MultipleFilesManager.StartNewFile("File2.cs", false);
            template.Append("Contents2");
            codeGen.MultipleFilesManager.EndBlock();

            //The file manager expects the files to have been saved in the Temp directory
            // when ODataT4CodeGenerator.TransformText() was called. Since we're using a dummy code generator
            // we need to manually ensure those files exist
            var file1TempPath = Path.Combine(Path.GetTempPath(), "File1.cs");

            File.WriteAllText(file1TempPath, "Contents1");
            var file2TempPath = Path.Combine(Path.GetTempPath(), "File2.cs");

            File.WriteAllText(file2TempPath, "Contents2");

            codeGenDescriptor.AddGeneratedClientCodeAsync().Wait();
            var expectedMainFilePath = Path.Combine(TestProjectRootPath, ServicesRootFolder, serviceName, "Main.cs");
            var mainFile             = handlerHelper.AddedFiles.FirstOrDefault(f => f.CreatedFile == expectedMainFilePath);

            Assert.IsNotNull(mainFile);
            Assert.AreEqual("Generated code", File.ReadAllText(mainFile.SourceFile));
            var expectedFile1Path = Path.Combine(TestProjectRootPath, ServicesRootFolder, serviceName, "File1.cs");
            var file1             = handlerHelper.AddedFiles.FirstOrDefault(f => f.CreatedFile == expectedFile1Path);

            Assert.IsNotNull(file1);
            Assert.AreEqual("Contents1", File.ReadAllText(file1.SourceFile));
            var expectedFile2Path = Path.Combine(TestProjectRootPath, ServicesRootFolder, serviceName, "File2.cs");
            var file2             = handlerHelper.AddedFiles.FirstOrDefault(f => f.CreatedFile == expectedFile2Path);

            Assert.IsNotNull(file2);
            Assert.AreEqual("Contents2", File.ReadAllText(file2.SourceFile));
        }