From d74a635e4150441211903ae505332974846cc7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 8 Aug 2017 19:22:04 +0200 Subject: [PATCH] [util] update 'default' values and improve test coverage for 'code_to_language()' and 'language_to_code()' --- gallery_dl/util.py | 8 ++++---- test/test_util.py | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 7271674e..12196162 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -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: diff --git a/test/test_util.py b/test/test_util.py index 5fd5c3c4..3283ff90 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -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__':