glossary: now supports quoted and unquoted entries (incl. tests)
This commit is contained in:
parent
a1a090bcae
commit
580c506483
@ -58,7 +58,11 @@ class Glossary:
|
||||
raise GlossaryError(f"File type '{file_path.suffix}' is not supported")
|
||||
with open(file_path, "r") as fd:
|
||||
try:
|
||||
data = yaml.load(fd, Loader=yaml.FullLoader)
|
||||
# use BaseLoader so every entry is read as a string
|
||||
# - disables automatic conversions
|
||||
# - makes it possible to omit quoting for YAML keywords in entries (e. g. 'yes')
|
||||
# - also correctly reads quoted entries
|
||||
data = yaml.load(fd, Loader=yaml.BaseLoader)
|
||||
clean_entries = data['Entries']
|
||||
return cls(name=data['Name'],
|
||||
source_lang=data['SourceLang'],
|
||||
|
||||
@ -9,8 +9,10 @@ glossary_suffix: str = Glossary.file_suffix
|
||||
|
||||
class TestGlossary(unittest.TestCase):
|
||||
|
||||
def test_from_file_valid_yaml(self) -> None:
|
||||
# Prepare a temporary YAML file with valid content
|
||||
def test_from_file_yaml_unquoted(self) -> None:
|
||||
"""
|
||||
Test glossary creatiom from YAML with unquoted entries.
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as yaml_file:
|
||||
yaml_file.write("Name: Sample\n"
|
||||
"Description: A brief description\n"
|
||||
@ -20,7 +22,9 @@ class TestGlossary(unittest.TestCase):
|
||||
"Entries:\n"
|
||||
" hello: hola\n"
|
||||
" goodbye: adiós\n"
|
||||
" 'yes': sí\n") # 'yes' is a YAML keyword and therefore quoted
|
||||
# 'yes' is a YAML keyword and would normally be quoted
|
||||
" yes: sí\n"
|
||||
" I'm going home: me voy a casa\n")
|
||||
yaml_file_path = Path(yaml_file.name)
|
||||
|
||||
glossary = Glossary.from_file(yaml_file_path)
|
||||
@ -29,7 +33,39 @@ class TestGlossary(unittest.TestCase):
|
||||
self.assertEqual(glossary.ID, "123")
|
||||
self.assertEqual(glossary.source_lang, "en")
|
||||
self.assertEqual(glossary.target_lang, "es")
|
||||
self.assertEqual(glossary.entries, {"hello": "hola", "goodbye": "adiós", "yes": "sí"})
|
||||
self.assertEqual(glossary.entries, {"hello": "hola",
|
||||
"goodbye": "adiós",
|
||||
"yes": "sí",
|
||||
"I'm going home": "me voy a casa"})
|
||||
yaml_file_path.unlink() # Remove the temporary file
|
||||
|
||||
def test_from_file_yaml_quoted(self) -> None:
|
||||
"""
|
||||
Test glossary creatiom from YAML with quoted entries.
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w', delete=False, suffix=glossary_suffix) as yaml_file:
|
||||
yaml_file.write("Name: Sample\n"
|
||||
"Description: A brief description\n"
|
||||
"ID: '123'\n"
|
||||
"SourceLang: en\n"
|
||||
"TargetLang: es\n"
|
||||
"Entries:\n"
|
||||
" 'hello': 'hola'\n"
|
||||
" 'goodbye': 'adiós'\n"
|
||||
" 'yes': 'sí'\n"
|
||||
" \"I'm going home\": 'me voy a casa'\n")
|
||||
yaml_file_path = Path(yaml_file.name)
|
||||
|
||||
glossary = Glossary.from_file(yaml_file_path)
|
||||
self.assertEqual(glossary.name, "Sample")
|
||||
self.assertEqual(glossary.desc, "A brief description")
|
||||
self.assertEqual(glossary.ID, "123")
|
||||
self.assertEqual(glossary.source_lang, "en")
|
||||
self.assertEqual(glossary.target_lang, "es")
|
||||
self.assertEqual(glossary.entries, {"hello": "hola",
|
||||
"goodbye": "adiós",
|
||||
"yes": "sí",
|
||||
"I'm going home": "me voy a casa"})
|
||||
yaml_file_path.unlink() # Remove the temporary file
|
||||
|
||||
def test_to_file_writes_yaml(self) -> None:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user