-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from ouhammmourachid/userJourney
add UserJourney diagram to mermaid-py.
- Loading branch information
Showing
6 changed files
with
139 additions
and
1 deletion.
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,80 @@ | ||
import unittest | ||
from unittest.mock import MagicMock | ||
|
||
from mermaid.userjourney import Actor, Section, Task, UserJourney | ||
|
||
|
||
class TestTask(unittest.TestCase): | ||
def test_str_task_with_one_actor(self): | ||
actors = Actor('Me') | ||
task = Task('Make tea', 5, actors) | ||
expect_string = '\t\tMake tea: 5 : Me' | ||
self.assertEqual(expect_string, str(task)) | ||
|
||
def test_str_task_with_actors(self): | ||
actors = [Actor('Me'), Actor('Cat')] | ||
task = Task('Make tea', 5, actors) | ||
expect_string = '\t\tMake tea: 5 : Me, Cat' | ||
self.assertEqual(expect_string, str(task)) | ||
|
||
|
||
class TestSection(unittest.TestCase): | ||
def test_string_section(self): | ||
task_mock_1 = MagicMock(spec=Task) | ||
task_mock_2 = MagicMock(spec=Task) | ||
task_mock_1.__str__.return_value = '\t\tMake tea1: 5 : Me, Cat' | ||
task_mock_2.__str__.return_value = '\t\tMake tea2: 5 : Me, Cat' | ||
section = Section('My working day', [task_mock_1, task_mock_2]) | ||
expect_string = """\tsection My working day | ||
\t\tMake tea1: 5 : Me, Cat | ||
\t\tMake tea2: 5 : Me, Cat | ||
""" | ||
self.assertEqual(expect_string, str(section)) | ||
|
||
|
||
class TestUserJourney(unittest.TestCase): | ||
def test_script_with_task(self): | ||
task_mock_1 = MagicMock(spec=Task) | ||
task_mock_2 = MagicMock(spec=Task) | ||
task_mock_1.__str__.return_value = '\tMake tea1: 5 : Me, Cat' | ||
task_mock_2.__str__.return_value = '\tMake tea2: 5 : Me, Cat' | ||
userjourney = UserJourney('simple user journey', | ||
[task_mock_1, task_mock_2]) | ||
expect_string = """--- | ||
title: simple user journey | ||
--- | ||
journey | ||
\ttitle simple user journey | ||
\tMake tea1: 5 : Me, Cat | ||
\tMake tea2: 5 : Me, Cat | ||
""" | ||
self.assertEqual(expect_string, userjourney.script) | ||
|
||
def test_script_with_section(self): | ||
section_mock_1 = MagicMock(spec=Section) | ||
section_mock_2 = MagicMock(spec=Section) | ||
section_mock_1.__str__.return_value = """\tsection section-1 | ||
\t\tMake tea1: 5 : Me, Cat | ||
\t\tMake tea2: 5 : Me, Cat | ||
""" | ||
section_mock_2.__str__.return_value = """\tsection section-2 | ||
\t\tMake tea1: 5 : Me, Cat | ||
\t\tMake tea2: 5 : Me, Cat | ||
""" | ||
userjourney = UserJourney('simple user journey', | ||
[section_mock_1, section_mock_2]) | ||
expect_string = """--- | ||
title: simple user journey | ||
--- | ||
journey | ||
\ttitle simple user journey | ||
\tsection section-1 | ||
\t\tMake tea1: 5 : Me, Cat | ||
\t\tMake tea2: 5 : Me, Cat | ||
\tsection section-2 | ||
\t\tMake tea1: 5 : Me, Cat | ||
\t\tMake tea2: 5 : Me, Cat | ||
""" | ||
self.assertEqual(expect_string, userjourney.script) |
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,23 @@ | ||
from typing import Union | ||
|
||
from mermaid.graph import Graph | ||
|
||
from .actor import Actor | ||
from .section import Section | ||
from .task import Task | ||
|
||
|
||
class UserJourney(Graph): | ||
def __init__(self, title: str, sections: list[Union[Section, | ||
Task]]) -> None: | ||
super().__init__(title, '') | ||
self.title: str = title | ||
self.sections: list[Union[Section, Task]] = sections | ||
self._build_script() | ||
|
||
def _build_script(self) -> None: | ||
super()._build_script() | ||
script: str = f'\njourney\n\ttitle {self.title}\n' | ||
for section in self.sections: | ||
script += f'{section}\n' | ||
self.script += script |
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,6 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Actor: | ||
name: str |
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,13 @@ | ||
from .task import Task | ||
|
||
|
||
class Section: | ||
def __init__(self, name: str, tasks: list[Task]) -> None: | ||
self.name: str = name | ||
self.tasks: list[Task] = tasks | ||
|
||
def __str__(self) -> str: | ||
string: str = f'\tsection {self.name}\n' | ||
for task in self.tasks: | ||
string += f'{task}\n' | ||
return string |
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,16 @@ | ||
from typing import Union | ||
|
||
from .actor import Actor | ||
|
||
|
||
class Task: | ||
def __init__(self, name: str, score: int, actors: Union[list[Actor], | ||
Actor]) -> None: | ||
self.name: str = name | ||
self.score: int = score | ||
self.actors: list[Actor] = actors if isinstance(actors, | ||
list) else [actors] | ||
|
||
def __str__(self) -> str: | ||
actor_string: str = ', '.join([actor.name for actor in self.actors]) | ||
return f'\t\t{self.name}: {self.score} : {actor_string}' |