"""Using the "tab" Blocks extension, test general cases for Blocks.""" from ... import util import unittest from pymdownx.blocks import block import markdown class TestTypeFunctions(unittest.TestCase): """Validate various type functions.""" def test_type_any(self): """Test `type_any`.""" self.assertEqual(3, block.type_any(3)) self.assertEqual({}, block.type_any({})) self.assertEqual('string', block.type_any('string')) def test_type_number(self): """Test `type_number`.""" self.assertEqual(3, block.type_number(3)) self.assertEqual(3.0, block.type_number(3.0)) with self.assertRaises(ValueError): block.type_number('string') def test_type_integer(self): """Test `type_integer`.""" self.assertEqual(3, block.type_integer(3)) self.assertEqual(3, block.type_integer(3.0)) with self.assertRaises(ValueError): block.type_integer(3.3) def test_type_ranged_number(self): """Test `type_ranged_number`.""" self.assertEqual(4.7, block.type_ranged_number(3, 8)(4.7)) with self.assertRaises(ValueError): block.type_ranged_number(3, 8)(2.7) with self.assertRaises(ValueError): block.type_ranged_number(3, 8)(9.2) self.assertEqual(-4.7, block.type_ranged_number(None, 8)(-4.7)) with self.assertRaises(ValueError): block.type_ranged_number(None, 8)(9.2) self.assertEqual(1004.7, block.type_ranged_number(3, None)(1004.7)) with self.assertRaises(ValueError): block.type_ranged_number(3, None)(2.3) with self.assertRaises(ValueError): block.type_ranged_number(3, 8)('string') def test_type_ranged_integer(self): """Test `type_ranged_integer`.""" self.assertEqual(4, block.type_ranged_integer(3, 8)(4)) self.assertEqual(4, block.type_ranged_integer(3, 8)(4.0)) with self.assertRaises(ValueError): block.type_ranged_integer(3, 8)(4.3) with self.assertRaises(ValueError): block.type_ranged_integer(3, 8)(2) with self.assertRaises(ValueError): block.type_ranged_integer(3, 8)(9) def test_type_html_identifier(self): """Test `type_html_tag`.""" self.assertEqual('div', block.type_html_identifier('div')) with self.assertRaises(ValueError): block.type_html_identifier('3bad') def test_type_boolean(self): """Test `type_boolean`.""" self.assertEqual(True, block.type_boolean(True)) self.assertEqual(False, block.type_boolean(False)) with self.assertRaises(ValueError): block.type_boolean(None) def test_type_ternary(self): """Test `type_ternary`.""" self.assertEqual(True, block.type_ternary(True)) self.assertEqual(False, block.type_ternary(False)) self.assertEqual(None, block.type_ternary(None)) with self.assertRaises(ValueError): block.type_ternary(3) def test_type_string(self): """Test `type_string`.""" self.assertEqual('string', block.type_string('string')) with self.assertRaises(ValueError): block.type_string(3) def test_type_string_insensitive(self): """Test `type_string_insensitive`.""" self.assertEqual('string', block.type_string_insensitive('STRING')) with self.assertRaises(ValueError): block.type_string_insensitive(3) def test_type_string_in(self): """Test `type_string_in`.""" self.assertEqual('this', block.type_string_in(['this', 'that'])('this')) self.assertEqual('this', block.type_string_in(['this', 'that'])('This')) self.assertEqual('this', block.type_string_in(['this', 'that'], insensitive=False)('this')) with self.assertRaises(ValueError): block.type_string_in(['this', 'that'], insensitive=False)('This') with self.assertRaises(ValueError): block.type_string_in(['this', 'that'])('bad') def test_type_string_delimiter(self): """Test `type_string_delimiter`.""" self.assertEqual(['this', 'that'], block.type_string_delimiter(';')('this; that')) self.assertEqual(['this', 'that'], block.type_string_delimiter(' ')('this that')) def test_type_html_classes(self): """Test `type_html_classes`.""" self.assertEqual(['this', 'that'], block.type_html_classes('this that')) def test_type_multi(self): """Test `type_multi`.""" t = block.type_multi(block.type_ternary, block.type_string_in(['this', 'that'])) self.assertEqual(True, t(True)) self.assertEqual(False, t(False)) self.assertEqual(None, t(None)) self.assertEqual('this', t('this')) with self.assertRaises(ValueError): t(3) with self.assertRaises(ValueError): t('other') with self.assertRaises(ValueError): block.type_multi()(True) class TestRegister(unittest.TestCase): """Test registration cases.""" def test_duplicates(self): """Test duplicates.""" with self.assertRaises(ValueError): markdown.markdown('test', extensions=['pymdownx.blocks.admonition', 'pymdownx.blocks.admonition']) class TestBlockUndefinedOption(util.MdCase): """Test Blocks with undefined options.""" extension = ['pymdownx.blocks.html', 'pymdownx.blocks.definition'] def test_undefined_option(self): """An undefined option will cause the block parsing to fail.""" self.check_markdown( R''' /// html | div option: whatever content /// ''', '''
/// html | div option: whatever
content ///
''', True ) def test_bad_option(self): """An undefined option will cause the block parsing to fail.""" self.check_markdown( R''' /// html | div attrs: whatever content /// ''', '''/// html | div attrs: whatever
content ///
''', True ) def test_no_arg(self): """Test no options.""" self.check_markdown( R''' /// html attrs: whatever content /// ''', '''/// html attrs: whatever
content ///
''', True ) def test_too_many_args(self): """Test too many options.""" self.check_markdown( R''' /// define option: whatever content /// ''', '''/// define option: whatever
content ///
''', True ) def test_bad_frontmatter(self): """Test bad frontmatter.""" self.check_markdown( R''' /// define bad() content /// ''', '''/// define bad()
content ///
''', True ) def test_bad_frontmatter2(self): """Test bad frontmatter.""" self.check_markdown( R''' /// define & content /// ''', '''/// define &
content ///
''', True ) def test_commented_frontmatter(self): """Test commented frontmatter.""" self.check_markdown( R''' /// html | div # attrs: {class: test} content /// ''', '''content
import foo
python
import foo
Other content
''',
True
)
class TestAttributes(util.MdCase):
"""Test Blocks tab cases."""
extension = ['pymdownx.blocks.admonition']
def test_attributes(self):
"""Test attributes."""
self.check_markdown(
R'''
/// admonition | Title
attrs: {class: some classes, id: an-id, name: some value}
content
///
''',
'''
Title
content
/// admonition | Title attrs: {'+': 'value'} content ///
''', True ) class TestNestedBlocksAndLists(util.MdCase): """Test Nested blocks and lists.""" extension = ['pymdownx.blocks.tab', 'pymdownx.blocks.html'] extension_configs = { 'pymdownx.blocks.tab': {'alternate_style': True} } def test_nested_blocks_in_lists(self): """Test a nested blocks case with lists.""" self.check_markdown( R""" //// html | div.my-div - List - List /// tab | TEST1 Content /// /// tab | TEST2 - A list Paragraph Code /// //// """, """List
List
Content
A list
Paragraph
Code
Hello I'm in a div which can contain markdown!
foo foo
Some content
item 1
item 2
item A
item B
Some more content.
Some content
And more content.
Content
''', # noqa: E501 True ) def test_with_lists(self): """Test with lists.""" self.check_markdown( ''' - List /// tab | Tab - Paragraph Paragraph /// ''', '''List
Paragraph
Paragraph
List
Paragraph
Paragraph
Paragraph
paragraph
List
Paragraph
Paragraph
Paragraph
Paragraph
List
Definition
More text
Another definition
Even more text
And more content again.
Parent 1
Parent 1
Admonition 1
Admonition 1
Test tab 1
Admonition 2
Admonition 2
Test tab 2
Admonition 3
Admonition 3