Skip to content

Commit

Permalink
Class diagram reference and multiple inheritance: #41.
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Aug 31, 2021
1 parent 631ede7 commit 7e5ef85
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 13 deletions.
23 changes: 22 additions & 1 deletion adia/class_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
Expand All @@ -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_)

Expand All @@ -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)
}
}
18 changes: 15 additions & 3 deletions adia/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
29 changes: 20 additions & 9 deletions tests/test_class_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,41 @@ 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)
int bar(a, *b, c)
*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
''')

0 comments on commit 7e5ef85

Please sign in to comment.