public static ISchemaLoader GetSchemaLoader(this SourceSchemaType schemaType)
        {
            switch (schemaType)
            {
            case SourceSchemaType.RawJson:
                return(new RawJsonSchemaLoader());

            case SourceSchemaType.Swagger:
                return(new RefLoaderSchemaLoader());

            case SourceSchemaType.OpenApi:
                return(new OpenApiSchemaLoader());

            default:
                throw new NotImplementedException($"Todo : implement {schemaType}SchemaLoader and use it in {nameof(SchemaTypeExtensions)}.{nameof(GetSchemaLoader)}.");
            }
        }
Exemple #2
0
        public async Task ShouldGetAndApplyOpenApiTemplates(SourceSchemaType schemaType, string schemaPath, string[] templatesPaths, string expectedResultPath)
        {
            var tmpFolder = Path.GetRandomFileName();

            try
            {
                var loader = schemaType.GetSchemaLoader();

                await CodeGenRunner.RunAsync(new[] { schemaPath }, loader, templatesPaths.ToList(), tmpFolder, null);

                var expectedFiles = Directory.GetFiles(expectedResultPath, "*.*", SearchOption.AllDirectories).ToArray();
                var resultFiles   = Directory.GetFiles(tmpFolder, "*.*", SearchOption.AllDirectories).ToArray();

                resultFiles
                .Select(f => Path.GetRelativePath(tmpFolder, f)).OrderBy(f => f)
                .ShouldBe(expectedFiles.Select(f => Path.GetRelativePath(expectedResultPath, f)).OrderBy(f => f));

                foreach (var expectedFile in expectedFiles)
                {
                    var relativePath = Path.GetRelativePath(expectedResultPath, expectedFile);
                    var resultPath   = Path.Combine(tmpFolder, relativePath);

                    if (!File.Exists(resultPath))
                    {
                        throw new FileNotFoundException($"File {relativePath} should be output from the template runner.");
                    }

                    _output.WriteLine($"Testing {relativePath} content.");

                    File.ReadAllText(resultPath).Replace("\r\n", "\n")
                    .ShouldBe(File.ReadAllText(expectedFile).Replace("\r\n", "\n"));
                }
            }
            finally
            {
                if (Directory.Exists(tmpFolder))
                {
                    Directory.Delete(tmpFolder, true);
                }
            }
        }
Exemple #3
0
        public async Task ShouldParseCommandLineArgumentsAndApplyTemplate(string source, string[] templates, string expectedDirectory, TemplateDuplicationHandlingStrategy templateDuplicationHandlingStrategy, SourceSchemaType sourceSchemaType)
        {
            var expectedMd5Output = Directory.GetFiles(expectedDirectory).Select(file => ComputeMD5(file)).ToList();

            var outputDirectory = GetTemporaryDirectory();

            try
            {
                var args = new[] {
                    "-l", sourceSchemaType.ToString(),
                    "-d", templateDuplicationHandlingStrategy.ToString(),
                    "-s", source,
                    "-o", outputDirectory
                }
                .Concat(templates.SelectMany(t => new[] { "-t", t }))
                .ToArray();

                await Program.Main(args);

                var actualMd5Output = Directory.GetFiles(outputDirectory).Select(file => ComputeMD5(file)).ToList();

                Assert.Equal(expectedMd5Output.Count, actualMd5Output.Count);

                foreach (var expectedMd5 in expectedMd5Output)
                {
                    Assert.Contains(expectedMd5, actualMd5Output);
                }
            }
            finally
            {
                Directory.Delete(outputDirectory, true);
            }
        }