From 6cbef9355d3f32844f348d26030cbe288e441502 Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 2 Aug 2017 10:16:34 +0000 Subject: [PATCH] Add unit tests --- test/__init__.py | 0 test/data/fr-t-k0-android.xml | 138 ++++++++ test/data/fr.json | 614 ++++++++++++++++++++++++++++++++++ test/test_cldr2json.py | 149 +++++++++ 4 files changed, 901 insertions(+) create mode 100644 test/__init__.py create mode 100644 test/data/fr-t-k0-android.xml create mode 100644 test/data/fr.json create mode 100755 test/test_cldr2json.py diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/test/data/fr-t-k0-android.xml b/test/data/fr-t-k0-android.xml new file mode 100644 index 000000000..1e76b81fa --- /dev/null +++ b/test/data/fr-t-k0-android.xml @@ -0,0 +1,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/data/fr.json b/test/data/fr.json new file mode 100644 index 000000000..2aeb67479 --- /dev/null +++ b/test/data/fr.json @@ -0,0 +1,614 @@ +{ + "levels": [ + { + "level": 1, + "mode": "default", + "rows": [ + [ + [ + "a", + "à", + "â", + "%", + "æ", + "á", + "ä", + "ã", + "å", + "ā", + "ª" + ], + [ + "z" + ], + [ + "e", + "é", + "è", + "ê", + "ë", + "%", + "ę", + "ė", + "ē" + ], + [ + "r" + ], + [ + "t" + ], + [ + "y", + "%", + "ÿ" + ], + [ + "u", + "ù", + "û", + "%", + "ü", + "ú", + "ū" + ], + [ + "i", + "î", + "%", + "ï", + "ì", + "í", + "į", + "ī" + ], + [ + "o", + "ô", + "œ", + "%", + "ö", + "ò", + "ó", + "õ", + "ø", + "ō", + "º" + ], + [ + "p" + ] + ], + [ + [ + "q" + ], + [ + "s" + ], + [ + "d" + ], + [ + "f" + ], + [ + "g" + ], + [ + "h" + ], + [ + "j" + ], + [ + "k" + ], + [ + "l" + ], + [ + "m" + ] + ], + [ + [ + "w" + ], + [ + "x" + ], + [ + "c", + "ç", + "ć", + "č" + ], + [ + "v" + ], + [ + "b" + ], + [ + "n" + ], + [ + "'", + "‘", + "’", + "‹", + "›" + ] + ], + [ + [ + "," + ], + [ + " " + ], + [ + ".", + "#", + "!", + ",", + "?", + "-", + ":", + "'", + "@" + ] + ] + ] + }, + { + "level": 2, + "mode": "latched", + "rows": [ + [ + [ + "A", + "À", + "Â", + "%", + "Æ", + "Á", + "Ä", + "Ã", + "Å", + "Ā", + "ª" + ], + [ + "Z" + ], + [ + "E", + "É", + "È", + "Ê", + "Ë", + "%", + "Ę", + "Ė", + "Ē" + ], + [ + "R" + ], + [ + "T" + ], + [ + "Y", + "%", + "Ÿ" + ], + [ + "U", + "Ù", + "Û", + "%", + "Ü", + "Ú", + "Ū" + ], + [ + "I", + "Î", + "%", + "Ï", + "Ì", + "Í", + "Į", + "Ī" + ], + [ + "O", + "Ô", + "Œ", + "%", + "Ö", + "Ò", + "Ó", + "Õ", + "Ø", + "Ō", + "º" + ], + [ + "P" + ] + ], + [ + [ + "Q" + ], + [ + "S" + ], + [ + "D" + ], + [ + "F" + ], + [ + "G" + ], + [ + "H" + ], + [ + "J" + ], + [ + "K" + ], + [ + "L" + ], + [ + "M" + ] + ], + [ + [ + "W" + ], + [ + "X" + ], + [ + "C", + "Ç", + "Ć", + "Č" + ], + [ + "V" + ], + [ + "B" + ], + [ + "N" + ], + [ + "'", + "‘", + "’", + "‹", + "›" + ] + ], + [ + [ + "," + ], + [ + " " + ], + [ + ".", + "#", + "!", + ",", + "?", + "-", + ":", + "'", + "@" + ] + ] + ] + }, + { + "level": 3, + "mode": "locked", + "rows": [ + [ + [ + "1", + "¹", + "½", + "⅓", + "¼", + "⅛" + ], + [ + "2", + "²", + "⅔" + ], + [ + "3", + "³", + "¾", + "⅜" + ], + [ + "4", + "⁴" + ], + [ + "5", + "⅝" + ], + [ + "6" + ], + [ + "7", + "⅞" + ], + [ + "8" + ], + [ + "9" + ], + [ + "0", + "ⁿ", + "∅" + ] + ], + [ + [ + "@" + ], + [ + "#" + ], + [ + "€", + "¢", + "£", + "$", + "¥", + "₱" + ], + [ + "%", + "‰" + ], + [ + "&" + ], + [ + "-", + "_", + "–", + "—", + "·" + ], + [ + "+", + "±" + ], + [ + "(", + "<", + "{", + "[" + ], + [ + ")", + ">", + "}", + "]" + ] + ], + [ + [ + "*", + "†", + "‡", + "★" + ], + [ + "\"", + "“", + "”", + "«", + "»" + ], + [ + "'", + "‘", + "’", + "‹", + "›" + ], + [ + ":" + ], + [ + ";" + ], + [ + "!", + "¡" + ], + [ + "?", + "¿" + ] + ], + [ + [ + "_" + ], + [ + "/" + ], + [ + " " + ], + [ + "," + ], + [ + ".", + "…" + ] + ] + ] + }, + { + "level": 4, + "mode": "locked", + "rows": [ + [ + [ + "~" + ], + [ + "`" + ], + [ + "|" + ], + [ + "•", + "♪", + "♥", + "♠", + "♦", + "♣" + ], + [ + "√" + ], + [ + "Π", + "π" + ], + [ + "÷" + ], + [ + "×" + ], + [ + "¶", + "§" + ], + [ + "∆" + ] + ], + [ + [ + "£" + ], + [ + "¥" + ], + [ + "$", + "¢" + ], + [ + "¢" + ], + [ + "^", + "↑", + "↓", + "←", + "→" + ], + [ + "°", + "′", + "″" + ], + [ + "=", + "≠", + "≈", + "∞" + ], + [ + "{" + ], + [ + "}" + ] + ], + [ + [ + "\\" + ], + [ + "©" + ], + [ + "®" + ], + [ + "™" + ], + [ + "℅" + ], + [ + "[" + ], + [ + "]" + ] + ], + [ + [ + "<", + "‹", + "≤", + "«" + ], + [ + ">", + "›", + "≥", + "»" + ], + [ + " " + ], + [ + "," + ], + [ + ".", + "…" + ] + ] + ] + } + ], + "locale": "fr", + "name": "French" +} \ No newline at end of file diff --git a/test/test_cldr2json.py b/test/test_cldr2json.py new file mode 100755 index 000000000..032843fef --- /dev/null +++ b/test/test_cldr2json.py @@ -0,0 +1,149 @@ +#!/usr/bin/python3 + +import json +import tempfile +import unittest +import xml.etree.ElementTree + +import cldr2json + + +class TestParseSingleKey(unittest.TestCase): + def test_ascii(self): + self.assertEqual(cldr2json.parse_single_key("a"), "a") + + def test_nonascii(self): + self.assertEqual(cldr2json.parse_single_key("Æ"), "Æ") + + def test_twochars(self): + self.assertEqual(cldr2json.parse_single_key("ԵՒ"), "ԵՒ") + + def test_decode(self): + self.assertEqual(cldr2json.parse_single_key("\\u{200D}"), "\u200d") + + def test_decode_threechars(self): + self.assertEqual(cldr2json.parse_single_key("ज\\u{94D}ञ"), "ज\u094Dञ") + + +class TestParseRow(unittest.TestCase): + def test_parse_row(self): + xml_string = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + """ + xml_tree = xml.etree.ElementTree.XML(xml_string) + json = [[ + ['a', 'à', 'â', '%', 'æ', 'á', 'ä', 'ã', 'å', 'ā', 'ª'], + ['z'], + ['e', 'é', 'è', 'ê', 'ë', '%', 'ę', 'ė', 'ē'], + ['r'], + ['t'], + ['y', '%', 'ÿ'], + ['u', 'ù', 'û', '%', 'ü', 'ú', 'ū'], + ['i', 'î', '%', 'ï', 'ì', 'í', 'į', 'ī'], + ['o', 'ô', 'œ', '%', 'ö', 'ò', 'ó', 'õ', 'ø', 'ō', 'º'], + ['p'] + ], [ + ['q'], + ['s'], + ['d'], + ['f'], + ['g'], + ['h'], + ['j'], + ['k'], + ['l'], + ['m'] + ], [ + ['w'], + ['x'], + ['c', 'ç', 'ć', 'č'], + ['v'], + ['b'], + ['n'], + ["'", '‘', '’', '‹', '›'] + ], [ + [','], + [' '], + ['.', '#', '!', ',', '?', '-', ':', "'", '@'] + ]] + self.assertEqual(cldr2json.parse_rows(xml_tree), json) + + +class TestConvertXml(unittest.TestCase): + # cldr2json.convert_xml(xml): + pass + + +class TestConvertFile(unittest.TestCase): + def test_fr(self): + outdir = tempfile.mkdtemp() + cldr2json.convert_file("test/data/fr-t-k0-android.xml", outdir) + with open("test/data/fr.json", encoding="utf-8") as expected_json_fd: + expected_json = json.load(expected_json_fd) + with open(outdir + "/fr.json", encoding="utf-8") as actual_json_fd: + actual_json = json.load(actual_json_fd) + self.assertEqual(expected_json, actual_json) + pass + + +class TestLocaleToXKB(unittest.TestCase): + def test_simple(self): + self.assertEqual(cldr2json.locale_to_xkb("fr", "French"), + "fr") + + def test_fallback(self): + self.assertEqual(cldr2json.locale_to_xkb("nb", "Norwegian Bokmål"), + "no") + + def test_fallback2(self): + self.assertEqual(cldr2json.locale_to_xkb("km", "Khmer"), + "kh") + + def test_override(self): + self.assertEqual(cldr2json.locale_to_xkb("en-GB", + "English Great Britain"), + "uk") + + +class LoadXKBMapplings(unittest.TestCase): + def test_dictionnary(self): + self.assertIsInstance(cldr2json.load_xkb_mappings(), dict) + + def test_mapping(self): + mapping = cldr2json.load_xkb_mappings() + self.assertEqual(mapping["French"], "fr") + + +if __name__ == '__main__': + unittest.main()