diff --git a/adia/class_.py b/adia/class_.py index 05f71c4..1f9ac13 100644 --- a/adia/class_.py +++ b/adia/class_.py @@ -127,6 +127,7 @@ class diagram section. def __init__(self, *args, **kwargs): super().__init__('title', *args, **kwargs) + self._classdict = {} def __repr__(self): return f'ClassDiagram: {self.title or "Untitled"}' @@ -150,6 +151,26 @@ def dumps(self): def _set_title(self, value, *args): self.title = value.strip() + def _complete(self): + classdict = {c.title: c for c in self} + + def _ensure_class(title): + c = classdict.get(title) + if c: + return + + c = Class_() + c.title = title + self.append(c) + + for c in self: + for p in c.parents: + _ensure_class(p) + + for m in c.members: + if m.ref: + _ensure_class(m.ref) + def _new_class(self, class_): self.append(class_) @@ -162,7 +183,7 @@ def _new_class(self, class_): 'start': { HASH: {EVERYTHING: {NEWLINE: GoTo('start', ignore=True)}}, NEWLINE: GoTo('start', ignore=True), - EOF: Terminate(), + EOF: Terminate(cb=_complete), NAME: New(Class_, 'start', cb=_new_class) } } diff --git a/adia/renderer.py b/adia/renderer.py index a6ad3bb..4172359 100644 --- a/adia/renderer.py +++ b/adia/renderer.py @@ -797,18 +797,30 @@ class ClassRenderer(BaseRenderer): _classplans_dict = None _classplans = None + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._classplans_dict = {} + self._classplans = [] + def _add_classplan(self, d): + if d.title in self._classplans_dict: + return + plan = ClassPlan(d) self._classplans_dict[d.title] = plan self._classplans.append(plan) def plan(self): - self._classplans_dict = {} - self._classplans = [] - for d in self.diagram: self._add_classplan(d) + # for p in d.parents: + # self._add_classplan(p) + + # for m in d.members: + # if m.ref: + # self._add_classplan(m.ref) + def _autoposition(self): col = 0 row = 0 diff --git a/tests/test_class.py b/tests/test_class.py index 3d53adc..9839318 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -60,3 +60,39 @@ def test_class_position(): . +-----+ +-----+ . ................... ''') + + +def test_class_ref(): + d = Diagram(''' + class: + foo + b -> bar + ''') + assert eqdia(d, ''' + ................... + . +-----+ +-----+ . + . | foo | | bar | . + . +-----+ +-----+ . + . | b | . + . +-----+ . + ................... + ''') + assert d[0][0].title == 'foo' + assert d[0][1].title == 'bar' + + +def test_class_inheritance(): + d = Diagram(''' + class: + foo(bar, baz) + ''') + assert eqdia(d, ''' + ........................... + . +-----+ +-----+ +-----+ . + . | foo | | bar | | baz | . + . +-----+ +-----+ +-----+ . + ........................... + ''') + assert d[0][0].title == 'foo' + assert d[0][1].title == 'bar' + assert d[0][2].title == 'baz' diff --git a/tests/test_class_interpreter.py b/tests/test_class_interpreter.py index 804f003..cb3d8ae 100644 --- a/tests/test_class_interpreter.py +++ b/tests/test_class_interpreter.py @@ -95,24 +95,25 @@ def test_class_method(): def test_class_reference(): - s = ''' + d = class_(''' class: Foo foo bar -> baz - ''' - eqrepr(s) - - d = class_(''' + ''') + assert len(d) == 2 + assert eqbigstr(d, ''' class: Foo + foo bar -> baz + + baz ''') - assert len(d) == 1 def test_class_inheritance(): - s = ''' + d = class_(''' class: Foo foo(bar) @@ -120,5 +121,15 @@ def test_class_inheritance(): *Bar bar(int *a) bar(baz, qux) - ''' - eqrepr(s) + ''') + assert eqbigstr(d, ''' + class: Foo + + foo(bar) + int bar(a, *b, c) + *Bar bar(int *a) + + bar(baz, qux) + baz + qux + ''')