From d9b5c0c6bbaef4279212a0dc83b92547893ba771 Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Tue, 30 Apr 2024 15:02:56 +0900 Subject: [PATCH] ci: Write first testcase --- .github/workflows/main.yml | 42 +++++++++++++++++++ pyproject.toml | 3 +- requirements-dev.lock | 6 +++ tests/.ruff.toml | 4 ++ .../budoux-example-plain/pseudoxml.txt | 9 ++++ .../patterns/budoux-example-plain/source.rst | 1 + tests/test_it.py | 29 +++++++++++++ 7 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/main.yml create mode 100644 tests/.ruff.toml create mode 100644 tests/patterns/budoux-example-plain/pseudoxml.txt create mode 100644 tests/patterns/budoux-example-plain/source.rst create mode 100644 tests/test_it.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..63b0462 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,42 @@ +name: Run CI + +on: + push: + branches: + - '**' + tags-ignore: + - '**' + pull_request: + workflow_dispatch: + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Lint by pre-commit + run: | + pip install pre-commit + pre-commit run --all-files + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Configure venv + run: | + pip install uv + uv venv + uv pip install -r requirements-dev.lock + - name: Run tests + run: | + source .venv/bin/activate + pytest diff --git a/pyproject.toml b/pyproject.toml index 645b759..d6cd2fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,13 +47,14 @@ build-backend = "hatchling.build" managed = true dev-dependencies = [ "esbonio~=0.16.4", + "pytest~=8.2.0", ] [tool.rye.scripts] setup = {chain = ["setup:sync", "setup:pre-commit"]} "setup:sync" = "rye sync --no-lock --all-features" "setup:pre-commit" = "pre-commit install" - +test = "pytest" [tool.ruff.lint] select = ["C90", "D", "E", "F", "I", "W"] diff --git a/requirements-dev.lock b/requirements-dev.lock index d16baa5..cc467b7 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -32,6 +32,8 @@ idna==3.7 # via requests imagesize==1.4.1 # via sphinx +iniconfig==2.0.0 + # via pytest jinja2==3.1.3 # via sphinx lsprotocol==2023.0.1 @@ -39,15 +41,19 @@ lsprotocol==2023.0.1 markupsafe==2.1.5 # via jinja2 packaging==24.0 + # via pytest # via sphinx platformdirs==4.2.1 # via esbonio +pluggy==1.5.0 + # via pytest pygls==1.3.1 # via esbonio pygments==2.17.2 # via sphinx pyspellchecker==0.8.1 # via esbonio +pytest==8.2.0 requests==2.31.0 # via sphinx snowballstemmer==2.2.0 diff --git a/tests/.ruff.toml b/tests/.ruff.toml new file mode 100644 index 0000000..d60ab9a --- /dev/null +++ b/tests/.ruff.toml @@ -0,0 +1,4 @@ +extend = "../pyproject.toml" + +[lint] +ignore = ["D100", "D103"] diff --git a/tests/patterns/budoux-example-plain/pseudoxml.txt b/tests/patterns/budoux-example-plain/pseudoxml.txt new file mode 100644 index 0000000..e359985 --- /dev/null +++ b/tests/patterns/budoux-example-plain/pseudoxml.txt @@ -0,0 +1,9 @@ + + + あなたに + + 寄り添う + + 最先端の + + テクノロジー。 diff --git a/tests/patterns/budoux-example-plain/source.rst b/tests/patterns/budoux-example-plain/source.rst new file mode 100644 index 0000000..ff9624f --- /dev/null +++ b/tests/patterns/budoux-example-plain/source.rst @@ -0,0 +1 @@ +あなたに寄り添う最先端のテクノロジー。 diff --git a/tests/test_it.py b/tests/test_it.py new file mode 100644 index 0000000..3c17ba4 --- /dev/null +++ b/tests/test_it.py @@ -0,0 +1,29 @@ +from pathlib import Path +from typing import List + +import budoux +import pytest +from docutils.core import publish_doctree, publish_from_doctree + +root = Path(__file__).parent + + +def get_pattern_dirs() -> List[Path]: + """Return structure of folder-path as test pattern. + + .. note:: This is for parametrize. + """ + return [p for p in (root / "patterns").glob("*") if p.is_dir()] + + +@pytest.mark.parametrize("pattern_dir", get_pattern_dirs()) +def test_parse_all_sentences(pattern_dir: Path): + from rst_budoux import parse_all_sentences + + source = pattern_dir / "source.rst" + expect = pattern_dir / "pseudoxml.txt" + parser = budoux.load_default_japanese_parser() + document = publish_doctree(source.read_text()) + document = parse_all_sentences(parser, document) + result = publish_from_doctree(document, writer_name="pseudoxml").decode("utf-8") + assert result == expect.read_text()