Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 40 create text class #42

Merged
merged 6 commits into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
pip install -r requirements.txt
- name: Test with pytest
run: |
pytest test/
pytest
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ sucuri.egg-info/
dist/
build/
*.pyc
env/
env/
__pycache__/
.pytest_cache/
22 changes: 22 additions & 0 deletions sucuri/element/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Text(object):
def __init__(self, value, variables={}):
self.value = value
self.variables = variables

def render(self):
self._replace_variables()
self._clean()
return self.value

def _replace_variables(self):
value = self.value
for key in self.variables:
old = '{' + key + '}'
new = self.variables[key]
value = value.replace(old, new)
self.value = value

def _clean(self):
one_line = self.value.replace("\n", " ")
split = one_line.split("|")
self.value = "\n".join(map(str.strip, split))
2 changes: 0 additions & 2 deletions tests/element/test_tag.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import sys
sys.path.append('...')

import pytest

from sucuri.element.tag import Tag

def test_should_reflect_when_just_passing_the_tag():
Expand Down
26 changes: 26 additions & 0 deletions tests/element/test_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys
sys.path.append('...')

from sucuri.element.text import Text

def test_should_reflect_when_there_are_variables_one_line():
text = Text('Hello {a}', {"a": "World!"})
assert text.render() == 'Hello World!'

def test_should_reflect_when_there_are_variables_more_than_one_line():
old = """Hello!
| Text
| with
| variable {foo} and
| more than
| one line
"""

new = """Hello!
Text
with
variable foo and
more than
one line"""
text = Text(old, {"foo": "foo"})
assert text.render() == new