Skip to content

Commit

Permalink
(#6) Add hypothesis tests for expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru-dinu committed Dec 30, 2022
1 parent df39cf4 commit 2a7e7b2
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 10 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
workflow_dispatch:

permissions:
contents: read
Expand All @@ -20,6 +21,9 @@ jobs:
with:
ghc-version: '9.2.5'
cabal-version: '3.6'
- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Cache
uses: actions/cache@v3
Expand All @@ -37,7 +41,13 @@ jobs:
run: |
cabal update
cabal build --only-dependencies --enable-tests --enable-benchmarks
pip3 install -r tests_requirements.txt
- name: Build
run: cabal build --enable-tests --enable-benchmarks all
- name: Run tests
run: cabal test all

- name: Run Haskell tests
run: make test-hs

- name: Run Hypothesis tests
run: make test-py
163 changes: 161 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Main

*~
*.lock
dist
Expand All @@ -25,3 +23,164 @@ cabal.project.local
cabal.project.local~
.HTF/
.ghc.environment.*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ hindent:
hlint:
$@ ./

test:
stack $@
test-hs:
cabal test all

test-py:
pytest -vv --hypothesis-show-statistics tests/test.py

clean:
stack $@
stack $@ --full
find . -name ".hypothesis" -print0 | xargs -0 rm -rf
find . -name ".pytest_cache" -print0 | xargs -0 rm -rf
find . -name "__pycache__" -print0 | xargs -0 rm -rf
4 changes: 2 additions & 2 deletions monadic-parser.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.0.
-- This file has been generated from package.yaml by hpack version 0.35.1.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -56,7 +56,7 @@ test-suite monadic-parser-test
TestUtils
Paths_monadic_parser
hs-source-dirs:
test
tests
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
Glob
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ executables:
tests:
monadic-parser-test:
main: Test.hs
source-dirs: test
source-dirs: tests
ghc-options:
- -threaded
- -rtsopts
Expand Down
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import ast
import subprocess
import tempfile

from hypothesis import given, settings
from hypothesis import strategies as st

NUM_VALUES = 2
OPS = ["+", "-", "*"]
MIN_VALUE = 0
MAX_VALUE = 100


@st.composite
def expr_gen(draw):
xs = []
op = []

for _ in range(NUM_VALUES):
xs.append(draw(st.integers(min_value=MIN_VALUE, max_value=MAX_VALUE)))

for _ in range(NUM_VALUES - 1):
op.append(draw(st.sampled_from(OPS)))

out = xs + op
out[::2] = xs
out[1::2] = op

return " ".join(map(str, out))


def safe_eval(expr: str):
return eval(compile(ast.parse(expr, mode="eval"), "<node>", "eval"), {}, {})


@settings(max_examples=50, deadline=None)
@given(expr_gen())
def test_expression(expr):
with tempfile.NamedTemporaryFile("wt") as f:
f.write(f"return {expr};")
f.flush()
result = subprocess.run(
f"stack run monadic-parser-exe {f.name}",
shell=True,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)

actual = result.stdout.decode().strip()
expected = safe_eval(expr)
if expected < 0:
assert actual == f"P (Right ({expected}))"
else:
assert actual == f"P (Right {expected})"
3 changes: 3 additions & 0 deletions tests_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pip
pytest
hypothesis

0 comments on commit 2a7e7b2

Please sign in to comment.