1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
"""Unit tests for beanbag_docutils.sphinx.ext.image_srcsets."""
from html.parser import HTMLParser
from beanbag_docutils.sphinx.ext import image_srcsets
from beanbag_docutils.sphinx.ext.tests.testcase import SphinxExtTestCase
class _TagCollector(HTMLParser):
"""Collect tags and attributes from a HTML fragment."""
def __init__(self):
super().__init__()
self.tags = []
def handle_starttag(self, tag, attrs):
self.tags.append((tag, dict(attrs)))
class ImageTests(SphinxExtTestCase):
"""Unit tests for image."""
extensions = [
image_srcsets.__name__,
]
def test_with_html(self):
"""Testing image with HTML"""
rendered = self.render_doc(
'.. image:: path/to/image.png\n'
)
self.assertEqual(
rendered,
'<img alt="path/to/image.png" src="path/to/image.png" />')
def test_with_html_and_srcset(self):
"""Testing image-srcset with HTML and srcset"""
rendered = self.render_doc(
'.. image:: path/to/image.png\n'
' :sources: 2x path/to/image@2x.png\n'
' 3x path/to/image@3x.png\n'
)
self.assertEqual(
rendered,
'<img srcset="_images/image.png 1x, _images/image%402x.png 2x,'
' _images/image%403x.png 3x" alt="_images/image.png"'
' src="_images/image.png" />')
def test_with_html_and_srcset_files(self):
"""Testing image-srcset with HTML and srcset @-based files"""
rendered = self.render_doc(
'.. image:: my/images/image.png\n',
extra_files={
'my/images/image.png': b'',
'my/images/image@2x.png': b'',
'my/images/image@3x.png': b'',
'my/images/image@4x.png': b'',
})
self.assertEqual(
rendered,
'<img srcset="_images/image.png 1x, _images/image%402x.png 2x,'
' _images/image%403x.png 3x, _images/image%404x.png 4x"'
' alt="_images/image.png" src="_images/image.png" />')
def test_with_html_and_width_height(self):
"""Testing image-srcset with HTML and srcset and width/height"""
rendered = self.render_doc(
'.. image:: path/to/image.png\n'
' :width: 100\n'
' :height: 200\n'
' :sources: 2x path/to/image@2x.png\n'
' 3x path/to/image@3x.png\n'
)
collector = _TagCollector()
collector.feed(rendered)
self.assertEqual(
collector.tags,
[
('a', {
'class': 'reference internal image-reference',
'href': '_images/image.png',
}),
('img', {
'alt': '_images/image.png',
'height': '200',
'src': '_images/image.png',
'srcset': (
'_images/image.png 1x, _images/image%402x.png 2x,'
' _images/image%403x.png 3x'
),
'width': '100',
}),
])
|