Add tests for linguist integration

This commit is contained in:
Jake Howard 2023-06-04 19:25:38 +01:00
parent 8f5ab16891
commit c2d8ef260a
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -1,8 +1,10 @@
import re
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.urls import reverse from django.urls import reverse
from .blocks import CodeStructValue, get_language_choices from .blocks import CodeStructValue, get_language_choices
from .utils import PYGMENTS_VERSION_SLUG from .utils import PYGMENTS_VERSION_SLUG, get_linguist_colours
class PygmentsStylesTestCase(SimpleTestCase): class PygmentsStylesTestCase(SimpleTestCase):
@ -35,6 +37,29 @@ class CodeStructValueTestCase(SimpleTestCase):
block = CodeStructValue(None, [("filename", ""), ("language", "Python")]) block = CodeStructValue(None, [("filename", ""), ("language", "Python")])
self.assertEqual(block.header_text(), "Python") self.assertEqual(block.header_text(), "Python")
def test_header_text_uses_language_mapping(self) -> None:
block = CodeStructValue(
None, [("filename", ""), ("language", "Python console session")]
)
self.assertEqual(block.header_text(), "Python")
def test_header_text_empty_when_text(self) -> None: def test_header_text_empty_when_text(self) -> None:
block = CodeStructValue(None, [("filename", ""), ("language", "Text only")]) block = CodeStructValue(None, [("filename", ""), ("language", "Text only")])
self.assertEqual(block.header_text(), "") self.assertEqual(block.header_text(), "")
def test_linguist_mapping(self) -> None:
linguist_languages = set(get_linguist_colours().keys())
for language in CodeStructValue.LINGUIST_MAPPING.values():
self.assertIn(language.lower(), linguist_languages)
class LinguistColoursTestCase(SimpleTestCase):
HEX_RE = re.compile(r"#[0-9A-F]", re.IGNORECASE)
def test_gets_colours(self) -> None:
colours = get_linguist_colours()
for colour in colours.values():
with self.subTest(colour):
self.assertRegex(colour, self.HEX_RE)