Correctly get language

Operator precedence is important
This commit is contained in:
Jake Howard 2022-09-21 13:23:33 +01:00
parent 82701a706c
commit 2d537d3b10
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 13 additions and 1 deletions

View file

@ -32,7 +32,7 @@ class CodeStructValue(StructValue):
if filename := self["filename"]:
return filename
if language := self["language"] and self["language"] != TextLexer.name:
if (language := self["language"]) != TextLexer.name:
return language
return ""

View file

@ -26,3 +26,15 @@ class CodeStructValueTestCase(SimpleTestCase):
None, [("source", "test"), ("language", language)]
)
self.assertIsInstance(block.code(), str)
def test_header_text_uses_filename(self) -> None:
block = CodeStructValue(None, [("filename", "test.txt")])
self.assertEqual(block.header_text(), "test.txt")
def test_header_text_uses_language(self) -> None:
block = CodeStructValue(None, [("filename", ""), ("language", "Python")])
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(), "")