-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Detect circular references more accurately
- Loading branch information
Showing
9 changed files
with
200 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from xsdata.codegen.container import ClassContainer | ||
from xsdata.codegen.handlers.detect_circular_references import DetectCircularReferences | ||
from xsdata.models.config import GeneratorConfig | ||
from xsdata.models.enums import DataType | ||
from xsdata.utils.testing import ( | ||
AttrFactory, | ||
AttrTypeFactory, | ||
ClassFactory, | ||
FactoryTestCase, | ||
) | ||
|
||
|
||
class DetectCircularReferencesTests(FactoryTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
config = GeneratorConfig() | ||
self.container = ClassContainer(config=config) | ||
self.processor = DetectCircularReferences(self.container) | ||
|
||
def test_process(self): | ||
first = ClassFactory.create(qname="first") | ||
second = ClassFactory.create(qname="second") | ||
third = ClassFactory.create(qname="third") | ||
|
||
first.attrs.append(AttrFactory.native(DataType.STRING)) | ||
first.attrs.append( | ||
AttrFactory.create( | ||
types=[ | ||
AttrTypeFactory.create(qname="second", reference=second.ref), | ||
AttrTypeFactory.create(qname="third", reference=third.ref), | ||
], | ||
choices=[ | ||
AttrFactory.reference("second", reference=second.ref), | ||
AttrFactory.reference("third", reference=third.ref), | ||
], | ||
) | ||
) | ||
|
||
second.attrs = AttrFactory.list(2) | ||
third.attrs.append(AttrFactory.reference("first", reference=first.ref)) | ||
self.container.extend([first, second, third]) | ||
|
||
self.processor.process(first) | ||
|
||
first_flags = [tp.circular for tp in first.types()] | ||
self.assertEqual([False, False, True, False, True], first_flags) | ||
|
||
second_flags = [tp.circular for tp in second.types()] | ||
self.assertEqual([False, False], second_flags) | ||
|
||
# First has the flags this doesn't need it :) | ||
third_flags = [tp.circular for tp in third.types()] | ||
self.assertEqual([False], third_flags) | ||
|
||
def test_build_reference_types(self): | ||
target = ClassFactory.create() | ||
inner = ClassFactory.create() | ||
|
||
outer_attr = AttrFactory.create() | ||
inner_attr = AttrFactory.reference("foo", reference=target.ref) | ||
|
||
inner.attrs.append(inner_attr) | ||
target.inner.append(inner) | ||
target.attrs.append(outer_attr) | ||
|
||
self.container.add(target) | ||
|
||
self.processor.build_reference_types() | ||
|
||
expected = { | ||
target.ref: [inner_attr.types[0]], | ||
inner.ref: [inner_attr.types[0]], | ||
} | ||
|
||
self.assertEqual(expected, self.processor.reference_types) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.