Skip to content

Commit

Permalink
Add link class to sequence module
Browse files Browse the repository at this point in the history
  • Loading branch information
ouhammmourachid committed Nov 19, 2023
1 parent 1bf83e7 commit 346b16b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 8 deletions.
1 change: 1 addition & 0 deletions mermaid/sequence/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from mermaid.sequence.element import Actor, Box, Participant
from mermaid.sequence.link import ArrowTypes, Link
24 changes: 17 additions & 7 deletions mermaid/sequence/element.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
"""Sequence diagram elements."""
from dataclasses import dataclass
from typing import Union

from mermaid._utils import text_to_snake_case


@dataclass
class Actor:
"""Actor class for mermaid sequence diagram.
Args:
name (str): Name of the actor.
"""
name: str
def __init__(self, name: str):
"""Initialize actor.
Args:
name (str): Name of the actor.
"""
self.name: str = name
self.id_: str = text_to_snake_case(self.name)

def __str__(self):
"""Return actor string.
Expand All @@ -23,23 +28,28 @@ def __str__(self):
return f'\tActor {self.name}\n'


@dataclass
class Participant:
"""Participant class for mermaid sequence diagram.
Args:
name (str): Name of the participant.
"""
name: str
def __init__(self, name: str):
"""Initialize participant.
Args:
name (str): Name of the participant.
"""
self.name: str = name
self.id_: str = text_to_snake_case(self.name)

def __str__(self):
"""Return participant string.
Returns:
str: Participant string.
"""
self.id = text_to_snake_case(self.name)
return f'\tparticipant {self.id} as {self.name}\n'
return f'\tparticipant {self.id_} as {self.name}\n'


class Box:
Expand Down
59 changes: 59 additions & 0 deletions mermaid/sequence/link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from mermaid.sequence.element import Actor, Participant
from typing import Union

from enum import Enum


ARROW_TYPES: dict[str,str] = {
'Solid-line':'->',
'Dotted-line':'-->',
'Solid-arrow':'->>',
'Dotted-arrow':'-->>',
'Solid-cross':'-x',
'Dotted-cross':'--x',
'Solid-async':'-)',
'Dotted-async':'--)'
}



class ArrowTypes(Enum):
SOLID_LINE = '->'
DOTTED_LINE = '-->'
SOLID_ARROW = '->>'
DOTTED_ARROW = '-->>'
SOLID_CROSS = '-x'
DOTTED_CROSS = '--x'
SOLID_ASYNC = '-)'
DOTTED_ASYNC = '--)'

class Link:
"""Link class for mermaid sequence diagram.
Args:
source (ActorParticipant): Source of the link.
target (ActorParticipant): Target of the link.
type_ (Union[str,ArrowTypes]): Type of the link.
activate_target (bool, optional): Activate the target. Defaults to False.
deactivate_target (bool, optional): Deactivate the target. Defaults to False.
"""
def __init__(self,
source: Union[Actor, Participant],
target: Union[Actor, Participant],
type_: Union[str,ArrowTypes],
activate_target: bool = False,
deactivate_target: bool = False)-> None:
self.source:Union[Actor, Participant] = source
self.target:Union[Actor, Participant] = target
self.type_:Union[str,ArrowTypes] = ARROW_TYPES[type_] if isinstance(type_,str) else type_.value
self.activate_target: bool = activate_target
self.deactivate_target: bool = deactivate_target

def __str__(self)-> str:
string:str = f'\t{self.source.id_}{self.type_}{self.target.id_}\n'
if self.activate_target:
string += f'activate {self.target.id_}\n'
if self.deactivate_target:
string += f'deactivate {self.target.id_}\n'

return string
36 changes: 35 additions & 1 deletion mermaid/tests/test_sequence.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from unittest import mock

from mermaid.sequence import Actor, Box, Participant
from mermaid.sequence import Actor, ArrowTypes, Box, Link, Participant


class TestActor(unittest.TestCase):
Expand Down Expand Up @@ -32,3 +32,37 @@ def test_str(self):
\tparticipant alice as Alice
\tend\n"""
self.assertEqual(str(box), expect_str)


class TestLink(unittest.TestCase):
def setUp(self):
self.source = mock.MagicMock(spec=Actor)
self.source.id_ = 'John'
self.target = mock.MagicMock(spec=Participant)
self.target.id_ = 'Alice'

def test_str_without_activation_deactivation(self):
link = Link(self.source, self.target, 'Solid-line')
expected_str = '\tJohn->Alice\n'
self.assertEqual(str(link), expected_str)

def test_str_with_arrow_type_classe(self):
link = Link(self.source, self.target, ArrowTypes.SOLID_LINE)
expected_str = '\tJohn->Alice\n'
self.assertEqual(str(link), expected_str)

def test_str_with_activation(self):
link = Link(self.source,
self.target,
'Solid-line',
activate_target=True)
expected_str = '\tJohn->Alice\nactivate Alice\n'
self.assertEqual(str(link), expected_str)

def test_str_with_deactivation(self):
link = Link(self.source,
self.target,
'Solid-line',
deactivate_target=True)
expected_str = '\tJohn->Alice\ndeactivate Alice\n'
self.assertEqual(str(link), expected_str)

0 comments on commit 346b16b

Please sign in to comment.