"""Test cases for Highlight."""
from .. import util
class TestHighlightGuess(util.MdCase):
"""Test that highlighting works with guessing."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'guess_lang': True
}
}
def test_guess(self):
"""Test guessing."""
self.check_markdown(
r'''
```
import test
test.test()
```
''',
'''
''', # noqa: E501
True
)
class TestHighlightGuessBlock(util.MdCase):
"""Test that highlighting works with guessing for block."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'guess_lang': "block"
}
}
def test_guess_block(self):
"""Test guessing for block."""
self.check_markdown(
r'''
```
import test
test.test()
```
''',
'''
''', # noqa: E501
True
)
def test_no_guess_inline(self):
"""Test inline code is not language guessed."""
self.check_markdown(
r'''
`int i = std::numeric_limits::min();`
''',
'''
int i = std::numeric_limits<int>::min();
''',
True
)
class TestHighlightAutoTitle(util.MdCase):
"""Test title cases."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'auto_title': True
}
}
def test_auto_tile(self):
"""Test auto title."""
self.check_markdown(
r'''
```pycon
>>> import test
```
''',
r'''
Python Console Session>>> import test
''', # noqa: E501
True
)
class TestHighlightAutoTitleMap(util.MdCase):
"""Test title cases."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'auto_title': True,
"auto_title_map": {
"Python Console Session": "Python"
}
}
}
def test_auto_tile_map(self):
"""Test auto title."""
self.check_markdown(
r'''
```pycon
>>> import test
```
''',
r'''
''', # noqa: E501
True
)
class TestHighlightInline(util.MdCase):
"""Test highlight inline."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'linenums_style': 'pymdownx-inline'
}
}
def test_pymdownx_inline(self):
"""Test new inline mode."""
self.check_markdown(
r'''
```python linenums="1"
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
class TestNoClass(util.MdCase):
"""Test no class."""
extension = ['pymdownx.highlight', 'pymdownx.superfences', 'markdown.extensions.attr_list']
extension_configs = {
'pymdownx.highlight': {
'css_class': ''
}
}
def test_no_class(self):
"""Test with no class."""
self.check_markdown(
r'''
```python
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
def test_no_class_and_user_class(self):
"""Test with no class and user class."""
self.check_markdown(
r'''
```{.python .more}
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
def test_no_class_and_user_class_linenums(self):
"""Test with no class and user class and table format."""
self.check_markdown(
r'''
```{.python .more linenums="1"}
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
class TestNoClassNoPygments(util.MdCase):
"""Test no class."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'css_class': '',
'use_pygments': False
}
}
def test_no_class_no_pygments(self):
"""Test with no class and no Pygments."""
self.check_markdown(
r'''
```python
import test
test.test()
```
''',
r'''
import test
test.test()
''',
True
)
class TestCustomLangPrefixNoPygments(util.MdCase):
"""Test custom language prefix."""
extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
extension_configs = {
'pymdownx.highlight': {
'language_prefix': 'lang-',
'use_pygments': False
}
}
def test_custom_prefix_no_pygments(self):
"""Test with custom prefix and no Pygments."""
self.check_markdown(
r'''
```python
import test
test.test()
```
''',
r'''
import test
test.test()
''',
True
)
def test_custom_prefix_no_pygments_inline(self):
"""Test with custom prefix and no Pygments with inline code."""
self.check_markdown(
'`#!python import test`',
'import test
'
)
class TestNoPygments(util.MdCase):
"""Test no Pygments."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'use_pygments': False
}
}
def test_no_pygments(self):
"""Test with no Pygments."""
self.check_markdown(
r'''
```python
import test
test.test()
```
''',
r'''
import test
test.test()
''',
True
)
def test_no_pygments_linenums(self):
"""Test with no Pygments and line numbers."""
self.check_markdown(
r'''
```python linenums="1"
import test
test.test()
```
''',
r'''
python linenums="1"
import test
test.test()
''',
True
)
def test_no_pygments_extraneous_new_lines(self):
"""Test that extraneous new lines are not included."""
self.check_markdown(
r'''
Test
import test
test.test()
Test
''',
r'''
Test
import test
test.test()
Test
''',
True
)
class TestNoPygmentsCustomLineClass(util.MdCase):
"""Test no Pygments with custom line number class."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'use_pygments': False,
'linenums_class': 'line-numbers',
'linenums': True
}
}
def test_no_pygments_linenums_custom_class(self):
"""Test with no Pygments and line numbers."""
self.check_markdown(
r'''
Text
import test
test.test()
Text
''',
r'''
Text
import test
test.test()
Text
''',
True
)
def test_no_pygments_linenums_custom_class_fences(self):
"""Test with no Pygments and line numbers in fences."""
self.check_markdown(
r'''
```python
import test
test.test()
```
''',
r'''
import test
test.test()
''',
True
)
class TestHighlightSpecial(util.MdCase):
"""Test highlight global special."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'linenums_style': 'pymdownx-inline',
'linenums_special': 2
}
}
def test_special(self):
"""Test global special mode."""
self.check_markdown(
r'''
```python linenums="1"
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
def test_special_override(self):
"""Test global special mode override."""
self.check_markdown(
r'''
```python linenums="1 1 1"
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
class TestDisabledLinenums(util.MdCase):
"""Test with line numbers globally disabled."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'linenums': False
}
}
def test_global_disable(self):
"""Test with line numbers globally disabled."""
self.check_markdown(
r'''
```python linenums="1"
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
class TestDisabledLinenumsNoPygments(util.MdCase):
"""Test with line numbers globally disabled with no Pygments."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'linenums': False,
'use_pygments': False
}
}
def test_global_disable(self):
"""Test with line numbers globally disabled and no Pygments."""
self.check_markdown(
r'''
```python linenums="1"
import test
test.test()
```
''',
r'''
python linenums="1"
import test
test.test()
''',
True
)
class TestGlobalLinenums(util.MdCase):
"""Test global line number cases."""
extension = ['pymdownx.highlight', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'linenums': True
}
}
def test_global_line_numbers(self):
"""Test that global line numbers works."""
self.check_markdown(
r'''
```python
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
def test_global_disabling_of_line_numbers(self):
"""Test that global line numbers can be disabled."""
self.check_markdown(
r'''
```{.python linenums="0"}
import test
test.test()
```
''',
r'''
''', # noqa: E501
True
)
class TestPygmentsLangClass(util.MdCase):
"""Test no Pygments with custom line number class."""
extension = ['pymdownx.highlight', 'pymdownx.inlinehilite', 'pymdownx.superfences']
extension_configs = {
'pymdownx.highlight': {
'pygments_lang_class': True
}
}
def test_superfences(self):
"""Test language classes with SuperFences."""
self.check_markdown(
r'''
```python
import test
```
''',
r'''
''', # noqa: E501
True
)
def test_inlinehilite(self):
"""Test language classes with InlineHilite."""
self.check_markdown(
'''
`#!python import test`
''',
'''
import test
''', # noqa: E501
True
)
class TestExtendedLang(util.MdCase):
"""Test extended language cases."""
extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
extension_configs = {
'pymdownx.highlight': {
'extend_pygments_lang': [
{'name': 'php-inline', 'lang': 'php', 'options': {'startinline': True}}
]
}
}
def test_extended_lang_inlinehilite(self):
"""Test extended language in InlineHilite."""
self.check_markdown(
'''
`#!php-inline $a = array("foo" => 0, "bar" => 1);`
''',
'''
$a = array("foo" => 0, "bar" => 1);
''', # noqa: E501
True
)
def test_extended_lang_superfences(self):
"""Test extended language in SuperFences."""
self.check_markdown(
'''
```php-inline
$a = array("foo" => 0, "bar" => 1);
```
''',
'''
$a = array("foo" => 0, "bar" => 1);
''', # noqa: E501
True
)
def test_extended_lang_case(self):
"""Test extended language in SuperFences."""
self.check_markdown(
'''
```PHP-Inline
$a = array("foo" => 0, "bar" => 1);
```
''',
'''
$a = array("foo" => 0, "bar" => 1);
''', # noqa: E501
True
)
class TestDefaultLang(util.MdCase):
"""Test default block language cases."""
extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
extension_configs = {
'pymdownx.highlight': {
'default_lang': 'python'
}
}
def test_default_block(self):
"""Test that default language affects block, but not inline code."""
self.check_markdown(
'''
`import code`
import code
```
import code
```
''',
'''
import code
''', # noqa: E501
True
)
class TestDefaultLangInline(util.MdCase):
"""Test default inline language cases."""
extension = ['pymdownx.highlight', 'pymdownx.superfences', 'pymdownx.inlinehilite']
extension_configs = {
'pymdownx.inlinehilite': {
'style_plain_text': 'python'
}
}
def test_default_inline(self):
"""Test that default language affects block, but not inline code."""
self.check_markdown(
'''
`import code`
import code
```
import code
```
''',
'''
import code
''', # noqa: E501
True
)