Exemple #1
0
        private Assembly TestSchema(
            string schema,
            IEnumerable <string> typeNamesToCheck = null,
            IEnumerable <KeyValuePair <string, string> > namespaceMapping = null,
            IEnumerable <string> generatedFilesToCheck = null)
        {
            // Create temp folder
            string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId);

            try
            {
                // Save schema
                string schemaFileName = Path.Combine(outputDir, $"{uniqueId}.avsc");
                System.IO.File.WriteAllText(schemaFileName, schema);

                // Generate from schema file
                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, namespaceMapping ?? new Dictionary <string, string>()), Is.EqualTo(0));

                // Check if all generated files exist
                if (generatedFilesToCheck != null)
                {
                    foreach (string generatedFile in generatedFilesToCheck)
                    {
                        Assert.That(new FileInfo(Path.Combine(outputDir, generatedFile)), Does.Exist);
                    }
                }

                // Compile into netstandard library and load assembly
                Assembly assembly = AvroGenHelper.CompileCSharpFilesIntoLibrary(
                    new DirectoryInfo(outputDir)
                    .EnumerateFiles("*.cs", SearchOption.AllDirectories)
                    .Select(fi => fi.FullName),
                    uniqueId);

                if (typeNamesToCheck != null)
                {
                    // Check if the compiled code has the same number of types defined as the check list
                    Assert.That(typeNamesToCheck.Count(), Is.EqualTo(assembly.DefinedTypes.Count()));

                    // Check if types available in compiled assembly
                    foreach (string typeName in typeNamesToCheck)
                    {
                        Type type = assembly.GetType(typeName);
                        Assert.That(type, Is.Not.Null);

                        // Instantiate
                        object obj = Activator.CreateInstance(type);
                        Assert.That(obj, Is.Not.Null);
                    }
                }

                return(assembly);
            }
            finally
            {
                Directory.Delete(outputDir, true);
            }
        }
Exemple #2
0
        public void NotSupportedSchema(string schema, Type expectedException)
        {
            // Create temp folder
            string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out string uniqueId);

            try
            {
                // Save schema
                string schemaFileName = Path.Combine(outputDir, $"{uniqueId}.avsc");
                System.IO.File.WriteAllText(schemaFileName, schema);

                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, new Dictionary <string, string>()), Is.EqualTo(1));
            }
            finally
            {
                Directory.Delete(outputDir, true);
            }
        }