Skip to content

Commit

Permalink
字符回退映射预置表添加测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
TakWolf committed Jun 27, 2024
1 parent b4f3ca0 commit 80abfb5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/pixel_font_knife/character_fallback_util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from importlib import resources
from os import PathLike
from typing import TypeAlias, Literal

import yaml

_data_dir = resources.files(__package__).joinpath('data')

PresetName: TypeAlias = Literal[
'cjk_unified_to_kangxi_radicals',
'cjk_unified_to_radicals_supplement',
'inherited_glyphs',
]


def load_yaml(
file_path: str | bytes | PathLike[str] | PathLike[bytes],
) -> dict[int, list[int]]:
with open(file_path, 'rb') as file:
return yaml.safe_load(file)


def load_preset(name: PresetName) -> dict[int, list[int]]:
return yaml.safe_load(_data_dir.joinpath(f'{name}.yaml').read_bytes())
31 changes: 31 additions & 0 deletions tests/test_character_fallback_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unidata_blocks

from pixel_font_knife import character_fallback_util

_block_cjk_unified = unidata_blocks.get_block_by_name('CJK Unified Ideographs')
_block_kangxi_radicals = unidata_blocks.get_block_by_name('Kangxi Radicals')
_block_radicals_supplement = unidata_blocks.get_block_by_name('CJK Radicals Supplement')


def test_preset_cjk_unified_to_kangxi_radicals():
mapping = character_fallback_util.load_preset('cjk_unified_to_kangxi_radicals')
for target, code_points in mapping.items():
assert target in _block_cjk_unified
for code_point in code_points:
assert code_point in _block_kangxi_radicals


def test_preset_cjk_unified_to_radicals_supplement():
mapping = character_fallback_util.load_preset('cjk_unified_to_radicals_supplement')
for target, code_points in mapping.items():
assert target in _block_cjk_unified
for code_point in code_points:
assert code_point in _block_radicals_supplement


def test_preset_inherited_glyphs():
mapping = character_fallback_util.load_preset('inherited_glyphs')
for target, code_points in mapping.items():
assert target in _block_cjk_unified
for code_point in code_points:
assert code_point in _block_cjk_unified

0 comments on commit 80abfb5

Please sign in to comment.