Fix unicode woes in doctests

This commit is contained in:
Matthias Urlichs committed 2019-02-04 13:00:54 +01:00
1 parent 58b32803df
commit aec041b014
2 files changed
+22 -17

No files matched your search

+7 -6
View File
@@ -79,17 +79,18 @@ def get_divider_line(c, message=None, length=78, indent=3):
Examples:
>>> get_divider_line("-")
>>> from octoprint.util import to_native_str
>>> to_native_str(get_divider_line("-"))
'------------------------------------------------------------------------------'
>>> get_divider_line("=", length=10)
>>> to_native_str(get_divider_line("=", length=10))
'=========='
>>> get_divider_line("-", message="Hi", length=10)
>>> to_native_str(get_divider_line("-", message="Hi", length=10))
'--- Hi ---'
>>> get_divider_line("-", message="A slightly longer text")
>>> to_native_str(get_divider_line("-", message="A slightly longer text"))
'--- A slightly longer text ---------------------------------------------------'
>>> get_divider_line("-", message="A slightly longer text", indent=5)
>>> to_native_str(get_divider_line("-", message="A slightly longer text", indent=5))
'----- A slightly longer text -------------------------------------------------'
>>> get_divider_line("-", message="Hello World!", length=10)
>>> to_native_str(get_divider_line("-", message="Hello World!", length=10))
'--- Hello World!'
>>> get_divider_line(None)
Traceback (most recent call last):
+15 -11
View File
@@ -434,11 +434,12 @@ class AnnouncementPlugin(octoprint.plugin.AssetPlugin,
_image_tag_re = re.compile(r'<img.*?/?>')
def _strip_images(text):
"""
>>> _strip_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>")
>>> from octoprint.util import to_native_str
>>> to_native_str(_strip_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>"))
"<a href='test.html'>I'm a link</a> and this is an image: "
>>> _strip_images("One <img src=\\"one.jpg\\"> and two <img src='two.jpg' > and three <img src=three.jpg> and four <img src=\\"four.png\\" alt=\\"four\\">")
>>> to_native_str(_strip_images("One <img src=\\"one.jpg\\"> and two <img src='two.jpg' > and three <img src=three.jpg> and four <img src=\\"four.png\\" alt=\\"four\\">"))
'One and two and three and four '
>>> _strip_images("No images here")
>>> to_native_str(_strip_images("No images here"))
'No images here'
"""
return _image_tag_re.sub('', text)
@@ -446,9 +447,10 @@ def _strip_images(text):
def _replace_images(text, callback):
"""
>>> callback = lambda img: "foobar"
>>> _replace_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>", callback)
>>> from octoprint.util import to_native_str
>>> to_native_str(_replace_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>", callback))
"<a href='test.html'>I'm a link</a> and this is an image: foobar"
>>> _replace_images("One <img src=\\"one.jpg\\"> and two <img src='two.jpg' > and three <img src=three.jpg> and four <img src=\\"four.png\\" alt=\\"four\\">", callback)
>>> to_native_str(_replace_images("One <img src=\\"one.jpg\\"> and two <img src='two.jpg' > and three <img src=three.jpg> and four <img src=\\"four.png\\" alt=\\"four\\">", callback))
'One foobar and two foobar and three foobar and four foobar'
"""
result = text
@@ -461,13 +463,14 @@ def _replace_images(text, callback):
_image_src_re = re.compile(r'src=(?P<quote>[\'"]*)(?P<src>.*?)(?P=quote)(?=\s+|>)')
def _lazy_images(text, placeholder=None):
"""
>>> _lazy_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>")
>>> from octoprint.util import to_native_str
>>> to_native_str(_lazy_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>"))
'<a href=\\'test.html\\'>I\\'m a link</a> and this is an image: <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src=\\'foo.jpg\\' alt=\\'foo\\'>'
>>> _lazy_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>", placeholder="ph.png")
>>> to_native_str(_lazy_images("<a href='test.html'>I'm a link</a> and this is an image: <img src='foo.jpg' alt='foo'>", placeholder="ph.png"))
'<a href=\\'test.html\\'>I\\'m a link</a> and this is an image: <img src="ph.png" data-src=\\'foo.jpg\\' alt=\\'foo\\'>'
>>> _lazy_images("One <img src=\\"one.jpg\\"> and two <img src='two.jpg' > and three <img src=three.jpg> and four <img src=\\"four.png\\" alt=\\"four\\">", placeholder="ph.png")
>>> to_native_str(_lazy_images("One <img src=\\"one.jpg\\"> and two <img src='two.jpg' > and three <img src=three.jpg> and four <img src=\\"four.png\\" alt=\\"four\\">", placeholder="ph.png"))
'One <img src="ph.png" data-src="one.jpg"> and two <img src="ph.png" data-src=\\'two.jpg\\' > and three <img src="ph.png" data-src=three.jpg> and four <img src="ph.png" data-src="four.png" alt="four">'
>>> _lazy_images("No images here")
>>> to_native_str(_lazy_images("No images here"))
'No images here'
"""
if placeholder is None:
@@ -487,9 +490,10 @@ def _lazy_images(text, placeholder=None):
def _strip_tags(text):
"""
>>> _strip_tags("<a href='test.html'>Hello world</a>&lt;img src='foo.jpg'&gt;")
>>> from octoprint.util import to_native_str
>>> to_native_str(_strip_tags("<a href='test.html'>Hello world</a>&lt;img src='foo.jpg'&gt;"))
"Hello world&lt;img src='foo.jpg'&gt;"
>>> _strip_tags("&#62; &#x3E; Foo")
>>> to_native_str(_strip_tags("&#62; &#x3E; Foo"))
'&#62; &#x3E; Foo'
"""
try: