Skip to content
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
8 changes: 4 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx_issues',
'sphinx.ext.intersphinx',
'sphinx.ext.doctest',
"sphinx.ext.autodoc",
"sphinx_issues",
"sphinx.ext.intersphinx",
"sphinx.ext.doctest",
"sphinxcontrib.jquery",
]

Expand Down
15 changes: 15 additions & 0 deletions snakemd/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,21 @@ def __repr__(self) -> str:
"""
return f"Code(code={self._code!r}, lang={self._lang!r})"

def __eq__(self, other) -> bool:
"""
Checks to see if this code block is equal to some
other object. Equality is based on the provided
arguments to the constructor (i.e., is the code
the same and the language the same?).

:return:
True if the constructor arguments are equivalent;
False, otherwise
"""
if isinstance(other, Code):
return self._code == other._code and self._lang == other._lang
return False

@staticmethod
def _process_backticks(code: str | Code) -> int:
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_document.py → tests/document/test_document.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from snakemd import Document, Heading, Paragraph, HorizontalRule

from snakemd import Document, Heading, HorizontalRule, Paragraph

# Method tests (singles)

Expand Down
49 changes: 35 additions & 14 deletions tests/test_code.py → tests/elements/test_code.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from snakemd import Code


# Constructor tests


def test_code_empty():
"""
Verifies that code block is correctly instantiated
for empty input. Also verifies the repr
representation of the code block.
representation of the code block. Markdown itself
is not verified as python-markdown does not
handle the language feature of code blocks.
"""
code = Code("")
assert str(code) == "```generic\n\n```"
Expand All @@ -19,7 +20,9 @@ def test_code_empty_java():
"""
Verifies that code block is correctly instantiated
for a custom language with empty input. Also verifies
the repr representation of the code block.
the repr representation of the code block. Markdown itself
is not verified as python-markdown does not
handle the language feature of code blocks.
"""
code = Code("", lang="java")
assert str(code) == "```java\n\n```"
Expand All @@ -30,18 +33,22 @@ def test_code_one_line():
"""
Verifies that a code block is correctly instantiated
given a single line of sample code. Also verifies
the repr representation of the code block.
the repr representation of the code block. Markdown itself
is not verified as python-markdown does not
handle the language feature of code blocks.
"""
code = Code("print('Hello, World!')")
assert str(code) == "```generic\nprint('Hello, World!')\n```"
assert repr(code) == r"""Code(code="print('Hello, World!')", lang='generic')"""


def test_code_one_line_nested_single_quotes():
"""
Verifies the same conditions as test_code_one_line()
with the quote style swapped. This is primarily a test
of repr.
of repr. Markdown itself
is not verified as python-markdown does not
handle the language feature of code blocks.
"""
code = Code('print("Hello, World!")')
assert str(code) == '```generic\nprint("Hello, World!")\n```'
Expand All @@ -52,7 +59,9 @@ def test_code_two_lines():
"""
Verifies that a code block is correctly instantiated
given a pair of lines of sample code. Also verifies
the repr representation of the code block.
the repr representation of the code block. Markdown itself
is not verified as python-markdown does not
handle the language feature of code blocks.
"""
code = Code("sum = 4 + 5\nprint(sum)")
print(repr(code))
Expand All @@ -63,19 +72,31 @@ def test_code_two_lines():
def test_code_nested():
"""
Verifies that a code block is correctly instantiated
given an existing code block. Also verifies the
repr representation of the code block.
given an existing code block. Also verifies the
repr representation of the code block. Markdown itself
is not verified as python-markdown does not
handle the language feature of code blocks.
"""
nested_code = Code("print('Hello, World!')", lang="python")
code = Code(nested_code, lang="markdown")
assert str(code) == "````markdown\n```python\nprint('Hello, World!')\n```\n````"
assert repr(code) == r"""Code(code=Code(code="print('Hello, World!')", lang='python'), lang='markdown')"""


assert (
repr(code)
== r"""Code(code=Code(code="print('Hello, World!')", lang='python'), lang='markdown')"""
)


# Method tests


def test_repr_can_create_object():
"""
Verifies that the __repr__ method can correctly
generate a string that can be used to create
an identical code block.
"""
code = Code("")
obj = eval(repr(code))
assert isinstance(obj, Code)
assert isinstance(obj, Code)
assert obj == code
assert id(obj) != id(code)
Loading