def test_from_config(self): config = GeneratorConfig() config.output.format = OutputFormat.DATACLASS writer = CodeWriter.from_config(config) self.assertIsInstance(writer.generator, DataclassGenerator) config.output.format = OutputFormat.PLANTUML writer = CodeWriter.from_config(config) self.assertIsInstance(writer.generator, PlantUmlGenerator)
class CodeWriterTests(FactoryTestCase): def setUp(self): generator = NoneGenerator() self.writer = CodeWriter(generator) @mock.patch.object(NoneGenerator, "render") @mock.patch.object(NoneGenerator, "designate") def test_write(self, mock_designate, mock_render): classes = ClassFactory.list(2) with TemporaryDirectory() as tmpdir: mock_render.return_value = [ GeneratorResult(Path(f"{tmpdir}/foo/a.py"), "file", "aAa"), GeneratorResult(Path(f"{tmpdir}/bar/b.py"), "file", "bBb"), GeneratorResult(Path(f"{tmpdir}/c.py"), "file", " "), ] self.writer.write(classes) self.assertEqual("aAa", Path(f"{tmpdir}/foo/a.py").read_text()) self.assertEqual("bBb", Path(f"{tmpdir}/bar/b.py").read_text()) self.assertFalse(Path(f"{tmpdir}/c.py").exists()) mock_designate.assert_called_once_with(classes) @mock.patch.object(NoneGenerator, "render") @mock.patch.object(NoneGenerator, "designate") @mock.patch("builtins.print") def test_print(self, mock_print, mock_designate, mock_render): classes = ClassFactory.list(2) mock_render.return_value = [ GeneratorResult(Path("foo/a.py"), "file", "aAa"), GeneratorResult(Path("bar/b.py"), "file", "bBb"), GeneratorResult(Path("c.py"), "file", ""), ] self.writer.print(classes) mock_designate.assert_called_once_with(classes) mock_print.assert_has_calls( [mock.call("aAa", end=""), mock.call("bBb", end="")]) def test_from_config(self): CodeWriter.unregister_generator("dataclasses") config = GeneratorConfig() with self.assertRaises(CodeGenerationError) as cm: CodeWriter.from_config(config) self.assertEqual("Unknown output format: 'dataclasses'", str(cm.exception)) CodeWriter.register_generator("dataclasses", DataclassGenerator) writer = CodeWriter.from_config(config) self.assertIsInstance(writer.generator, DataclassGenerator)
class CodeWriterTests(FactoryTestCase): def setUp(self): generator = NoneGenerator() self.writer = CodeWriter(generator) @mock.patch.object(NoneGenerator, "render") @mock.patch.object(NoneGenerator, "designate") def test_write(self, mock_designate, mock_render): classes = ClassFactory.list(2) with TemporaryDirectory() as tmpdir: mock_render.return_value = [ GeneratorResult(Path(f"{tmpdir}/foo/a.py"), "file", "aAa"), GeneratorResult(Path(f"{tmpdir}/bar/b.py"), "file", "bBb"), GeneratorResult(Path(f"{tmpdir}/c.py"), "file", " "), ] self.writer.write(classes) self.assertEqual("aAa", Path(f"{tmpdir}/foo/a.py").read_text()) self.assertEqual("bBb", Path(f"{tmpdir}/bar/b.py").read_text()) self.assertFalse(Path(f"{tmpdir}/c.py").exists()) mock_designate.assert_called_once_with(classes) @mock.patch.object(NoneGenerator, "render") @mock.patch.object(NoneGenerator, "designate") @mock.patch("builtins.print") def test_print(self, mock_print, mock_designate, mock_render): classes = ClassFactory.list(2) mock_render.return_value = [ GeneratorResult(Path(f"foo/a.py"), "file", "aAa"), GeneratorResult(Path(f"bar/b.py"), "file", "bBb"), GeneratorResult(Path(f"c.py"), "file", ""), ] self.writer.print(classes) mock_designate.assert_called_once_with(classes) mock_print.assert_has_calls( [mock.call("aAa", end=""), mock.call("bBb", end="")]) def test_from_config(self): config = GeneratorConfig() config.output.format = OutputFormat.DATACLASS writer = CodeWriter.from_config(config) self.assertIsInstance(writer.generator, DataclassGenerator) config.output.format = OutputFormat.PLANTUML writer = CodeWriter.from_config(config) self.assertIsInstance(writer.generator, PlantUmlGenerator)
def test_from_config(self): CodeWriter.unregister_generator("dataclasses") config = GeneratorConfig() with self.assertRaises(CodeGenerationError) as cm: CodeWriter.from_config(config) self.assertEqual("Unknown output format: 'dataclasses'", str(cm.exception)) CodeWriter.register_generator("dataclasses", DataclassGenerator) writer = CodeWriter.from_config(config) self.assertIsInstance(writer.generator, DataclassGenerator)
def process_classes(self): """Process the generated classes and write or print the final output.""" classes = [ cls for classes in self.class_map.values() for cls in classes ] class_num, inner_num = self.count_classes(classes) if class_num: logger.info("Analyzer input: %d main and %d inner classes", class_num, inner_num) self.assign_packages() classes = self.analyze_classes(classes) class_num, inner_num = self.count_classes(classes) logger.info("Analyzer output: %d main and %d inner classes", class_num, inner_num) writer = CodeWriter.from_config(self.config) if self.print: writer.print(classes) else: writer.write(classes) else: raise CodeGenerationError("Nothing to generate.")
def setUp(self): generator = NoneGenerator() self.writer = CodeWriter(generator)
from xsdata.codegen.writer import CodeWriter from xsdata_plantuml.generator import PlantUmlGenerator CodeWriter.register_generator("plantuml", PlantUmlGenerator)