"""Replay tutorial introduction through the deployed engine's layout routine.""" import json from pathlib import Path import unittest from script_text import patch_display_calls from test_cjk_layout import layout class IntroTests(unittest.TestCase): def test_intro_paragraphs_are_centered_without_orphan_punctuation(self): mapping = json.loads(Path(__file__).with_name('tutorial_cn.json').read_text(encoding='utf-8')) intros = [v for k, v in mapping.items() if k.startswith('\fNow')] self.assertEqual(len(intros), 2) for intro in intros: for width in (360, 400, 480): lines, _ = layout(intro, width=width, alignment=0) for raw, x, y in lines: text = raw.decode("gb2312") pixels = sum(5 if c == " " else 8 if ord(c) < 128 else 12 for c in text) self.assertEqual(x, (width - pixels) // 2) self.assertNotIn(raw.decode('gb2312')[0], ',。!?、;:”)') self.assertEqual(len(lines), 4) def test_alignment_override_is_explicit_and_preserves_newlines(self): source = b'SetMissionDescription("\\fA\\n\\aB");' mapping = {'\fA\n\aB': '\bA\n\bB'} with self.assertRaises(ValueError): patch_display_calls(source, mapping) self.assertEqual(patch_display_calls(source, mapping, allow_alignment_changes=True), b'SetMissionDescription("\\bA\\n\\bB");') with self.assertRaises(ValueError): patch_display_calls(source, {'\fA\n\aB': '\bA\bB'}, allow_alignment_changes=True) if __name__ == '__main__': unittest.main()