#!/usr/bin/env python3 """Safeguards for the Chinese translate.lua build.""" import importlib.util import tempfile import unittest from pathlib import Path SPEC = importlib.util.spec_from_file_location("build_zh_translate_lua", Path(__file__).with_name("build_zh_translate_lua.py")) MODULE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(MODULE) class TranslateLuaTest(unittest.TestCase): def test_build(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) en, table, out = root / "translate.lua", root / "table.tsv", root / "out.lua" en.write_bytes( b'exportTestForCharset = "\xfc "\ngameforge = {}\ngameforge.q = {}\ngameforge.q.sub = {}\n' b'gameforge.q._10_say = "Hello[ENTER]there. "\n' b'gameforge.q._20_say = "Hello[ENTER]there. "\n' b'gameforge.q.sub._30_say = "Let\xb4s say \\"hi\\" "\n' b'gameforge.q._40_say = "Long "\n') # 乗 is 0x81 0x5C in GBK. table.write_text("# comment\nq._10_say\t你好。\nq._40_say\t" + "长" * 30 + "乗\n", encoding="utf-8") done, total = MODULE.build(en, table, out) self.assertEqual((done, total), (3, 4)) lines = out.read_bytes().split(b"\n") self.assertEqual(lines[3], b"gameforge.q.sub = {}") self.assertEqual(lines[4], 'gameforge.q._10_say = "你好。 "'.encode("gbk")) self.assertEqual(lines[5], lines[4].replace(b"_10_", b"_20_")) # same English text self.assertEqual(lines[6], b'gameforge.q.sub._30_say = "Let\'s say \\"hi\\" "') wrapped = ("长" * 25 + "[ENTER]" + "长" * 5).encode("gbk") + b"\x81\\\\" self.assertEqual(lines[7], b'gameforge.q._40_say = "' + wrapped + b' "') if __name__ == "__main__": unittest.main()