1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

[util] update 'default' values and improve test coverage

for 'code_to_language()' and 'language_to_code()'
This commit is contained in:
Mike Fährmann 2017-08-08 19:22:04 +02:00
parent 0245a0ba5f
commit d74a635e41
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 35 additions and 13 deletions

View File

@ -80,15 +80,15 @@ def bdecode(data, alphabet="0123456789"):
return num
def code_to_language(code, default="English"):
def code_to_language(code, default=None):
"""Map an ISO 639-1 language code to its actual name"""
return CODES.get(code.lower(), default)
return CODES.get((code or "").lower(), default)
def language_to_code(lang, default="en"):
def language_to_code(lang, default=None):
"""Map a language name to its ISO 639-1 code"""
if lang is None:
return None
return default
lang = lang.capitalize()
for code, language in CODES.items():
if language == lang:

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2015 Mike Fährmann
# Copyright 2015-2017 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@ -14,16 +14,38 @@ import gallery_dl.util as util
class TestISO639_1(unittest.TestCase):
def test_code_to_language(self):
self.assertEqual(util.code_to_language("en"), "English")
self.assertEqual(util.code_to_language("FR"), "French")
self.assertEqual(util.code_to_language("xx"), "English")
self.assertEqual(util.code_to_language("xx", default=None), None)
d = "default"
self._run_test(util.code_to_language, {
("en",): "English",
("FR",): "French",
("xx",): None,
("" ,): None,
(None,): None,
("en", d): "English",
("FR", d): "French",
("xx", d): d,
("" , d): d,
(None, d): d,
})
def test_language_to_code(self):
self.assertEqual(util.language_to_code("English"), "en")
self.assertEqual(util.language_to_code("fRENch"), "fr")
self.assertEqual(util.language_to_code("xx"), "en")
self.assertEqual(util.language_to_code("xx", default=None), None)
d = "default"
self._run_test(util.language_to_code, {
("English",): "en",
("fRENch",): "fr",
("xx",): None,
("" ,): None,
(None,): None,
("English", d): "en",
("fRENch", d): "fr",
("xx", d): d,
("" , d): d,
(None, d): d,
})
def _run_test(self, func, tests):
for args, result in tests.items():
self.assertEqual(func(*args), result)
if __name__ == '__main__':