Skip to content

Commit

Permalink
introduce property-based testing using hypothesis
Browse files Browse the repository at this point in the history
  • Loading branch information
okapies committed Feb 6, 2020
1 parent 5ce71b6 commit 446777b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
@@ -1,7 +1,7 @@
env:
global:
- CIBW_TEST_REQUIRES=nose
- CIBW_TEST_COMMAND="nosetests {project}/test"
- CIBW_TEST_COMMAND="pip3 install -r {project}/requirements/test.txt && nosetests {project}/test"
- TWINE_USERNAME=aflc
matrix:
include:
Expand All @@ -15,13 +15,15 @@ matrix:
sudo: required
language: generic

script:
install:
- |
if [ "$TRAVIS_OS_NAME" == "osx" ]; then
sudo pip3 install cibuildwheel==0.10.1 twine
else
pip3 install cibuildwheel==0.10.1 twine
fi
script:
- cibuildwheel --output-dir wheelhouse
- |
if [[ $TRAVIS_TAG ]]; then
Expand Down
9 changes: 9 additions & 0 deletions README.rst
Expand Up @@ -39,6 +39,15 @@ It's quite simple:
editdistance.eval('banana', 'bahama')
# 2L
-----
Test
-----

.. code-block:: bash
pip install -r requirements/test.txt
pytest test/
----------------
Simple Benchmark
Expand Down
9 changes: 6 additions & 3 deletions appveyor.yml
@@ -1,12 +1,15 @@
environment:
TWINE_USERNAME: aflc
CIBW_SKIP: cp27-*
CIBW_TEST_REQUIRES: nose
CIBW_TEST_COMMAND: "nosetests {project}/test"
CIBW_TEST_COMMAND: "python -m pip install -r {project}\\requirements\\test.txt && nosetests {project}\\test"


install:
- python -m pip install cibuildwheel==1.1.0

build_script:
- pip install cibuildwheel==0.9.4
- cibuildwheel --output-dir wheelhouse
- python -m cibuildwheel --output-dir wheelhouse
- ps: >-
if ($env:APPVEYOR_REPO_TAG -eq "true") {
python -m pip install twine
Expand Down
3 changes: 3 additions & 0 deletions requirements/test.txt
@@ -0,0 +1,3 @@
pytest==5.3.5
hypothesis==5.4.2
textdistance==4.1.5
86 changes: 82 additions & 4 deletions test/test_editdistance.py
@@ -1,10 +1,88 @@
# -*- coding: utf-8 -*-

import unicodedata
import unittest

from hypothesis import event, given, strategies as st

from textdistance import Levenshtein


REFERENCE = Levenshtein().distance


MIN_LENGTH = 0
MAX_LENGTH = 16


class TestEditDistance(unittest.TestCase):
def test_editdistance(self):
@given(
str0=st.text(
alphabet=st.characters(
min_codepoint=0,
max_codepoint=127),
min_size=MIN_LENGTH,
max_size=MAX_LENGTH),
str1=st.text(
alphabet=st.characters(
min_codepoint=0,
max_codepoint=127),
min_size=MIN_LENGTH,
max_size=MAX_LENGTH))
def test_ascii_codes(self, str0, str1):
norm_str0 = unicodedata.normalize('NFKC', str0)
norm_str1 = unicodedata.normalize('NFKC', str1)
event('eval("{}", "{}")'.format(norm_str0, norm_str1))

import editdistance
assert REFERENCE(norm_str0, norm_str1) == \
editdistance.eval(norm_str0, norm_str1)

@given(
str0=st.text(
alphabet=st.characters(
min_codepoint=12354, # あ
max_codepoint=12435), # ん
min_size=MIN_LENGTH,
max_size=MAX_LENGTH),
str1=st.text(
alphabet=st.characters(
min_codepoint=12354,
max_codepoint=12435),
min_size=MIN_LENGTH,
max_size=MAX_LENGTH))
def test_japanese_hiraganas(self, str0, str1):
norm_str0 = unicodedata.normalize('NFKC', str0)
norm_str1 = unicodedata.normalize('NFKC', str1)
event('eval("{}", "{}")'.format(norm_str0, norm_str1))

import editdistance
self.assertEqual(1, editdistance.eval('abc', 'aec'))

assert REFERENCE(norm_str0, norm_str1) == \
editdistance.eval(norm_str0, norm_str1)

@given(
str0=st.text(
alphabet=st.characters(
min_codepoint=12450, # ア
max_codepoint=12531), # ン
min_size=MIN_LENGTH,
max_size=MAX_LENGTH),
str1=st.text(
alphabet=st.characters(
min_codepoint=12450,
max_codepoint=12531),
min_size=MIN_LENGTH,
max_size=MAX_LENGTH))
def test_japanese_katakanas(self, str0, str1):
norm_str0 = unicodedata.normalize('NFKC', str0)
norm_str1 = unicodedata.normalize('NFKC', str1)
event('eval("{}", "{}")'.format(norm_str0, norm_str1))

import editdistance
assert REFERENCE(norm_str0, norm_str1) == \
editdistance.eval(norm_str0, norm_str1)


if __name__ == '__main__':
unittest.main()
import pytest
pytest.main([__file__])

0 comments on commit 446777b

Please sign in to comment.