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:
parent
0245a0ba5f
commit
d74a635e41
@ -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:
|
||||
|
@ -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__':
|
||||
|
Loading…
Reference in New Issue
Block a user