diff --git a/website/contrib/code_block/tests.py b/website/contrib/code_block/tests.py index 66ac346..0fdf158 100644 --- a/website/contrib/code_block/tests.py +++ b/website/contrib/code_block/tests.py @@ -1,8 +1,10 @@ +import re + from django.test import SimpleTestCase from django.urls import reverse 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): @@ -35,6 +37,29 @@ class CodeStructValueTestCase(SimpleTestCase): block = CodeStructValue(None, [("filename", ""), ("language", "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: block = CodeStructValue(None, [("filename", ""), ("language", "Text only")]) 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)