Skip to content

Commit

Permalink
Merge pull request #50 from eumiro/better-testing
Browse files Browse the repository at this point in the history
Tests: Pytest, Tox, Github Actions
  • Loading branch information
lxneng authored Dec 14, 2020
2 parents fe19af6 + 4f21395 commit b36d2ce
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 99 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test xpinyin

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ python:
- "3.7"
- "3.8"
- "3.9"
script: nosetests
install:
- pip install -r requirements-dev.txt
script: tox
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
xpinyin
==========

.. image:: https://github.com/lxneng/xpinyin/workflows/CI/badge.svg
:target: https://github.com/lxneng/xpinyin/actions?query=workflow%3ACI

.. image:: https://img.shields.io/travis/lxneng/xpinyin.svg
:target: https://travis-ci.org/lxneng/xpinyin

Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
tox
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@
test_suite='xpinyin.tests',
package_dir={'': 'src'},
include_package_data=True,
extras_require={
"test": [
"pytest>=6.2.0"
],
},
license="BSD")
98 changes: 0 additions & 98 deletions src/xpinyin/tests.py

This file was deleted.

105 changes: 105 additions & 0 deletions tests/test_xpinyin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import pytest

from xpinyin import Pinyin
from xpinyin.combs import _get_comb_indexes, get_combs


@pytest.fixture(scope='module')
def pinyin():
return Pinyin()


def test_get_pinyin_with_default_splitter(pinyin):
assert pinyin.get_pinyin('上海') == 'shang-hai'


def test_get_pinyin_with_splitter(pinyin):
assert pinyin.get_pinyin('上海', splitter='') == 'shanghai'


def test_get_pinyin_mixed_words(pinyin):
assert pinyin.get_pinyin('Apple发布iOS7', splitter='-') == 'Apple-fa-bu-iOS7'


def test_get_pinyin_with_tone_marks(pinyin):
assert pinyin.get_pinyin('上海', tone_marks='marks') == 'sh\xe0ng-h\u01cei'
assert pinyin.get_pinyin('秋', tone_marks='marks') == 'qiū'


def test_get_initial(pinyin):
assert pinyin.get_initial('你') == 'N'


def test_get_initials(pinyin):
assert pinyin.get_initials('你好') == 'N-H'


def test_get_initials_with_splitter(pinyin):
assert pinyin.get_initials('你好', ' ') == 'N H'
assert pinyin.get_initials('你好', '') == 'NH'


# --- testing combinations auxiliary functions ---

def test_get_comb_indexes(pinyin):
assert [[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 0]] == _get_comb_indexes([2, 2, 1])


def test_get_comb_indexes_max_num(pinyin):
assert [[0, 0, 0], [0, 1, 0], [1, 0, 0]], _get_comb_indexes([2, 2, 1], 3)


def test_get_combs(pinyin):
assert ['a1@', 'a1#', 'a2@', 'a2#', 'b1@', 'b1#', 'b2@', 'b2#'] == get_combs([['a', 'b'], ['1', '2'], ['@', '#']])


def test_get_combs_splitter_max_num(pinyin):
assert ['a 1 @', 'a 1 #', 'a 2 @', 'a 2 #', 'b 1 @'] == get_combs([['a', 'b'], ['1', '2'], ['@', '#']],
splitter=' ', n=5)


def test_get_combs_max_num_too_big(pinyin):
assert ['a||1||@', 'a||1||#', 'a||2||@', 'a||2||#', 'b||1||@', 'b||1||#', 'b||2||@', 'b||2||#'] == get_combs(
[['a', 'b'], ['1', '2'], ['@', '#']], splitter='||', n=100)


# --- testing pinyin combinations ---

def test_get_pinyins_with_default_splitter(pinyin):
assert pinyin.get_pinyins('上海') == ['shang-hai']


def test_get_pinyins_single_char(pinyin):
# 4E50 LE4 YUE4 YAO4 LUO4 LIAO2
assert ['lè', 'yuè', 'yào', 'luò', 'liáo'] == pinyin.get_pinyins('乐', splitter='', tone_marks='marks')


def test_get_pinyins_two_chars(pinyin):
combs1 = pinyin.get_pinyins('音', splitter='', tone_marks='marks')
combs2 = pinyin.get_pinyins('乐', splitter='', tone_marks='marks')
combs12 = pinyin.get_pinyins('音乐', splitter='', tone_marks='marks')
assert len(combs12) == len(combs1) * len(combs2)
assert 'yīnyuè' in combs12


def test_get_pinyins_no_tones_uniq(pinyin):
assert ['ma'] == pinyin.get_pinyins('吗', splitter='', tone_marks=None)


def test_get_pinyins_max_num(pinyin):
assert 5 == len(pinyin.get_pinyins('音乐', splitter='', n=5))


def test_get_pinyins_mixed_words(pinyin):
assert pinyin.get_pinyins('ABC串123', splitter=' ', tone_marks='marks') == ['ABC chuàn 123', 'ABC guàn 123']


def test_get_pinyins_long_seq(pinyin):
text = """汉语拼音(Hànyǔ Pīnyīn),
簡稱拼音,是一種以拉丁字母作普通话(現代標準漢語)標音的方案,為中文羅馬拼音的國際標準規範。
汉语拼音在中国大陆作为基础教育内容全面使用,是义务教育的重要内容。在海外,特别是常用現代標準漢語的地区如新加坡、
马来西亚、菲律宾和美国唐人街等,目前也在汉语教育中进行汉语拼音教学。臺灣自2008年開始,
中文譯音使用原則也採用漢語拼音[1],但舊護照姓名和部分地名、道路名稱仍採用威妥瑪拼音、
郵政式拼音、國語羅馬字、國音二式抑或通用拼音[2]。"""
assert 20 == len(pinyin.get_pinyins(text, n=20))
assert 10 == len(pinyin.get_pinyins(text)) # limited to 10 by default
14 changes: 14 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[tox]
envlist = py{36,37,38,39}
skip_missing_interpreters = true

[testenv]
deps = -rrequirements-dev.txt
commands = pytest

[gh-actions]
python =
3.6: py36
3.7: py37
3.8: py38
3.9: py39

0 comments on commit b36d2ce

Please sign in to comment.