From 3fa0c3c5d2ef67f5a5a95602bcdd60b36400f51f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Sun, 13 Dec 2020 11:53:53 +0100 Subject: [PATCH 1/4] Move tests out of src --- src/xpinyin/tests.py => tests/test_xpinyin.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/xpinyin/tests.py => tests/test_xpinyin.py (100%) diff --git a/src/xpinyin/tests.py b/tests/test_xpinyin.py similarity index 100% rename from src/xpinyin/tests.py rename to tests/test_xpinyin.py From 7b2c4d24cbde1cfa695f69d92fff060b0196773f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Sun, 13 Dec 2020 12:19:24 +0100 Subject: [PATCH 2/4] Use pytest instead of nosetest --- .travis.yml | 4 +- requirements-dev.txt | 1 + setup.py | 5 ++ tests/test_xpinyin.py | 147 ++++++++++++++++++++++-------------------- 4 files changed, 86 insertions(+), 71 deletions(-) create mode 100644 requirements-dev.txt diff --git a/.travis.yml b/.travis.yml index 491c77e..d287cf7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,4 +7,6 @@ python: - "3.7" - "3.8" - "3.9" -script: nosetests +install: + - pip install -r requirements-dev.txt +script: pytest diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..92b0566 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1 @@ +pytest>=6.2.0 \ No newline at end of file diff --git a/setup.py b/setup.py index a5135e5..69ec003 100644 --- a/setup.py +++ b/setup.py @@ -29,4 +29,9 @@ test_suite='xpinyin.tests', package_dir={'': 'src'}, include_package_data=True, + extras_require={ + "test": [ + "pytest>=6.2.0" + ], + }, license="BSD") diff --git a/tests/test_xpinyin.py b/tests/test_xpinyin.py index 7220334..82721d2 100644 --- a/tests/test_xpinyin.py +++ b/tests/test_xpinyin.py @@ -1,98 +1,105 @@ -#! /usr/bin/env python3 -import unittest +import pytest +from xpinyin import Pinyin from xpinyin.combs import _get_comb_indexes, get_combs -class PinyinTests(unittest.TestCase): - @staticmethod - def Pinyin(*a, **kw): - from xpinyin import Pinyin +@pytest.fixture(scope='module') +def pinyin(): + return Pinyin() - return Pinyin(*a, **kw) - def setUp(self): - self.p = self.Pinyin() +def test_get_pinyin_with_default_splitter(pinyin): + assert pinyin.get_pinyin('上海') == 'shang-hai' - def test_get_pinyin_with_default_splitter(self): - self.assertEqual(self.p.get_pinyin('上海'), 'shang-hai') - def test_get_pinyin_with_splitter(self): - self.assertEqual(self.p.get_pinyin('上海', splitter=''), 'shanghai') +def test_get_pinyin_with_splitter(pinyin): + assert pinyin.get_pinyin('上海', splitter='') == 'shanghai' - def test_get_pinyin_mixed_words(self): - self.assertEqual(self.p.get_pinyin('Apple发布iOS7', splitter='-'), - 'Apple-fa-bu-iOS7') - def test_get_pinyin_with_tone_marks(self): - self.assertEqual(self.p.get_pinyin('上海', tone_marks='marks'), 'sh\xe0ng-h\u01cei') - self.assertEqual(self.p.get_pinyin('秋', tone_marks='marks'), 'qiū') +def test_get_pinyin_mixed_words(pinyin): + assert pinyin.get_pinyin('Apple发布iOS7', splitter='-') == 'Apple-fa-bu-iOS7' - def test_get_initial(self): - self.assertEqual(self.p.get_initial('你'), 'N') - def test_get_initials(self): - self.assertEqual(self.p.get_initials('你好'), 'N-H') +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_initials_with_splitter(self): - self.assertEqual(self.p.get_initials('你好', ' '), 'N H') - self.assertEqual(self.p.get_initials('你好', ''), 'NH') - # --- testing combinations auxiliary functions --- +def test_get_initial(pinyin): + assert pinyin.get_initial('你') == 'N' - def test_get_comb_indexes(self): - self.assertEqual([[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(self): - self.assertEqual([[0, 0, 0], [0, 1, 0], [1, 0, 0]], _get_comb_indexes([2, 2, 1], 3)) +def test_get_initials(pinyin): + assert pinyin.get_initials('你好') == 'N-H' - def test_get_combs(self): - self.assertEqual(['a1@', 'a1#', 'a2@', 'a2#', 'b1@', 'b1#', 'b2@', 'b2#'], - get_combs([['a', 'b'], ['1', '2'], ['@', '#']])) - def test_get_combs_splitter_max_num(self): - self.assertEqual(['a 1 @', 'a 1 #', 'a 2 @', 'a 2 #', 'b 1 @'], - get_combs([['a', 'b'], ['1', '2'], ['@', '#']], splitter=' ', n=5)) +def test_get_initials_with_splitter(pinyin): + assert pinyin.get_initials('你好', ' ') == 'N H' + assert pinyin.get_initials('你好', '') == 'NH' - def test_get_combs_max_num_too_big(self): - self.assertEqual(['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 --- +# --- testing combinations auxiliary functions --- - def test_get_pinyins_with_default_splitter(self): - self.assertEqual(self.p.get_pinyins('上海'), ['shang-hai']) +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_pinyins_single_char(self): - self.assertEqual(['lè', 'yuè', 'yào', 'luò', 'liáo'], # 4E50 LE4 YUE4 YAO4 LUO4 LIAO2 - self.p.get_pinyins('乐', splitter='', tone_marks='marks')) - def test_get_pinyins_two_chars(self): - combs1 = self.p.get_pinyins('音', splitter='', tone_marks='marks') - combs2 = self.p.get_pinyins('乐', splitter='', tone_marks='marks') - combs12 = self.p.get_pinyins('音乐', splitter='', tone_marks='marks') - self.assertEqual(len(combs12), len(combs1) * len(combs2)) - self.assertIn('yīnyuè', combs12) +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_pinyins_no_tones_uniq(self): - self.assertEqual(['ma'], self.p.get_pinyins('吗', splitter='', tone_marks=None)) - def test_get_pinyins_max_num(self): - self.assertEqual(5, len(self.p.get_pinyins('音乐', splitter='', n=5))) +def test_get_combs(pinyin): + assert ['a1@', 'a1#', 'a2@', 'a2#', 'b1@', 'b1#', 'b2@', 'b2#'] == get_combs([['a', 'b'], ['1', '2'], ['@', '#']]) - def test_get_pinyins_mixed_words(self): - self.assertEqual(self.p.get_pinyins('ABC串123', splitter=' ', tone_marks='marks'), - ['ABC chuàn 123', 'ABC guàn 123']) - def test_get_pinyins_long_seq(self): - text = """汉语拼音(Hànyǔ Pīnyīn), - 簡稱拼音,是一種以拉丁字母作普通话(現代標準漢語)標音的方案,為中文羅馬拼音的國際標準規範。 - 汉语拼音在中国大陆作为基础教育内容全面使用,是义务教育的重要内容。在海外,特别是常用現代標準漢語的地区如新加坡、 - 马来西亚、菲律宾和美国唐人街等,目前也在汉语教育中进行汉语拼音教学。臺灣自2008年開始, - 中文譯音使用原則也採用漢語拼音[1],但舊護照姓名和部分地名、道路名稱仍採用威妥瑪拼音、 - 郵政式拼音、國語羅馬字、國音二式抑或通用拼音[2]。""" - self.assertEqual(20, len(self.p.get_pinyins(text, n=20))) - self.assertEqual(10, len(self.p.get_pinyins(text))) # limited to 10 by default +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) - if __name__ == '__main__': - unittest.main() + +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 From 24156fa5432644a9224ce4036775a9073dea9df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Sun, 13 Dec 2020 12:24:05 +0100 Subject: [PATCH 3/4] Add tox --- .travis.yml | 2 +- requirements-dev.txt | 3 ++- tox.ini | 7 +++++++ 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 tox.ini diff --git a/.travis.yml b/.travis.yml index d287cf7..26925c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,4 +9,4 @@ python: - "3.9" install: - pip install -r requirements-dev.txt -script: pytest +script: tox diff --git a/requirements-dev.txt b/requirements-dev.txt index 92b0566..642edbd 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1 +1,2 @@ -pytest>=6.2.0 \ No newline at end of file +pytest +tox \ No newline at end of file diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..687a35f --- /dev/null +++ b/tox.ini @@ -0,0 +1,7 @@ +[tox] +envlist = py{36,37,38,39} +skip_missing_interpreters = true + +[testenv] +deps = -rrequirements-dev.txt +commands = pytest \ No newline at end of file From 4f2139548d91c03a6bd44eb73ff7ac096d7775cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Sun, 13 Dec 2020 12:31:37 +0100 Subject: [PATCH 4/4] Add GitHub Actions --- .github/workflows/ci.yml | 24 ++++++++++++++++++++++++ README.rst | 3 +++ tox.ini | 9 ++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..b8aa8a7 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file diff --git a/README.rst b/README.rst index 4eef47a..c23452f 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/tox.ini b/tox.ini index 687a35f..c04cf3f 100644 --- a/tox.ini +++ b/tox.ini @@ -4,4 +4,11 @@ skip_missing_interpreters = true [testenv] deps = -rrequirements-dev.txt -commands = pytest \ No newline at end of file +commands = pytest + +[gh-actions] +python = + 3.6: py36 + 3.7: py37 + 3.8: py38 + 3.9: py39 \ No newline at end of file